Get the quantity of each component in an assembly?

Get the quantity of each component in an assembly?

jake_egley
Advocate Advocate
193 Views
2 Replies
Message 1 of 3

Get the quantity of each component in an assembly?

jake_egley
Advocate
Advocate

I need to get the quantity of each component in an assembly and put that quantity into a cell in an excel spreadsheet. I attached my code below which gets the hole count for each part and puts the hole count into a cell. 

 

Dim oAsmDoc As AssemblyDocument
Set oAsmDoc = ThisApplication.ActiveDocument
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("C:\Users\Filname.xltx")

Dim oRefDocs As DocumentsEnumerator
Set oRefDocs = oAsmDoc.AllReferencedDocuments
Dim oRefDoc As Document
    

    
For Each oRefDoc In oRefDocs
     openDoc = ThisApplication.Documents.Open(oRefDoc.FullDocumentName, True)
     Set oDoc = ThisApplication.ActiveDocument
         'HOLE COUNT
         Dim Holes As Integer
         Holes = oFace.EdgeLoops.Count - 1

         objExcel.cells(counter, 7).Value = Holes
         counter = counter + 1
     ThisApplication.ActiveDocument.Close
Next

 

I have tried the following code but I keep getting a type mismatch error. I also don't know how it would work. 

 

Dim oBOM As BOM
oBOM = oAsmDoc.ComponentDefinition.BOMQuantity.GetBaseQuantity(52225)

 

Thank you guys for any help.

Jake Egley
Inventor 2022
Accepted solutions (1)
194 Views
2 Replies
Replies (2)
Message 2 of 3

WCrihfield
Mentor
Mentor

Hi @jake_egley.  You will likely need to clarify a few things before we can help you with this challenge.  Neither of the code examples you posted makes much sense to me, so determining specifically what you are trying to do is a little confusing.

  • What exactly are you trying to count?
    • component occurrences
      • If so, then only the top level ones, or also all the ones down within sub assemblies also?
    • holes
      • If so, then what type of holes?
        • Only round holes, or other shaped holes also?
        • Only holes made with Hole features, or all types of holes?
  • Are you trying to use this code within a VBA macro, or within iLogic rules?
    • There is a difference between way the code needs to be written for each application, so knowing this is important.
    • Some of your example code looks like it was meant for VBA (where you are using the 'Set' keyword), but then some of it looks like it was for an iLogic rule, and the two should not be mixed together.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 3

jake_egley
Advocate
Advocate
Accepted solution

I figured it out. Final code is below:

Dim PQty As Integer
Dim oBOM As BOM
Set oBOM = oAsmDoc.ComponentDefinition.BOM
' Make sure that the parts only view is enabled.
oBOM.PartsOnlyViewEnabled = True
' Set a reference to the "Parts Only" BOMView
Dim oPartsOnlyBOMView As BOMView
Set oPartsOnlyBOMView = oBOM.BOMViews.Item("Parts Only")
PQty = oPartsOnlyBOMView.BOMRows.Item(counter).ItemQuantity

 

Jake Egley
Inventor 2022
0 Likes