Announcements

Starting in December, we will archive content from the community that is 10 years and older. This FAQ provides more information.

Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to get Document Object for all components in an assembly or a weldment?

8 REPLIES 8
SOLVED
Reply
Message 1 of 9
j.pavlicek
1718 Views, 8 Replies

How to get Document Object for all components in an assembly or a weldment?

Hello,

what is proper way to obtain Document Objects for all components of given Assembly or Weldment?

 

I know about AssemblyDocument.ReferencedDocuments Property but this returns not only component document, right?

 

Thank you for suggestions!



Inventor 2022, Windows 10 Pro
Sorry for bad English.
Labels (3)
8 REPLIES 8
Message 2 of 9
WCrihfield
in reply to: j.pavlicek

Hi @j.pavlicek.  The best way to know for sure about these sorts of things is to investigate them.  If you have large and complex assemblies in which many of the files may have various other types of documents attached to them, and you are unsure about if all documents returned by the 'ReferencedDocuments' will return any of those other types of documents, then I would suggest setting up a loop of those documents that checks the document type of each one and uses a feedback message or makes log entries inside of the loop that will report to you what it finds.  But beyond that, here is one of those 'fancy' codes that uses the IEnumerable functionality to filter the collection to only AssemblyDocuments and PartDocuments.

Dim oADoc As AssemblyDocument = ThisApplication.ActiveDocument
Dim oAllModelDocs As IEnumerable(Of Inventor.Document) = oADoc.ReferencedDocuments.Cast(Of Inventor.Document) _
.Where(Function(d) d.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Or _
d.DocumentType = DocumentTypeEnum.kPartDocumentObject)
'now you can use that oAllModelDocs variable here

This filtering code does not attempt to filter out things like Content Center stuff, iPart/iAssembly/ModelState member type documents (vs FactoryDocument), or other such things though.  If you want to be more thorough though, you will probably just have to use a traditional loop with more filtering check type codes within, to make sure you are left with only the ones you want.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 9
j.pavlicek
in reply to: j.pavlicek

 

@WCrihfield Are you sure that AssemblyDocument.ReferencedDocuments set is equal to (component documents) set?

 

Because when I create an empty Assembly (with no components) and link some parameter from external file, this file document is returned in AssemblyDocument.ReferencedDocuments although it is not definitely a component!

 

Consider this code:

Dim AsDoc As AssemblyDocument = ThisDoc.Document
For Each Doc In AsDoc.ReferencedDocuments
  Logger.Info(Doc.DisplayName)	
Next Doc

And it's output in an empty Assembly with linked parameter

jpavlicek_0-1640075719642.png

 

 



Inventor 2022, Windows 10 Pro
Sorry for bad English.
Message 4 of 9
JelteDeJong
in reply to: j.pavlicek

You can try it this way:

Dim doc As AssemblyDocument = ThisDoc.Document
Dim refDocs = doc.ComponentDefinition.Occurrences.Cast(Of ComponentOccurrence).
    Select(Function(occ) occ.ReferencedDocumentDescriptor.ReferencedDocument).
    Distinct().ToList()

For Each refDoc As Document In refDocs
    Logger.Info(refDoc.DisplayName)
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 5 of 9
j.pavlicek
in reply to: JelteDeJong

Thanks for you reply! I did same assumption - iterate over ComponentDefinition.Occurrences.

 

Could you please help me to understand to this part of code?

.Cast(Of ComponentOccurrence).
Select(Function(occ) occ.ReferencedDocumentDescriptor.ReferencedDocument). Distinct().ToList()

Cast(), Select(), Distinct() and ToList() look like Enumerable Class methods.

 

  • Cast is used here because if we are trying "to extract" items of ComponentDefinition.Occurrences but not using Item Property, it doesn't know about it's type?
  • Select is  using lambda function (for each item "extracted by Cast") which returns ReferencedDocumentDescriptor.ReferencedDocument?
  • Distinct is there to "filter out duplicity"?
  • ToList "packs" all these Documents into a List?

 

 



Inventor 2022, Windows 10 Pro
Sorry for bad English.
Message 6 of 9
JelteDeJong
in reply to: j.pavlicek

  • .Cast(of ComponentOccurrence)
    • Converts the ComponentOccurrences object to the object  IEnumerable(of ComponentOccurrence). That allows us to use the LINQ functions of vb.net
  • .Select(Function(occ) occ.ReferencedDocumentDescriptor.ReferencedDocument)
    • This will create an IEnumerable(of Document). The documents are selected from the occurrences.
  • Distinct()
    • Will remove all duplicates from the list. You will have duplicates if you use 1 document for multiple occurrences
  • .ToList()
    • This will convert the object IEnumerable(of Document) to a List(of Document). Strictly this is not needed as you can loop over an IEnumerable(of Document) but because LINQ is lazy it will only do all the above when you create a list. (or start using the IEnumerable like in a for/each loop). I experienced some extra performances when I used a List instead of the IEnumerable

 

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 7 of 9
j.pavlicek
in reply to: JelteDeJong

Thank you very much!


Inventor 2022, Windows 10 Pro
Sorry for bad English.
Message 8 of 9
j.pavlicek
in reply to: JelteDeJong

Just a little note:

@JelteDeJong 's code returns List(of Object), so if you want a different type, you need to put another Cast(of DesiredType) before ToList().



Inventor 2022, Windows 10 Pro
Sorry for bad English.
Message 9 of 9
JelteDeJong
in reply to: j.pavlicek

the problem here is that:

occ.ReferencedDocumentDescriptor.ReferencedDocument

returns an object instead of a document (as you might expect). Anyway, the following might be a bit cleaner code:

Dim doc As AssemblyDocument = ThisDoc.Document
Dim refDocs = doc.ComponentDefinition.Occurrences.Cast(Of ComponentOccurrence).
    Select(Of Document)(Function(occ) occ.ReferencedDocumentDescriptor.ReferencedDocument).
    Distinct().ToList()

For Each refDoc As Document In refDocs
    Logger.Info(refDoc.DisplayName)
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

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report