Using iLogic to Rename Browser Nodes

Using iLogic to Rename Browser Nodes

Anonymous
Not applicable
11,926 Views
18 Replies
Message 1 of 19

Using iLogic to Rename Browser Nodes

Anonymous
Not applicable

I'm looking for some help figuring out how to create an iLogic rule that automatically renames the browser nodes based on part number.  I know that this is a function in Inventor itself.  The problem is that there is no built-in functionality to automatically change the default browser name to something besides part number.  I've done a lot of digging and I came across the following code from an old thread discussing Inventor 2011.  I am interested in 2013.  It is also worth noting that when I run this code, my Inventor crashes.  

 

If someone knows more about programming than I do and can get this to work, it would be much appreciated.

 

 

 

PrivateSubMain
UpdateAssyBrowser(ThisDoc.Document)
End Sub

PrivateSubUpdateAssyBrowser(ByValoDocAsInventor.Document)
DimoAssyAsInventor.AssemblyDocument
DimoCompAsInventor.ComponentOccurrence
DimoSubDocAsInventor.Document

DimNodeName()AsString
DimInstNumAsString

IfoDoc.DocumentType=Inventor.DocumentTypeEnum.kAssemblyDocumentObjectThen
oAssy=CType(oDoc, AssemblyDocument)

ForEachoCompInoAssy.ComponentDefinition.Occurrences
oSubDoc=CType(oComp.Definition.Document, Document)
NodeName=oComp.Name.Split(NewChar() {":"c}, StringSplitOptions.None)
InstNum=":"&NodeName(1)
oComp.Name=GetProp(oSubDoc, "Design Tracking Properties", "Part Number")&InstNum

IfoSubDoc.DocumentType=Inventor.DocumentTypeEnum.kAssemblyDocumentObjectThen
CallUpdateAssyBrowser(oSubDoc)
EndIf

NextoComp
EndIf
End Sub

PrivateFunctionGetProp(ByValoDocAsInventor.Document, ByValPropSetNameAsString, ByValPropNameAsString)AsString
DimoPropSetAsInventor.PropertySet
DimsHoldAsString=""

oPropSet=oDoc.PropertySets(PropSetName)

DimoPropAsInventor.Property

ForEachoPropInoPropSet
IfoProp.DisplayName="Part Number"Then
sHold=CStr(oProp.Value)
ExitFor
EndIf
NextoProp

ReturnsHold
End Function

Accepted solutions (1)
11,927 Views
18 Replies
Replies (18)
Message 2 of 19

GosponZ
Collaborator
Collaborator
0 Likes
Message 3 of 19

Anonymous
Not applicable
Accepted solution

Here you go. Please let me know if you have any questions on how this works:

 

Sub Main()

'Grab the Assembly Document

Dim oDoc As AssemblyDocument oDoc = ThisApplication.ActiveDocument 'Grab the Active Assembly Component Definition

Dim oAsmCompDef As AssemblyComponentDefinition oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition ' Get the model browser

Dim oPane As BrowserPane oPane = oDoc.BrowserPanes.Item("Model") ' Grab the occurrence of the object Dim oOcc As ComponentOccurrence For Each oOcc In oDoc.ComponentDefinition.Occurrences 'Grab Document from Occurrence

Dim oOccDoc As Document oOccDoc = oOcc.Definition.Document 'get design properties set'

Dim invDesignInfo As PropertySet invDesignInfo = oOccDoc.PropertySets.Item("Design Tracking Properties") ' Get the part number property.

Dim invPartNumberProperty As Inventor.Property invPartNumberProperty = invDesignInfo.Item("Part Number") ' Get the browser node that corresponds to the new item

Dim oSubAssyNode As BrowserNode oSubAssyNode = oPane.GetBrowserNodeFromObject(oOcc) If oSubAssyNode.NativeObject.Name <> invPartNumberProperty.Value Then 'Set The name
oSubAssyNode.NativeObject.Name = (invPartNumberProperty.Value) End If Next End Sub
0 Likes
Message 4 of 19

Anonymous
Not applicable

That code looks like it works better, the only problem there is that it doesn't detect the instance number, so when I have

 

part001:1 

part001:2 

 

I get an error if I try to run the rule.  I appreciate the help!  

0 Likes
Message 5 of 19

Anonymous
Not applicable

What would you like it to do in that case?

0 Likes
Message 6 of 19

Anonymous
Not applicable

I would like to be able to maintain the instance number as is in Inventor so that the above example would be

 

partnumber001:1

partnumber001:2

 

0 Likes
Message 7 of 19

Anonymous
Not applicable

That shouldn't be too hard. You just need to check if a name already exists and add :X to the end where X is the most recent instance +1. I'm out of time today but I might be able to whip that up in the next few days. 

0 Likes
Message 8 of 19

Anonymous
Not applicable

Alright I got it after playing with it a little bit.  I added the appropriate code and it works perfectly.  I really appreciate your help! 

    Dim NodeName() As String
    NodeName=oOcc.Name.Split( {":"c},StringSplitOptions.None)
    InstNum=":" & NodeName(1)
0 Likes
Message 9 of 19

Anonymous
Not applicable

Hi

 

Is it possible to get your full working code ? I can't get this to work...

 

Borre

0 Likes
Message 10 of 19

Anonymous
Not applicable

I have the code set to iterate through the active assembly and rename all non standard content Center components based on Description. 

 

As a disclaimer, this code will not count unique occurrences for components.

 

 

Message 11 of 19

GosponZ
Collaborator
Collaborator

Working perfect on my machine. I'm working with multi sheets.

 

oDoc = ThisDoc.Document

oSheets = oDoc.Sheets

      For Each oSheet In oSheets

            oSheet.activate

            oView = oSheet.DrawingViews.Item(1)

            modelName = oView.ReferencedDocumentDescriptor.ReferencedDocument

            oProp = modelName.PropertySets.Item("Design Tracking Properties")

            ActiveSheet.Sheet.Name = oProp.Item("Part Number").Value & " " & oProp.Item("Description").Value & " " & oProp.Item("Authority").Value

 

      Next

oSheets(1).activate

0 Likes
Message 12 of 19

Anonymous
Not applicable

Thank's a lot. I can see where I failed, but my skills was not good enough to figure it out myself. This works perfect. Thank you again.

0 Likes
Message 13 of 19

Nsteel
Contributor
Contributor

Can someone please help,

I have modified this script to rename the nodes as "Part Number - Revision Number"

There is an assembly file in it I don't want it to change, could somone show me what to add and so it skips over this one?

The node name for this file is "02002.Tube and Pipe Runs"

 

Thanks in advance.

0 Likes
Message 14 of 19

J.Classens
Enthusiast
Enthusiast

I have a quastion about this old post.

 

When i want to customize the rule with other properties. 

It will fail and not work again. 

 

original:

    ' Get the part number property.
    Dim invPartNumberProperty As Inventor.Property
    invPartNumberProperty = invDesignInfo.Item("Description")
	

The description i want to change to multiple properties like Partnumber, description, title.

I can't get it to work like that. 

 

Any suggestions? 

 

Further i noticed that this rule only work at the first level from the BOM structure. 

The only childeren from assemblies doesn't update. 

 

I don't know how we can edit this. 

 

Thanks in advance!  

0 Likes
Message 15 of 19

benjamin.eidemL3D38
Explorer
Explorer

Any solution on this problem?

0 Likes
Message 16 of 19

J.Classens
Enthusiast
Enthusiast

I have found a Ilogic rule to do the trick.

This Ilogic rule i have modified with the needed properties.

 

When you want to use VBA you write a code that opens this Ilogic rule. 

 

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
	
	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
		
	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
Message 17 of 19

J.Classens
Enthusiast
Enthusiast

I have found a Ilogic rule to do the trick.

This Ilogic rule i have modified with the needed properties.

 

When you want to use VBA you write a code that opens this Ilogic rule. 

 

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
	
	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
		
	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
0 Likes
Message 18 of 19

benjamin.eidemL3D38
Explorer
Explorer

Thank you! I have used the rule with great success, but recently got an error message:

 

System.Runtime.InteropServices.COMException (0x80004005): Unspecified error (Exception from HRESULT: 0x80004005 (E_FAIL))
at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
at Inventor.ComponentOccurrence.get_Definition()
at ThisRule.SetOccName(ComponentOccurrence oComp, Int32 oInt)
at ThisRule.Main()
at Autodesk.iLogic.Exec.AppDomExec.ExecRuleInAssembly(Assembly assem)
at iLogic.RuleEvalContainer.ExecRuleEval(String execRule)

 

Any idea on what's wrong here?

 

(PS! No "expert" in iLogic and coding 😅)

0 Likes
Message 19 of 19

benjamin.eidemL3D38
Explorer
Explorer

Thank you! I have used the rule with great success, but recently got an error message:

 

System.Runtime.InteropServices.COMException (0x80004005): Unspecified error (Exception from HRESULT: 0x80004005 (E_FAIL))
at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
at Inventor.ComponentOccurrence.get_Definition()
at ThisRule.SetOccName(ComponentOccurrence oComp, Int32 oInt)
at ThisRule.Main()
at Autodesk.iLogic.Exec.AppDomExec.ExecRuleInAssembly(Assembly assem)
at iLogic.RuleEvalContainer.ExecRuleEval(String execRule)

 

Any idea on what's wrong here?

 

(PS! No "expert" in iLogic and coding 😅)

0 Likes