AnsweredAssumed Answered

Get the third and additional mate for locking rotation.

Question asked by Divyanshu Bhardwaj on Feb 3, 2016
Latest reply on Feb 10, 2016 by Divyanshu Bhardwaj

Hi all,

     I am trying to write a macro for selecting the components (Fasteners inserted though Design Library), and then select the Front plane of the assembly. I want to add parallel mate in this case to lock the rotation of the fastener. I wrote the code as:

Option Explicit

Dim swApp As SldWorks.SldWorks


Sub main()

Set swApp = Application.SldWorks


Dim swModel As SldWorks.ModelDoc2

Set swModel = swApp.ActiveDoc


Dim swAssembly As SldWorks.AssemblyDoc

Set swAssembly = swModel


Dim swSelMgr As SldWorks.SelectionMgr

Dim swModelDocExt As SldWorks.ModelDocExtension

Set swModelDocExt = swModel.Extension

Set swSelMgr = swModel.SelectionManager


Dim swComponent As SldWorks.Component2

Dim sel_count As Integer

Dim sel_type As Integer

'Dim swPlane As Plane

Dim sel_obj As Object


Dim path_name As String

path_name = swModel.GetPathName

Dim assem_name() As String

assem_name = Split(path_name, "\")

Dim new_assem_name As String

new_assem_name = assem_name(UBound(assem_name))

Dim Assembly() As String

Assembly = Split(new_assem_name, ".SLDASM")

Dim assem_name_to As String

assem_name_to = Assembly(0)


Set sel_obj = swSelMgr.GetSelectedObject6(1, -1)

sel_count = swSelMgr.GetSelectedObjectCount2(-1)

Dim i As Integer

Dim sel_comp_name As String

Dim bool As Boolean, Rbool As Boolean

bool = False

Rbool = False


Dim temp As Integer

temp = 0

Dim plane_name As String

Dim swMate As Mate2

Dim error As Long

Dim p_name As String

Dim retbool As Boolean

retbool = False

If sel_count = 0 Then

    MsgBox "You haven't selected anything!!!"


    For i = 0 To sel_count - 1

        bool = False

        bool = swAssembly.Extension.SelectByID2("Front Plane", "PLANE", 0, 0, 0, True, 0, Nothing, 0)

        If temp <> 0 Then

            bool = swAssembly.Extension.SelectByID2("Front Plane", "PLANE", 0, 0, 0, True, 0, Nothing, 0)

        End If

        sel_type = swSelMgr.GetSelectedObjectType3(1, -1)

        If sel_type = 20 Then

            p_name = "PLANE"

        End If

        Set swComponent = swSelMgr.GetSelectedObjectsComponent4(i + 1, -1)

        sel_comp_name = swComponent.Name2()

        plane_name = "Front Plane" & "@" & sel_comp_name & "@" & assem_name_to

        Rbool = swAssembly.Extension.SelectByID2(plane_name, p_name, 0, 0, 0, True, 0, Nothing, 0)

        Set swMate = swAssembly.AddMate4(swMateType_e.swMatePARALLEL, swMateAlign_e.swMateAlignALIGNED, True, 0, 0, 0, 0, 0, 0, 0, 0, False, False, error)


        'swAssembly.Extension.DeleteSelection2 (swDeleteSelectionOptions_e.swDelete_Absorbed)

        'swAssembly.ClearSelection2 True

        Debug.Print sel_comp_name

        Rbool = False

        temp = temp + 1

    Next i

End If

End Sub


Here What i am trying to do is, select "Front Plane" or "Plane1" of selected component in the assembly, and one "Front Plane" which will be common for main assembly, bring them together and add mate (parallel). Then again in the next go it should pick the appropriate plane from the component and one common front plane from assembly. But my problem is, how to clear the previous selection for component plane without clearing overall selection. After this, if a fastener is inserted at circumference of the rotatory part (Considering, that right now i am inserting the fasteners parallel to the axis of cylindrical part), it should select assembly plane or face as user choice so that once again i bring some mate to constrain the fastener. I don't want to use LOCK MATE or FIX options.