get filename of first instance in an assembly

get filename of first instance in an assembly

william
Advocate Advocate
1,536 Views
7 Replies
Message 1 of 8

get filename of first instance in an assembly

william
Advocate
Advocate

Hello 

I would like to get the filename of the first part in an assembly. I have some code which will get the filename of an instance in the assembly, but it does not work as i would expect. Item(3) returns the value of the fist part, while Item(1) is for the last component. 

openDoc = ThisDoc.Document
Dim docFile As Document
Dim docName As String

docFile = openDoc.ReferencedDocuments.Item(3)
docName = System.IO.Path.GetFileName(docFile.FullFileName)

MessageBox.Show(docName, "Filename")

How do I reliably get the  filename of the part at the top of the feature tree, when the number of parts will change in different assemblies. 

 

Screenshot attached

0 Likes
Accepted solutions (1)
1,537 Views
7 Replies
Replies (7)
Message 2 of 8

JhoelForshav
Mentor
Mentor
Accepted solution

Hi @william 

Could you maybe use the BrowserPane to get the first occurrence node in the top node?

Something like this:

Dim oAsm As AssemblyDocument = ThisDoc.Document
Dim oNode As BrowserNode = oAsm.BrowserPanes.ActivePane.TopNode
Dim oFileName As String

For i = 1 To oNode.BrowserNodes.Count
	Try
	If TypeOf (oNode.BrowserNodes(i).BrowserNodeDefinition.NativeObject) Is ComponentOccurrence
		oFileName = oNode.BrowserNodes(i).BrowserNodeDefinition.NativeObject.Definition.Document.FullFileName
		Exit For
	End If
	Catch
	End Try
Next

MsgBox(oFileName)
Message 3 of 8

_dscholtes_
Advocate
Advocate

First, [the filename of the first part in an assembly] and [the  filename of the part at the top of the feature tree] are two different things. The order of parts in an assembly is the order in which they were added/placed. The order of nodes (items) in the feature/model tree usually follows the same order, but they can be re-ordered manually. That's why you discovered that [Item(3) returns the value of the fist part].

To get the first item in an assembly, I would not use the ReferencedDocuments, but use something like:  (VBA)

'Get the active assembly document
Dim oDoc As AssemblyDocument
Set oDoc = ThisApplication.ActiveEditDocument

'Set a reference to component definition of the active document.
Dim oCompDef As AssemblyComponentDefinition
Set oCompDef = oDoc.ComponentDefinition

'Set reference to the first component
Dim oOcc As ComponentOccurrence
Set oOcc = oCompdef.Occurrences(1)

'Set document name
Dim sDocName as String
sDocName = oOcc.FullDocumentName

To get the first node in the feature/model tree, try the code posted by JhoelForshav.

 

Message 4 of 8

WCrihfield
Mentor
Mentor

I know it's practically the same thing, but here's the quickest shortest route to that first placed Occurence's FullFileName.

 

MsgBox(ThisApplication.ActiveDocument.ComponentDefinition.Occurrences(1).Definition.Document.FullFileName)

 

If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click 'LIKE" 👍.

Also, when you have time, please review & vote for these 'Ideas' I'd like to get implemented.

  • Add more capabilities to the 'Customize' dialog box (exe. Add Tab & Add Panel) Click Here
  • Constrain & Dimension Images In Assembly Sketches & Drawing Sketches (TitleBlocks & SketchedSymbols) Click Here
  • Save Section View Status In DesignViewRepresentation (So It Can Be Used In The Drawing) Click Here
  • Add SolidBodies Folder In iLogic Rule Editor Model Tab Click Here
  • Convert All Views To Raster Before Autosave Stores To 'OldVersions' Folder Click Here
  • SetDesignViewRepresentation - Fix limitations for DrawingView of a Part Click Here
  • Create DocumentSubTypeEnum Click Here
  • Add kRevisionTag or kDrawingRevisionTag to ObjectTypeEnum Click Here

Inventor 2020 Help | Inventor Forum | Inventor Customization Forum | Inventor Ideas Forum

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 5 of 8

_dscholtes_
Advocate
Advocate

@WCrihfield Your code is better, because it fixes the mistake in mine. I forgot the .Definition.Document part.

 

0 Likes
Message 6 of 8

william
Advocate
Advocate

Thanks everyone. The code by @JhoelForshav  works well. 

I am using it in an assembly to pull in iproperties from a component, and it will still work if there is a copy design/file name change. Code below. 

 

Dim oAsm As AssemblyDocument = ThisDoc.Document
Dim oNode As BrowserNode = oAsm.BrowserPanes.ActivePane.TopNode
Dim oFile As Document
Dim docName As String

For i = 1 To oNode.BrowserNodes.Count
	Try
	If TypeOf (oNode.BrowserNodes(i).BrowserNodeDefinition.NativeObject) Is ComponentOccurrence
		oFile = oNode.BrowserNodes(i).BrowserNodeDefinition.NativeObject.Definition.Document
		docName = System.IO.Path.GetFileName(oFile.FullFileName)
		Exit For
	End If
	Catch
	End Try
Next

'MessageBox.Show(docName, "Filename")

iProperties.Value("Project", "Stock Number") = iProperties.Value(docName, "Project", "Stock Number")
iProperties.Value("Project", "Description") = iProperties.Value(docName, "Project", "Description")
iProperties.Value("Project", "Project") = iProperties.Value(docName,"Project", "Project")
0 Likes
Message 7 of 8

johan
Enthusiast
Enthusiast

Hi, 

 

This indeed gets the filename of assembly occurences, but how to do the same for suboccurences (parts)? If the occurence is a part, then the code does not work

0 Likes
Message 8 of 8

WCrihfield
Mentor
Mentor

If it is a 'sub-assembly' then instead of checking for 'ComponentOccurrence' you would check for 'AssemblyComponentDefinition'.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes