SOLIDWORKS 2020 Launch Broadcast, on Tue, Oct 1 at 9:00 AM ET  REGISTER NOW ›

Add-in Series 5 – Handle Selections [VIDEO]

Article by Adam Bridgman, CSWE created/updated January 10, 2014

The mouse events are triggered quite frequently, and many times nothing in SolidWorks has changed.  It is possible to use selection change events instead.  This means your code can assume that it needs to reprocess the selection. In this example I am going to set up my task pane to react to selection changes in an assembly.

  1. To demonstrate that this code is reacting to selections in an assembly the form should react to selections.  Add a few images to the user control

 

Image1

 

Then add code to control their visibility

 

    Sub SetImages(ByVal CurrentDocType As Integer)

        If CurrentDocType = SwConst.swDocumentTypes_e.swDocPART Then

            picPrt.Visible = True

            picAsm.Visible = False

            picDrw.Visible = False

        ElseIf CurrentDocType = SwConst.swDocumentTypes_e.swDocASSEMBLY Then

            picPrt.Visible = False

            picAsm.Visible = True

            picDrw.Visible = False

        Else

            picPrt.Visible = False

            picAsm.Visible = False

            picDrw.Visible = True

        End If

    End Sub

 

  1. UserControl1 may be accessed from many methods and should only be updated in standardized fashions.   To control this, a public property can be used to set the display.  To create a public property add the following code to the UserControl1 class.

 

   Private _CurrentDocType As Integer

    Public Property CurrentDocType() As Integer

        Get

            Return _CurrentDocType

        End Get

        Set(value As Integer)

            _CurrentDocType = value

            SetImages(value)

        End Set

    End Property

When set this property will call the previous code which sets the visibility of the pictures.  The advantage of setting them through this property is greater control of updates to the code.  For example we could modify the set images method to read _CurrentDocType instead of passing it as a parameter and it would still work with the property, but not if it was called directly.

 

  1. Add a modeldoc2 object to the AssemblyEvents Class

Public Class AssemblyEventHandler

    Inherits DocumentEventHandler

    Dim WithEvents iAssembly As AssemblyDoc

    Dim swAddin As SwAddin

    Dim swModel As ModelDoc2

    Overrides Function Init(ByVal sw As SldWorks.SldWorks, ByVal addin As SwAddin, ByVal model As ModelDoc2) As Boolean

        userAddin = addin

        iAssembly = model

        swModel = model

        iDocument = iAssembly

        iSwApp = sw

        swAddin = addin

End Function

 

The ModelDoc2 object allows the class to access every method that affects documents in SolidWorks.

 

  1. Modify the AssemblyDoc_NewSelectionNotify() to determine if parts, assemblies, or parts and assemblies are selected.

 

(Explanation of this code is in green comments below)

    Function AssemblyDoc_NewSelectionNotify() As Integer

        Dim swSelMgr As SelectionMgr = swModel.SelectionManager ‘Read selections in SolidWorks

        If swSelMgr.GetSelectedObjectCount2(-1) = 1 Then ‘(-1) means nothing here, if only one object is selected then proceed with the following code

            ‘Get the type

            Dim swComponent As Component2 = swSelMgr.GetSelectedObjectsComponent4(1, -1) ‘Get the component object (components are either parts or assemblies

            If swComponent Is Nothing Then

                Exit Function ‘If it is not possible to get a component then quit

            End If

            Dim swCompModel As ModelDoc2 = swComponent.GetModelDoc2 ‘Get the ModelDoc2 for the selected component

            iTaskHost.CurrentDocType = swCompModel.GetType ‘Set the property that was created earlier and pass the document type

        Else ‘more than one file selected (similar to above except checks to see if only assemblies are selected or if there is a combination of parts and assemblies

            ‘Get the types

            Dim boolHasParts As Boolean = False

            Dim boolhasAssemblies As Boolean = False

            Dim swComponent As Component2 = Nothing

            Dim swCompModel As ModelDoc2 = Nothing

            For i As Integer = 1 To swSelMgr.GetSelectedObjectCount2(-1) ‘Loop through selection

                swComponent = swSelMgr.GetSelectedObjectsComponent4(i, -1) ‘Get each component

                If swComponent IsNot Nothing Then

                    swCompModel = swComponent.GetModelDoc2

                    If swCompModel.GetType = SwConst.swDocumentTypes_e.swDocPART Then

                        boolHasParts = True

                    ElseIf swCompModel.GetType = SwConst.swDocumentTypes_e.swDocASSEMBLY Then

                        boolhasAssemblies = True

                    End If

                End If

            Next

            ‘Set task pane images

            If boolhasAssemblies And boolHasParts Then ‘has both parts and assemblies

                iTaskHost.CurrentDocType = 100

            ElseIf boolhasAssemblies Then ‘only has assemblies

                iTaskHost.CurrentDocType = SwConst.swDocumentTypes_e.swDocASSEMBLY

            ElseIf boolHasParts Then ‘only has parts

                iTaskHost.CurrentDocType = SwConst.swDocumentTypes_e.swDocPART

            Else ‘No parts or assemblies selected

                iTaskHost.CurrentDocType = 0

            End If

        End If

    End Function

 

  1. Disable the click event from the mouse handler so it does not interfere

 

   Private Function Mouse_LBtnUpNotify(x As Integer, y As Integer, wparam As Integer) As Integer

        ‘Dim swModel As SldWorks.ModelDoc2 = swApp.ActiveDoc

        ‘iTaskHost.CurrentDocType = swModel.GetType

   End Function

 

  1. Adjust the set images to show parts and assemblies simultaneously if they are both selected

 

    Sub SetImages(ByVal CurrentDocType As Integer)

        If CurrentDocType = SwConst.swDocumentTypes_e.swDocPART Then

            picPrt.Visible = True

            picAsm.Visible = False

            picDrw.Visible = False

        ElseIf CurrentDocType = SwConst.swDocumentTypes_e.swDocASSEMBLY Then

            picPrt.Visible = False

            picAsm.Visible = True

            picDrw.Visible = False

        ElseIf CurrentDocType = SwConst.swDocumentTypes_e.swDocDRAWING Then

            picPrt.Visible = False

            picAsm.Visible = False

            picDrw.Visible = True

        ElseIf CurrentDocType = 100 Then ‘For parts and assemblies

            picPrt.Visible = True

            picAsm.Visible = True

            picDrw.Visible = False

        Else ‘None selected

            picPrt.Visible = False

            picAsm.Visible = False

            picDrw.Visible = False

        End If

    End Sub

 

Posts related to 'Add-in Series 5 – Handle Selections [VIDEO]'

Adam Bridgman, CSWE

Want to learn SOLIDWORKS?

Take a training course from our team of Certified SOLIDWORKS Experts