I have a drawing and I need to get a list of parts. I see *most* solutions use the parts list on the sheet itself. I will not have access to that. Is there a way to get a list of parts WITHOUT accessing oSheet.PartsList[1] ?
Thanks
Solved! Go to Solution.
Solved by ekinsb. Go to Solution.
Hi bsee1,
If you need the part list of the referenced assembly:
Dim drawingDoc As DrawingDocument Set drawingDoc = ThisApplication.ActiveDocument Dim views As DrawingViews Set views = drawingDoc.Sheets(1).DrawingViews Dim assemblyDoc As AssemblyDocument Set assemblyDoc = views(1).ReferencedDocumentDescriptor.ReferencedDocument Dim occurrence As ComponentOccurrence For each occurrence In assemblyDoc.ComponentDefinition.Occurrences Dim partDoc As PartDocument Set partDoc = occurrence.Definition.Document Dim partNumber As String partNumber = partDoc.PropertySets("Design Tracking Properties").Item("Part Number").value MsgBox partnumber Next
Otherwise, I don't know how.
Pascal
Does this code work for you? In this line it tells me it cant cast a COM object of this type to PartDocument
Dim partDoc As PartDocument Set partDoc = occurrence.Definition.Document
I'm writing this in C#, but C# and VB are able to convert back and forth fairly easily. Here's what I have so far. Everythign works until I try to cast partDoc.
DrawingDocument drawingDoc = (DrawingDocument)m_inventorApplication.ActiveDocument; DrawingViews views = drawingDoc.Sheets[1].DrawingViews; AssemblyDocument assemDoc = (AssemblyDocument)views[1].ReferencedDocumentDescriptor.ReferencedDocument; foreach(ComponentOccurrence occurrence in assemDoc.ComponentDefinition.Occurrences) { PartDocument partDoc = (PartDocument)occurrence.Definition.Document; var partNumber = partDoc.PropertySets["Design Tracking Properties"].ItemByPropId[1].Value; }
If you have any idea feel free to give suggestion in VB. I can convert it.
Yes! Your problem is your occurrence reference to something else than a part, maybe another assembly?
Create an recursive method like that:
Private function GetPartNumber(doc as document) as string if TypeOf document is partdocument then ' Return Part Number Return CType(doc, PartDocument).PropertySets("Design Tracking Properties").ItemByPropId(1).Value elseIf typeof document is AssemblyDocument then ' Call itself Return GetPartNumber(doc) endif end function
Pascal
I don't see how this is supposed to work. If the type of the document is an AssemblyDocument, it would result in an infinite loop.
I'm guessing here, but I currently have the doc variable being passed to the function set as the active document. Is that right?
I'm not sure what I'm doign wrong, just that usign your original code, it never results in a partDocument.
Yeah, it would result in an infinite loop... I forgot something before calling the recursive method. I change this example to a Sub method.
P.S.: I write it by heart so, there maybe some typing errors.
Private Sub ShowPartNumber(doc as Document) If TypeOf doc Is PartDocument Then MsgBox CType(doc, PartDocument).PropertySets("Design Tracking Properties").ItemByPropId(1).Value ElseIf TypeOf document Is AssemblyDocument Then For Each occ as ComponentOccurrence in CType(doc, AssemblyDocument).ComponentDefinition.Occurrences ' Call itself ShowPartNumber(occ.Definition.Document) Next End If End Sub
You might also find this post useful. It goes over some concepts and also has some code that traverses through an assembly.
http://modthemachine.typepad.com/my_weblog/2009/03/accessing-assembly-components.html
Brian, that's actually the code I was using before I came to the forum. I have that code working, but I only want a list of the parts, whereas each of those examples return the part with a colon at the end to show which occurrence it is.
Is there a way to just get the unique name of each part?(without anything that says what occurrence number it is?)
Using the code on the page you mentioned, one of the parts comes back with the name "_Weldbead:1". I'm not even sure where it's pulling that name from, since a materials list calls it by the name "AISC - 8x4x3/8 - 38". (I typically will not have access to a Materials List).
I know this sounds rather stupid, but I need to recreate exactly what a Materials List shows in its Part Number column...without using the built in Materials List.
I found code that SHOULD allow me to access the part names. I'm actually using the code from an Autodesk employee here: http://forums.autodesk.com/t5/Autodesk-Inventor-Customization/Method-to-exclude-number-sequence-in-B...
This is my code in C#, but it has an error. oCompDef doesn't seem to have a component called PropertySets. Yet, every example I've found shows people gettign the propertysets value of oCompDef.(Look in the if statement)
BOM oBOM = oAsmDoc.ComponentDefinition.BOM; oBOM.StructuredViewFirstLevelOnly = true; oBOM.StructuredViewEnabled = true; BOMView oStructuredBOMView = oBOM.BOMViews["Structured"]; for (int i = 1; i < oStructuredBOMView.BOMRows.Count; i++) { BOMRow oRow = oStructuredBOMView.BOMRows[i]; ComponentDefinition oCompDef = oRow.ComponentDefinitions[1]; Property oPartName; if ((oCompDef.GetType () is VirtualComponentDefinition)) { oPartName = oCompDef.PropertySets.Item["Design Tracking Properties"].Item["Part Number"]; } else { oPartName = oCompDef.Document.PropertySets.Item["Design Tracking Properties"].Item["Part Number"]; } }
Please help. I'm supposed to ahve this done today, and for whatever reason this relatively simple function has become quite a large problem.
You can see in your code that it's special casing for virtual components. Virtual components don't have an associated document which is where iProperties are typically stored. Virtual components do support the ability to have iProperties but they're accessed directly from the associated component definition. The problem you've run into is the PropertySets is not supported by the ComponentDefinition object but instead by the VirtualComponentDefinition object. In the section of the code that handles virtual components you can declare a variable to be of VirtualComponentDefinition type and cast oCompDef to it. You can then use the PropertySets property on it.
BOMRow oRow = oStructuredBOMView.BOMRows[i]; ComponentDefinition oCompDef = oRow.ComponentDefinitions[1]; Property oPartName; if ((oCompDef.GetType () is VirtualComponentDefinition)) { oPartName = oCompDef.PropertySets.Item["Design Tracking Properties"].Item["Part Number"]; } else { oPartName = oCompDef.Document.PropertySets.Item["Design Tracking Properties"].Item["Part Number"]; }
Maybe this is what you intended, but the block of code in your comment is exactly the same as mine.
Regarding the special casing for VirtualComponents, neither part of my if statement code works. Both claim there is no PropertySets function. What confuses me is that the code I'm using is posted verbatim all over the internet(in VB), and it seems to work fine for everyone else. Yet, Visual Studio cannot find the PropertySet function.
For some things C# is quite different than VB and you ran into some of them here. Here's a cleaned up version of the program that works in C#
BOM oBOM = asmDoc.ComponentDefinition.BOM; oBOM.StructuredViewFirstLevelOnly = true; oBOM.StructuredViewEnabled = true; BOMView oStructuredBOMView = oBOM.BOMViews["Structured"]; foreach (BOMRow row in oStructuredBOMView.BOMRows) { ComponentDefinition compDef = row.ComponentDefinitions[1]; string partName; if (compDef is VirtualComponentDefinition) { VirtualComponentDefinition virtualDef = (VirtualComponentDefinition)compDef; partName = virtualDef.PropertySets["Design Tracking Properties"]["Part Number"].Value; } else { Inventor.Document doc = (Inventor.Document)compDef.Document; partName = doc.PropertySets["Design Tracking Properties"]["Part Number"].Value; } }
This code worked great until I found a user that uses level of detail, and therefore structured view is not accessible. How might I do the same thing, but use "Model Data" view? (In code, I am calling it Unnamed because of the api)
In the foreach loop, oRow has an error in its component definitions element. I've looked, but I cannot find anywhere in Inventor documentation or forums that suggests a solution. Here's my code: https://dl.dropbox.com/u/46698764/unnamedViewCode.txt
All I want the code to do is loop through the top level of modeldata, and return a list of all the part numbers.