This website uses cookies to ensure you get the best experience on our website. By using our website you agree on the following Cookie Policy, Privacy Policy, and Terms Of Use
OUT_FOLDER - Full path to output folder where the resulting files need to be saved
Const OUT_FOLDER AsString = "OUTPUT FOLDER PATH"
PERMUTE_ASSEMBLY_CONF option allows to specify if the configurations of assembly need to be used in permutation or only components
Const PERMUTE_ASSEMBLY_CONF AsBoolean = True'True to include assembly configurations, false to only include components
Notes
Original states of the components will not be restored as the result of running the macro. It is recommended to open the assembly as read only
Total number of files equals to {number of configuration of component 1}x{number of configuration of component 2}x...x
Const PERMUTE_ASSEMBLY_CONF AsBoolean = TrueConst OUT_FOLDER AsString = "OUTPUT FOLDER PATH"Dim swApp As SldWorks.SldWorks
Sub main()
Set swApp = Application.SldWorks
Dim swAssy As SldWorks.AssemblyDoc
Set swAssy = GetActiveAssembly(swApp)
IfNot swAssy IsNothingThenIf swAssy.ResolveAllLightWeightComponents(True) = swComponentResolveStatus_e.swResolveOk Then
PermuteAssembly swAssy, PERMUTE_ASSEMBLY_CONF
Else
MsgBox "Components must be resolved"EndIfElse
MsgBox "Please open assembly"EndIfEndSubSub PermuteAssembly(assy As SldWorks.AssemblyDoc, permuteAssemblyConfs AsBoolean)
Dim vComps AsVariantIf permuteAssemblyConfs ThenDim vAssyConfs AsVariant
vAssyConfs = assy.GetConfigurationNames()
Dim i AsIntegerFor i = 0 To UBound(vAssyConfs)
assy.ShowConfiguration CStr(vAssyConfs(i))
vComps = assy.GetComponents(True)
PermuteComponents assy, vComps
NextElse
vComps = swAssy.GetComponents(True)
PermuteComponents assy, vComps
EndIfEndSubSub PermuteComponents(assy As SldWorks.AssemblyDoc, vComps AsVariant)
IfNot IsEmpty(vComps) And UBound(vComps) > -1 ThenDim swComp As SldWorks.Component2
Set swComp = vComps(0)
IfNot swComp.IsSuppressed() ThenDim vNextComps() As SldWorks.Component2
If UBound(vComps) > 0 ThenReDim vNextComps(UBound(vComps) - 1)
Dim k AsIntegerFor k = 1 To UBound(vComps)
Set vNextComps(k - 1) = vComps(k)
NextEndIfDim swCompModel As SldWorks.ModelDoc2
Set swCompModel = swComp.GetModelDoc2
Dim vConfNames AsVariant
vConfNames = swCompModel.GetConfigurationNames
Dim i AsIntegerFor i = 0 To UBound(vConfNames)
swComp.ReferencedConfiguration = CStr(vConfNames(i))
PermuteComponents assy, vNextComps
NextEndIfElse
assy.EditRebuild
ExportCurrentAssemblySnapshot assy, OUT_FOLDER
EndIfEndSubFunction ExportCurrentAssemblySnapshot(assy As SldWorks.AssemblyDoc, outFolder AsString) AsBooleanDim fso AsObjectSet fso = CreateObject("Scripting.FileSystemObject")
Dim filePath AsStringDim index AsInteger
index = 1
Const EXT AsString = ".sldasm"Do
filePath = TrimEnd(outFolder, "\") & "\" & TrimEnd(assy.GetTitle(), EXT) & index & EXT
index = index + 1
LoopWhile fso.FileExists(filePath)
Dim swAssyModel As SldWorks.ModelDoc2
Set swAssyModel = assy
ExportCurrentAssemblySnapshot = swAssyModel.Extension.SaveAs(filePath, swSaveAsVersion_e.swSaveAsCurrentVersion, swSaveAsOptions_e.swSaveAsOptions_Silent + swSaveAsOptions_e.swSaveAsOptions_Copy, Nothing, 0, 0)
EndFunctionFunction GetActiveAssembly(app As SldWorks.SldWorks) As SldWorks.AssemblyDoc
OnErrorResumeNextDim swAssy As SldWorks.AssemblyDoc
Set swAssy = app.ActiveDoc
Set GetActiveAssembly = swAssy
EndFunctionFunction TrimEnd(inputText AsString, text AsString) AsStringDim textLen AsInteger
textLen = Len(text)
If LCase(Right(inputText, textLen)) = LCase(text) Then
inputText = Left(inputText, Len(inputText) - textLen)
EndIf
TrimEnd = inputText
EndFunction
Notifications
Join session by SOLIDWORKS and PDM API expret Artem Taturevych at 3DEXPERIENCE World 2025 on Feb 26 at 08:30 AM CST to explore 10 essential macros for automating drawings, assemblies, custom properties, and more