This macro saves all selected bodies bodies (or all bodies if none selected) from the active part document into individual part documents.
Configuration
Specify the option to handle the transfer of custom properties by modifying the CUT_LIST_PRPS_TRANSFER constant
Specify the output directory in the OUT_DIR. If this variable is empty then bodies will be saved in the same directory as source part document.
Const CUT_LIST_PRPS_TRANSFER AsLong = swCutListTransferOptions_e.swCutListTransferOptions_CutListProperties 'move properties to cut-listsConst OUT_DIR AsString = "D:\Parts"'Export bodies to the Parts directory
Notes
Bodies remain linked to the original part
Output files will be named after the bodies
Special symbols which cannot be used in the file name (e.g. ?, *, : etc) will be replaced with _
Macro will not create an output folder if it does not exist and will fail
Const CUT_LIST_PRPS_TRANSFER AsLong = swCutListTransferOptions_e.swCutListTransferOptions_FileProperties
Const OUT_DIR AsString = ""Dim swApp As SldWorks.SldWorks
Sub main()
Set swApp = Application.SldWorks
Dim swPart As SldWorks.PartDoc
Set swPart = swApp.ActiveDoc
Dim vBodies AsVariant
vBodies = GetSelectedBodies(swPart.SelectionManager)
If IsEmpty(vBodies) Then
vBodies = swPart.GetBodies2(swBodyType_e.swSolidBody, True)
EndIfDim i AsIntegerFor i = 0 To UBound(vBodies)
Dim swBody As SldWorks.Body2
Set swBody = vBodies(i)
IfFalse <> swBody.Select2(False, Nothing) ThenDim outFilePath AsString
outFilePath = GetOutFilePath(swPart, swBody, OUT_DIR)
Dim errs AsLongDim warns AsLongIfFalse <> swPart.SaveToFile3(outFilePath, swSaveAsOptions_e.swSaveAsOptions_Silent, CUT_LIST_PRPS_TRANSFER, False, "", errs, warns) Then
swApp.CloseDoc outFilePath
Else
Err.Raise vbError, "", "Failed to save body " & swBody.Name & " to file " & outFilePath & ". Error code: " & errs
EndIfElse
Err.Raise vbError, "", "Failed to select body " & swBody.Name
EndIfNextEndSubFunction GetSelectedBodies(selMgr As SldWorks.SelectionMgr) AsVariantDim isInit AsBoolean
isInit = FalseDim swBodies() As SldWorks.Body2
Dim i AsIntegerFor i = 1 To selMgr.GetSelectedObjectCount2(-1)
Dim swBody As SldWorks.Body2
Set swBody = GetSelectedObjectBody(selMgr, i)
IfNot swBody IsNothingThenIfNot isInit ThenReDim swBodies(0)
Set swBodies(0) = swBody
isInit = TrueElseIfNot Contains(swBodies, swBody) ThenReDimPreserve swBodies(UBound(swBodies) + 1)
Set swBodies(UBound(swBodies)) = swBody
EndIfEndIfEndIfNextIf isInit Then
GetSelectedBodies = swBodies
Else
GetSelectedBodies = Empty
EndIfEndFunctionFunction GetSelectedObjectBody(selMgr As SldWorks.SelectionMgr, index AsInteger) As SldWorks.Body2
Dim swBody As SldWorks.Body2
Dim selObj AsObjectSet selObj = selMgr.GetSelectedObject6(index, -1)
IfNot selObj IsNothingThenIfTypeOf selObj Is SldWorks.Body2 ThenSet swBody = selObj
ElseIfTypeOf selObj Is SldWorks.Face2 ThenDim swFace As SldWorks.Face2
Set swFace = selObj
Set swBody = swFace.GetBody
ElseIfTypeOf selObj Is SldWorks.Edge ThenDim swEdge As SldWorks.Edge
Set swEdge = selObj
Set swBody = swEdge.GetBody
ElseIfTypeOf selObj Is SldWorks.Vertex ThenDim swVertex As SldWorks.Vertex
Set swVertex = selObj
Set swBody = swVertex.GetBody
EndIfEndIfSet GetSelectedObjectBody = swBody
EndFunctionFunction Contains(vArr AsVariant, item AsObject) AsBooleanDim i AsIntegerFor i = 0 To UBound(vArr)
If vArr(i) Is item Then
Contains = TrueExitFunctionEndIfNext
Contains = FalseEndFunctionFunction GetOutFilePath(model As SldWorks.ModelDoc2, body As SldWorks.Body2, outDir AsString) AsStringIf outDir = ""Then
outDir = model.GetPathName()
If outDir = ""Then
Err.Raise vbError, "", "Output directory cannot be composed as file was never saved"EndIf
outDir = Left(outDir, InStrRev(outDir, "\") - 1)
EndIfIf Right(outDir, 1) = "\"Then
outDir = Left(outDir, Len(outDir) - 1)
EndIf
GetOutFilePath = ReplaceInvalidPathSymbols(outDir & "\" & body.Name & ".sldprt")
EndFunctionFunction ReplaceInvalidPathSymbols(path AsString) AsStringConst REPLACE_SYMB AsString = "_"Dim res AsString
res = Right(path, Len(path) - Len("X:\"))
Dim drive AsString
drive = Left(path, Len("X:\"))
Dim invalidSymbols AsVariant
invalidSymbols = Array("/", ":", "*", "?", """", "<", ">", "|")
Dim i AsIntegerFor i = 0 To UBound(invalidSymbols)
Dim invalidSymb AsString
invalidSymb = CStr(invalidSymbols(i))
res = Replace(res, invalidSymb, REPLACE_SYMB)
Next
ReplaceInvalidPathSymbols = drive + res
EndFunction