5 Replies Latest reply on Nov 13, 2017 12:10 PM by Diogo Teixeira

    API- How to select two faces from different components in an assembly, and add them to the same selection list?

    Diogo Teixeira

      The purpose is to mate two components using one selected face from each of them. In other words, I intend to use a procedure that gets into the selected component and goes through all faces until it finds the one by name, selecting it. Therefore the procedure is called two times, after selecting each of the components individually, so both are traversed in its faces, and then having both faces selected.

       

      The issue starts after selecting the first face when I try to get into the second component without succeeding in adding the following face to the selection list.

       

      I would like to know how to have these two faces selected so I can proceed with the mate.

       

      This is the code:

       

      Sub mate_pino_porca()

       

          Dim swApp As Object

          Dim AssyDoc As Object

          Dim longstatus As Long

          Dim MyMate As Object

          Dim CompName As String

          Dim FaceName As String

          Dim CCompName As String

          Dim FFaceName As String

          Dim boolstatus As Boolean

         

          Set swApp = CreateObject("SldWorks.Application")

          ' Get active Assembly document

          Set AssyDoc = swApp.ActiveDoc()

       

          CompName = "porca quadrada-1@Assem_pino_porca"

          FaceName = "concentrico_porca"

          CCompName = CompName

          FFaceName = FaceName

         

          boolstatus = AssyDoc.Extension.SelectByID2(CompName, "COMPONENT", 0, 0, 0, False, 0, Nothing, 0)

          SelectComponentFaceByName CCompName, FFaceName

       

          CompName = "pino-1@Assem_pino_porca"

          FaceName = "concentrico_pino"

          CCompName = CompName

          FFaceName = FaceName

         

          boolstatus = AssyDoc.Extension.SelectByID2(CompName, "COMPONENT", 0, 0, 0, True, 0, Nothing, 0) '<---If False it works!

          SelectComponentFaceByName CCompName, FFaceName

         

          Set MyMate = AssyDoc.AddMate5(1, 0, True, 0#, 0#, 0.001, 0.001, 0.001, 0#, 0#, 0#, False, False, 0, longstatus)

          'Part.ClearSelection2 True

         

      End Sub

       

       

      Public Sub SelectComponentFaceByName(CompName As String, FaceName As String)

         

          Dim swApp As Object

          Dim AssyDoc As Object

          Dim swAssy As SldWorks.AssemblyDoc

          Dim swComp As SldWorks.Component2

          Dim SelMgr As Object

          Dim SelData As SldWorks.SelectData

          Dim Comp As Object

          Dim Body As Object

          Dim Face As Object

          Dim CurFaceName As String

          Dim boolstatus As Boolean

       

          Const swSelCOMPONENTS = 20

          Set swApp = CreateObject("SldWorks.Application")

          ' Get active Assembly document

          Set AssyDoc = swApp.ActiveDoc()

          Set SelMgr = AssyDoc.SelectionManager()

          Set SelData = SelMgr.CreateSelectData

         

          'AssyDoc.SelectByID "pino", "COMPONENT", 0, 0, 0

          Set Comp = SelMgr.GetSelectedObject3(1)

         

          'If (SelMgr.GetSelectedObjectType2(1) <> swSelCOMPONENTS) Then

              'swApp.SendMsgToUser "Please Select an Assembly Component."

              'Exit Sub

          'End If

         

          Set Body = Comp.GetBody() ' Get the Component Body

         

          If (Body Is Nothing) Then

              swApp.SendMsgToUser "Component Body Unavailable."

              swApp.SendMsgToUser "Make sure not lightweight or suppressed"

              Exit Sub

          End If

         

          'boolstatus = AssyDoc.DeSelectByID(CompName, "COMPONENT", 0, 0, 0)

          Set Face = Body.GetFirstFace

         

          ' Traverse thru all body faces

          Do While Not Face Is Nothing

              ' Call ModelDoc.GetEntityName.

              CurFaceName = AssyDoc.GetEntityName(Face)

              If (CurFaceName = FaceName) Then

                  'Face.Select (0) ' Select the face

                  boolstatus = Face.Select4(False, SelData)

                  Exit Do

              End If

              Debug.Print CurFaceName

              Set Face = Face.GetNextFace

          Loop

       

      End Sub

       

      Many thanks in advance!

      Diogo Teixeira

          • Re: API- How to select two faces from different components in an assembly, and add them to the same selection list?
            Deepak Gupta

            Diogo Teixeira wrote:

             

            Sorry for not attaching the assembly document, due to lack of space

            Is the file (zipped via pack and go) is over 25 mb? If yes then you can post them on Dropbox or Google Drive and post the link here.

             

            OR can post them in one than one post here.

             

            Change False in the first face selection line to True to resolve the issue.

            boolstatus = AssyDoc.Extension.SelectByID2(CompName, "COMPONENT", 0, 0, 0, False, 0, Nothing, 0)

              • Re: API- How to select two faces from different components in an assembly, and add them to the same selection list?
                Diogo Teixeira

                Thanks Deepak, now it’s available!

                 

                I did act in accordance with what you said, but the issue persists. I think the problem is related to the variable “CurFaceName” which only sets to values of the first analysed component. The variable should only assume the string value of "concentrico_pino", which is the name of the face required in the second component.

                 

                The first selection is interfering with the second object, I think.

                  • Re: API- How to select two faces from different components in an assembly, and add them to the same selection list?
                    Simon Turner

                    You shouldn't select the components themselves.

                    Just use AssyDoc.GetComponentByName to get the Component2 object.

                    Fixed code below:

                     

                    Sub mate_pino_porca()

                        Dim swApp As Object

                        Dim AssyDoc As Object

                        Dim longstatus As Long

                        Dim MyMate As Object

                        Dim CompName As String

                        Dim FaceName As String

                        Dim CCompName As String

                        Dim FFaceName As String

                        Dim boolstatus As Boolean

                        Dim myComp1 As Component2

                        Dim myComp2 As Component2

                       

                        Set swApp = CreateObject("SldWorks.Application")

                        ' Get active Assembly document

                        Set AssyDoc = swApp.ActiveDoc()

                        AssyDoc.ClearSelection2 True

                     

                        'CompName = "porca quadrada-1@Assem_pino_porca"

                        CompName = "porca quadrada-1"

                        FaceName = "concentrico_porca"

                        CCompName = CompName

                        FFaceName = FaceName

                      

                        'boolstatus = AssyDoc.Extension.SelectByID2(CompName, "COMPONENT", 0, 0, 0, False, 0, Nothing, 0)

                        SelectComponentFaceByName CCompName, FFaceName

                     

                        'CompName = "pino-1@Assem_pino_porca"

                        CompName = "pino-1"

                        FaceName = "concentrico_pino"

                        CCompName = CompName

                        FFaceName = FaceName

                      

                        'boolstatus = AssyDoc.Extension.SelectByID2(CompName, "COMPONENT", 0, 0, 0, True, 0, Nothing, 0) '<---If False it works!

                        SelectComponentFaceByName CCompName, FFaceName

                      

                        Set MyMate = AssyDoc.AddMate5(1, 0, True, 0#, 0#, 0.001, 0.001, 0.001, 0#, 0#, 0#, False, False, 0, longstatus)

                        'Part.ClearSelection2 True

                      

                    End Sub

                     

                     

                    Public Sub SelectComponentFaceByName(CompName As String, FaceName As String)

                      

                        Dim swApp As Object

                        Dim AssyDoc As Object

                        Dim swAssy As SldWorks.AssemblyDoc

                        Dim swComp As SldWorks.Component2

                        Dim SelMgr As Object

                        Dim SelData As SldWorks.SelectData

                        Dim Comp As Object

                        Dim Body As Object

                        Dim Face As Object

                        Dim CurFaceName As String

                        Dim boolstatus As Boolean

                       

                        Const swSelCOMPONENTS = 20

                        Set swApp = CreateObject("SldWorks.Application")

                        ' Get active Assembly document

                        Set AssyDoc = swApp.ActiveDoc()

                        Set SelMgr = AssyDoc.SelectionManager()

                        Set SelData = SelMgr.CreateSelectData

                      

                        'AssyDoc.SelectByID "pino", "COMPONENT", 0, 0, 0

                        Set Comp = AssyDoc.GetComponentByName(CompName)

                      

                        'If (SelMgr.GetSelectedObjectType2(1) <> swSelCOMPONENTS) Then

                            'swApp.SendMsgToUser "Please Select an Assembly Component."

                            'Exit Sub

                        'End If

                      

                        Set Body = Comp.GetBody() ' Get the Component Body

                      

                        If (Body Is Nothing) Then

                            swApp.SendMsgToUser "Component Body Unavailable."

                            swApp.SendMsgToUser "Make sure not lightweight or suppressed"

                            Exit Sub

                        End If

                      

                        'boolstatus = AssyDoc.DeSelectByID(CompName, "COMPONENT", 0, 0, 0)

                        Set Face = Body.GetFirstFace

                      

                        ' Traverse thru all body faces

                        Do While Not Face Is Nothing

                            ' Call ModelDoc.GetEntityName.

                            CurFaceName = AssyDoc.GetEntityName(Face)

                            If (CurFaceName = FaceName) Then

                                'Face.Select (0) ' Select the face

                                boolstatus = Face.Select4(True, SelData)

                                Exit Do

                            End If

                            Debug.Print CurFaceName

                            Set Face = Face.GetNextFace

                        Loop

                     

                    End Sub