Renaming Solid Bodies tool is not following order shown in Solidbody Tree

Renaming Solid Bodies tool is not following order shown in Solidbody Tree

C_Haines_ENG
Collaborator Collaborator
522 Views
7 Replies
Message 1 of 8

Renaming Solid Bodies tool is not following order shown in Solidbody Tree

C_Haines_ENG
Collaborator
Collaborator

Hello all!

 

I've tried to ignore this problem for quite some time but now its starting to bug me. I have written some code to renumber all the solid bodies in a part file. However alot of the time the numbering tool will completely skip the order shown in the Solid Body tree list. ( See below ) 

 

chainesL5H3G_0-1693946233500.png

 

It starts correctly at 10, then 17, then follows the correct order, almost....

 

I believed this was based on the order in which the solid bodies were created, but this also seems to be untrue?

 

does anyone know how to fix this? Something to do with the "For Each" line of code?

 

 

 

 

0 Likes
Accepted solutions (1)
523 Views
7 Replies
Replies (7)
Message 2 of 8

Andrii_Humeniuk
Advisor
Advisor

Hi @C_Haines_ENG . Try these 2 examples of getting a body: 1. Get the model from the browser. 2. Classic method, through ComponentDefinition.

Dim oDoc As PartDocument = ThisDoc.Document
Dim oNode As BrowserNode = oDoc.BrowserPanes(2).TopNode.BrowserNodes(3)
For i As Integer = 1 To oNode.BrowserNodes.Count
	Dim oBody As SurfaceBody = oNode.BrowserNodes(i).NativeObject
	MessageBox.Show(oBody.Name, "Node - " & i)	
Next i
Dim oBodies As SurfaceBodies = oDoc.ComponentDefinition.SurfaceBodies
For i As Integer = 1 To oBodies.Count
	MessageBox.Show(oBodies(i).Name, "Body - " & i)
Next i

 

Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor

LinkedIn | My free Inventor Addin | My Repositories

Did you find this reply helpful ? If so please use the Accept as Solution/Like.

EESignature

Message 3 of 8

C_Haines_ENG
Collaborator
Collaborator

Getting an error on line 2, "unspecified".

 

0 Likes
Message 4 of 8

WCrihfield
Mentor
Mentor

Hi @C_Haines_ENG.  Navigating the model browser by code can be a bit complicated at times.  There are often objects not being shown on your screen, but are encountered while iterating BrowserNodes.  But if getting the bodies in the same order you see them in the model browser tree is important, that is likely the best way to go, as Andrii pointed out above.  Here is another example of navigating them through the model browser.

Dim oPDoc As PartDocument = ThisDoc.Document
Dim oTN As BrowserNode = oPDoc.BrowserPanes.Item("PmDefault").TopNode
Dim oSolidBodiesNode As BrowserNode = Nothing
For Each oBN As BrowserNode In oTN.BrowserNodes
	If oBN.BrowserNodeDefinition.Label.StartsWith("Solid Bodies") Then oSolidBodiesNode = oBN
Next
Dim oBodyNodes As BrowserNodesEnumerator = oSolidBodiesNode.BrowserNodes
For i As Integer = 1 To oBodyNodes.Count
	Dim oBody As SurfaceBody = oBodyNodes.Item(i).NativeObject
	Logger.Info("oBody.Name = " & oBody.Name)
	Logger.Info("oBodyNodes.Item(" & i & ").BrowserNodeDefinition.Label = " & oBodyNodes.Item(i).BrowserNodeDefinition.Label)
Next

And yes, the Index numbers go by which ones existed first, even if the body may have been changed later.  But sometimes a feature or process creates a whole new body, and includes the previously existing body within the new one, or splits an existing body into 2 or more bodies, so those Index numbers can be deceiving at times.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 5 of 8

C_Haines_ENG
Collaborator
Collaborator

Unfortunately it is important to keep everything organized, thank you for the help again.

0 Likes
Message 6 of 8

C_Haines_ENG
Collaborator
Collaborator

Now im running into errors, it was working perfectly fine for the first model, but when I go to another model, it doesnt work anymore?

 

Its failing specifically at this line of your code: 

Dim oBody As SurfaceBody = oBodyNodes.Item(i).NativeObject

 

 

Its giving me "Unidentified Errors" and "Class not registered"

 

Here is the more info section

 

 

 

System.Runtime.InteropServices.COMException (0x80004005): Class not registered

at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
at Inventor.BrowserNode.get_NativeObject()
at ThisRule.Main() in external rule: SolidRename:line 41
at Autodesk.iLogic.Exec.AppDomExec.ExecRuleInAssembly(Assembly assem)
at iLogic.RuleEvalContainer.ExecRuleEval(String execRule)

 

 

 

Also the unspecified error:

 

Unspecified error (Exception from HRESULT: 0x80004005 (E_FAIL))

 

Do you have any idea what might be going wrong?

 

0 Likes
Message 7 of 8

WCrihfield
Mentor
Mentor
Accepted solution

Hi @C_Haines_ENG.  Yes, do believe I know what's going on there.  I did not keep in mind the differences between a 'regular/standard' part and a sheet metal part.  In a sheet metal part, there is a top level folder named "Folded Model", then the folder with the name starting with "Solid Bodies" is inside that folder (along with most other features and other common folders).  And there are definitely some BrowserNodes you will encounter that do not like you calling the NativeObject property of (will either throw an error, or return Nothing).  I created an updated version of that code that can be used for both types of parts.  I left in some of my 'trial & error' Logger lines, but commented a couple of them out for now.  It's quite a bit longer, but code length doesn't matter that much, as long as it's doing something useful for you, and you can understand what's going on.

Dim oPDoc As PartDocument = ThisDoc.Document
Dim oTN As BrowserNode = oPDoc.BrowserPanes.Item("PmDefault").TopNode
Dim oSolidBodiesNode As BrowserNode = Nothing
For Each oBN As BrowserNode In oTN.BrowserNodes
	'Logger.Info(oBN.BrowserNodeDefinition.Label)
	If oBN.BrowserNodeDefinition.Label.StartsWith("Solid Bodies") Then
		oSolidBodiesNode = oBN
		'Logger.Info("Found Solid Bodies Folder Node In Top Level")
		Exit For
	End If
	If oBN.BrowserNodeDefinition.Label = "Folded Model" Then
		For Each oSBN As BrowserNode In oBN.BrowserNodes
			If oSBN.BrowserNodeDefinition.Label.StartsWith("Solid Bodies") Then
				oSolidBodiesNode = oSBN
				'Logger.Info("Found Solid Bodies Folder Node In Second Level")
				Exit For
			End If
		Next 'oSBN
	End If
	If oSolidBodiesNode IsNot Nothing Then Exit For
Next 'oBN
If oSolidBodiesNode Is Nothing Then : Logger.Debug("Solid Bodies folder node not found.") : Exit Sub : End If
Dim oBodyNodes As BrowserNodesEnumerator = oSolidBodiesNode.BrowserNodes
For i As Integer = 1 To oBodyNodes.Count
	Dim oNO As Object = Nothing
	Try : oNO = oBodyNodes.Item(i).NativeObject : Catch : End Try
	If Not TypeOf oNO Is SurfaceBody Then : Logger.Debug("TypeName(oNO) = " & TypeName(oNO)) : Continue For : End If
	Dim oBody As SurfaceBody = oNO
	Logger.Info("oBody.Name = " & oBody.Name)
	Logger.Info("oBodyNodes.Item(" & i & ").BrowserNodeDefinition.Label = " & oBodyNodes.Item(i).BrowserNodeDefinition.Label)
Next

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)

0 Likes
Message 8 of 8

C_Haines_ENG
Collaborator
Collaborator

Certianly more complicated but it does the trick. 🤣

0 Likes