Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.

Multibody parts Renamining bodies with a Rule

chachaman
Collaborator

Multibody parts Renamining bodies with a Rule

chachaman
Collaborator
Collaborator

Hello,

 

In my multibody workflow, it makes the most sense to pattern bodies inside the multi-body part. Is there a snippet of code ( Rule) that can rename the body so that it derives from the original body ?  Example:  Body1, Body1_copy1, Body1_copy2 etc....

 

Right now, I have to tediously manually rename all the bodies, as you can see in the image:

chachaman_0-1736181600261.png

 

 

Thanks in advance !

0 Likes
Reply
Accepted solutions (3)
833 Views
41 Replies
Replies (41)

C_Haines_ENG
Collaborator
Collaborator

How is "Original Body" Defined? Are you talking about the first solid body in the list?

0 Likes

chachaman
Collaborator
Collaborator

Hello C_Haines,

 

I hope this image helps explain further:

 

The first solid : CLP_Em_RN is created buisiness as usual. As the multi body develops, the copies of it are simply patterned without modification.

 

As the bodies are renamed, the copies are named using the original part as the root of the name and the suffix  copy with a number showing it's quantity of  occurrance.

 

It would be nice if I could automate the process of renaming only the copies. I would manually name all original bodies and the rule would rename the copies automaically.

 

I hope that explanation helps.

 

Thx

 

 

chachaman_0-1736264051505.png

 

0 Likes

C_Haines_ENG
Collaborator
Collaborator

Genuinely the most convoluted thing on the planet to just get the BASE SOLID of a pattern. Incredibly complex for 0 reason. The numbering convention isn't perfect, but it absolutely gets the job done. 

Sub Main

	Dim oDoc As PartDocument = ThisDoc.Document
	Dim oDocDef As PartComponentDefinition = oDoc.ComponentDefinition
	Dim BrowserPane As BrowserPane = oDoc.BrowserPanes.Item("Model")
	Dim oParent As String
	Dim SUFFIX As String = "_COPY_"

	Dim oSolids As New Dictionary(Of String, String)

	For Each oNodeA As BrowserNode In BrowserPane.TopNode.BrowserNodes(2).BrowserNodes
		For Each oNodeB As BrowserNode In oNodeA.BrowserNodes
			If InStr(oNodeB.BrowserNodeDefinition.Label, "Pattern of") <> 0 Then

				oParent = Replace(Split(oNodeB.BrowserNodeDefinition.Label, ":")(0), "Pattern of ", "")

				oSolids.Add(oNodeA.BrowserNodeDefinition.Label, oParent)

			End If
		Next
	Next

	For Each oSolid As SurfaceBody In oDocDef.SurfaceBodies

		If oSolids.ContainsKey(oSolid.Name)

			For i = 1 To 100
				Try
					oSolid.Name = oSolids(oSolid.Name) & SUFFIX & i
				Catch
				End Try
			Next

		End If

	Next

End Sub

 

0 Likes

chachaman
Collaborator
Collaborator

Hello,

 

Tried to run the code, but nothing happens. Perhaps I did not deploy it correctly ?  Anyinsights ?

 

Attached: test File.

 

Thanks  a bunch  !

0 Likes

C_Haines_ENG
Collaborator
Collaborator
Accepted solution

Well that's funny! I was programming on Inventor 2019 which doesn't have Model States, and the program was trying to get the 2nd browser node (which on modern inventor is model states). Try this code:

 

Sub Main

	Dim oDoc As PartDocument = ThisDoc.Document
	Dim oDocDef As PartComponentDefinition = oDoc.ComponentDefinition
	Dim BrowserPane As BrowserPane = oDoc.BrowserPanes.Item("Model")
	Dim oParent As String
	Dim SUFFIX As String = "_COPY_"

	Dim oSolids As New Dictionary(Of String, String)

	For Each oNodeA As BrowserNode In BrowserPane.TopNode.BrowserNodes(3).BrowserNodes
		For Each oNodeB As BrowserNode In oNodeA.BrowserNodes
			If InStr(oNodeB.BrowserNodeDefinition.Label, "Pattern of") <> 0 Then

				oParent = Replace(Split(oNodeB.BrowserNodeDefinition.Label, ":")(0), "Pattern of ", "")

				oSolids.Add(oNodeA.BrowserNodeDefinition.Label, oParent)

			End If
		Next
	Next

	For Each oSolid As SurfaceBody In oDocDef.SurfaceBodies

		If oSolids.ContainsKey(oSolid.Name)

			For i = 1 To 100
				Try
					oSolid.Name = oSolids(oSolid.Name) & SUFFIX & i
				Catch
				End Try
			Next

		End If

	Next

End Sub

 

 

 

chachaman
Collaborator
Collaborator
Accepted solution

Bingo !

 

It now  works. 😊

 

How do I buy you a coffee ?  This is above average help !

 

Thanks again.

0 Likes

C_Haines_ENG
Collaborator
Collaborator

Its no problem! I love the challenge!

 

If you need it tweaked in the future let me know. Be sure to mark the solution as accepted!

0 Likes

chachaman
Collaborator
Collaborator
OK, I will and thanks again !!!

chachaman
Collaborator
Collaborator

Hello,

The program is not picking up patterned bodies that are sketch driven ? I attached the culprit file.

Can something be done ?

I attached the file.

 

Thx

chachaman_0-1736883236672.png

 

0 Likes

C_Haines_ENG
Collaborator
Collaborator

I should have added in more error correction in the first edition. Same issue as last time, the Solid Body node is in a different position. I got this new code working with your file. It runs much slower but it works.

Sub Main

	Dim oDoc As PartDocument = ThisDoc.Document
	Dim oDocDef As PartComponentDefinition = oDoc.ComponentDefinition
	Dim BrowserPane As BrowserPane = oDoc.BrowserPanes.Item("Model")
	Dim oParent As String
	Dim SUFFIX As String = "_COPY_"
	Dim oSolidNode As BrowserNode

	Dim oSolids As New Dictionary(Of String, String)

	For Each oTopNode As BrowserNode In BrowserPane.TopNode.BrowserNodes
		For Each oSubNode As BrowserNode In oTopNode.BrowserNodes
			If oSubNode.BrowserNodeDefinition.Label.Contains("Solid Bodies")
				oSolidNode = oSubNode
				Exit For
			End If
		Next : If oSolidNode IsNot Nothing Then Exit For
	Next

	For Each oNodeA As BrowserNode In oSolidNode.BrowserNodes
		For Each oNodeB As BrowserNode In oNodeA.BrowserNodes
			If InStr(oNodeB.BrowserNodeDefinition.Label, "Pattern of") <> 0 Then

				oParent = Replace(Split(oNodeB.BrowserNodeDefinition.Label, ":")(0), "Pattern of ", "")

				oSolids.Add(oNodeA.BrowserNodeDefinition.Label, oParent)

			End If
		Next
	Next

	For Each oSolid As SurfaceBody In oDocDef.SurfaceBodies

		If oSolids.ContainsKey(oSolid.Name)

			For i = 1 To 100
				Try
					oSolid.Name = oSolids(oSolid.Name) & SUFFIX & i
				Catch
				End Try
			Next

		End If

	Next

End Sub

 

0 Likes

chachaman
Collaborator
Collaborator

Hello C_Haines,

 

It now works !

 

there is a slight glitch in that if I edit some body names and run it again, the suffixes compound....see image.

have a fix ?

 

Thx

 

chachaman_0-1736902686696.png

 

0 Likes

C_Haines_ENG
Collaborator
Collaborator

That is likely happening because the item it is a copy of has been given the "Copy" suffix. Try renaming the base component to something simple and see if it still happens.

 

What I mean by this is that the item being copied has likely been accidentally renamed "ITEM_COPY_4". If you rename it to something like "ITEM_1" and rerun the tool it should correct itself. However if you are patterning copies of items then it would cause that problem. If that is the case, I can figure out a workaround. 

0 Likes

chachaman
Collaborator
Collaborator

Hi C,

I think what is happening is that a pattern has been re-patterned, and hence  it's building on the existing suffixed name.

 

Thx

0 Likes

C_Haines_ENG
Collaborator
Collaborator

Could you send the file you are using in the screenshot? You have significantly fewer solids in that one, and I cant seem to replicate the problem in the original file you sent me.

0 Likes

chachaman
Collaborator
Collaborator

Hello,

Here is the current version of the file....

 

Thx

0 Likes

C_Haines_ENG
Collaborator
Collaborator
Accepted solution

Oh you're gonna love this one. You were correct, you had patterns of patterns. Problem has been fixed, and OPTIMIZED. Guaranteed correct numbering, and it runs much faster. It does however have a 1 in 1,000,000+ chance of failing, part of the fun!

Sub Main

	Dim oDoc As PartDocument = ThisDoc.Document
	Dim oDocDef As PartComponentDefinition = oDoc.ComponentDefinition
	Dim BrowserPane As BrowserPane = oDoc.BrowserPanes.Item("Model")
	Dim oParent As String
	Dim SUFFIX As String = "_COPY_"
	Dim oSolidNode As BrowserNode

	Dim oSolids As New Dictionary(Of SurfaceBody, String)
	
	'[ FIND SOLID BODY NODE
	For Each oTopNode As BrowserNode In BrowserPane.TopNode.BrowserNodes
		For Each oSubNode As BrowserNode In oTopNode.BrowserNodes
			If oSubNode.BrowserNodeDefinition.Label.Contains("Solid Bodies")
				oSolidNode = oSubNode : GoTo FoundNode
			End If
		Next
	Next
	']

	FoundNode :
	
	'[ GO THROUGH EACH SOLID IN THE SOLIDBODIES NODES BROWSER TO CHECK IF COMPONENT IS A PATTERN OF ANOTHER SOLID
	For Each oNodeA As BrowserNode In oSolidNode.BrowserNodes
		For Each oNodeB As BrowserNode In oNodeA.BrowserNodes
			If oNodeB.BrowserNodeDefinition.Label.Contains("Pattern of")
				Try
					oParent = Replace(Split(Split(oNodeB.BrowserNodeDefinition.Label, ":")(0), "_COPY")(0), "Pattern of ", "")
					oSolids.Add(oNodeA.NativeObject, oParent) : 
				Catch : End Try

			End If
		Next
	Next
	']
	
	'[ RENAME ALL PATTERNED PARTS WITH A 5-6 DIGIT LONG RANDOM NUMBER, AND THEN RENUMBER ALL SOLIDS CORRECTLY
	Dim oMainSolids As New Dictionary(Of String, Integer), oMainName As String
	
	For i = 1 To 2
		For Each oSolid As SurfaceBody In oSolids.Keys
			oMainName = oSolids(oSolid)
			If i = 1
				If Not oMainSolids.ContainsKey(oMainName) Then oMainSolids.Add(oMainName, 1)
				oSolid.Name = oMainName & SUFFIX & Int(Rnd * 1000000)
			ElseIf i = 2
				oSolid.Name = oMainName & SUFFIX & oMainSolids(oMainName)
				oMainSolids(oMainName) += 1
			End If
		Next
	Next
	']

End Sub

 

0 Likes

chachaman
Collaborator
Collaborator

It works nicely !

This routine should part of the base program !!!

Thx

chachaman
Collaborator
Collaborator

Hello C,

If a patterned body gets modified, could you exclude the inheritance of the paprent body name.

If that same body modified body has patterned children, can you continue the inheritance sequence, as depicted by the image below.

In the image, GREEN is the original body.

PURPLE is an unmodified patterned body

 

RED is a modified patterned body. If it has patterned children, the naming with suffix continues.

 

Thx for letting me know

 

 

chachaman_0-1737506214067.png

 

 

0 Likes

C_Haines_ENG
Collaborator
Collaborator

Can you attach the file, this one is quite tricky.

0 Likes