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
This VBA macro allows to rename all bodies which belong to cut-list folder (e.g. Sheet Metal or Weldment) based on the predefined naming template. It is possible to use free text in combination with custom property placeholder which will allow to use custom property in the name.
Custom property must be enclosed within <> symbols
For example to rename all sheet metal bodies using SM_ prefix followed by value of the thickness, NAME_TEMPLATE variable should be defined as:
Const NAME_TEMPLATE AsString = "SM_<Thickness>"
Notes
Macro may require model rebuild after the run to refresh the names of the features
If several bodies reside within one cut-list folder, index will be used to differentiate the names, e.g. -1, -2, -3
Macro will perform the cut-list update before renaming
Const NAME_TEMPLATE AsString = "<PartNo>"Dim swApp As SldWorks.SldWorks
Sub main()
Set swApp = Application.SldWorks
Dim swPart As SldWorks.PartDoc
Set swPart = swApp.ActiveDoc
ProcessCutLists swPart
EndSubSub ProcessCutLists(model As SldWorks.ModelDoc2)
Dim swFeat As SldWorks.Feature
Set swFeat = model.FirstFeature
DoWhileNot swFeat IsNothingDim swBodyFolder As SldWorks.BodyFolder
If swFeat.GetTypeName2() = "SolidBodyFolder"ThenSet swBodyFolder = swFeat.GetSpecificFeature2
swBodyFolder.UpdateCutList
ElseIf swFeat.GetTypeName2() = "CutListFolder"ThenSet swBodyFolder = swFeat.GetSpecificFeature2
Dim name AsString
name = ComposeName(NAME_TEMPLATE, swFeat)
RenameBodies swBodyFolder.GetBodies(), name
EndIfSet swFeat = swFeat.GetNextFeature
LoopEndSubSub RenameBodies(bodies AsVariant, bodyName AsString)
IfNot IsEmpty(bodies) ThenDim i AsIntegerFor i = 0 To UBound(bodies)
Dim swBody As SldWorks.Body2
Set swBody = bodies(i)
swBody.name = bodyName & IIf(i > 0, "-" & CStr(i), "")
NextEndIfEndSubFunction ComposeName(template AsString, cutListFeat As SldWorks.Feature) AsStringDim regEx AsObjectSet regEx = CreateObject("VBScript.RegExp")
regEx.Global = True
regEx.IgnoreCase = True
regEx.Pattern = "<[^>]*>"Dim regExMatches AsObjectSet regExMatches = regEx.Execute(template)
Dim i AsIntegerDim outName AsString
outName = template
For i = regExMatches.Count - 1 To 0 Step -1
Dim regExMatch AsObjectSet regExMatch = regExMatches.Item(i)
Dim prpName AsString
prpName = Mid(regExMatch.Value, 2, Len(regExMatch.Value) - 2)
outName = Left(outName, regExMatch.FirstIndex) & GetPropertyValue(cutListFeat.CustomPropertyManager, prpName) & Right(outName, Len(outName) - (regExMatch.FirstIndex + regExMatch.Length))
Next
ComposeName = outName
EndFunctionFunction GetPropertyValue(custPrpMgr As SldWorks.CustomPropertyManager, prpName AsString) AsStringDim resVal AsString
custPrpMgr.Get2 prpName, "", resVal
GetPropertyValue = resVal
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