Starting in December, we will archive content from the community that is 10 years and older. This FAQ provides more information.
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!
Solved! Go to Solution.
Solved by JelteDeJong. Go to Solution.
Solved by JelteDeJong. Go to Solution.
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)
@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
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
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.
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
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().
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
Can't find what you're looking for? Ask the community or share your knowledge.