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 macro renames all the features in active model in the sequential order using SOLIDWORKS API, preserving the base names .
Only indices are renamed and the base name is preserved. For example Sketch21 will be renamed to Sketch1 for the first appearance of the sketch feature.
Filter
Macro can be configured to only process features that match the specific type pattern. Use Get Feature Type Name macro to identify feature type.
Fill the FEATS_FILTER with the type names. Wildcard is supported
Dim FEATS_FILTER AsVariantSub main()
FEATS_FILTER = Array("Mate*", "MirrorCompFeat") 'only process mates and Mirror Component features Set swApp = Application.SldWorks
Notes
Only features with number at the end will be renamed (e.g. Front Plane will not be renamed to Front Plane1 and My1Feature will not be renamed)
Case is ignored (case insensitive search)
Only modelling features are renamed (the ones created after the Origin feature)
In the assembly documents, only assembly feature are renamed (components are ignored)
If components are selected in the assembly, features of those components will be renamed
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim FEATS_FILTER AsVariantSub main()
'FEATS_FILTER = Array("Mate*")Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
try_:
OnErrorGoTo catch_
IfNot swModel IsNothingThen
swModel.FeatureManager.EnableFeatureTree = False
swModel.FeatureManager.EnableFeatureTreeWindow = FalseDim vComps AsVariant
vComps = GetSelectedComponents(swModel.SelectionManager)
IfNot IsEmpty(vComps) ThenDim i AsIntegerFor i = 0 To UBound(vComps)
Dim swComp As SldWorks.Component2
Set swComp = vComps(i)
ProcessFeatureTree swComp.FirstFeature, swComp
NextElse
ProcessFeatureTree swModel.FirstFeature, swModel
EndIfElse
Err.Raise vbError, "", "Please open model"EndIfGoTo finally_
catch_:
swApp.SendMsgToUser2 Err.Description, swMessageBoxIcon_e.swMbStop, swMessageBoxBtn_e.swMbOk
finally_:
IfNot swModel IsNothingThen
swModel.FeatureManager.EnableFeatureTree = True
swModel.FeatureManager.EnableFeatureTreeWindow = TrueEndIfEndSubSub ProcessFeatureTree(firstFeat As SldWorks.Feature, owner AsObject)
Dim passedOrigin AsBoolean
passedOrigin = FalseDim featNamesTable AsObjectDim processedFeats() As SldWorks.Feature
Set featNamesTable = CreateObject("Scripting.Dictionary")
featNamesTable.CompareMode = vbTextCompare 'case insensitiveDim swFeat As SldWorks.Feature
Set swFeat = firstFeat
WhileNot swFeat IsNothingIf passedOrigin ThenIfNot Contains(processedFeats, swFeat) ThenIf (Not processedFeats) = -1 ThenReDim processedFeats(0)
ElseReDimPreserve processedFeats(UBound(processedFeats) + 1)
EndIfSet processedFeats(UBound(processedFeats)) = swFeat
RenameFeature swFeat, featNamesTable, owner
EndIfDim swSubFeat As SldWorks.Feature
Set swSubFeat = swFeat.GetFirstSubFeature
WhileNot swSubFeat IsNothingIfNot Contains(processedFeats, swSubFeat) ThenIf (Not processedFeats) = -1 ThenReDim processedFeats(0)
ElseReDimPreserve processedFeats(UBound(processedFeats) + 1)
EndIfSet processedFeats(UBound(processedFeats)) = swSubFeat
RenameFeature swSubFeat, featNamesTable, owner
EndIfSet swSubFeat = swSubFeat.GetNextSubFeature
Wend
EndIfIf swFeat.GetTypeName2() = "OriginProfileFeature"Then
passedOrigin = TrueEndIfSet swFeat = swFeat.GetNextFeature
Wend
EndSubSub RenameFeature(feat As SldWorks.Feature, featNamesTable AsObject, owner AsObject)
If MatchesFilter(feat) ThenDim baseFeatName AsStringIf TryGetBaseName(feat.name, baseFeatName) ThenDim nextIndex AsIntegerIf featNamesTable.Exists(baseFeatName) Then
nextIndex = featNamesTable.item(baseFeatName) + 1
featNamesTable.item(baseFeatName) = nextIndex
Else
nextIndex = 1
featNamesTable.Add baseFeatName, nextIndex
EndIfDim newName AsString
newName = baseFeatName & nextIndex
If LCase(feat.name) <> LCase(newName) Then
ResolveFeatureNameConflict owner, newName
Debug.Print "Renaming '" & feat.name & "' to '" & newName & "'"
feat.name = newName
EndIfEndIfEndIfEndSubFunction MatchesFilter(feat As SldWorks.Feature) AsBooleanDim typeName AsString
typeName = feat.GetTypeName2()
If typeName <> "Reference"And typeName <> "ReferencePattern"ThenIfNot IsEmpty(FEATS_FILTER) ThenDim i AsIntegerFor i = 0 To UBound(FEATS_FILTER)
If typeName LikeCStr(FEATS_FILTER(i)) Then
MatchesFilter = TrueExitFunctionEndIfNext
MatchesFilter = FalseElse
MatchesFilter = TrueEndIfElse
MatchesFilter = FalseEndIfEndFunctionFunction TryGetBaseName(name AsString, ByRef baseName AsString)
TryGetBaseName = False
baseName = ""Dim regEx AsObjectSet regEx = CreateObject("VBScript.RegExp")
regEx.Global = True
regEx.IgnoreCase = True
regEx.Pattern = "(.+?)(\d+)$"Dim regExMatches AsObjectSet regExMatches = regEx.Execute(name)
If regExMatches.Count = 1 ThenIf regExMatches(0).SubMatches.Count = 2 Then
baseName = regExMatches(0).SubMatches(0)
TryGetBaseName = TrueEndIfEndIfEndFunctionSub ResolveFeatureNameConflict(owner AsObject, name AsString)
Const INDEX_OFFSET AsInteger = 100
Dim index AsIntegerDim swFeatMgr As SldWorks.FeatureManager
Dim swFeat As SldWorks.Feature
IfTypeOf owner Is SldWorks.Component2 ThenDim swComp As SldWorks.Component2
Set swComp = owner
Dim swRefModel As SldWorks.ModelDoc2
Set swRefModel = swComp.GetModelDoc2
IfNot swRefModel IsNothingThenSet swFeatMgr = swRefModel.FeatureManager
Set swFeat = swComp.FeatureByName(name)
Else
Err.Raise vbError, "", "Component model is not loaded"EndIfElseIfTypeOf owner Is SldWorks.ModelDoc2 ThenDim swModel As SldWorks.ModelDoc2
Set swModel = owner
Set swFeatMgr = swModel.FeatureManager
Set swFeat = swModel.FeatureByName(name)
Else
Err.Raise vbError, "", "Not supported owner"EndIfIfNot swFeat IsNothingThenDim baseName AsStringIf TryGetBaseName(name, baseName) ThenDim newName AsString
newName = baseName & (INDEX_OFFSET + index)
WhileFalse <> swFeatMgr.IsNameUsed(swNameType_e.swFeatureName, newName)
index = index + 1
newName = baseName & (INDEX_OFFSET + index)
Wend
swFeat.name = newName
ElseExitSubEndIfEndIfEndSubFunction Contains(vArr AsVariant, item AsObject) AsBooleanDim i AsIntegerFor i = 0 To UBound(vArr)
If vArr(i) Is item Then
Contains = TrueExitFunctionEndIfNext
Contains = FalseEndFunctionFunction GetSelectedComponents(selMgr As SldWorks.SelectionMgr) AsVariantDim swComps() As SldWorks.Component2
Dim i AsIntegerFor i = 1 To selMgr.GetSelectedObjectCount2(-1)
Dim swComp As SldWorks.Component2
Set swComp = selMgr.GetSelectedObjectsComponent4(i, -1)
IfNot swComp IsNothingThenIf (Not swComps) = -1 ThenReDim swComps(0)
Set swComps(0) = swComp
ElseIfNot Contains(swComps, swComp) ThenReDimPreserve swComps(UBound(swComps) + 1)
Set swComps(UBound(swComps)) = swComp
EndIfEndIfEndIfNextIf (Not swComps) = -1 Then
GetSelectedComponents = Empty
Else
GetSelectedComponents = swComps
EndIfEndFunction
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