Wednesday, February 27, 2019

Rename Browser nodes from an Assembly and all Parts


last time i found a small iLogic-Code to rename the Display Name of an Inventor-Part

oDoc = ThisApplication.ActiveDocument
oDoc.DisplayName = iProperties.Value("Project", "Part Number") &  " - " & iProperties.Value("Project", "Description")
MessageBox.Show("Displayname = " & oDoc.DisplayName, "iLogic")

now it would be nice to do this from a top of an assembly for all instances of the assembly.
I found same codes for other ilogics, but i'm not a profi about ilogic.

So here is my question. Is it possible to change all display names from components of an assembly and the display name of the assembly.

I know there are other posts in the community, but i didn't find an answer

Jörg

Re: Rename Browser nodes from an Assembly and all Parts

Here's a small VBA macro that should do what you want.

Public Sub ChangeDisplayName()
    Dim asmDoc As AssemblyDocument
    Set asmDoc = ThisApplication.ActiveDocument
    
    ' Change the name for the assembly.
    asmDoc.DisplayName = BuildName(asmDoc)
    
    ' Change the name in every referenced document.
    Dim doc As Document
    For Each doc In asmDoc.AllReferencedDocuments
        doc.DisplayName = BuildName(doc)
    Next
End Sub

Private Function BuildName(doc As Document) As String
    Dim name As String
    Dim designTrackProps As PropertySet
    Set designTrackProps = doc.PropertySets.Item("Design Tracking Properties")
    
    name = designTrackProps.Item("Part Number").Value & " - " & _
           designTrackProps.Item("Description").Value
           
    BuildName = name
End Function

Re: Rename Browser nodes from an Assembly and all Parts

Hi,

Yes this should be possible. I am not finding a complete example that does what you want to do however.

The VBA procedure QueryModelTree() in the help file iterates through the browser model tree of an open part or assembly document, printing the node labels to the VBA debug screen (the "Immediate" window in VBA, if enabled).
Your code would need to do something similar. When it finds a node that you want to rename you would have to get the native object and change the DisplayName or name property for proxies. You can't just change the label.

This is from from the API help:
>> >>
BrowserNodeDefinition Object
Label
Property that gets the label of the BrowserNode. This is the string that is displayed to the user. In the case of BuiltIns, this Label is really a reflection of the corresponding name held by the underlying data - for example, a Sketch or a WorkPlane object. In order to set the Label of a ClientBrowserNodeDefinition, see SetLabel on that object.
<< <<


I tried a quick example. Here is a VBA code snippet. This is just to show what objects in the API you could use to get the native object that needs to change the display. (recurse is part of the QueryModelTree() VBA example in the help) 

Sub recurse(node As BrowserNode)
    If (node.Visible = True) Then
       ' Debug.Print node.BrowserNodeDefinition.Label
       
        Dim oNativeObj As Object
        Set oNativeObj = node.NativeObject
       
        Debug.Print TypeName(oNativeObj)
        Dim oDoc As Document
        Dim oAsmCompDef As AssemblyComponentDefinition
        Dim oCompOcc As ComponentOccurrence
        Dim oCompOccproxy As ComponentOccurrenceProxy
        
        If TypeName(oNativeObj) = "AssemblyComponentDefinition" Then
            Set oAsmCompDef = node.NativeObject
            Set oDoc = oAsmCompDef.Document
            oDoc.DisplayName = "WB"
        End If
       
        If TypeName(oNativeObj) = "ComponentOccurrenceProxy" Then
            Set oCompOccproxy = node.NativeObject
            oCompOccproxy.name = "WB_Test"
        End If

Once you have the VBA code working you can move it to .NET and then to iLogic. This is what I usually do. Prototyping in VBA is a way to quickly get things working. Pasting VBA code to a VB.NET module removes the Set statements and gets you closer to what iLogic will want the code to be. (iLogic rules can run VB.NET code).

No comments:

Post a Comment