Custom selection context commands #81
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.







SolidDNA changes
Summary
This PR adds a more structured API for SolidDNA context menu popup icons and context menu items (groups/submenus), plus updates the Menus tutorial (
Tutorials/05-Menus) with improved examples, icon assets, and documentation.Key changes
CommandContext*types) and related create-info / created-state types.CommandManagerconstruction to accept the SolidWorks add-in cookie (new constructor overload) and store it for context-menu registration, the old constructor is marked obsolete. SolidWorks APIs require this cookie for registration but it cannot be read back from theICommandManagerobject, so it must be passed in. This avoids relying on global application access for the cookie and makes the lifetime/ownership clearer.Icons/icons{size}.png)Icons/icon_*{size}.png)IApplication/Application naming/property situation and I don’t want to build changes on top of that until it’s stabilized/renamed.Main types
Creatable→Created):CommandContext*classes are configuration + top-level creation logic (what to create, for which doc types, which selection context, how to build nested paths).CommandContext*Createdclasses are the registered SolidWorks artifacts (what was actually added to the UI) and own the created-state lifetime/disposal behavior.CommandContextItem/CommandContextIconare intended to be treated as creation-time configuration, afterCreate(...)they effectively become immutable for a given registration (prevents “create twice / change fields / create again” issues).ICommandContextCreateInfocarries runtime data like the SolidWorks cookie,CommandContextItemCreateInfoadditionally carries the current path for nested@hierarchy.CommandContextGroupis a creatable “container” that creates aCommandContextGroupCreated.CommandContextGroupCreatedholds the created children and is responsible for disposing them (and for passing derived create info, like the nested path, down to child items).Design notes / rationale
Createshould only move an object to a “created” state.IEnumerable<T>for returned collections (details/justification): Iterator pattern approach discussionCommandIdafter creation to prevent inconsistent CommandManager state.Style / conventions
_fieldnaming over legacymFieldnaming (older classes may still usem).#regionblocks in small classes (keeps files shorter and relies on IDE navigation instead).SolidWorks API context
Raw SolidWorks API interop
ISldWorks.AddMenuPopupItem3per(DocumentType, SelectionType)combination, using the add-in cookie and SolidDNA callback strings (SolidAddIn.Callback(CallbackId)/SolidAddIn.ItemStateCheck(CallbackId)).IFrame.AddMenuPopupIcon3(frame obtained fromISldWorks.Frame), also using the add-in cookie plus the same callback/update pattern.SolidWorks API bugs / limitations
ISldWorks.AddMenuPopupItem3: SolidWorks documentation suggests you can add a separator by appending an at-sign (@) and quoting the popup item name, but this does not work (observed on SW 2022 and 2024). SolidWorks still parses the quoted string and treats the empty part as a submenu name.SelectedEnabled,DeselectedEnabled, andHiddenstates are supported, disabled states are not supported.Removal / unregister limitations (Dispose strategy)
CommandContext*Created), library users are not expected to manually dispose these.Hiddento effectively remove the item/icon from the UI.ISldWorks.RemoveMenuPopupItem2always returnsfalseand does not remove the item.ISldWorks.RemoveFromPopupMenucan returntruebut the item is still not removed.IFrame.AddMenuPopupItem2callback path is C++ only.HiddenviaOnStateCheck.IFrame.RemoveMenuPopupIconis C++ only.Tools → Add-ins → checkbox unchecked), SolidWorks removes the add-in’s registered context menu items/icons from the UI (this happens regardless of theHidden/Dispose workaround).SolidWorks UI/behavior limitations
@) does not work reliably (observed SW 2022/2024), SolidWorks still parses@and treats the empty segment as a submenu name.SelectionType.Component(even though it’s a Part), interesting observation.Out of scope / deferred
SpecificFeatureSelectionType(and related selection-type details) is intentionally deferred to a follow-up PR.CommandContext*primitives) are intentionally deferred to follow-up PRs.Application/IApplication) changes during/after merge, this PR may need a small follow-up to align with the updated/official application access approach.