How to delete parts body name that was added?

How to delete parts body name that was added?

Chirpyboy2060647
Advocate Advocate
332 Views
2 Replies
Message 1 of 3

How to delete parts body name that was added?

Chirpyboy2060647
Advocate
Advocate

Currently running a rule that I found through the forums to add a prefix text and suffix to the solid body's. Im not great at writing out code minus some iLogic stuff I can figure out. Was looking for a way that could search for the added text to the solid bodies, and delete it. 

 

So say the bodies list was like:

 

LE

RE

Backer

Nailer

Door

 

Run the code below and it say something like

 

CB_LE_5523

CB_RE_5523

CB_Backer_5523

CB_Nailer_5523

CB_Door_5523

 

So would need something that would delete the added text. 

 

in case others might want that code to rename as well:

 

Dim oDoc As PartDocument = ThisDoc.Document
Dim oDef As PartComponentDefinition = oDoc.ComponentDefinition

Dim oSolid As SurfaceBody
Dim oSolidName As String
Dim oPrefix As String = InputBox("Provide a prefix for each solid. If error, keep undoing untill names are back to original:", "Solid Body Prefix","")
Dim oSuffix As String = InputBox("Provide a suffix for each solid. If error, keep undoing untill names are back to original:", "Solid Body Suffix","")
'iterate through all solids and rename if default
For Each oSolid In oDef.SurfaceBodies
    oSolidName = oSolid.Name
    oSolid.Name = oPrefix & oSolidName & oSuffix
Next oSolid

 

0 Likes
Accepted solutions (1)
333 Views
2 Replies
Replies (2)
Message 2 of 3

A.Acheson
Mentor
Mentor
Accepted solution

Have a look at the string functions of contains and replace. 

AAcheson_0-1669851491529.png

Dim oDoc As PartDocument = ThisDoc.Document
Dim oDef As PartComponentDefinition = oDoc.ComponentDefinition

Dim oSolid As SurfaceBody
Dim oSolidName As String
Dim oPrefix As String = InputBox("Provide a prefix for each solid. If error, keep undoing untill names are back to original:", "Solid Body Prefix","CB_")
Dim oSuffix As String = InputBox("Provide a suffix for each solid. If error, keep undoing untill names are back to original:", "Solid Body Suffix","_5523")
'iterate through all solids and rename if default
For Each oSolid In oDef.SurfaceBodies
    oSolidName = oSolid.Name
	If oSolidName.Contains(oPrefix) Then
	   oSolidName = oSolidName.Replace(oPrefix, "")
	End If
	If oSolidName.Contains(oSuffix) Then
	   oSolidName = oSolidName.Replace(oSuffix, "")
	End If
	oSolid.Name = oSolidName
Next oSolid

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 3 of 3

Chirpyboy2060647
Advocate
Advocate

Awesome and thank you! Finally getting back into Inventor with new job and exploring ways to make things less tedious. Appreciate it!