iLogic to change Solid Body names of selected

iLogic to change Solid Body names of selected

brianmD486F
Enthusiast Enthusiast
533 Views
4 Replies
Message 1 of 5

iLogic to change Solid Body names of selected

brianmD486F
Enthusiast
Enthusiast

Looking to see if there's an easy way to change the names on selected Solid Bodies in a part file. to write a "name" then counting how many selected and adding "_01" and so on. 

 

So, an example would be Rib_01 next would-be Rib_02 ect. but I only want it for the ones I've selected in the Model Tree

 

Thanks!

0 Likes
Accepted solutions (2)
534 Views
4 Replies
Replies (4)
Message 2 of 5

WCrihfield
Mentor
Mentor
Accepted solution

Hi @brianmD486F.  You can give this code example a try.

To use it, first select one or more bodies, then, while they are still selected, run the rule.  It will then prompt you to enter the base name for these bodies.  It will handle the index number at the end.

Sub Main
	Dim oPDoc As PartDocument = TryCast(ThisDoc.Document, Inventor.PartDocument)
	If oPDoc Is Nothing Then Return
	Dim oSS As Inventor.SelectSet = oPDoc.SelectSet
	If oSS.Count = 0 Then Return
	Dim iIndex As Integer = 0
	Dim sBaseName As String = InputBox("Enter body base name.", "New Body Name", "")
	If sBaseName = "" Then Return
	For Each oObj In oSS
		If TypeOf oObj Is Inventor.SurfaceBody Then
			Dim oBody As Inventor.SurfaceBody = oObj
			iIndex = iIndex + 1
			Dim sIndex As String = iIndex.ToString("00")
			oBody.Name = sBaseName & "_" & sIndex
		End If
	Next oObj
	oPDoc.Update2(True)
End Sub

If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 5

brianmD486F
Enthusiast
Enthusiast

Awesome! Thanks for putting this together. When I select the bodies and run the rule nothing happens and no errors either. 


Edit: Okay nvm this worked. I guess it wouldnt run the rule when in an assembly but inside the part file. I opened the part and then ran the code that worked! Thanks for this!

0 Likes
Message 4 of 5

WCrihfield
Mentor
Mentor
Accepted solution

Hi @brianmD486F.  When I quickly typed that code example up, I knew that only parts have bodies, so I assumed the code should only run when it obtains a part type document.  Assemblies do not have any SurfaceBody objects in them, but instead have a collection of assembly components (ComponentOccurrence objects).  However, I did do a little additional testing this morning with an assembly as my active document, and did find out that if we set the selection filter to 'Select Body Priority' mode, then that would allow us to select the individual bodies, that are within parts, within the assembly components.  And that this selection set would translate OK into the code I created...with a few minor tweaks, such as getting rid of the specific document type filter in the first line, then changing the variable representing the document in a few places.

WCrihfield_0-1721306061195.png

Here is the modified code example that should be usable from either a part or assembly, as long as you are using that selection filter if in an assembly.

Sub Main
	Dim oDoc As Inventor.Document = ThisDoc.Document
	Dim oSS As Inventor.SelectSet = oDoc.SelectSet
	If oSS.Count = 0 Then Return
	Dim iIndex As Integer = 0
	Dim sBaseName As String = InputBox("Enter body base name.", "New Body Name", "")
	If sBaseName = "" Then Return
	For Each oObj In oSS
		If TypeOf oObj Is Inventor.SurfaceBody Then
			Dim oBody As Inventor.SurfaceBody = oObj
			iIndex = iIndex + 1
			Dim sIndex As String = iIndex.ToString("00")
			oBody.Name = sBaseName & "_" & sIndex
		End If
	Next oObj
	oDoc.Update2(True)
End Sub

The thought also crossed my mind that we could further develop a routine like this so that, if nothing was 'pre-selected', instead of immediately exiting the rule, it would then prompt us to manually 'Pick' a body to rename, and put that 'Pick' action within a loop, to allow the user to keep picking another one until the user exits the loop by not picking anything.  The 'Pick' method has its own selection filter that can be applied.

 

If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 5 of 5

brianmD486F
Enthusiast
Enthusiast

@WCrihfield Brilliant and as helpful as ever! Appreciate your efforts a lot! Saves me a lot of time and energy with this script!

0 Likes