This macro finds and replaces the text in the dimension names of the selected feature or features (similar to Find-Replace feature in text editors) using SOLIDWORKS API:
Specify the text to find and the text to replace. Only include short dimension name.
For example the dimension D1 in Sketch1 will have a short name D1 and full name D1@Sketch1. Specifying D in find field and B in replace field will result in dimension to be renamed to B1@Sketch1.
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swSelMgr As SldWorks.SelectionMgr
Sub main()
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swSelMgr = swModel.SelectionManager
IfNot swModel IsNothingThenDim fromText AsStringDim toText AsString
fromText = InputBox("Specify the text to find")
toText = InputBox("Specify the text to replace")
Dim i AsIntegerDim isFeatSelected AsBoolean
isFeatSelected = FalseFor i = 1 To swSelMgr.GetSelectedObjectCount2(-1)
Dim swFeat As SldWorks.Feature
Set swFeat = swSelMgr.GetSelectedObject6(i, -1)
IfNot swFeat IsNothingThen
isFeatSelected = TrueDim swDispDim As SldWorks.DisplayDimension
Set swDispDim = swFeat.GetFirstDisplayDimension
WhileNot swDispDim IsNothingDim swDim As SldWorks.Dimension
Set swDim = swDispDim.GetDimension2(0)
swDim.Name = Replace(swDim.Name, fromText, toText)
Set swDispDim = swFeat.GetNextDisplayDimension(swDispDim)
Wend
EndIfNextIfNot isFeatSelected Then
MsgBox "Please select feature(s) you want to rename dimensions in"EndIfElse
MsgBox "Please open the model"EndIfEndSub