Generic Addin Framework R1

Implementation Details

The solution consists of three libraries: SwAddinBase, SwAddinHelpers and SwAddinHelpersComExtender.

ISwAddin Implementation

  • implements ISwAddin interface methods necessary to connect and disconnect to an instance of SolidWorks.
  • implements ISwAddinBase exposing the addin's SolidWorks instance and UI elements. Every addin component holds a reference to SwAddinBase through this interface.
  • inherits ComRegistrableAddinImpl which registers the addin with SolidWorks.

UI Elements

SolidWorks GUI

SolidWorks user defined controls:
  • Task Pane
  • Model View Tab
  • Feature Tree Tab
  • Property Page

All UI elements derive from System.Windows.Forms.UserControl. Each type of UI element listed above has its own type of host. The host is responsible for UI element creation and destruction within SolidWorks and subscription to events. Hosts hold a reference to ISwAddinBase while each user control holds a reference to its host. User controls and their hosts communicate with other controls, hosts and the SolidWorks instance through the ISwAddinBase interface as well as through explicitly exposed parameterless methods marked with the [InvokableMethodAttribute(string FriendlyMethodName)]. The addin is capable of hosting multiple UI element hosts of the same type, however only one instance of a specific user control type is allowed at any given time. (This design limitation decision was made since it did not make sense to have the same user control appearing more than once in the SolidWorks UI.) Individual control types are defined by the user configurable enum enumHostedControl which is passed in to a host constructor.

See External User Controls for hosting user controls from external libraries.
Documentation Inheritance Chain
Task Pane Host TaskPaneHost:UserControlHostBase:AddinProxy
Model View Tab Host ModelViewTabHost:UserControlHostBase:AddinProxy
Feature Tree Tab Host FeatureViewHost:UserControlHostBase:AddinProxy
Property Page Host PropertyManagerHost:UserControlHostBase:AddinProxy
User Controls
Documentation Inheritance Chain Lifetime
Task Pane Control TaskPaneControlX:TaskPaneControlBase:UserControlBase:UserControl Addin
Model View Tab Control ModelViewTabControlX:UserControlBase:UserControl Document
Feature View Tab Control FeatureViewControlX:UserControlBase:UserControl Document
Property Page Control PropertyManagerControlX:ActiveXUserControlBase:UserControlBase.UserControl Addin

Internal User control creation requirements:
  • Class must be tagged with [ComVisible(true), ProgId(PROGID)]
  • Class must contain a unique public constant PROGID (public const string PROGID = "SwAddinHelpers.MySpecialTaskPaneControl";)
  • Property Page only: Class must contain public constants for height and width of the control at runtime (public const short HEIGHT = 420; public const short WIDTH = 150;)

External User control creation requirements:


  • Menu
  • Command
  • Popup

Event Handling

The swAppEventSink class is exposed as a public property through the ISwAddinBase interface. The purpose of the swAppEventSink class is to create/destroy/fire event handlers. SolidWorks exposes application, document and UI element event handlers.
Enum enumEventSink is a user defined enumeration of the available event handlers. More handlers and their logic can be added to the swAppEventSink class and exposed via the SinkMethodAttribute.
Host and user control methods tagged with the SinkMethodAttribute will be invoked only after the host has subscribed the method to the appropriate event. Event subscription is currently done by calling each host's SubscribeEvent(enumEventSink, enumEventSubscriptionLevel) method. The enumEventSubscriptionLevel determines whether the event should be tied to a method in the host class or the user control class. All events are unsubscribed during the host Teardown() which is done when the addin is unloaded from SolidWorks. The UnsubscribeEvent(enumEventSink) host method can be called to manually untie event handlers.
Late Subscription
User controls are also capable of late subscription while the host's SubscribeEvent method is called as usual. The Property Page user control, for example, is created only after the Property Page is displayed to the user by calling PropertyManagerPage2.Show(). This call invokes PropertyManagerPage2Handler7.OnActiveXControlCreated(int,bool) at which point the user control can be harvested from PropertyManagerPageActiveX.GetControl(). Late subscription is accomplished by setting the host's IsLateSubscriber property to true. Event subscription is done when the host's GuiControl property is set, such as during a call to PropertyManagerPageActiveX.GetControl().
ActiveModelDocChangePreNotify Event
The ActiveModelDocChangeNotify delegate keeps track of the previous active document while a different document is activated. This is helpful with two things: unsubscription of event handlers from the previously active doument and the addition of a custom active document pre-notification event. For example, the ModelViewTabHost holds a reference to a ModelViewManager on top of which it creates a Model View Tab user control. The ActiveModelDocChangeNotify event causes the user control to be created, while the ActiveModelDocChangePreNotify event causes the user control to be destroyed from the previously active document, referenced by the ModelViewManager.Document property. The effect is that the user control exists only on the active document, and any subscribed events are fired only on the active document. The target method receives a SolidWorksEventArgs parameter which contains user definable information. Two of which are the document title and document creation date. In combination they can be used to determine if the action of the method fired by ActiveModelDocChangePreNotify.


Several custom attributes exist to extend the programming model at design time. Attribute Reference
Attribute Parameters Purpose
InvokableMethodAttribute Friendly name of a mehod User control parameterless methods are tagged with this attribute. Tagged methods are visible and invokable through their friendly name through their host. Relevant host methods: GetInvokableMethods() and InvokeMethod(string FriendlyName). Invoke Method Example 1
SinkMethodAttribute enumEventSink event and a boolean Host and user control methods are tagged with this attribute. These methods are invoked when the specified event is fired by SolidWorks. The boolean parameter is used when external controls are created.
IdentifiableControlAttribute Type of unique user control Enum enumHostedControl is tagged with this attribute. The host uses this attribute's Type value to create its user control. This attribute is not used with external user controls.
HostedAsAttribute none HostedAsTaskpane, HostedAsFeatureView, HostedAsModelView, HostedAsPropertyPage are used to tag external control classes. This attribute is used to determine the host to use when creating the external control.

COM Types

It is possible to retrieve the .NET managed type from a COM object using SwAddinHelpersComExtender.GetManagedTypeForSWObject(object, bool) and SwAddinHelpersComExtender.GetManagedTypesForSWObject(object) extension methods. The returned type is a fully qualified interface type from the SolidWorks interop library.

For example, selecting a face in a model and querying the returned value from SelectionMgr.GetSelectedObject() would yield the following IEntity,IFace,IFace2,Entity,Face,Face2. Only the types prefixed with 'I' return a MemberInfo collection by calling GetMembers() on the returned type.

Last edited Jun 18, 2010 at 3:15 PM by fixitchris, version 37


No comments yet.