Does this code look correct?... Rename browser names to match file names

Does this code look correct?... Rename browser names to match file names

chris
Advisor Advisor
449 Views
3 Replies
Message 1 of 4

Does this code look correct?... Rename browser names to match file names

chris
Advisor
Advisor

I asked Chat GPT Plus to:

chris_0-1679797685728.png

Here's the code:

 

' Get a reference to the active assembly document
Dim oDoc As AssemblyDocument = ThisApplication.ActiveDocument

' Loop through all components in the assembly
For Each oComp As ComponentOccurrence In oDoc.ComponentDefinition.Occurrences
' Get the file name of the component
Dim sFileName As String = oComp.Definition.Document.FullFileName

' Extract the file name without extension
Dim sNameWithoutExt As String = System.IO.Path.GetFileNameWithoutExtension(sFileName)

' Rename the browser node to match the file name
oComp.Name = sNameWithoutExt
Next

 

 

I get this error when I try to run it, but I don't know what it means or what to fix/change

 

chris_0-1679797987770.png

 

 

0 Likes
450 Views
3 Replies
Replies (3)
Message 2 of 4

JelteDeJong
Mentor
Mentor

The bot forgot that you need to give unique names to all occurrences. If you use the filenames directly you get into trouble if you use the same file 2 times. The most simple method of making something unique is to add an incremental number. Something like this:

Dim oDoc As AssemblyDocument = ThisApplication.ActiveDocument
Dim i = 1000
For Each oComp As ComponentOccurrence In oDoc.ComponentDefinition.Occurrences
    Dim sFileName As String = oComp.Definition.Document.FullFileName
    Dim sNameWithoutExt As String = System.IO.Path.GetFileNameWithoutExtension(sFileName)

    oComp.Name = sNameWithoutExt & ":" & i
    i = i + 1
Next

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

Message 3 of 4

WCrihfield
Mentor
Mentor

Another thing that comes to mind is that you can essentially 'reset' the component names to their default, which I believe is the file name, by simply setting the component's name to an empty String.  When you do that it will usually resume its default name (what it would have naturally been named when you first inserted it).  But beware, if any of the components are Suppressed, you will not be able to access some of its properties without throwing errors.  And accessing the 'Definition' of a component when it is suppressed will definitely throw an error.  So, you may want to enclose the code within the loop in a Try...Catch block, to help avoid the potential errors.  You may also try going through the ComponentOccurrence.ReferencedDocumentDescriptor.ReferencedFileDescriptor.FullFileName, in an attempt to avoid that other potential error.  Just some thoughts.

But there is a simple user interface tool specifically for this task too.  It can be found in the Assemble tab > Productivity panel (may not be showing), in the drop-down list is a tool named 'Rename Browser Nodes'.  When you click on it, it gives you a few options for how to rename them, including 'Filename' & 'Default'.

WCrihfield_0-1679921895639.png

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 4 of 4

GosponZ
Collaborator
Collaborator

Try this one

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")
'ocProp = modelName.PropertySets.Item("Inventor User Defined Properties") 'Custom property set

ActiveSheet.Sheet.Name = oProp.Item("Part Number").Value & " " & oProp.Item("Description").Value '_
'& " " & "QTY"


Next

oSheets(1).activate

0 Likes