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).

Replace the File Reference by Inventor API


Comments

nina.gogeshvili@business-consulting.de said...
Error: oDoc is not a Drawing.Document. it's a .iam Document. So
Dim oDoc As Inventor.Document is better :D
greetz 
Nina
Matt said...
It should say Inventor.AssemblyDocument I think

Sub ChangeFileNameReferencedFromAssembly()
  Dim asmDoc As AssemblyDocument
  Set asmDoc = ThisApplication.ActiveDocument
  
  Dim asmCompDef As AssemblyComponentDefinition
  Set asmCompDef = asmDoc.ComponentDefinition
  
  Dim asmOcc As ComponentOccurrence
  Set asmOcc = asmCompDef.Occurrences(1)
  
  Dim partDoc As PartDocument
  Set partDoc = asmOcc.Definition.Document
  
  Dim fileMgr As FileManager
  Set fileMgr = ThisApplication.FileManager
  
  Dim oldFileName As String
  oldFileName = partDoc.FullFileName
  
  ' Create new file with new file name
  Dim newFileName As String
  newFileName = oldFileName + "_new.ipt"
  
  Call fileMgr.CopyFile(oldFileName, newFileName)
  
  ' Change reference
  ' The file you replace the reference to needs
  ' to be the same file or one derived from the
  ' original file
  Call asmOcc.ReferencedDocumentDescriptor. _
    ReferencedFileDescriptor.ReplaceReference(newFileName)
      
  ' Delete the original file (if you want)
  Call fileMgr.DeleteFile(oldFileName)
End Su


change Referance file name VBA

change Referance file name VBA

hi
i want to change name of files referanced iam file
i write code but give me error (Wrong number of arguments or invaild property assignment)
how do you change referance files name
thanks

Private Sub Form_Load()
Dim invApprentice As New ApprenticeServerComponent
Dim odoc As ApprenticeServerDocument
Set odoc = invApprentice.Open("C:\Altar\Assembly1.iam")


Dim oRefDoc As ApprenticeServerDocument
For Each oRefDoc In odoc.AllReferencedDocuments
oRefDoc.FullFileName = InputBox("Change File Name", "Change")

Next
Set odoc = Nothing
DoEvents

End Sub
Report


Re: change Referance file name VBA

Hi -

You can't change file names that way.

It sounds like you should be looking at ApprenticeServer's FileSaveAs object. Check out the API documentation for this.

There's also some information in the Apprentice Server overview section on this.

Cheers,
Jeff
Report

Re: change Referance file name VBA

hi jeff
thank you very much for answer

are there more Tutorials inventor Apprentice

Merry chrismas
Report



Re: change Referance file name VBA

Jeff, the example code from Autodesk does not work. Even the example from
ADN website does not work.

Joe ...


wrote in message 
news:6310790@discussion.autodesk.com...
Hi -

You can't change file names that way.

It sounds like you should be looking at ApprenticeServer's FileSaveAs 
object. Check out the API documentation for this.

There's also some information in the Apprentice Server overview section on 
this.

Cheers,
Jeff

Report

Re: change Referance file name VBA

hi joe
this code is apprentice code, also Apprentice cannot be used within
Inventor’s VBA, so this sample must be run from either Visual Basic or VBA within another product, i.e.

Dim oApprentice As New ApprenticeServerComponent
' Open a document.
Dim oDoc As ApprenticeServerDocument
Set oDoc = oApprentice.Open("C:\Temp\Assembly1.iam")
' Iterate through the references looking for a
' reference to a specific file.
Dim oRefFileDesc As ReferencedFileDescriptor
For Each oRefFileDesc In oDoc.ReferencedFileDescriptors
If oRefFileDesc.FullFileName = "C:\Temp\OldPart.ipt" Then
' Replace the reference.
Call oRefFileDesc.PutLogicalFileNameUsingFull( _ "C:\Temp\NewPart.ipt")
Exit For
End If Next
' Set a reference to the FileSaveAs object.
Dim oFileSaveAs As FileSaveAs
Set oFileSaveAs = oApprentice.FileSaveAs
' Save the assembly.
Call oFileSaveAs.AddFileToSave(oDoc, oDoc.FullFileName)
Call oFileSaveAs.ExecuteSave
Report

in reply to: Rank

Re: change Referance file name VBA

hi joe
this code is apprentice code, also Apprentice cannot be used within
Inventor’s VBA, so this sample must be run from either Visual Basic or VBA within another product, i.e.

Dim oApprentice As New ApprenticeServerComponent
' Open a document.
Dim oDoc As ApprenticeServerDocument
Set oDoc = oApprentice.Open("C:\Temp\Assembly1.iam")
' Iterate through the references looking for a
' reference to a specific file.
Dim oRefFileDesc As ReferencedFileDescriptor
For Each oRefFileDesc In oDoc.ReferencedFileDescriptors
If oRefFileDesc.FullFileName = "C:\Temp\OldPart.ipt" Then
' Replace the reference.
Call oRefFileDesc.PutLogicalFileNameUsingFull( _ "C:\Temp\NewPart.ipt")
Exit For
End If Next
' Set a reference to the FileSaveAs object.
Dim oFileSaveAs As FileSaveAs
Set oFileSaveAs = oApprentice.FileSaveAs
' Save the assembly.
Call oFileSaveAs.AddFileToSave(oDoc, oDoc.FullFileName)
Call oFileSaveAs.ExecuteSave


Re: change Referance file name VBA

Sorry - this code does not work. It fails on the
"PutLogicalFilenameUsingFull" method.

I am running it in VB6SP6 with IV2010 loaded.

Joe ...


wrote in message news:6314748@discussion.autodesk.com...
hi joe
this code is apprentice code, also Apprentice cannot be used within
Inventor's VBA, so this sample must be run from either Visual Basic or VBA 
within another product, i.e.

Dim oApprentice As New ApprenticeServerComponent
' Open a document.
Dim oDoc As ApprenticeServerDocument
Set oDoc = oApprentice.Open("C:\Temp\Assembly1.iam")
' Iterate through the references looking for a
' reference to a specific file.
Dim oRefFileDesc As ReferencedFileDescriptor
For Each oRefFileDesc In oDoc.ReferencedFileDescriptors
If oRefFileDesc.FullFileName = "C:\Temp\OldPart.ipt" Then
' Replace the reference.
Call oRefFileDesc.PutLogicalFileNameUsingFull( _ 
"C:\Temp\NewPart.ipt")
Exit For
End If Next
' Set a reference to the FileSaveAs object.
Dim oFileSaveAs As FileSaveAs
Set oFileSaveAs = oApprentice.FileSaveAs
' Save the assembly.
Call oFileSaveAs.AddFileToSave(oDoc, oDoc.FullFileName)
Call oFileSaveAs.ExecuteSave

Report

Re: change Referance file name VBA

hi
you must generate C:\Temp\Assembly1.iam (include oldpart.ipt) then you must generate newpart.ipt on C:\Temp\
if you do thıs it is run
maybe you have add referance to vba6 "autodesk inventor's appreance object library"

iProperty Object Model Inventor


Comments

Brad Johnson said...
When I run the following code in VB6 
Private Sub Form_Load()
Call GetActiveDocument
End Sub
Public Sub GetActiveDocument()
' Get the active document.
Dim oDoc As Document
Set oDoc = ThisApplication.ActiveDocument
MsgBox "Got " & oDoc.FullFileName
End Sub
'
I get the following error
Run-time error '429':
ActiveX component can't create object
Can you tell me why?
Tommy Rosberg said...
A quick question:
I can see in the table that some iProperty categories have the word 'Inventor' added to their name. The category earlier called 'Summary Information' is now called 'Inventor Summary Information'. Have I understood this correct?
Best regards,
Tommy Rosberg
Avalon PLM AB