-
Notifications
You must be signed in to change notification settings - Fork 29
Description
SolidWorks API allows us to work with some collection, like features and annotations via the Iterator pattern. Currently we have an awkward approach with iterateing collection via callback. It has many restrictions and is not convenient.
Can we try other approaches?
Table of comparison approaches:
| Approach | Lazy evaluation | User can dispose when want | User has way to automatically dispose | Understandable for beginners | Compatible with LINQ |
|---|---|---|---|---|---|
| Final collection | X | ✓ | X | ✓ | ✓ |
| Treating with callback | X* | X | ✓ | X | X |
| Enumerating | ✓ | ✓ | ✓ | X | ✓ |
* - is possible to implement via return value or Args
I prefer to use Enumerating. Because implementation of Iterator pattern in .NET is IEnumerable. And most pure way is to wrap SW "GetNext" iterator methods to its .NET alternative - IEnumerable. But if we care about object disposing it seems not possible, but it's not.
I saw some similar approach in ReactiveUI. It has "DisposeWith" extensions method that allows to add new observable subscription to one composite disposable, it is awesome idea. I think we can use this approach when iterating through SW collection, and when user ends with it, he disposes input disposable (or use "using"). But it optional and we can don't do this (COM object dispose I mean).
Let me demonstrate example with model’s notes iteration.

Then let me explain.
In "firstNote" we terminate collection iteration when found note with matching Name. Advantage of lazy approach is if it first Note in model it will cost O(1) if last O(N) and if we average results we get ~ O(N/2). It’s just example doesn’t be strict.
"notEmptyNotes" and "extractedDigits" demonstrates how seamlessly transitions into LINQ.
When operating with enumerable is ended we can dispose "disposable" or use "using" that dispose in end of stack frame.
That's what it is, what do you think? Is it the best approach, or we have some better alternatives? Is this idea worth pursuing?
I can try to add PR for Enumerable for Note enumeration, and I think we can try something similar with SelectionManager.GetEnumerateSelected.