Hello,
I want user to select the part/parts to explode per axis and space. Below is want i get but failing at highlighted line. Can someone assist me with this?
Option Explicit
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.AssemblyDoc
Dim config As SldWorks.Configuration
Dim swMdl As SldWorks.ModelDoc2
Dim explStep As SldWorks.ExplodeStep
Dim comp As SldWorks.Component2
Dim var As Variant
Dim boolstatus As Boolean
Dim i As Long
Dim errCode As Long
Sub main()
' Connect to SolidWorks
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set config = swModel.ConfigurationManager.ActiveConfiguration
Set swMdl = swModel
' Ask user to select components
MsgBox "Please select the components you want to explode in the graphics area, then click OK.", vbInformation
' Get selected components
Dim selMgr As SldWorks.SelectionMgr
Set selMgr = swModel.SelectionManager
Dim compCount As Long
compCount = selMgr.GetSelectedObjectCount2(-1)
If compCount = 0 Then
MsgBox "No components selected!", vbCritical
Exit Sub
End If
' Ask user for axis name
Dim axisName As String
axisName = InputBox("Enter axis name (AxisX, AxisY, AxisZ):", "Select Axis", "AxisY")
If axisName = "" Then
MsgBox "Axis name is required!", vbCritical
Exit Sub
End If
' Ask user for explode distance in millimeters
Dim explodeDistMM As Double
explodeDistMM = Val(InputBox("Enter explode distance in millimeters:", "Explode Distance", "50"))
If explodeDistMM <= 0 Then
MsgBox "Invalid distance!", vbCritical
Exit Sub
End If
' Convert mm to meters for SolidWorks API
Dim explodeDistM As Double
explodeDistM = explodeDistMM / 1000
' Select axis reference
boolstatus = swModel.Extension.SelectByID2(axisName, "AXIS", 0, 0, 0, True, 2, Nothing, 0)
If Not boolstatus Then
MsgBox "Axis '" & axisName & "' not found!", vbCritical
Exit Sub
End If
' Add explode step
Set explStep = config.AddExplodeStep2(explodeDistM, -1, False, 0, -1, False, False, False, errCode)
' Add selected components to explode step
Dim compArray() As SldWorks.Component2
ReDim compArray(compCount - 1)
For i = 1 To compCount
Set compArray(i - 1) = selMgr.GetSelectedObjectsComponent3(i, -1)
Next
explStep.SetComponents compArray
' Rebuild
swMdl.EditRebuild3
' Print details
Debug.Print "Explode step: " & explStep.Name
Debug.Print "Axis used: " & axisName
Debug.Print "Explode distance: " & explodeDistMM & " mm"
Debug.Print "Components moved:"
For i = 0 To UBound(compArray)
Debug.Print " " & compArray(i).Name2
Next
MsgBox "Explode step created successfully!" & vbCrLf & _
"Parts Selected: " & compArray(0).Name2 & vbCrLf & _
"Axis: " & axisName & vbCrLf & _
"Distance: " & explodeDistMM & " mm", vbInformation
End Sub
