Macro to Mate Origin - Origin

Programming and macros
User avatar
zwei
Posts: 701
Joined: Mon Mar 15, 2021 9:17 pm
Answers: 18
Location: Malaysia
x 185
x 600

Macro to Mate Origin - Origin

Unread post by zwei »

Asking around before i start writing my own macro...

Do anyone has a macro or similar macro that do the following?

The current idea for the workflow is that:
1. Select TWO component in feature tree
2. Run the macro
3. The macro mate the two component together using origin
by Stefan Sterk » Fri Jul 08, 2022 6:22 am
I contacted API Support and they suggest using AddMate5 with swMateCOORDINATE, which will align the axes.
The code snippet below works as expected. Have fun with it ;)

Code: Select all

Sub main()
    Dim swApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    Dim swAsm As SldWorks.AssemblyDoc
    Dim swSelMgr As SldWorks.SelectionMgr
    Dim swFeat As SldWorks.Feature
    Dim swSketch As SldWorks.Sketch
    Dim swSkPoint As SldWorks.SketchPoint
    Dim SelComps(1) As Object
    Dim i As Integer

    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    
    If swModel Is Nothing Then MsgBox "Please open a Assembly!": End
    If swModel.GetType <> swDocASSEMBLY Then MsgBox "Please open a Assembly!": End
    
    Set swAsm = swModel
    Set swSelMgr = swModel.SelectionManager
    Set SelComps(0) = swSelMgr.GetSelectedObjectsComponent(1)
    Set SelComps(1) = swSelMgr.GetSelectedObjectsComponent(2)
    
    If swSelMgr.GetSelectedObjectCount2(-1) > 2 Then MsgBox "Please select only two components!": End
    If swSelMgr.GetSelectedObjectCount2(-1) <> 2 Then MsgBox "Please select two components!": End
    If SelComps(0) Is Nothing Or SelComps(1) Is Nothing Then MsgBox "Please select two components!": End
    
    swModel.ClearSelection2 True
    For i = 0 To 1
        Set swFeat = SelComps(i).FirstFeature
        Do While Not swFeat Is Nothing
            If "OriginProfileFeature" = swFeat.GetTypeName Then
                Set swSketch = swFeat.GetSpecificFeature2
                Set swSkPoint = swSketch.GetSketchPoints2()(0)
                swSkPoint.Select4 True, Nothing
                Exit Do
            End If
            Set swFeat = swFeat.GetNextFeature
        Loop
    Next i
    
    swAsm.AddMate5 20, -1, False, 0, 0, 0, 0, 0, 0, 0, 0, False, False, 0, Empty
    swModel.EditRebuild3
    swModel.ClearSelection2 True
    
End Sub
Go to full post
Far too many items in the world are designed, constructed and foisted upon us with no understanding-or even care-for how we will use them.
User avatar
Stefan Sterk
Posts: 37
Joined: Tue Aug 10, 2021 2:40 am
Answers: 3
x 51
x 77

Re: Macro to Mate Origin - Origin

Unread post by Stefan Sterk »

Hi Zhen,

The code that follows fulfills your request. The only issue I can identify is that the Origin Axes don't align as they would if I did it manually. grumph

Code: Select all

Sub main()
    Dim swApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    Dim swAsm As SldWorks.AssemblyDoc
    Dim swSelMgr As SldWorks.SelectionMgr
    Dim swSelComp As SldWorks.Component2
    Dim swSelComp1 As SldWorks.Component2
    Dim swSelComp2 As SldWorks.Component2
    Dim swFeat As SldWorks.Feature
    Dim swSketch As SldWorks.Sketch
    Dim swSkPoint As SldWorks.SketchPoint
    Dim swCoincMateData As SldWorks.CoincidentMateFeatureData
    Dim EntitiesToMate(1) As Object
    Dim EntitiesToMateVar As Variant
    Dim i As Integer

    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    
    If swModel Is Nothing Then MsgBox "Please open a Assembly!": End
    If swModel.GetType <> swDocASSEMBLY Then MsgBox "Please open a Assembly!": End
    
    Set swAsm = swModel
    Set swSelMgr = swModel.SelectionManager
    Set swSelComp1 = swSelMgr.GetSelectedObjectsComponent(1)
    Set swSelComp2 = swSelMgr.GetSelectedObjectsComponent(2)
    
    ' Check selection
    If swSelMgr.GetSelectedObjectCount2(-1) > 2 Then MsgBox "Please select only two components!": End
    If swSelMgr.GetSelectedObjectCount2(-1) <> 2 Then MsgBox "Please select two components!": End
    If swSelComp1 Is Nothing Or swSelComp2 Is Nothing Then MsgBox "Please select two components!": End
    
    ' Get origins to mate
    For i = 0 To 1
        Set swSelComp = swSelMgr.GetSelectedObjectsComponent(i + 1)
        Set swFeat = swSelComp.FirstFeature
        Do While Not swFeat Is Nothing
            If "OriginProfileFeature" = swFeat.GetTypeName Then
                Set swSketch = swFeat.GetSpecificFeature2
                Set swSkPoint = swSketch.GetSketchPoints2()(0)
                Set EntitiesToMate(i) = swSkPoint
                Exit Do
            End If
            Set swFeat = swFeat.GetNextFeature
        Loop
    Next i
    EntitiesToMateVar = EntitiesToMate

    ' Create CoincidentMateFeatureData
    Set swCoincMateData = swModel.CreateMateData(0)
    swCoincMateData.EntitiesToMate = (EntitiesToMateVar)
    swCoincMateData.MateAlignment = 0
    swModel.CreateMate swCoincMateData
    
End Sub
User avatar
Stefan Sterk
Posts: 37
Joined: Tue Aug 10, 2021 2:40 am
Answers: 3
x 51
x 77

Re: Macro to Mate Origin - Origin

Unread post by Stefan Sterk »

I contacted API Support and they suggest using AddMate5 with swMateCOORDINATE, which will align the axes.
The code snippet below works as expected. Have fun with it ;)

Code: Select all

Sub main()
    Dim swApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    Dim swAsm As SldWorks.AssemblyDoc
    Dim swSelMgr As SldWorks.SelectionMgr
    Dim swFeat As SldWorks.Feature
    Dim swSketch As SldWorks.Sketch
    Dim swSkPoint As SldWorks.SketchPoint
    Dim SelComps(1) As Object
    Dim i As Integer

    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    
    If swModel Is Nothing Then MsgBox "Please open a Assembly!": End
    If swModel.GetType <> swDocASSEMBLY Then MsgBox "Please open a Assembly!": End
    
    Set swAsm = swModel
    Set swSelMgr = swModel.SelectionManager
    Set SelComps(0) = swSelMgr.GetSelectedObjectsComponent(1)
    Set SelComps(1) = swSelMgr.GetSelectedObjectsComponent(2)
    
    If swSelMgr.GetSelectedObjectCount2(-1) > 2 Then MsgBox "Please select only two components!": End
    If swSelMgr.GetSelectedObjectCount2(-1) <> 2 Then MsgBox "Please select two components!": End
    If SelComps(0) Is Nothing Or SelComps(1) Is Nothing Then MsgBox "Please select two components!": End
    
    swModel.ClearSelection2 True
    For i = 0 To 1
        Set swFeat = SelComps(i).FirstFeature
        Do While Not swFeat Is Nothing
            If "OriginProfileFeature" = swFeat.GetTypeName Then
                Set swSketch = swFeat.GetSpecificFeature2
                Set swSkPoint = swSketch.GetSketchPoints2()(0)
                swSkPoint.Select4 True, Nothing
                Exit Do
            End If
            Set swFeat = swFeat.GetNextFeature
        Loop
    Next i
    
    swAsm.AddMate5 20, -1, False, 0, 0, 0, 0, 0, 0, 0, 0, False, False, 0, Empty
    swModel.EditRebuild3
    swModel.ClearSelection2 True
    
End Sub
User avatar
AlexLachance
Posts: 2184
Joined: Thu Mar 11, 2021 8:14 am
Answers: 17
Location: Quebec
x 2364
x 2013

Re: Macro to Mate Origin - Origin

Unread post by AlexLachance »

Hey guys, just stopping by to hola at both of you!
User avatar
zwei
Posts: 701
Joined: Mon Mar 15, 2021 9:17 pm
Answers: 18
Location: Malaysia
x 185
x 600

Re: Macro to Mate Origin - Origin

Unread post by zwei »

Stefan Sterk wrote: Fri Jul 08, 2022 6:22 am I contacted API Support and they suggest using AddMate5 with swMateCOORDINATE, which will align the axes.
The code snippet below works as expected. Have fun with it ;)

Code: Select all

Sub main()
    Dim swApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    Dim swAsm As SldWorks.AssemblyDoc
    Dim swSelMgr As SldWorks.SelectionMgr
    Dim swFeat As SldWorks.Feature
    Dim swSketch As SldWorks.Sketch
    Dim swSkPoint As SldWorks.SketchPoint
    Dim SelComps(1) As Object
    Dim i As Integer

    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    
    If swModel Is Nothing Then MsgBox "Please open a Assembly!": End
    If swModel.GetType <> swDocASSEMBLY Then MsgBox "Please open a Assembly!": End
    
    Set swAsm = swModel
    Set swSelMgr = swModel.SelectionManager
    Set SelComps(0) = swSelMgr.GetSelectedObjectsComponent(1)
    Set SelComps(1) = swSelMgr.GetSelectedObjectsComponent(2)
    
    If swSelMgr.GetSelectedObjectCount2(-1) > 2 Then MsgBox "Please select only two components!": End
    If swSelMgr.GetSelectedObjectCount2(-1) <> 2 Then MsgBox "Please select two components!": End
    If SelComps(0) Is Nothing Or SelComps(1) Is Nothing Then MsgBox "Please select two components!": End
    
    swModel.ClearSelection2 True
    For i = 0 To 1
        Set swFeat = SelComps(i).FirstFeature
        Do While Not swFeat Is Nothing
            If "OriginProfileFeature" = swFeat.GetTypeName Then
                Set swSketch = swFeat.GetSpecificFeature2
                Set swSkPoint = swSketch.GetSketchPoints2()(0)
                swSkPoint.Select4 True, Nothing
                Exit Do
            End If
            Set swFeat = swFeat.GetNextFeature
        Loop
    Next i
    
    swAsm.AddMate5 20, -1, False, 0, 0, 0, 0, 0, 0, 0, 0, False, False, 0, Empty
    swModel.EditRebuild3
    swModel.ClearSelection2 True
    
End Sub
Sorry for the late reply... This totally slipped my mind after my vacation...

The macro work like a charm

Thanks a lot.
Far too many items in the world are designed, constructed and foisted upon us with no understanding-or even care-for how we will use them.
User avatar
mp3-250
Posts: 630
Joined: Tue Sep 28, 2021 4:09 am
Answers: 20
Location: Japan
x 702
x 347

Re: Macro to Mate Origin - Origin

Unread post by mp3-250 »

I was trying addmate5 for a while and the answer helped me to understand the problem was the axis alignment required an undocumented "-1" after the swMateCOORDINATE "20". lol

Thank you!

https://help.solidworks.com/2024/Englis ... Redirect=1

Member Description
swAlignAGAINST Obsolete. Do not use.
swAlignNONE Obsolete. Do not use.
swAlignSAME Obsolete. Do not use.
swMateAlignALIGNED 0
swMateAlignANTI_ALIGNED 1
swMateAlignCLOSEST 2
Post Reply