AnsweredAssumed Answered

Macro to mate parts in an assembly

Question asked by Joe Pickens on Feb 17, 2016
Latest reply on Feb 17, 2016 by Joe Pickens

I am running this macro to mate components in an assembly and its not making the selection like it should


'Sub Add_Mates(SWAsmName As String, SWCompName As String, SWAsm As SldWorks.AssemblyDoc)

Sub Add_Mates()


Dim SWApp               As SldWorks.SldWorks

Dim SWModel             As SldWorks.ModelDoc2

Dim SWDocExt            As SldWorks.ModelDocExtension

Dim SWMateFeat          As SldWorks.Feature

Dim Asm                 As SldWorks.AssemblyDoc

Dim MateName            As String

Dim FirstSelection      As String

Dim SecondSelection     As String

Dim CompName            As String

Dim AsmName             As String

Dim AsmName2            As String

Dim boolstat            As Boolean

Dim SWMateError         As Long

Dim Str                 As Variant


Set SWApp = CreateObject("SldWorks.Application")

Set SWModel = SWApp.ActiveDoc


'Create the name of the mate and the names of the entities to use for mating

'CompName = SWCompName

'AsmName = SWAsmName

'Asm = SWAssy


CompName = "148024005-1"

AsmName2 = SWModel.GetTitle

Str = Split(AsmName2, ".")

AsmName = Str(0)

Set Asm = SWModel


MateName = "top_coinc_" + CompName

FirstSelection = "Point1@Origin@" + CompName & "@" + AsmName

SecondSelection = "Point1@Origin@" + AsmName


SWModel.ClearSelection2 (True)


Set SWDocExt = SWModel.Extension


'Select entities for mating

boolstat = SWDocExt.SelectByID2(FirstSelection, "DATUMPOINT", 0, 0, 0, True, 1, Nothing, swSelectOptionDefault)

boolstat = SWDocExt.SelectByID2(SecondSelection, "DATUMPOINT", 0, 0, 0, True, 1, Nothing, swSelectOptionDefault)


'Add the mate

Set SWMateFeat = Asm.AddMate3(swMateCOINCIDENT, swMateAlignALIGNED, False, 0, 0, 0, 0, 0, 0, 0, 0, False, SWMateError)

SWMateFeat.Name = MateName


End Sub