Rename browser nodes

Rename browser nodes

J.Classens
Enthusiast Enthusiast
3,336 Views
9 Replies
Message 1 of 10

Rename browser nodes

J.Classens
Enthusiast
Enthusiast

Hey,

 

Currently i'm using the next ilogic rule to rename browser nodes.

 

'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

The rule works almost fine but it only changes de browser node from de assembly.

Not the parts lying beneath in this assy.

 

I tried different option but can't get it to work.

 

JClassens_1-1628507757296.png

 

Could someone help me with the right solution? 

 

 

0 Likes
Accepted solutions (1)
3,337 Views
9 Replies
Replies (9)
Message 2 of 10

dutt.thakar
Collaborator
Collaborator

@J.Classens 

 

You can use recursive loop, I believe, Try below code and see if that works. Please note that you will have to use a counter in the end of the name just because if you are using same part more than one time and trying to rename them with the values from it's "Part Number" and "Description" properties, Inventor will throw error because it can not give two components same name in the model browser and that's why we I am using a counter in the end to differentiate them, which is "i" in the code below.

 

 

Sub Main

Dim oDoc As AssemblyDocument = ThisApplication.ActiveDocument
Dim oDef As AssemblyComponentDefinition = oDoc.ComponentDefinition
Call SetOccName(oDoc)
End Sub


Sub SetOccName(asmDoc As AssemblyDocument)
	Dim i As Integer = 0
	For Each oOcc As ComponentOccurrence In asmDoc.ComponentDefinition.Occurrences
		oOcc.Name = iProperties.Value(oOcc.Name,"Project", "Part Number") & " (" & iProperties.Value(oOcc.Name,"Project", "Description") & ")" & "-" & i
		i = i+1
		If oOcc.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject
			SetOccName(oOcc.Definition.Document)
		End If
	Next
End Sub

 

I hope this will help.

 

If this answer has solved your problem please ACCEPT SOLUTION and hit like if you found it helpful..!


Regards,
Dutt Thakar
LinkedIn
Message 3 of 10

J.Classens
Enthusiast
Enthusiast

This works perfect! 

 

The only problem that now occurs. 

Is the custom property i have added. 

In some files this property isn't pressent. 

 

When i fix it like the rule beneath it will skip the complete browser note. 

Is there a method that the rule only skip this property? 

 

Sub Main

Dim oDoc As AssemblyDocument = ThisApplication.ActiveDocument
Dim oDef As AssemblyComponentDefinition = oDoc.ComponentDefinition
Call SetOccName(oDoc)
End Sub



Sub SetOccName(asmDoc As AssemblyDocument)
	Dim i As Integer = 0
	
	For Each oOcc As ComponentOccurrence In asmDoc.ComponentDefinition.Occurrences
		oOcc.Name = iProperties.Value(oOcc.Name,"Project", "Part Number") & " (" & iProperties.Value(oOcc.Name,"Custom", "Article No") & " " & iProperties.Value(oOcc.Name,"Summary", "Title") & ")" & "-" & i
		i = i + 1

		On Error Resume Next
		
		If oOcc.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject
			SetOccName(oOcc.Definition.Document)
		End If
	Next
End Sub

 

0 Likes
Message 4 of 10

mcgyvr
Consultant
Consultant

Do a "Try/Catch"..

"Try" to set it.. If it fails... "Catch" to set it to whatever you want without the custom iprop.

 

https://knowledge.autodesk.com/support/inventor-engineer-to-order/learn-explore/caas/CloudHelp/cloud...



-------------------------------------------------------------------------------------------
Inventor 2023 - Dell Precision 5570

Did you find this reply helpful ? If so please use the Accept Solution button below.
Maybe buy me a beer through Venmo @mcgyvr1269
Message 5 of 10

WCrihfield
Mentor
Mentor

Building from your last posted code, then adding in a few possible checks, to give you some ideas, then incorporating the Try...Catch block as @mcgyvr suggested, here is one version of the resulting code you may be able to use.  Basically, just before attempting to incorporate all those iProperty values directly into the component name, I gathered the values of those individual iProperties to variables, then checked them to see if they contained values, then recorded those findings into boolean variables that can be used as flags later to help us determine which ones we want to use.  I then used a couple If...ElseIf...Then block to offer a couple alternatives.  Then the part of the code that tries to assign the new name to the component, I placed inside of the Try...Catch block, because that is the most error prone line, and we may need to deal with potential errors there.

Here's the code:

Sub Main
Dim oDoc As AssemblyDocument = ThisApplication.ActiveDocument
Dim oDef As AssemblyComponentDefinition = oDoc.ComponentDefinition
SetOccName(oDoc)
End Sub

Sub SetOccName(asmDoc As AssemblyDocument)
	Dim i As Integer = 0
	For Each oOcc As ComponentOccurrence In asmDoc.ComponentDefinition.Occurrences
		Dim oPNEmpty, oArticleNoEmpty, oTitleEmpty As Boolean 'False by default
		Dim oPN As String = iProperties.Value(oOcc.Name, "Project", "Part Number")
		If String.IsNullOrEmpty(oPN) Then oPNEmpty = True
		Dim oArticleNo As String = iProperties.Value(oOcc.Name, "Custom", "Article No")
		If String.IsNullOrEmpty(oArticleNo) Then oArticleNoEmpty = True
		Dim oTitle As String = iProperties.Value(oOcc.Name, "Summary", "Title")
		If String.IsNullOrEmpty(oTitle) Then oTitleEmpty = True
		Try
			If oPNEmpty = False And oArticleNoEmpty = False And oTitleEmpty = False Then
				oOcc.Name = oPN & " (" & oArticleNo & " " & oTitle & ")" & "-" & i
			ElseIf oArticleNoEmpty = True Then
				oOcc.Name = oPN & " (" & oTitle & ")" & "-" & i
			End If
		Catch
			'MsgBox("Assigning new name to component failed.", , "")
		End Try
		i = i + 1
		If oOcc.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject
			SetOccName(oOcc.Definition.Document)
		End If
	Next
End Sub

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 6 of 10

J.Classens
Enthusiast
Enthusiast

@WCrihfield Thanks! 

 

Seems like logic way to solve the problem.

When i tried the rule the only problem is that the following line always provides true even if the property isn't empty. 

 

		If String.IsNullOrEmpty(oArticleNo) Then oArticleNoEmpty = True 

 That's why i always get the next line.

			ElseIf oArticleNoEmpty = True Then
				oOcc.Name = oPN & " (" & oTitle & ")" & "-" & i

I tried to fix it with a deny function. But this isn't recognized. 

		If String.IsNotNullOrEmpty(oArticleNo) Then oArticleNoEmpty = False
		If String.IsNullOrEmpty(oArticleNo) Then oArticleNoEmpty = True 

  You put also this rule in. 

	Dim oPNEmpty, oArticleNoEmpty, oTitleEmpty As Boolean  'False by default

Is it alway false by default? Or may the problem be in this rule?

 

0 Likes
Message 7 of 10

WCrihfield
Mentor
Mentor

Are you sure the name of that custom iProperty "Article No" is spelled the same in all components and in the code?

 

Yes, I'm sure those Boolean type variables are False by default when they are first created.  And they are created within the loop of components, so they are created new for each loop and can't maintain their value from the previous loop.  But, if it will ease your mind, by all means go ahead and create them individually and set their values to False each time.  What I did was just condensed into one line.

 

I have altered the code a bit to give the Boolean variables a definite value one way or the other when checking iProperty values.  I also switched from the iProperty.Value() snippets to the normal Document.PropertySets.PropertySet.Property.Value route of accessing those iProperties, and incorporated an extra Try...Catch block of code around the part where I try to get the value of that custom iProperty, in case it doesn't exist yet, to avoid potential errors, and to know for sure whether it exists or not.  There are multiple options for what we could put in the Catch portion of that check, but I just put a message in there for now.  I also created more lines for other possibilities within the If...ElseIf...Then block of code where the component name is assembled & assigned, just in case some components may not have their Part Number and/or Title filled out either. (There are 6 possible combinations of those 3 iProperties having values.)

Here's the updated code:

Sub Main
	Dim oDoc As AssemblyDocument = ThisApplication.ActiveDocument
	Dim oDef As AssemblyComponentDefinition = oDoc.ComponentDefinition
	SetOccName(oDef)
End Sub

Sub SetOccName(oADef As AssemblyComponentDefinition)
	Dim i As Integer = 0
	For Each oOcc As ComponentOccurrence In oADef.Occurrences
		Dim oOccDoc As Document = oOcc.Definition.Document
		Dim oPSets As PropertySets = oOccDoc.PropertySets
		Dim oPNEmpty, oArticleNoEmpty, oTitleEmpty As Boolean 'False by default
		Dim oPN As String = oPSets.Item(3).Item("Part Number").Value
		If String.IsNullOrEmpty(oPN) Then oPNEmpty = True Else oPNEmpty = False
		Dim oTitle As String = oPSets.Item(1).Item("Title").Value
		If String.IsNullOrEmpty(oTitle) Then oTitleEmpty = True Else oTitleEmpty = False
		Dim oArticleNo As String
		Try
			'the custom iProperty may not exist, so use Try...Catch block to check it
			oArticleNo = oPSets.Item(4).Item("Article No").Value
			oArticleNoEmpty = False
		Catch
			'oArticleNoEmpty = True
			'above failed, so it was not found
			'you could try to create it here, or just ignore it, or use a message to let you know
			MsgBox("Custom iProperty 'Article No' was not found it component named " & oOcc.Name,,"")
		End Try
		If String.IsNullOrEmpty(oArticleNo) Then oArticleNoEmpty = True Else oArticleNoEmpty = False
		
		Try
			If oPNEmpty = False And oArticleNoEmpty = False And oTitleEmpty = False Then 'all present
				oOcc.Name = oPN & " (" & oArticleNo & " " & oTitle & ")" & "-" & i
			ElseIf oPNEmpty = False And oArticleNoEmpty = True And oTitleEmpty = False Then 'ArticleNo missing
				oOcc.Name = oPN & " (" & oTitle & ")" & "-" & i
			ElseIf oPNEmpty = True And oArticleNoEmpty = False And oTitleEmpty = False Then 'PN missing
				oOcc.Name = "(" & oArticleNo & " " & oTitle & ")" & "-" & i
			ElseIf oPNEmpty = False And oArticleNoEmpty = False And oTitleEmpty = True Then 'Title missing
				oOcc.Name = oPN & "(" & oArticleNo & ")" & "-" & i
			End If
		Catch
			MsgBox("Assigning new name to component named '" & oOcc.Name & "' failed.", , "")
		End Try
		i = i + 1
		If TypeOf oOcc.Definition Is AssemblyComponentDefinition Then
			SetOccName(oOcc.Definition)
		End If
	Next
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 💡or you can Explore My CONTRIBUTIONS

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 8 of 10

J.Classens
Enthusiast
Enthusiast

@WCrihfield 

 

That seems to work! 

The only problem that now pops up is that it doesn't reset the article No.

 

JClassens_1-1628607357902.png

The MAP file has a article No. The MAM beneath doesn't have one. 

It uses now the article No of the last article No it passes.   

 

0 Likes
Message 9 of 10

WCrihfield
Mentor
Mentor
Accepted solution

Hmm... Doesn't seem possible for it to carry that 'Article No' value from one component to another component, since the variable that holds its value is newly created each time within the loop of each component.  The only thing I can think of in that code that might somehow maintain that value would be near the end of the loop's code, where it checks to see if the component is representing a sub-assembly, then recursively running the same Sub on that sub-assembly's components.  We could do a couple things differently, to see if they help, such as maybe setting the values of those 3 variables back to "" (empty string) before the end of the loop, or setting them to "" before assigning the iProperty value to them the first time, but I'm not confident these measures would work either.

Here's a new version of the rule code you can test out:

Sub Main
	Dim oADoc As AssemblyDocument = ThisApplication.ActiveDocument
	Dim oADef As AssemblyComponentDefinition = oADoc.ComponentDefinition
	Dim i As Integer = 0
	For Each oOcc As ComponentOccurrence In oADef.Occurrences
		SetOccName(oOcc, i)
		i = i + 1
	Next
End Sub

Sub SetOccName(oComp As ComponentOccurrence, oInt As Integer)
	Dim oOccDoc As Document = oComp.Definition.Document
	Dim oPSets As PropertySets = oOccDoc.PropertySets
	Dim oPNEmpty As Boolean = False
	Dim oArticleNoEmpty As Boolean = False
	Dim oTitleEmpty As Boolean  = False
	Dim oPN As String = oPSets.Item(3).Item("Part Number").Value
	If String.IsNullOrEmpty(oPN) Then oPNEmpty = True Else oPNEmpty = False
	Dim oTitle As String = oPSets.Item(1).Item("Title").Value
	If String.IsNullOrEmpty(oTitle) Then oTitleEmpty = True Else oTitleEmpty = False
	Dim oArticleNo As String
	Try
		'the custom iProperty may not exist, so use Try...Catch block to check it
		oArticleNo = oPSets.Item(4).Item("Article No").Value
	Catch
		'above failed, so it was not found
		'create it, or ignore it, or show a message
		MsgBox("Custom iProperty 'Article No' was not found it component named " & oComp.Name,,"")
	End Try
	If String.IsNullOrEmpty(oArticleNo) Then oArticleNoEmpty = True Else oArticleNoEmpty = False
		
	Dim oNewName As String = "" 'to assemble name in variable first (no errors expected)
	'could just set it up so that if any are missing or empty, skip trying to rename this component to simplify
	'If oPNEmpty Or oArticleNoEmpty Or oTitleEmpty Then Continue For 'skip to next item in loop
	If oPNEmpty = False And oArticleNoEmpty = False And oTitleEmpty = False Then 'all present
		oNewName = oPN & " (" & oArticleNo & " " & oTitle & ")" & "-" & oInt
	ElseIf oPNEmpty = False And oArticleNoEmpty = True And oTitleEmpty = False Then 'ArticleNo missing
		oNewName = oPN & " (" & oTitle & ")" & "-" & oInt
	ElseIf oPNEmpty = True And oArticleNoEmpty = False And oTitleEmpty = False Then 'PN missing
		oNewName = "(" & oArticleNo & " " & oTitle & ")" & "-" & oInt
	ElseIf oPNEmpty = False And oArticleNoEmpty = False And oTitleEmpty = True Then 'Title missing
		oNewName = oPN & " (" & oArticleNo & ")" & "-" & oInt
	ElseIf oPNEmpty = True And oArticleNoEmpty = False And oTitleEmpty = True Then 'PN & Title missing
		oNewName = "(" & oArticleNo & ")" & "-" & oInt
	ElseIf oPNEmpty = False And oArticleNoEmpty = False And oTitleEmpty = True Then 'ArticleNo & Title missing
		oNewName = oPN & "-" & oInt
	End If
		
	Try 'just in case changing component name fails
		oComp.Name = oNewName
	Catch
		MsgBox("Assigning new name to component named '" & oComp.Name & "' failed.", , "")
	End Try
	
	'reset all String variables to empty (very likely not necessary, but we'll see)
	oNewName = ""
	oPN = ""
	oArticleNo = ""
	oTitle = ""
		
	If oComp.SubOccurrences.Count > 0 Then 'if it has any SubOccurrences, it's a sub-assembly
		For Each oSOcc As ComponentOccurrence In oComp.SubOccurrences
			SetOccName(oSOcc, oInt)
		Next
	End If
End Sub

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 10 of 10

J.Classens
Enthusiast
Enthusiast

Perfect! 

Now it resets te propertie. 

 

Thanks for the help on this one. 

0 Likes