Announcements

The Autodesk Community Forums has a new look. Read more about what's changed on the Community Announcements board.

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 (2)
299 Views
15 Replies
Replies (15)

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