Macro to rename feature name using custom properties?

Apprecaite your help I am trying to write a macro to overwrite Flat pattern feature name using by custom properties. I am struggling retrieving the custom properties. this is what I have so far. 

 

Option Explicit
Sub RenameFlatPatternFeatures()
   ' Declare SolidWorks application objects
   Dim swApp As SldWorks.SldWorks
   Dim swModel As SldWorks.ModelDoc2
   Dim swFeature As SldWorks.Feature
   Dim partNumber As String
   Dim revisionNumber As String
   Dim featureName As String
   Dim newFeatureName As String
   Dim flatPatternCount As Integer
   Dim customPropMgr As SldWorks.CustomPropertyManager
   Dim resolvedValue As String
   Dim errors As Long
   ' Get the active SolidWorks application
   Set swApp = Application.SldWorks
   ' Get the active model document
   Set swModel = swApp.ActiveDoc
   ' Ensure a document is open
   If swModel Is Nothing Then
       MsgBox "No document is active!", vbCritical, "Error"
       Exit Sub
   End If
   ' Get the custom property manager for the active model
   Set customPropMgr = swModel.Extension.CustomPropertyManager("")
<span class="marker mark">   ' Retrieve the custom properties</span>
<span class="marker mark">   'customPropMgr.Get2 "PartNo", resolvedValue, errors</span>
<span class="marker mark">   'partNumber = resolvedValue</span>
<span class="marker mark">    partNumber = "replace"</span>
<span class="marker mark">    </span>
<span class="marker mark">   'customPropMgr.Get2 "DWRevisionNumber", resolvedValue, errors</span>
<span class="marker mark">   'revisionNumber = resolvedValue</span>
<span class="marker mark">   revisionNumber = "me"</span>
   ' Initialize flat pattern count
   flatPatternCount = 0
   ' Get the first feature
   Set swFeature = swModel.FirstFeature
   ' Loop through all features in the model
   Do While Not swFeature Is Nothing
       ' Check if the feature is a flat pattern
       If swFeature.GetTypeName2 = "FlatPattern" Then
           flatPatternCount = flatPatternCount + 1
           ' Get the original feature name
           featureName = swFeature.Name
           ' Create the new feature name using the specified format
           If Len(featureName) >= 2 Then
               newFeatureName = partNumber & "-" & Left(featureName, 2) & "-R" & revisionNumber
           Else
               newFeatureName = partNumber & "-" & featureName & "-R" & revisionNumber ' Fallback if the feature name is too short
           End If
           ' Rename the flat pattern feature
           swFeature.Name = newFeatureName
       End If
       ' Move to the next feature
       Set swFeature = swFeature.GetNextFeature
   Loop
   ' Notify user of changes made
   'MsgBox flatPatternCount & " flat pattern features renamed.", vbInformation, "Renaming Complete"
End Sub
Answers 3
LB
LB 2025-04-07 Best Answer

Working Full Code:- 

 

Option Explicit

Sub RenameFlatPatternFeatures()
   ' Declare SolidWorks application objects
   Dim swApp As SldWorks.SldWorks
   Dim swModel As SldWorks.ModelDoc2
   Dim swFeature As SldWorks.Feature
   Dim partNumber As String
   Dim revisionNumber As String
   Dim featureName As String
   Dim newFeatureName As String
   Dim flatPatternCount As Integer
   Dim customPropMgr As SldWorks.CustomPropertyManager
   Dim resolvedValue As String
   Dim wasResolved As Boolean
   Dim isLinked As Boolean
   Dim result As Long

   ' Get the active SolidWorks application
   Set swApp = Application.SldWorks
   ' Get the active model document
   Set swModel = swApp.ActiveDoc

   ' Ensure a document is open
   If swModel Is Nothing Then
       MsgBox "No document is active!", vbCritical, "Error"
       Exit Sub
   End If

   ' Get the custom property manager for the active model (default configuration)
   Set customPropMgr = swModel.Extension.CustomPropertyManager("")

   ' Retrieve the "PartNo" custom property
   result = customPropMgr.Get6("PartNo", False, partNumber, resolvedValue, wasResolved, isLinked)
   If result = 0 Then
       MsgBox "Error: The 'PartNo' property is not defined.", vbCritical, "Property Not Found"
       Exit Sub
   Else
       partNumber = resolvedValue ' Use the resolved value of PartNo
   End If

   ' Retrieve the "DWRevisionNumber" custom property
   result = customPropMgr.Get6("DWRevisionNumber", False, revisionNumber, resolvedValue, wasResolved, isLinked)
   If result = 0 Then
       MsgBox "Error: The 'DWRevisionNumber' property is not defined.", vbCritical, "Property Not Found"
       Exit Sub
   Else
       revisionNumber = resolvedValue ' Use the resolved value of DWRevisionNumber
   End If

   ' Initialize flat pattern count
   flatPatternCount = 0

   ' Get the first feature in the model tree
   Set swFeature = swModel.FirstFeature

   ' Loop through all features in the model tree to find and rename flat patterns
   Do While Not swFeature Is Nothing
       ' Check if the feature is a flat pattern feature
       If swFeature.GetTypeName2 = "FlatPattern" Then
           flatPatternCount = flatPatternCount + 1

           ' Get the original feature name
           featureName = swFeature.Name

           ' Create the new feature name using the specified format: PartNo-XX-RRevisionNumber (where XX is part of original name)
           If Len(featureName) >= 2 Then
               newFeatureName = partNumber & "-" & Left(featureName, 2) & "-R" & revisionNumber
           Else
               newFeatureName = partNumber & "-" & featureName & "-R" & revisionNumber ' Fallback if name is too short
           End If

           ' Rename the flat pattern feature to the new name format
           swFeature.Name = newFeatureName
       End If

       ' Move to the next feature in the model tree
       Set swFeature = swFeature.GetNextFeature()
   Loop

   ' Notify user of changes made (number of features renamed)
   'MsgBox flatPatternCount & " flat pattern features renamed.", vbInformation, "Renaming Complete"
End Sub

 

KC
KC 2025-04-04

May not matter, but Get2 is at Get6.
Also, I don't see configuration manager in there, see this example:

 

 

TR
TR 2025-04-07

The corrected VBA macro for SolidWorks retrieves custom property values ("PartNo" and "DWRevisionNumber") using `customPropMgr.Get2` with `valueOut` and `typeOut` variables, checking for errors. If the properties exist, their values are used to rename Flat Pattern EZPassPA login features in the format "PartNo-XX-RRevisionNumber"; otherwise, default values are used. Ensure these custom properties are defined in your SolidWorks file for the macro to function correctly.