Ilogic; BOM Qty; need help with code

Ilogic; BOM Qty; need help with code

Anonymous
Not applicable
2,881 Views
3 Replies
Message 1 of 4

Ilogic; BOM Qty; need help with code

Anonymous
Not applicable

I modified a code for BOM qty across all parts. The previous code only works with "Parts only"...

The modified code works on the Structured BOM. 

 

That said it does not go through all sub assemblies "levels". 

 

Can some one help me so it runs on all levels?

Thanks,

Josh

 

Inventor 2017

 

SyntaxEditor Code Snippet

doc = ThisDoc.Document
Dim oAssyDef As AssemblyComponentDefinition = doc.ComponentDefinition
Dim oBOM As BOM = oAssyDef.BOM

oBOM.StructuredViewEnabled = True

Dim oBOMView As BOMView = oBOM.BOMViews.Item("Structured")

Dim oBOMRow As BOMRow

For Each oBOMRow In oBOMView.BOMRows
    'Set a reference to the primary ComponentDefinition of the row
    Dim oCompDef As ComponentDefinition
    oCompDef = oBOMRow.ComponentDefinitions.Item(1)
    
    Dim CompFullDocumentName As String = oCompDef.Document.FullDocumentName
    Dim CompFileNameOnly As String
    Dim index As Integer = CompFullDocumentName.lastindexof("\")
    
    CompFileNameOnly = CompFullDocumentName.substring(index+1)
    
    'MessageBox.Show(CompFileNameOnly)
    
    Dim Qty As String
    Qty = oBOMRow.TotalQuantity

    
    iProperties.Value(CompFileNameOnly, "Custom", "PartQty") = Qty    
    iProperties.Value(CompFileNameOnly, "Custom", "SO") =iProperties.Value("Custom", "SO")
    
    
Next

 

Accepted solutions (1)
2,882 Views
3 Replies
Replies (3)
Message 2 of 4

MechMachineMan
Advisor
Advisor

You need to use recursion.

 

Samples of recursion found here:

http://modthemachine.typepad.com/my_weblog/2009/03/accessing-assembly-components.html

 

Sub Main()

doc = ThisDoc.Document Dim oAssyDef As AssemblyComponentDefinition = doc.ComponentDefinition Dim oBOM As BOM = oAssyDef.BOM oBOM.StructuredViewEnabled = True Dim oBOMView As BOMView = oBOM.BOMViews.Item("Structured") Dim oBOMRow As BOMRow Dim oCompDef As ComponentDefinition

For Each oBOMRow In oBOMView.BOMRows

oCompDef = oBOMRow.ComponentDefinitions.Item(1)

Call SetRowProps(oCompDef As ComponentDefinition, QTY As String)

If Not oBOMRow.ChildRows is Nothing
Call RecurseBOMRow(oBOMRow)
End if
Next
End Sub

Sub RecurseBOMRow(oBOMRow As BOMRow)
For Each oBOMRow In oBOMRow.ChildRows
Dim oCompDef As ComponentDefinition
oCompDef = oBOMRow.ComponentDefinitions.Item(1)

Call SetRowProps(oCompDef, oBOMRow.TotalQuantity)

If Not oBOMRow.ChildRows is Nothing
Call RecurseBOMRow(oBOMRow)
End ig
Next
End Sub

Sub SetRowProps(oCompDef As ComponentDefinition, QTY As String)
Dim CompFullDocumentName As String = oCompDef.Document.FullDocumentName
Dim CompFileNameOnly As String
Dim index As Integer = CompFullDocumentName.lastindexof("\")
CompFileNameOnly = CompFullDocumentName.substring(index+1)

'MessageBox.Show(CompFileNameOnly)

iProperties.Value(CompFileNameOnly, "Custom", "PartQty") = QTY
iProperties.Value(CompFileNameOnly, "Custom", "SO") = iProperties.Value("Custom", "SO")
End Sub

 


--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
0 Likes
Message 3 of 4

Anonymous
Not applicable

Thanks for the additional info! I will read this thoroughly  today. 

 

I tired your code. I am getting errors in line 19 

 

SyntaxEditor Code Snippet

 Call SetRowProps(oCompDef As ComponentDefinition. QTY As String)

any thoughts?

 

0 Likes
Message 4 of 4

MechMachineMan
Advisor
Advisor
Accepted solution

If you are digging this far into code/iLogic, it would really benefit you to read some more about programming and basic debugging.

 

You would quickly find that the only place an "As" should occur is when declaring a variable or function, and not in a function call.

 

This is like 101 level programming bugs.

 

Sub Main()

    doc = ThisDoc.Document
    Dim oAssyDef As AssemblyComponentDefinition = doc.ComponentDefinition
    Dim oBOM As BOM = oAssyDef.BOM

    oBOM.StructuredViewEnabled = True

    Dim oBOMView As BOMView = oBOM.BOMViews.Item("Structured")

    Dim oBOMRow As BOMRow
    Dim oCompDef As ComponentDefinition

    For Each oBOMRow In oBOMView.BOMRows

      oCompDef = oBOMRow.ComponentDefinitions.Item(1)
      
       'OLD : NOTICE THE 'AS' KEYWORD INCORRECTLY THERE.
      'Call SetRowProps(oCompDef As ComponentDefinition, QTY As String)

'New Line. Notice the LACK of 'As' Keyword. Call SetRowProps(oCompDef, oBOMRow.TotalQuantity) If Not oBOMRow.ChildRows is Nothing Call RecurseBOMRow(oBOMRow) End if Next End Sub Sub RecurseBOMRow(oBOMRow As BOMRow) For Each oBOMRow In oBOMRow.ChildRows Dim oCompDef As ComponentDefinition oCompDef = oBOMRow.ComponentDefinitions.Item(1) Call SetRowProps(oCompDef, oBOMRow.TotalQuantity) If Not oBOMRow.ChildRows is Nothing Call RecurseBOMRow(oBOMRow) End ig Next End Sub Sub SetRowProps(oCompDef As ComponentDefinition, QTY As String) Dim CompFullDocumentName As String = oCompDef.Document.FullDocumentName Dim CompFileNameOnly As String Dim index As Integer = CompFullDocumentName.lastindexof("\") CompFileNameOnly = CompFullDocumentName.substring(index+1) 'MessageBox.Show(CompFileNameOnly) iProperties.Value(CompFileNameOnly, "Custom", "PartQty") = QTY iProperties.Value(CompFileNameOnly, "Custom", "SO") = iProperties.Value("Custom", "SO") End Sub

 


--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type