- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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
(Not an Autodesk Employee)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
@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 DocAnd it's output in an empty Assembly with linked parameter
Inventor 2022, Windows 10 Pro
Sorry for bad English.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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.
Blog: hjalte.nl - github.com
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
- .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.
Blog: hjalte.nl - github.com
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Inventor 2022, Windows 10 Pro
Sorry for bad English.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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.
Blog: hjalte.nl - github.com