Oh no! Where's the JavaScript?
Your Web browser does not have JavaScript enabled or does not support JavaScript. Please enable JavaScript on your Web browser to properly view this Web site, or upgrade to a Web browser that does support JavaScript.

SOLIDWORKS VBA ASSEMBLY - SolidWorks VBA to automate assembly tasks

Last updated on 2 months ago
C
caaSuper Admin
Posted 2 months ago
Creating a VBA (Visual Basic for Applications) script for SolidWorks to automate assembly tasks can be a powerful way to streamline your workflow. Below is a step-by-step guide and a sample VBA macro for creating a simple assembly in SolidWorks.
SolidWorks VBA Assembly Sample

Objective: Create a simple assembly consisting of two parts: a base plate and a block. We'll position the block on top of the base plate using mates.
Prerequisites

SolidWorks Installed: Ensure you have SolidWorks installed on your machine with VBA enabled.
Basic Understanding of SolidWorks: Familiarity with SolidWorks parts and assemblies is helpful.
VBA Editor: Access to the VBA editor within SolidWorks.

Step-by-Step Guide

Create Parts:
Base Plate: A simple rectangular plate.
Block: A cube or rectangular block to place on top of the base plate.

Save Parts:
Save these parts as separate SolidWorks part files (.SLDPRT).

Open VBA Editor:
In SolidWorks, go to Tools -> Macro -> New or Edit to open the VBA editor.

Write the VBA Macro:
Use the following sample code to create an assembly and add the parts with mates.
C
caaSuper Admin
Posted 2 months ago
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
C
caaSuper Admin
Posted 2 months ago

 ' 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
C
caaSuper Admin
Posted 2 months ago

 ' 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
C
caaSuper Admin
Posted 2 months ago
Explanation

Initialize SolidWorks Application:
Set swApp = Application.SldWorks initializes the SolidWorks application object.

Create New Assembly:
swApp.NewDocument creates a new assembly document using the specified template.

Add Components:
swAssembly.AddComponent inserts parts into the assembly at specified coordinates.

Add Mate:
Mates are added to position the components relative to each other.
AddMate3 creates a coincident mate between specified faces.

Save Assembly:
swModel.SaveAs saves the assembly to the specified path.

Important Notes

Paths: Update partPath1, partPath2, and assemblyPath with your actual file paths.
Face Selection: Adjust face selection logic as needed for your specific parts.
Error Handling: The macro includes basic error handling for failed operations.

Additional Tips

Recording Macros: Use SolidWorks’ built-in macro recorder to capture repetitive tasks and adapt the generated code.
Debugging: Use breakpoints and the immediate window in the VBA editor to debug and test your macros.
SolidWorks API Documentation: Refer to SolidWorks API documentation for detailed information on available functions and classes.
You can view all discussion threads in this forum.
You cannot start a new discussion thread in this forum.
You cannot reply in this discussion thread.
You cannot start on a poll in this forum.
You cannot upload attachments in this forum.
You cannot download attachments in this forum.
Sign In
Not a member yet? Click here to register.
Forgot Password?
Users Online Now
Guests Online 1
Members Online 0

Total Members: 10
Newest Member: rain