Tuesday, February 28, 2012

iLogic to Rename All Solid Bodies

You've created a multi-body solid model with the intent to use the Make Components tool to write out each solid as a new part file. This works fine, but you find yourself wishing there was an easier way to tag each solid with a new name based on a prefix and a series number. For instance, you'd like to name each solid in a part file called 45-723 as such: 45-723_01, 45-723_02, 45-723_03, etc.

You can use this iLogic rule to quickly rename the solid bodies for you. In this rule we first look for a custom iProperty named "Prefix" and create it if it is not present. Then we write the part number to the custom iProperty if it is found to be empty. Then the rule uses the value of the custom iProperty named "Prefix" in an input box to query the user for the prefix:

The user can change the prefix at this point or accept the existing one. When we press the OK button the rule renames each solid body using the prefix and an increment number, adding a leading zero for each number less than 10.

Thanks to Barbara Han for the original code I used for this rule.

'------- start of ilogic ------
'check for custom iProperty and add it if not found
Dim prefix As String = "Prefix"
customPropertySet = ThisDoc.Document.PropertySets.Item _
("Inventor User Defined Properties")

         prop= customPropertySet.Item(prefix)
      ' Assume error means not found
            customPropertySet.Add("", prefix)
End Try

'write the part number to the Prefix iProperty if it is empty
if iProperties.Value("Custom", "Prefix") = "" Then
iProperties.Value("Custom", "Prefix") = iProperties.Value("Project", "Part Number") & "_"
end if

'check that this active document is a part file   
Dim partDoc As PartDocument
If ThisApplication.ActiveDocument.DocumentType <> kPartDocumentObject Then
MessageBox.Show ("Please open a part document", "iLogic")
End If

'define the active document
partDoc = ThisApplication.ActiveDocument
Dim solid As SurfaceBody
Dim i As Integer

'get input from user
prefix = InputBox("Enter a prefix for the solid body names", "iLogic", iProperties.Value("Custom", "Prefix"))

'write input back to custom iProperty
iProperties.Value("Custom", "Prefix") = prefix
i = 1
'rename all solid bodies incrementing suffix
For Each solid In partDoc.ComponentDefinition.SurfaceBodies
solid.Name = prefix + IIf(i < 10, "0" + CStr(i), CStr(i))
i = i + 1
------- end of ilogic ------