Show part number and description in tree

Show part number and description in tree

xavi.saus
Contributor Contributor
3,240 Views
22 Replies
Message 1 of 23

Show part number and description in tree

xavi.saus
Contributor
Contributor

Hello everybody

 

I am struggling trying to use a rule to show both part number and description on the tree.  I used this code I found on the forum:

'create reference to part
openDoc = ThisApplication.ActiveDocument
 
'format display name
openDoc.DisplayName = iProperties.Value("Project", "Part Number") & " (" & iProperties.Value("Project", "Description") & ")"
openDoc.Rebuild
 
'update
iLogicVb.UpdateWhenDone = True

 

But every time I make a save aand replace, it keeps old name and description, and I need to open the part from the assembly and save it again. Inventor also shows errors when using this rule.

 

Also, what would be the best option to trigger the desired rule? After saving, when changes on the iproperties...

0 Likes
Accepted solutions (1)
3,241 Views
22 Replies
Replies (22)
Message 2 of 23

oransen
Collaborator
Collaborator

It's not clear where and when you run this rule.

 

Do you do a save after the rule runs?

 

The DisplayName is not the same as the filename. You can do a SaveAs maybe, like this:

 

openDoc.Rebuild
openDoc.SaveAs  ("D:\TEST\testSave.ipt", False) 

 

 

 

 

0 Likes
Message 3 of 23

xavi.saus
Contributor
Contributor

Hi Oransen

 

I tried to trigger the rule before saving, after saving, when opening, everytime, but not working.

 

Just to clarify, I need a rule to show in the tree description the part number (as it works always with Inventor) and also the Description of the part or assembly, so it is easier to navigate when having lots of parts/assemblies, just like this:

IMAGE 1.png

 

 

 

 

The code I tried works OK, it shows part name and description, but:

- When I copy and replace the part "PARTNUMBER_01" to "PARTNUMBER_02", it does not update anything unless I open the part individually, but it changes the Display Name of the main assembly. On the following picture, I just copied PARTNUMBER_01 and saved and replaced it for PARTNUMBER_02:

IMAGE 2.png

- Also the partnumber and description of the assembly that contains the copied part changes from what it had to the part number and description of the copied part. This can be solved by saving the assembly, but can be annoying

- Main problem is the fact that rule does not work when save and replace part.

- Sometimes also this error shows up:

 

System.Runtime.InteropServices.COMException (0x80004005): Error no especificado (Excepción de HRESULT: 0x80004005 (E_FAIL))
en Microsoft.VisualBasic.CompilerServices.LateBinding.InternalLateCall(Object o, Type objType, String name, Object[] args, String[] paramnames, Boolean[] CopyBack, Boolean IgnoreReturn)
en Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateCall(Object Instance, Type Type, String MemberName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack, Boolean IgnoreReturn)
en ThisRule.Main()
en Autodesk.iLogic.Exec.AppDomExec.ExecRuleInAssembly(Assembly assem)
en iLogic.RuleEvalContainer.ExecRuleEval(String execRule)

0 Likes
Message 4 of 23

Curtis_Waguespack
Consultant
Consultant
Accepted solution

Hi @xavi.saus 

 

I think this example should do work for you.

 

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

oAsmDoc = ThisApplication.ActiveDocument 
Dim oOcc As ComponentOccurrence

For Each oOcc In oAsmDoc.ComponentDefinition.Occurrences
	oSplit = Split(oOcc.Name,":")
	oInstance = ":" & oSplit(UBound(oSplit))

	oPn = iProperties.Value(oOcc.Name, "Project", "Part Number")
	oDn = iProperties.Value(oOcc.Name, "Project", "Description")
	oOcc.Name = oPn & " (" & oDn & ")" & oInstance

Next

 

 

 

EESignature

0 Likes
Message 5 of 23

paulo.correia98ZAP
Contributor
Contributor

Hi @Curtis_Waguespack 

 

Thank you for this, as I was looking for something similar.

However it seams to change the name of the parts and sub-assemblies on the first level (parts inside the sub-assemblies aren't changed).

Would be possible to change the names in the full tree (all parts of sub-assemblies too)?

 

Thank you

Paulo

0 Likes
Message 6 of 23

Curtis_Waguespack
Consultant
Consultant

Hi @paulo.correia98ZAP 

 

I didn't test this, but I thin this should work. Just post back if it doesn't and I'll have another look.

 

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

 

Sub Main
	Dim oDoc As AssemblyDocument = ThisApplication.ActiveDocument
	Dim oOccs As ComponentOccurrences = oDoc.ComponentDefinition.Occurrences
	Call TraverseAssembly(oOccs)
End Sub

Function TraverseAssembly(oOccs As ComponentOccurrences)

	Dim oOcc As ComponentOccurrence
	For Each oOcc In oOccs

		oSplit = Split(oOcc.Name, ":")
		oInstance = ":" & oSplit(UBound(oSplit))

		oPn = iProperties.Value(oOcc.Name, "Project", "Part Number")
		oDn = iProperties.Value(oOcc.Name, "Project", "Description")
		oOcc.Name = oPn & " (" & oDn & ")" & oInstance

		If oOcc.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
			Call TraverseAssembly(oOccs)
		End If
	Next

End Function



 

EESignature

0 Likes
Message 7 of 23

paulo.correia98ZAP
Contributor
Contributor

Hi @Curtis_Waguespack 

 

Thank you for having a look on this.

I tried and didn't worked... Inventor crashes...

 

Paulo

0 Likes
Message 8 of 23

Curtis_Waguespack
Consultant
Consultant

Hi @paulo.correia98ZAP 

 

okay, I suspect the issue was that it was attempting to rename the assembly occurrences as it was using that name to work through the components still

 

try this version... I did test this on a simple assembly and it worked as expected.

 

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

 

 

 

Dim oDoc As AssemblyDocument = ThisApplication.ActiveDocument


Dim oOcc As ComponentOccurrence
i = 1
For Each oOcc In oDoc.ComponentDefinition.Occurrences.AllLeafOccurrences

	oSplit = Split(oOcc.Name, ":")
	oInstance = ":" & oSplit(UBound(oSplit))

	oPn = iProperties.Value(oOcc.Name, "Project", "Part Number")
	oDn = iProperties.Value(oOcc.Name, "Project", "Description")
	Try
		oOcc.Name = oPn & " (" & oDn & ")" & oInstance
	Catch
		oOcc.Name = oPn & " (" & oDn & ")" & oInstance + i
	End Try
	i = i + 1

Next

For Each oOcc In oDoc.ComponentDefinition.Occurrences
	i = 1
	If oOcc.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then

		oSplit = Split(oOcc.Name, ":")
		oInstance = ":" & oSplit(UBound(oSplit))

		oPn = iProperties.Value(oOcc.Name, "Project", "Part Number")
		oDn = iProperties.Value(oOcc.Name, "Project", "Description")
		Try
			oOcc.Name = oPn & " (" & oDn & ")" & oInstance
		Catch
			oOcc.Name = oPn & " (" & oDn & ")" & oInstance + i
		End Try
		i = i + 1

	End If
Next


 

EESignature

Message 9 of 23

paulo.correia98ZAP
Contributor
Contributor

Hi @Curtis_Waguespack 

 

Thank you so much. Works great.

 

Paulo

0 Likes
Message 10 of 23

cmyersSNP6E
Explorer
Explorer

It looks like the original post wanted to show description as well as part number in the model tree, but was also wanting to rename everything (which may be useful for me as well down the road), but I didn't see that the request just to display the part number and description was solved.  I had tried the code from another forum as well, but it throws an error at the  openDoc.Rebuild step, and when I comment it out, only the parent item has the description shown, none of the components beneath it.  All I really want is to include description in the model tree display, not rename.  What is "broken" in the code?

Message 11 of 23

luka.pavlinek
Participant
Participant

Thank you @Curtis_Waguespack for the code.

I tried to make it work for both parts and assemblies. Forgive my crude methods of coding, as I am new to this.

This code works OK, when run manually. When I try to run it with a trigger on open, I get the following error.

 

System.ArgumentException: iProperties: The component named "X000008485:1" was not found.
at iLogic.CadPropertiesInRule.FindCompoOrDoc(Object compoOrDocName)
at iLogic.CadPropertiesInRule.InvPropertyInCompoOrDoc(Object compoOrDocName, String setName, String propName, Boolean createCustom)
at iLogic.CadPropertiesInRule.get_Value(Object compoOrDocName, String setName, String propName)
at ThisRule.Main() in external rule: Description_model_tree:line 17
at Autodesk.iLogic.Exec.AppDomExec.ExecRuleInAssembly(Assembly assem)
at iLogic.RuleEvalContainer.ExecRuleEval(String execRule)

 

If I then run the rule manually, again it will run. So there is a difference when running this via triger on open.

 

Can someone advise how to modify this code to work with triggers?

 

'drawing
If ThisApplication.ActiveDocument.DocumentType = kDrawingDocumentObject Then
		MessageBox.Show("Please run this rule from the assembly or part file.", "iLogic")
	Exit Sub
End If

'assembly
If ThisApplication.ActiveDocument.DocumentType = kAssemblyDocumentObject Then
	'Dim oDoc As AssemblyDocument = ThisDoc.Document 
	Dim oDoc As AssemblyDocument = ThisApplication.ActiveDocument
	Dim oOcc As ComponentOccurrence
	i = 1
	For Each oOcc In oDoc.ComponentDefinition.Occurrences.AllLeafOccurrences
		' Not a frame
	    If oOcc.ReferencedDocumentDescriptor.ReferencedDocument.DocumentInterests.HasInterest("{AC211AE0-A7A5-4589-916D-81C529DA6D17}") = False
			oSplit = Split(oOcc.Name, ":")
			oInstance = ":" & oSplit(UBound(oSplit))
			oPn = iProperties.Value(oOcc.Name, "Project", "Part Number")
			oDn = iProperties.Value(oOcc.Name, "Project", "Description")
			Try
				oOcc.Name = oPn & " (" & oDn & ")" & oInstance
			Catch
				'oOcc.Name = oPn & " (" & oDn & ")" & oInstance + i ' I get error when working with imported STEP files
			End Try
			i = i + 1
		End If
	Next
	For Each oOcc In oDoc.ComponentDefinition.Occurrences
		i = 1
		If oOcc.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
	    	If oOcc.ReferencedDocumentDescriptor.ReferencedDocument.DocumentInterests.HasInterest("{AC211AE0-A7A5-4589-916D-81C529DA6D17}") = False
				oSplit = Split(oOcc.Name, ":")
				oInstance = ":" & oSplit(UBound(oSplit))
				oPn = iProperties.Value(oOcc.Name, "Project", "Part Number")
				oDn = iProperties.Value(oOcc.Name, "Project", "Description")
				Try
					oOcc.Name = oPn & " (" & oDn & ")" & oInstance
				Catch
					oOcc.Name = oPn & " (" & oDn & ")" & oInstance + i
				End Try
				i = i + 1
			End If
		End If
	Next	
End If

'part
If ThisApplication.ActiveDocument.DocumentType = kPartDocumentObject Then
	Dim oDoc As PartDocument = ThisDoc.Document
	'Dim oOcc As ComponentOccurrence
	'check if frame
	If oDoc.DocumentInterests.HasInterest("{AC211AE0-A7A5-4589-916D-81C529DA6D17}") = False
		'oSplit = Split(oOcc.Name, ":")
		oSplit = Split(oDoc.DisplayName, ":")
		oInstance = ":" & oSplit(UBound(oSplit))
		oPn = iProperties.Value("Project", "Part Number")
		oDn = iProperties.Value("Project", "Description")
		Try
			'oOcc.Name = oPn & " (" & oDn & ")" & oInstance
			oDoc.DisplayName = oPn & " (" & oDn & ")"
		Catch
		End Try
	End If
End If

 

0 Likes
Message 12 of 23

JShearer98
Participant
Participant

Hi Luka,

 

Did you ever get this code working as I'm having the same issue?

 

The other methods mentioned on this thread have the same problem of working when it is run manually but any attempt at using triggers results in an error message per part saying it cannot find iProperties of the part but when you close all errors it has actually done what it was meant to do.

 

Obviously it is good that it works at all but when I am opening Assemblies with 50+ parts shutting all the error messages is not optimal.

 

openDoc = ThisApplication.ActiveDocument


Dim docFile As Document
 
 'iterate through all components in Assembly
 For Each docFile In openDoc.AllReferencedDocuments
               
         'format file name                   
         Dim FNamePos As Long
         FNamePos = InStrRev(docFile.FullFileName, "\", -1)                        
         Dim docFName As String 
         docFName = Right(docFile.FullFileName, Len(docFile.FullFileName) - FNamePos)      

 

         'set display name
         docFile.DisplayName = iProperties.Value(docFName, "Project", "Part Number") & " <" & iProperties.Value(docFName, "Project", "Description") & ">"

 

         'rebuild to update the display
         docFile.Rebuild

 

        Next    

 

'update all
iLogicVb.UpdateWhenDone = True

 

 

jshearerDBSNF_0-1743687222892.png

jshearerDBSNF_1-1743687244887.png

 

0 Likes
Message 13 of 23

WCrihfield
Mentor
Mentor

Hi @JShearer98.  Here is an example iLogic rule code for that process which uses Inventor API way of working with iProperties, to avoid the need of extracting the file name, then specifying that to indicate which document you want to get/set the iProperties of, because it accesses the actual Property object of each document directly in the process.  I split it into two sections, so that the same process could also be ran on the main/active document, before running it on all referenced documents, and to make it more modular for use in other scenarios, if needed.

I also added in several good checks with iLogic Logger type feedback, to avoid the message boxes, but still stay informed of anything that does not go perfectly.  Like when documents can not be modified for some reason, or empty property values are encountered.   But you may also want to add something into the code that will actually Save the changes made to their Files on disk.  You would likely only need to save the main document, which would then also save all referenced documents that have been 'dirtied' (changes made since last saved).

Sub Main
	Dim oDoc As Inventor.Document = ThisDoc.Document
	SetDisplayNameToPartNumberAndDescription(oDoc)
	For Each oRefDoc As Inventor.Document In oDoc.AllReferencedDocuments
		SetDisplayNameToPartNumberAndDescription(oRefDoc)
	Next
	oDoc.Rebuild2(True)
End Sub

Sub SetDisplayNameToPartNumberAndDescription(oDoc As Inventor.Document)
	If Not oDoc.IsModifiable Then
		Logger.Warn("Document named '" & oDoc.DisplayName & "' was not modifiable!")
		Return
	End If
	If oDoc.FileSaveCounter = 0 Then 'if not saved, then no path or file name
		Logger.Warn("You need to save Document named '" & oDoc.DisplayName & "' first!")
		Return
	End If
	'get Part Number
	Dim sPN As String = oDoc.PropertySets.Item(3).Item(2).Value
	If sPN = "" Then
		Logger.Warn("Document named '" & oDoc.DisplayName & "' had empty Part Number value!")
		Return 'exit routine, without going any further
	End If
	'get Description
	Dim sDesc As String = oDoc.PropertySets.Item(3).Item(14).Value
	If sDesc = "" Then
		Logger.Warn("Document named '" & oDoc.DisplayName & "' had empty Description value!")
		Return 'exit routine, without going any further
	End If
	Dim sNewDispName As String = sPN & " <" & sDesc & ">"
	If oDoc.DisplayName <> sNewDispName Then
		Try
			oDoc.DisplayName = sNewDispName
		Catch ex As Exception
			Logger.Error("Error setting DisplayName of '" & oDoc.DisplayName & "' to & '" & sNewDispName & "'!")
		End Try
	End If
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 14 of 23

Jonnox
Contributor
Contributor

Hi Curtis,

 

I have just come across this code and it works perfect! Thank you.

 

What would i have to change for it to display the "Filename" (no extension) instead of "Part Number"??

 

Also, i would love for it to state the "design view" and "position view" if they are not "master". like what it does out the box for "model states".

 

j_leeSF4ZY_0-1750857357407.png

 

Hope this makes sense, let me know if you need any more information.

 

Best Regards,

JL

 

 

0 Likes
Message 15 of 23

WCrihfield
Mentor
Mentor

Although the basics of what you want do sound possible, and relatively easy to do, it would also require something like an Inventor Add-In to keep accurate, because the code that runs to set the Document.DisplayName value would need to be ran every single time any model type document (part or assembly) has any of its 'representations' changed.  The add-in would have to monitor for ModelState change event, design view representation change event, and positional representation change event, and know what document the change happened in, if multiple are open in Inventor at that moment in time, gather which representations are now active, then re-set the DisplayName of that document.  Just so you are aware of the challenges involved later.

If a simple rule that just sets the DisplayName of the 'active' document when you manually run a rule is OK, then we can help with that.  I don't do add-ins, due to restrictions where I work, but do understand how to monitor those specific events, if it comes to that at a later stage.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 16 of 23

Jonnox
Contributor
Contributor

Hi @WCrihfield , 

 

Thanks for the quick response.

 

I just ran the code (in message 6) and was happy with it, then realised if the filename is changed the assembly tree will not update automatically, same if you add a new part into the assembly.

So, I decided to run the code every time the active document is saved (via the triggers). Then realised this takes a lot of time on larger assemblies as its going through all the sub assembly levels.

 

I went back to Curtis's first code (message 4) which works only at the level you are on.

This is then a lot quicker and only required on the files that have changed. So, I will end up with all the info I need and it being as up to date as it can be.

 

Long answer to your question that was, but yes it will be activated manually, not needing it to look all the time for changes. We dont use load of models states, design views and psotional views here, but would be nice to know when they have been.

 

Hope that helps.

 

Best Regards

JL

0 Likes
Message 17 of 23

WCrihfield
Mentor
Mentor

OK.  Those earlier code examples were just changing the names of the component occurrence names (ComponentOccurrence.Name), not necessarily the Document.DisplayName of the document that the component occurrence is referencing.  Component names usually conform to the source model's file name by default, or to the display name of the document that they reference, if that has been changed from its default value before the component occurrence was placed into the assembly.  Some folks like to always rename all of the component occurrences in their assemblies, to 'stabilize' them, so that they do not change, even if they are 'replaced' with another source model file, that way any iLogic rules which refer to them by their component names will still be able to find them after those replacements have been made.  There is actually a built-in utility in the assembly environment, within the ribbon panel named 'Productivity''s drop-down list, called 'Rename Browser Nodes', which has 3 options for renaming them (Filename, Part Number, or Default).  So, if you just want to reset them all back to their file names, that's probably the way to go.  But if you want the representation names included, that would require an iLogic rule.

If you just want to rename top level components in an assembly only, then that is the simplest to do, and would require the least code.  Just setting them to the file name of their source model file would also be about the simplest, unless there are some suppressed ones, some virtual ones, some welds in a weldment type assembly, or odd situations like that.  But including representation names to, and only when they are not the 'default' ones, is a lot more complex, and would require more code.

 

Below is an example iLogic rule that you can try out for that most complex scenario.  This may look a bit longer, and more complicated than you had in mind, but that's because I am trying to make it as error free as possible by looking out for every tiny little detail that could potentially cause a problem, and trying to avoid the errors that they could cause.  In doing so, I am also using a lot of Try...Catch...End Try statements that I have 'condensed' down into one line, to keep the code a bit shorter looking, and to avoid one tiny detail from causing a component to get skipped over.

Dim oInvApp As Inventor.Application = ThisApplication
Dim oADoc As AssemblyDocument = TryCast(ThisDoc.FactoryDocument, Inventor.AssemblyDocument)
If oADoc Is Nothing Then Return
For Each oOcc As ComponentOccurrence In oADoc.ComponentDefinition.Occurrences
	'skip over any that are suppressed
	If oOcc.Suppressed Then Continue For
	'skip over any that are 'Virtual' or 'Welds'
	Dim oCD As Inventor.ComponentDefinition = oOcc.Definition
	If TypeOf oCD Is VirtualComponentDefinition Then Continue For
	If TypeOf oCD Is WeldsComponentDefinition Then Continue For
	'<<< get 'Index', FullFileName, then File Name (without path or file extension)
	Dim sIndex, sFFN, sFileName As String
	Try : sIndex = oOcc.Name.Split(":").Last() : Catch : End Try
	Try : sFFN = oCD.Document.FullFileName : Catch : End Try
	Try : sFileName = System.IO.Path.GetFileNameWithoutExtension(sFFN) : Catch : End Try
	'<<< get representation names
	Dim sMSName As String = oOcc.ActiveModelState
	Dim sViewRepName As String = oOcc.ActiveDesignViewRepresentation
	Dim sPosRepName As String = oOcc.ActivePositionalRepresentation
	'<<< determine if representation names should be included
	Dim bDefaultMS, bDefaultViewRep, bDefaultPos As Boolean 'all False by default
	Try : bDefaultMS = (oCD.ModelStates.Item(1).Name = sMSName) : Catch : End Try
	Dim oRepsMgr As RepresentationsManager = Nothing
	Try : oRepsMgr = oCD.RepresentationsManager : Catch : End Try
	Try : bDefaultViewRep = (oRepsMgr.DesignViewRepresentations.Item(1).Name = sViewRepName) : Catch : End Try
	Try : bDefaultPos = ((oRepsMgr.PositionalRepresentations Is Nothing) OrElse _
		(oRepsMgr.PositionalRepresentations.Count = 0) OrElse _
		(oRepsMgr.PositionalRepresentations.Item(1).Name = sPosRepName)) : Catch : End Try
	'<<< put new component name parts together, one by one, as needed
	'add File Name first
	Dim sNewOccName As String = sFileName
	'only add ModelState name if it is not the default one (not master or primary)
	If Not bDefaultMS Then sNewOccName &= " (" & sMSName & ")"
	'only add design view name if it is not the default one (not master or primary)
	If Not bDefaultViewRep Then sNewOccName &= " (" & sViewRepName & ")"
	'only add positional rep name if it is not the default one (not master or primary)
	If Not bDefaultPos Then sNewOccName &= " (" & sPosRepName & ")"
	'add 'Index' at end
	sNewOccName &= sIndex
	'now try to actually set that new name to the component
	Try
		oOcc.Name = sNewOccName
	Catch
		Logger.Error("Failed to rename occurrrence originally named:  " & oOcc.Name)
	End Try
Next 'oOcc
oADoc.Update2(True)

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 18 of 23

Jonnox
Contributor
Contributor

Hi @WCrihfield ,

 

Thanks again for your response. I didn’t know about the "Rename Browser Nodes", definitely will come in use (even more so now I'm messing about with those names!).

 

Displaying the filename, description, Model State*, Design View* and position view* is a must (* if they are not set to "Master").

 

Don’t worry about long codes, I'm no expert at all, so if it works, it works! I can just about get the rules running, other than that I'm completely new to this. 

 

I created two test parts and two test assemblies, to make a simply 3 level assembly.

Jonnox_4-1750889124911.png

 

 

When I ran your code on the highest level "Test Assembly Filename 2", with assembly "Test Assembly Filename" Representation options set to Master, Master, Master.

Jonnox_6-1750889721184.png

 

It displayed the following -

Jonnox_5-1750889355657.png

So, this worked as it only renamed the files underneath the top-level assembly, just leaving two brackets displaying.

Would it be able to display "Description" from the iProperties after the brackets?

 

When I ran your code again on the highest level "Test Assembly Filename 2", with assembly "Test Assembly Filename" Representation options set to Model State1, Default, Position1.

Jonnox_8-1750889910318.png

 

 

It displayed the following -

Jonnox_9-1750889944130.png

So again, it only changed that level, it shows the model state and the position view, great.

It doesn’t remove the previous text and it’s not showing the design view.

 

I did the "rename browser nodes" and ran the code again.

it now displayed the following-

Jonnox_10-1750890170622.png

No design view text, and the "model state1" is repeated at the end.

 

 

 

The next test I did was to run the code on the sub assembly level (which just contains two single parts)

Jonnox_11-1750890348978.png

 

and it always returns the below -

Jonnox_0-1750888210378.png

Jonnox_12-1750890450411.png

 

No text changed in the assembly tree, my guess is because single parts don't have a positional view.

Jonnox_13-1750890578903.png

FYI, I'm currently using 2022 but on Tuesday next week I should be upgrading to 2025.

 

I hope all this information helps!

 

 

0 Likes
Message 19 of 23

WCrihfield
Mentor
Mentor

Hi @Jonnox.  Thanks for the detailed feedback.  I guess that maybe I did not know all of your design intent, and just made some assumptions.  For example, I did not know that you wanted/needed the 'description' iProperty value included in that renaming routine.  Another issue that contributed to the confusion or functionality failure was the fact that the one property for accessing the name of the 'active' design view representation for a ComponentOccurrence object seems to have been accidentally 'hidden' within the Inventor API for several years, which makes it difficult to access.  It could be seen in the 2020 Inventor API online documentation (Link), then was not seen again until the 2026 Inventor API online help (ComponentOccurrence.ActiveDesignViewRepresentation).  Since it was hidden, it is also not 'suggested' by the 'Intellisense' system when we type the dot after the ComponentOccurrence term in the iLogic rule editor dialog, so we just have to blindly type in what we think it should be, and hope that it works, which is what I did in that example above.  Seems like another thing that may be going on here is a setting within your software, where it automatically also includes the ModelState name, no matter what the name of the component is.  I think that is the setting called 'Show Extended Names', which can be accessed through the model browser tree's dockable window's menu, under 'Display Preferences, as seen in the following screen shot.

WCrihfield_0-1750949944336.png

When that is turned on, some information will show for each browser node of an assembly component, that is beyond our control.  Which is why you see the ModelState name mentioned twice in some of your screen captured images.

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 20 of 23

WCrihfield
Mentor
Mentor

Here is an updated version of my previous code.  I added in the Description, as the second thing in the new names, right after file name, but before ModelState name.  It is still only iterating though 'top level' components, because I assumed that is the way you wanted it to work.  I also added in some additional checks to see if the values we are trying to get, such as the Description, ModelState name, the problematic design view name (DVR), and the positional representation name, are 'Null' or empty.  If they are null or empty, then they should not be added to the component name anymore.  I am not sure how else to get the design view representation name though, if that hidden property is not working.  One option might be to dig down into the document that the component is referencing, then check which DVR is its 'active' one, then just assume that is also the component occurrences active one, but not sure if that will always be true.

Dim oInvApp As Inventor.Application = ThisApplication
Dim oADoc As AssemblyDocument = TryCast(ThisDoc.FactoryDocument, Inventor.AssemblyDocument)
If oADoc Is Nothing Then Return
For Each oOcc As ComponentOccurrence In oADoc.ComponentDefinition.Occurrences
	'skip over any that are suppressed
	If oOcc.Suppressed Then Continue For
	'skip over any that are 'Virtual' or 'Welds'
	Dim oCD As Inventor.ComponentDefinition = oOcc.Definition
	If TypeOf oCD Is VirtualComponentDefinition Then Continue For
	If TypeOf oCD Is WeldsComponentDefinition Then Continue For
	Dim oRefDoc As Inventor.Document = oCD.Document
	'<<< get 'Index', FullFileName, then File Name (without path or file extension)
	Dim sIndex, sFFN, sFileName As String
	Try : sIndex = oOcc.Name.Split(":").Last() : Catch : End Try
	Try : sFFN = oRefDoc.FullFileName : Catch : End Try
	Try : sFileName = System.IO.Path.GetFileNameWithoutExtension(sFFN) : Catch : End Try
	'<<< get 'Description' iProperty value
	Dim sDescription As String = oRefDoc.PropertySets.Item(3).Item(14).Value
	'<<< get representation names
	Dim sMSName As String = oOcc.ActiveModelState
	Dim sViewRepName As String = oOcc.ActiveDesignViewRepresentation
	Dim sPosRepName As String = oOcc.ActivePositionalRepresentation
	'<<< determine if representation names should be included
	Dim bDefaultMS, bDefaultViewRep, bDefaultPos As Boolean 'all False by default
	Try : bDefaultMS = (oCD.ModelStates.Item(1).Name = sMSName) : Catch : End Try
	Dim oRepsMgr As RepresentationsManager = Nothing
	Try : oRepsMgr = oCD.RepresentationsManager : Catch : End Try
	Try : bDefaultViewRep = (oRepsMgr.DesignViewRepresentations.Item(1).Name = sViewRepName) : Catch : End Try
	Try : bDefaultPos = ((oRepsMgr.PositionalRepresentations Is Nothing) OrElse _
		(oRepsMgr.PositionalRepresentations.Count = 0) OrElse _
		(oRepsMgr.PositionalRepresentations.Item(1).Name = sPosRepName))
	Catch : End Try
	'<<< put new component name parts together, one by one, as needed
	'add File Name first
	Dim sNewOccName As String = sFileName
	'add the Description iProperty value
	If (Not String.IsNullOrWhiteSpace(sDescription)) Then sNewOccName &= sDescription
	'only add ModelState name if it is not the default one (not master or primary)
	If (Not bDefaultMS) AndAlso _
		(Not String.IsNullOrWhiteSpace(sMSName)) Then
		sNewOccName &= " (" & sMSName & ")"
	End If
	'only add design view name if it is not the default one (not master or primary)
	If (Not bDefaultViewRep) AndAlso _
		(Not String.IsNullOrWhiteSpace(sViewRepName)) Then
		sNewOccName &= " (" & sViewRepName & ")"
	End If
	'only add positional rep name if it is not the default one (not master or primary)
	If (Not bDefaultPos) AndAlso _
		(Not String.IsNullOrWhiteSpace(sPosRepName)) Then
		sNewOccName &= " (" & sPosRepName & ")"
	End If
	'add 'Index' at end
	sNewOccName &= sIndex
	'now try to actually set that new name to the component
	Try
		oOcc.Name = sNewOccName
	Catch
		Logger.Error("Failed to rename occurrrence originally named:  " & oOcc.Name)
	End Try
Next 'oOcc
oADoc.Update2(True)

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)