Inventor Customization

Reply
Mentor
bsee1
Posts: 211
Registered: ‎11-14-2011
Message 1 of 13 (479 Views)
Accepted Solution

How to get a parts list?

479 Views, 12 Replies
05-16-2012 11:45 AM

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

*****************************
Win7 x64 - 16gb ram
i7 3610qm
FirePro M4000

Inventor 2013
ETO 6.1
Valued Contributor
planglais
Posts: 58
Registered: ‎03-10-2011
Message 2 of 13 (473 Views)

Re: How to get a parts list?

05-16-2012 12:46 PM in reply to: bsee1

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

Mentor
bsee1
Posts: 211
Registered: ‎11-14-2011
Message 3 of 13 (471 Views)

Re: How to get a parts list?

05-16-2012 01:07 PM in reply to: planglais

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.

*****************************
Win7 x64 - 16gb ram
i7 3610qm
FirePro M4000

Inventor 2013
ETO 6.1
Valued Contributor
planglais
Posts: 58
Registered: ‎03-10-2011
Message 4 of 13 (463 Views)

Re: How to get a parts list?

05-16-2012 05:45 PM in reply to: bsee1

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

Mentor
bsee1
Posts: 211
Registered: ‎11-14-2011
Message 5 of 13 (453 Views)

Re: How to get a parts list?

05-17-2012 09:02 AM in reply to: planglais

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.

*****************************
Win7 x64 - 16gb ram
i7 3610qm
FirePro M4000

Inventor 2013
ETO 6.1
Valued Contributor
planglais
Posts: 58
Registered: ‎03-10-2011
Message 6 of 13 (449 Views)

Re: How to get a parts list?

05-17-2012 11:23 AM in reply to: bsee1

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

 

ADN Support Specialist
ekinsb
Posts: 300
Registered: ‎07-21-2006
Message 7 of 13 (445 Views)

Re: How to get a parts list?

05-17-2012 11:27 AM in reply to: planglais

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 Ekins
Developer Technical Services
Autodesk Developer Network
Autodesk, Inc.
Mentor
bsee1
Posts: 211
Registered: ‎11-14-2011
Message 8 of 13 (440 Views)

Re: How to get a parts list?

05-17-2012 01:10 PM in reply to: ekinsb

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.

*****************************
Win7 x64 - 16gb ram
i7 3610qm
FirePro M4000

Inventor 2013
ETO 6.1
Mentor
bsee1
Posts: 211
Registered: ‎11-14-2011
Message 9 of 13 (427 Views)

Re: How to get a parts list?

05-18-2012 08:57 AM in reply to: bsee1

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. 

*****************************
Win7 x64 - 16gb ram
i7 3610qm
FirePro M4000

Inventor 2013
ETO 6.1
ADN Support Specialist
ekinsb
Posts: 300
Registered: ‎07-21-2006
Message 10 of 13 (422 Views)

Re: How to get a parts list?

05-18-2012 12:09 PM in reply to: bsee1
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"];
	}

 


 



Brian Ekins
Developer Technical Services
Autodesk Developer Network
Autodesk, Inc.

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community


Inventor Exchange Apps

Created by the community for the community, Autodesk Exchange Apps for Autodesk Inventor helps you achieve greater speed, accuracy, and automation from concept to manufacturing.

Connect with Inventor

Twitter

Facebook

Blogs

Pinterest

Youtube