Option Explicit
Sub Main()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swAssembly As SldWorks.AssemblyDoc
Dim swComp As SldWorks.Component2
Dim swMate As SldWorks.Mate2
Dim swFeature As SldWorks.Feature
Dim partPath1 As String
Dim partPath2 As String
Dim assemblyPath As String
' Initialize SolidWorks application
Set swApp = Application.SldWorks
' Create a new assembly document
Set swModel = swApp.NewDocument("C:ProgramDataSolidWorksSOLIDWORKS 2022templatesAssembly.asmdot", 0, 0, 0)
Set swAssembly = swModel
' Paths to the part files
partPath1 = "C:pathtoBasePlate.sldprt" ' Change this to your actual path
partPath2 = "C:pathtoBlock.sldprt" ' Change this to your actual path
' Insert base plate
Set swComp = swAssembly.AddComponent(partPath1, 0, 0, 0)
If swComp Is Nothing Then
MsgBox "Failed to insert base plate."
Exit Sub
End If
' Insert block
Set swComp = swAssembly.AddComponent(partPath2, 0, 0, 0.1)
If swComp Is Nothing Then
MsgBox "Failed to insert block."
Exit Sub
End If
' Mate the block to the base plate
Set swMate = swAssembly.AddMate(0, 0, 0, 1, swComp)
' If you want to add specific mates:
' 1. Get the faces to mate
Dim swFace1 As SldWorks.Face2
Dim swFace2 As SldWorks.Face2
Dim swPart1 As SldWorks.PartDoc
Dim swPart2 As SldWorks.PartDoc
Dim swComp1 As SldWorks.Component2
Dim swComp2 As SldWorks.Component2
' Set components
Set swComp1 = swAssembly.GetComponentByName("BasePlate-1")
Set swComp2 = swAssembly.GetComponentByName("Block-1")
' Set parts
Set swPart1 = swComp1.GetModelDoc2
Set swPart2 = swComp2.GetModelDoc2
' Get faces (assuming the first face of each part)
Set swFace1 = swPart1.GetEntityByName("Face<1>", swSelectType_e.swSelFACES)
Set swFace2 = swPart2.GetEntityByName("Face<1>", swSelectType_e.swSelFACES)
' Add mate
Set swMate = swAssembly.AddMate3(swMateType_e.swMateCOINCIDENT, swMateAlign_e.swAlignSTANDARD, True, 0, 0, 0, 0, 0, 0, 0, 0, False, swFace1, swFace2)
If swMate Is Nothing Then
MsgBox "Failed to add mate."
End If
' Save the assembly
assemblyPath = "C:pathtoAssembly1.sldasm" ' Change this to your actual path
swModel.SaveAs assemblyPath
MsgBox "Assembly created and saved successfully!"
End Sub
' Insert block
Set swComp = swAssembly.AddComponent(partPath2, 0, 0, 0.1)
If swComp Is Nothing Then
MsgBox "Failed to insert block."
Exit Sub
End If
' Mate the block to the base plate
Set swMate = swAssembly.AddMate(0, 0, 0, 1, swComp)
' If you want to add specific mates:
' 1. Get the faces to mate
Dim swFace1 As SldWorks.Face2
Dim swFace2 As SldWorks.Face2
Dim swPart1 As SldWorks.PartDoc
Dim swPart2 As SldWorks.PartDoc
Dim swComp1 As SldWorks.Component2
Dim swComp2 As SldWorks.Component2
' Set components
Set swComp1 = swAssembly.GetComponentByName("BasePlate-1")
Set swComp2 = swAssembly.GetComponentByName("Block-1")
' Set parts
Set swPart1 = swComp1.GetModelDoc2
Set swPart2 = swComp2.GetModelDoc2
' Get faces (assuming the first face of each part)
Set swFace1 = swPart1.GetEntityByName("Face<1>", swSelectType_e.swSelFACES)
Set swFace2 = swPart2.GetEntityByName("Face<1>", swSelectType_e.swSelFACES)
' Add mate
Set swMate = swAssembly.AddMate3(swMateType_e.swMateCOINCIDENT, swMateAlign_e.swAlignSTANDARD, True, 0, 0, 0, 0, 0, 0, 0, 0, False, swFace1, swFace2)
If swMate Is Nothing Then
MsgBox "Failed to add mate."
End If
' Save the assembly
assemblyPath = "C:pathtoAssembly1.sldasm" ' Change this to your actual path
swModel.SaveAs assemblyPath
MsgBox "Assembly created and saved successfully!"
End Sub
' Get faces (assuming the first face of each part)
Set swFace1 = swPart1.GetEntityByName("Face<1>", swSelectType_e.swSelFACES)
Set swFace2 = swPart2.GetEntityByName("Face<1>", swSelectType_e.swSelFACES)
' Add mate
Set swMate = swAssembly.AddMate3(swMateType_e.swMateCOINCIDENT, swMateAlign_e.swAlignSTANDARD, True, 0, 0, 0, 0, 0, 0, 0, 0, False, swFace1, swFace2)
If swMate Is Nothing Then
MsgBox "Failed to add mate."
End If
' Save the assembly
assemblyPath = "C:pathtoAssembly1.sldasm" ' Change this to your actual path
swModel.SaveAs assemblyPath
MsgBox "Assembly created and saved successfully!"
End Sub