Change BOM Structure to Reference at parts in assembly and subassembly

Change BOM Structure to Reference at parts in assembly and subassembly

autodeskpelinder
Enthusiast Enthusiast
2,072 Views
11 Replies
Message 1 of 12

Change BOM Structure to Reference at parts in assembly and subassembly

autodeskpelinder
Enthusiast
Enthusiast

Hi!

 

I need to change the BOM structure at all parts that contains "BQ" in the filenamn to Reference.

I know it exist a lot of information for that here, but I can´t get it to work.

This works, but only if it is no subassemblys.

ThisDoc.Document.ComponentDefinition.BOM.BOMViews(1).BOMRows(1).BOMStructure  = 51972

 

On Error Resume Next
'Define the open document
Dim oDoc As Document
oDoc = ThisDoc.Document

If Not ThisApplication.ActiveDocument.DocumentType = kAssemblyDocumentObject Then
Return
End If

Dim docFile As Document 'Look at all of the files referenced in the open document
For Each docFile In ThisDoc.Document.AllReferencedDocuments

ThisApplication.Documents.Open(docFile.FullFileName, True) 'open the indexed file: true opens the file normaly, false opens the file programatically without generating the graphics

auto = iLogicVb.Automation 'run rule in the drawing document

If docFile.FullFileName.Contains("BQ") 'without extension. Om filnamnet innehåller "BQ" gäller följnade:
k = docFile.FullFileName 'k = Sökväg och filnamn
n = InStrRev(k, "\", - 1) ' n =87
j = k.Length ' j = längden på filnamnet med sökväg = 107
a = k.substring(k.Length - (j - n)) 'a = Filnamnet med filändelse (.ipt)
iProperties.Value(a, "Project", "Stock Number") = "LISEGA" 'Ändrar Stock number till "LISEGA"
langd = Len (a) 'langd = längden på filnamnet inklusive filändelse (.ipt)
name = a.Substring(0, (langd - 4)) ' Filnamnet - filändelsen, de 4 sista tecknen (.ipt)
p = InStr(a, " ") 'p = antal tecken i filnamnet fram till mellanrum.
u = InStr(a, "_") 'u = antal tecken i filnamnet fram till _ understreck.

'Ändrar iProperties/Project till de tecknen fram till mellanrum eller _understreck eller i filnamnet eller till filnamnet.
If p < 2 And u > 2 Or p > 14 And u > 2 Then 'Om det inte finns mellanslag eller om det är mer än 14 tecken fram till mellanslaget Och det finns _understreck
iProperties.Value(a, "Project", "Project") = a.Substring(0, u - 1) 'i = antal tecken i filnamnet fram till _ understreck.
Else If p >= 2 And p <= 14 Then 'Om det är mer än 1 och mindre än 15 tecken fram till mellanslag, det normala
iProperties.Value(a, "Project", "Project") = a.Substring(0, p - 1) 'without extension. Ändrar Project/Project till filnamnet fram till första mellanslaget
Else If langd > 15 Then 'Om det är mer än 12 tecken fram till mellanslaget eller det inte finns mellanslag och det inte finns _ och filnamnet är längre än 15 tecken.
iProperties.Value(a, "Project", "Project") = a.Substring(0, 12) 'without extension. Ändrar Project/Project till filnamnets 12 första tecken.
Else 'Om det inte finns mellanslag eller _understreck i filnamnet och filnamnet är kortare än 14 tecken.
iProperties.Value(a, "Project", "Project") = name 'Ändrar Project/Project till filnamnet utan de 4 sista tecknen (.ipt)
End If
iProperties.Value(a, "Summary", "Keywords") = iProperties.Value(a, "Project", "Project") 'Ändrar Summery/Keywords till Project/Project'

'THIS IS THE PROBLEM:==========================================================================================================
'THIS WORKS, BUT ONLY IF THERE IS NOT A SUBASSEMBLY =========================================================
ThisDoc.Document.ComponentDefinition.BOM.BOMViews(1).BOMRows(1).BOMStructure = 51972 'Ändrar BOM structure till Reference


'I HAVE TRYED MANY DIFFERENT COMMANDS:
docFile.Document.ComponentDefinition.BOM.BOMViews(1).BOMRows(1).BOMStructure = 51972 'Ändrar BOM structure till Reference

doc = ThisDoc.Document
def = doc.ComponentDefinition
thebom = def.BOM
thebomview= thebom.BOMViews(1)
thebomrow = thebomview.BOMRows(1)
thebomrow.BOMStructure = 51972 'kReferenceBOMStructure

k.BOMStructure = BOMStructureEnum.kReferenceBOMStructure
BOMStructure = BOMStructureEnum.kReferenceBOMStructure
ThisDoc.Document.BOMStructure = BOMStructureEnum.kReferenceBOMStructure

docFile.FullFileName.BOMStructure = BOMStructureEnum.kReferenceBOMStructure

oCompOcc = Component.InventorComponent(k)
oCompOcc.BOMStructure = BOMStructureEnum.kReferenceBOMStructure

oCompOcc = Component.InventorComponent(a)
oCompOcc.BOMStructure = BOMStructureEnum.kReferenceBOMStructure

BOMStructure = 51972

compOcc = ThisDoc.Document
compOcc.Definition.BOMStructure = 51972
a.Definition.BOMStructure = 51972

doc = ThisDoc.Document 'This line only needs to appear once in the rule
vPart = Component.InventorComponent(docFile.FullFileName) 'This line is only required once per virtual part
vPart.BOMStructure = BOMStructureEnum.kReferenceBOMStructure 'Sets BOM Structure to Reference (Remove from BOM)

vPart = Component.InventorComponent(a) 'This line is only required once per virtual part
vPart.BOMStructure = BOMStructureEnum.kReferenceBOMStructure 'Sets BOM Structure to Reference (Remove from BOM)
'==================================================================================================================

iProperties.Material(a) = "S235JRG2" 'Ändrar materialet till S235JRG2
InventorVb.DocumentUpdate() 'Uppdaterar filen, ibland måste man uppdatera för att ändringarna ska slå fast
iProperties.Value(a, "Project", "Part Number") = a 'Ändrar Project/Part Number till filnamnet

End If
'ThisDoc.Save 'Saves the file, don´t know the different of "ThisDoc.Save" and "docFile.Save".
docFile.Save
docFile.Close

Next
iLogicVb.UpdateWhenDone = True
'JOPE 2015-10-05

Johan Sweden
0 Likes
Accepted solutions (1)
2,073 Views
11 Replies
Replies (11)
Message 2 of 12

MechMachineMan
Advisor
Advisor

http://forums.autodesk.com/t5/inventor-customization/change-bom-structure-of-all-assy-children-to-ph...

 

All that is required to be changed is the enumerator to make it reference instead of phantom, and then adding checks as you require.


--------------------------------------
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 12

autodeskpelinder
Enthusiast
Enthusiast
I only want the parts to be Reference If docFile.FullFileName.Contains("BQ") .

Do I need all that code, then I already hade this:
Dim oDoc As Document
oDoc = ThisDoc.Document
If Not ThisApplication.ActiveDocument.DocumentType = kAssemblyDocumentObject Then
Return
End If
Dim docFile As Document 'Look at all of the files referenced in the open document
For Each docFile In ThisDoc.Document.AllReferencedDocuments
ThisApplication.Documents.Open(docFile.FullFileName, True) 'open the indexed file: true opens the file normaly, false opens the file programatically without generating the graphics
auto = iLogicVb.Automation 'run rule in the drawing document
If docFile.FullFileName.Contains("BQ") ??
Johan Sweden
0 Likes
Message 4 of 12

MechMachineMan
Advisor
Advisor
Using reference documents works just fine. Yes, you just need to iterate
through the collection check the condition and then change BOM a structure
if it evaluates true.

Warning: Using ref'd docs can result in changes to library and content
center content, so you should add checks to ensure the file is not in
either of those locations. Easy way is to ensure the file location of the
doc you want to change is in your current project workspace (if you use
.ipj per model/project), or to check exclusion from content centre/library
file path.

--------------------------------------
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 5 of 12

autodeskpelinder
Enthusiast
Enthusiast
Thanks for your help, but I´m sorry, but I don´t understand.
Can you please help me with the code?
I don´t think I have any cc-parts that contains "BQ", but you are weolcome to help me with that saftey-code too. I am using .ipj
Here is my code again, but I have shortened it:
Thank you
On Error Resume Next
'Define the open document
Dim oDoc As Document
oDoc = ThisDoc.Document

If Not ThisApplication.ActiveDocument.DocumentType = kAssemblyDocumentObject Then
Return
End If

Dim docFile As Document 'Look at all of the files referenced in the open document
For Each docFile In ThisDoc.Document.AllReferencedDocuments

ThisApplication.Documents.Open(docFile.FullFileName, True) 'open the indexed file: true opens the file normaly, false opens the file programatically without generating the graphics

auto = iLogicVb.Automation 'run rule in the drawing document

If docFile.FullFileName.Contains("BQ")
'SOMETHING IS WRONG:
'docfileOccurrence.Definition.BOMStructure = BOMStructureEnum.kReferenceBOMStructure


InventorVb.DocumentUpdate()
End If
'ThisDoc.Save 'Saves the file, don´t know the different of "ThisDoc.Save" and "docFile.Save".
docFile.Save
docFile.Close

Next
iLogicVb.UpdateWhenDone = True
Johan Sweden
0 Likes
Message 6 of 12

MechMachineMan
Advisor
Advisor
Option Compare Text

Sub Main()

On Error Resume Next
Dim oDoc As Document
oDoc = ThisApplication.ActiveDocument

If Not ThisApplication.ActiveDocument.DocumentType = kAssemblyDocumentObject Then
     Return
End If

'For top level part, uncomment line below
'oDoc.ComponentDefinition.BOMStructure = BOMStructureEnum.kReferenceBOMStructure

Dim docFile As Document

For Each docFile In oDoc.AllReferencedDocuments

    ThisApplication.Documents.Open(docFile.FullFileName, False)

    If docFile.FullFileName Like "*BQ*"
        docFile.ComponentDefinition.BOMStructure = BOMStructureEnum.kReferenceBOMStructure
        docFile.Update
    End If

    docFile.Save
    docFile.Close

Next

oDoc.Rebuild
oDoc.Update

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
Message 7 of 12

autodeskpelinder
Enthusiast
Enthusiast
Thank you very much! I think it works now. I will check more tomorrow.
Sometimes I have parts directley in the main assembly and sometimes there is several sub-assembly.
I chaged oDoc = ThisDoc.Document to: oDoc = ThisApplication.ActiveDocument

and I wrote this code in the beginning:
If ThisApplication.ActiveDocument.Contains("BQ")
oDoc.ComponentDefinition.BOMStructure = BOMStructureEnum.kReferenceBOMStructure
End If
And this later:
If docFile.FullFileName Like "*BQ*"
docFile.ComponentDefinition.BOMStructure = BOMStructureEnum.kReferenceBOMStructure
docFile.Update
End If

What is the different of "ThisDoc.Save" and "docFile.Save"?
Best regards / Johan
Johan Sweden
0 Likes
Message 8 of 12

MechMachineMan
Advisor
Advisor
I like using LIKE rather than contains as you can make LIKE
case-insensitive with Option Compare Text present. Contains is always case
sensitive.

ThisDoc is an iLogic snippet that is very similar to
ThisApplication.ActiveDocument. however, it's better practice to avoid
using snippets where possible.

Also, it's better to use docfile rather than ThisDoc because you are
explicitly giving the object rather than making a call for an object that
may be what you are looking for. Much better practice for if you ever
expand to multiple rules/subs, classes, functions, etc.

--------------------------------------
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 9 of 12

autodeskpelinder
Enthusiast
Enthusiast
Hi again!

How shall I write in the beginning to change the BOM at the top level, if I like to use "like"?
I tryed this:
If docFile.FullFileName Like "*BQ*"
oDoc.ComponentDefinition.BOMStructure = BOMStructureEnum.kReferenceBOMStructure

And this:
If ThisApplication.ActiveDocument Like "*BQ*"
oDoc.ComponentDefinition.BOMStructure = BOMStructureEnum.kReferenceBOMStructure

But it doesn´t work. Thank you
Johan Sweden
0 Likes
Message 10 of 12

MechMachineMan
Advisor
Advisor
Accepted solution
oDoc.FullFileName

--------------------------------------
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 11 of 12

MechMachineMan
Advisor
Advisor
Vba is programming in such a way that the language is:

Object.Method Or Object.Property with an number of possible object preceding that. Even some methods return objects.

In looking at any code, if the First word in the call DOES NOT have something to do with the application, it is undoubtedly a shortcut and would be defined somewhere else in the program.

ie;

oDoc is actually only usable following the lines
Dim oDoc As Document
oDoc = ThisApplication.ActiveDocument

This can be utilized in further shortcuts, ie:
Dim oSheet As Sheet
oSheet = oDoc.Sheets.Item(1)

Which is actually the shortened and more efficient version of
oSheet = ThisApplication.ActiveDocument.Sheets.Item(1).

--------------------------------------
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 12 of 12

autodeskpelinder
Enthusiast
Enthusiast
Thank you
Johan Sweden
0 Likes