ds-blue-logo
Preview  |  SOLIDWORKS USER FORUM
Use your SOLIDWORKS ID or 3DEXPERIENCE ID to log in.
DTDiogo Teixeira11/11/2017

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