8 Replies Latest reply on Aug 13, 2015 9:22 AM by John Alexander

    Component selection from design tree

    John Alexander

      Hey guys, just wondering if anyone has a suggestion here. This subroutine grabs an array of the currently selected components in an assembly drawing. The problem I am having is in the section highlighted red below. When a component is selected in the design tree, the macro throws a type mismatch error at the red highlighted line. This means that the type is swSelCOMPONENTS but the selection manager isn't returning a Component2 type object. Any feedback is appreciated.

       

      Sub getSelectedComponentInAssemblyDrawing(swSelectedComponents() As Component2, swSelectedEntities() As Entity)

          'Grabs the currently selected components in an assembly drawing

          Dim swSelectedFeature           As IFeature

          Dim swSelectedEntity            As SldWorks.Entity

          Dim swSelectedComponent         As SldWorks.IComponent2

          Dim swSelectedFace              As IFace2

          Dim swSelectedEdge              As Edge

          Dim swSelectedBody              As Body2

          Dim swSelectedVertex            As Vertex

         

          Dim numselections               As Integer

          Dim selection                   As Integer

          Dim numComponents               As Integer

          Dim i                           As Integer

          Dim j                           As Integer

          Dim flagValid                   As Boolean

         

          Dim BomNameCandidate            As String

          Dim BomNameList()               As String

         

         

          numComponents = 0

          ReDim swSelectedComponents(numComponents)

          ReDim swSelectedEntities(numComponents)

          ReDim BomNameList(0)

          BomNameList(0) = ""

         

          numselections = swSelectionMgr.GetSelectedObjectCount

          For i = 0 To numselections

              flagValid = False

              selection = swSelectionMgr.GetSelectedObjectType3(i, -1)

              Select Case swSelectionMgr.GetSelectedObjectType3(i, -1)

                  Case swSelectType_e.swSelCOMPONENTS

                      Set swSelectedComponent = swSelectionMgr.GetSelectedObject6(i, -1)

                      Set swSelectedEntity = Nothing

                      flagValid = True

                  Case swSelectType_e.swSelVERTICES

                      Set swSelectedVertex = swSelectionMgr.GetSelectedObject6(i, -1)

                      If Not (swSelectedVertex Is Nothing) Then

                          Set swSelectedEntity = swSelectedVertex

                          Set swSelectedComponent = swSelectedEntity.getComponent

                          flagValid = True

                      End If

                  Case swSelectType_e.swSelEDGES

                      Set swSelectedEdge = swSelectionMgr.GetSelectedObject6(i, -1)

                      If Not (swSelectedEdge Is Nothing) Then

                          Set swSelectedEntity = swSelectedEdge

                          Set swSelectedComponent = swSelectedEntity.getComponent

                          flagValid = True

                      End If

                  Case swSelectType_e.swSelFACES

                      Set swSelectedFace = swSelectionMgr.GetSelectedObject6(i, -1)

                      If Not (swSelectedFace Is Nothing) Then

                          Set swSelectedFeature = swSelectedFace.IGetFeature

                          Debug.Assert (Not (swSelectedFace Is Nothing))

                          Debug.Assert (Not (swSelectedFeature Is Nothing))

                          Set swSelectedEntity = swSelectedFeature

                          Set swSelectedComponent = swSelectedEntity.getComponent

                          flagValid = True

                      End If

              End Select

              If Not (swSelectedComponent Is Nothing) Then

                  If flagValid Then

                      Set swSelectedComponent = findTop(swSelectedComponent) 'Find the top level parent of the selected component in the drawing assembly

                      BomNameCandidate = getBomPartNumber(swSelectedComponent)

                      For j = 0 To (numComponents)

                          If (StrComp(BomNameList(j), BomNameCandidate) = 0) Then

                              'Compare the current component to every component in the selected components list, flag false for duplicates

                              flagValid = False

                              Exit For

                          End If

                      Next j

                      If flagValid Then

                          numComponents = numComponents + 1

                          ReDim Preserve swSelectedComponents(numComponents)

                          ReDim Preserve swSelectedEntities(numComponents)

                          ReDim Preserve BomNameList(numComponents)

                         

                          BomNameList(numComponents - 1) = BomNameCandidate

                          Set swSelectedComponents(numComponents - 1) = swSelectedComponent

                          Set swSelectedEntities(numComponents - 1) = swSelectedEntity

                      End If

                  End If

              End If

          Next i

      End Sub

       

       

      Thank you,

       

      John