Model Tree Labeling Question

Model Tree Labeling Question

harrisons22K9B
Enthusiast Enthusiast
895 Views
3 Replies
Message 1 of 4

Model Tree Labeling Question

harrisons22K9B
Enthusiast
Enthusiast

Hello,

 

A question about Model Trees and iLogic.

 

The current way I'm structuring the model tree for iLogic is Renaming the part number to be a "title" that iLogic Rules can always scan for (as an example, seen on left)

Is there an iLogic rule or has anyone made one that can add an iLogic generated Part Number to the Model Tree Labels?  (as shown on right)
Main Unit.asy                 | Main Unit.asy

-Front wall.ipt                |    -Front wall (Num-1-1).ipt

-Rear wall.ipt                 |    -Rear wall (Num-1-2).ipt

-Left wall.ipt                  |    -Left wall (Num-1-3).ipt

-Right wall.ipt               |    -Right wall (Num-1-4).ipt

-etc.                                |    -etc (Num-x-x)

 

I'm picturing this rule to be ran as the last step before sending the drawings to our fab shop, makes their lives a little easier.

 

Also, I am new to iLogic, and would request you take the time to comment your code so we all can learn.

 

Thanks!

 

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

sundars
Autodesk
Autodesk
Accepted solution

Hi @harrisons22K9B 

 

Not sure if this is what you want, but we have a utility to rename browser nodes based on part number. You could potentially run that command after you have run your iLogic script. It can be found in the Assembly Bonus tools addin. 

 

Thanks

-shiva

 

sundars_0-1646062796298.png

 

Shiva Sundaram
Inventor Development
Message 3 of 4

WCrihfield
Mentor
Mentor

Hi @harrisons22K9B.  Although the task of renaming components within an assembly is fairly common, it can also get fairly complicated, depending on your expectations/needs.  I noticed that in your example, you are injecting some text with two individual numbers within it, between the file's name, and the file's file extension.  What would each of those two numbers represent, and how/why/when do you expect them to increment?  How many levels deep is your assembly (components within sub assemblies are another level deeper, not top level components)?  If your assembly is multiple levels deep, then do you expect the component renaming task to step down into those deeper levels?  If so, how should that effect the naming convention?

 

In my mind, I'm thinking that maybe the first number should only increment for each unique document being represented within the assembly by components.  And I'm also thinking that maybe the second number would continue to increase by 1 for every component within the assembly, so that the last component to be renamed would contain a number equal to the total number of components in the assembly.  But I don't know if that's what you had in mind or not.

Here is an untested iLogic rule example, in which I am attempting to apply my ideas, as mentioned above, to the assembly to rename all of its top level only components (does not go down into sub assemblies).

Sub Main
	'specify which document to target
	Dim oADoc As AssemblyDocument = ThisDoc.Document
	'get ComponentDefinition contains all components, geometry, features, etc
	oADef = oADoc.ComponentDefinition
	'get all top level components
	oOccs = oADef.Occurrences
	
	'create a new empty Dictionary to hold list of Document objects paired with a number
	'we will use this to help us process any duplicates properly
	'(multiple components can be representing the same document)
	Dim oDocs As New Dictionary(Of Inventor.Document, Integer)
	
	'create a variable to represent the number to pair with each document
	Dim oDocNum As Integer = 0
	
	'define main parts of text to add to every component name
	'this is the first part of that text to add in
	Dim oAddStart As String = " (Num-"
	'and this is a variable to represent the first number
	Dim oAdd1stNum As Integer
	'and this is a variable to represent the second number
	Dim oAdd2ndNum As Integer
	
	For Each oOcc As ComponentOccurrence In oOccs
		'get the document that this component represents
		Dim oOccDoc As Document = oOcc.ReferencedDocumentDescriptor.ReferencedDocument
		'check if it is in our list yet
		If Not oDocs.Keys.Contains(oOccDoc) Then
			'it was not in the list yet, so add it with next available number
			'increment our oDocNum variable by 1
			oDocNum = oDocNum + 1
			'each Dictionary entry has a Key and a Value
			'add the document object as Key, then oDocNum as Value in this entry
			oDocs.Add(oOccDoc, oDocNum)
			'assign this new oDocNum value to our oAdd1stNum variable
			oAdd1stNum = oDocNum
		Else
			'it was already in the list, so get its number from the list
			'we can get the Dictionary entry's Value, using the associated Key
			oAdd1stNum = oDocs.Item(oOccDoc)
		End If
		'get document file name, without path, but with file extension
		Dim oFileName As String = System.IO.Path.GetFileName(oOccDoc.FullFileName)
		'get file extension, (with leading dot (.))
		Dim oExt As String = System.IO.Path.GetExtension(oOccDoc.FullFileName)
		'put all the text together that we will be adding to the component name
		Dim oAddAll As String = oAddStart & oAdd1stNum & "-" & oAdd2ndNum & ")"
		'now put the component name together
		Dim oNewOccName As String = oFileName & oAddAll & oExt
		Try
			'try to assign the new name to the component
			oOcc.Name = oNewOccName
			'if that worked, then increment the second number, for use in next loop
			oAdd2ndNum = oAdd2ndNum + 1
		Catch oEx As Exception
			'it failed to assign the new name to the component
			'so write something to the Logger about this failure
			Logger.Error("Error trying to rename Occ from '" & oOcc.Name & "' to '" & oNewOccName & "'." & vbCrLf & _
			oEx.Message & vbCrLf & oEx.StackTrace)
		End Try
		'loop back to start of our 'For Each' statement (next component)
	Next
	'past loop now, no more components to process, end of main code
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) 👍.

If you want and have time, I would appreciate your Vote(s) for My IDEAS :bulb: or you can Explore My CONTRIBUTIONS

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 4 of 4

harrisons22K9B
Enthusiast
Enthusiast

I appreciate the code, but for our shop they think the built in tool that @sundars clued us in on will work better for them at the moment.  As for the code, it is something I will play with to see if that can improve our efficiency.

Thanks.