Ilogic code for placing both structured and parts only BOMs on a drawing sheet

Ilogic code for placing both structured and parts only BOMs on a drawing sheet

femi_osusa
Observer Observer
462 Views
5 Replies
Message 1 of 6

Ilogic code for placing both structured and parts only BOMs on a drawing sheet

femi_osusa
Observer
Observer

Hi , can someone please provide me a code for placing both BOMs simultaneously into a drawing sheet in inventor 2021 or 2022?  

I have tried using Chatgpt and the above code but i keep receiving an error.

 

 Error in rule: Rule0, in document: Assembly1 femi

Public member 'ComponentDefinition' on type '_DocumentClass' not found. 

 

Below is the code i tried to input.

 

ub Main()
    PlaceBothBOMsOnSheet()
End Sub

Sub PlaceBothBOMsOnSheet()
    If Not TypeOf ThisApplication.ActiveEditDocument Is DrawingDocument Then
        MsgBox("Please open a drawing document.")
        Exit Sub
    End If
    
    Dim drawingDoc As DrawingDocument = ThisApplication.ActiveEditDocument
    Dim ActiveSheet As Sheet = drawingDoc.ActiveSheet
    
    ' Check if a drawing sheet is active
    If ActiveSheet Is Nothing Then
        MsgBox("Please activate a drawing sheet.")
        Exit Sub
    End If
    
    ' Find the associated assembly document
    Dim asmDoc As AssemblyDocument = Nothing
    For Each refDoc As Document In ThisApplication.Documents
        If TypeOf refDoc Is AssemblyDocument AndAlso refDoc.FullFileName = drawingDoc.ComponentDefinition.ReferencedDocumentDescriptors(1).FullDocumentName Then
            asmDoc = refDoc
            Exit For
        End If
    Next
    
    ' Check if an assembly document was found
    If asmDoc Is Nothing Then
        MsgBox("No associated assembly document found.")
        Exit Sub
    End If
    
    ' Get the BOM views from the assembly
    Dim structuredBOMView As BOMView = asmDoc.ComponentDefinition.BOM.BOMViews.Item("Structured")
    Dim partsOnlyBOMView As BOMView = asmDoc.ComponentDefinition.BOM.BOMViews.Item("Parts Only")
    
    ' Define positions for the BOMs on the drawing sheet
    Dim structuredBOMPosition As Point2d = ThisApplication.TransientGeometry.CreatePoint2d(50, 50)
    Dim partsOnlyBOMPosition As Point2d = ThisApplication.TransientGeometry.CreatePoint2d(50, 350)
    
    ' Add structured BOM view to the drawing sheet
    ActiveSheet.DrawingBOMs.AddBOMView(structuredBOMView, structuredBOMPosition)
    
    ' Add parts-only BOM view to the drawing sheet
    ActiveSheet.DrawingBOMs.AddBOMView(partsOnlyBOMView, partsOnlyBOMPosition)
End Sub
0 Likes
463 Views
5 Replies
Replies (5)
Message 2 of 6

Michael.Navara
Advisor
Advisor

I'm not sure if anyone on this forum want to fix code from ChatGPT. In my opinion it is very bad idea.

But to your question. It is not possible to place both Structured and PartsOnly BOM from one assembly to single sheet.

See API reference for more information

https://help.autodesk.com/view/INVNTOR/2023/ENU/?guid=GUID-64121FC0-E936-4A2B-9F84-46D9BD5701EC

Message 3 of 6

A.Acheson
Mentor
Mentor

Hi @femi_osusa 

You will need to carefully define your terminology because the partslist and BOM (Bill Of Materials) are completely different objects. There is samples for each in the API help. 

Here is the sample for creating a partslist written in VBA.

Michael has already supplied a link to the PartsList Object.

AAcheson_0-1692657072252.png

 

Here is the sample for exporting a BOM to excel. written in VBA

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 4 of 6

gerrardhickson
Collaborator
Collaborator

As others have said, take care with ChatGPT - it's useful if you know what you're doing, but if you can't write code then you can't debug it either - its best to learn from the ground up.

 

To start with, I recommend that you learn VBA first - it has lots of existing code samples - in fact, you don't have to look very hard, and you'll find the code sample below in your help file.

Public Sub CreatePartsList()    
    On Error Resume Next
    
    ' Set a reference to the drawing document.
    ' This assumes a drawing document is active.
    Dim oDrawDoc As DrawingDocument
    Set oDrawDoc = ThisApplication.ActiveDocument

    'Set a reference to the active sheet.
    Dim oSheet As Sheet
    Set oSheet = oDrawDoc.ActiveSheet
    
    ' Set a reference to the first drawing view on
    ' the sheet. This assumes the first drawing
    ' view on the sheet is not a draft view.
    Dim oDrawingView As DrawingView
    Set oDrawingView = oSheet.DrawingViews(1)
    
    ' Set a reference to th sheet's border
    Dim oBorder As Border
    Set oBorder = oSheet.Border
    
    Dim oPlacementPoint As Point2d
    
    If Not oBorder Is Nothing Then
        ' A border exists. The placement point
        ' is the top-right corner of the border.
        Set oPlacementPoint = oBorder.RangeBox.MaxPoint
    Else
        ' There is no border. The placement point
        ' is the top-right corner of the sheet.
        Set oPlacementPoint = ThisApplication.TransientGeometry.CreatePoint2d(oSheet.Width, oSheet.height)
    End If
    
    ' Create the parts list.
    Dim oPartsList As PartsList
    Set oPartsList = oSheet.PartsLists.Add(oDrawingView, oPlacementPoint)
End Sub

 

For what its worth - your example is almost the same as the help file, but we would duplicate line 37, and add 'kStructured' and 'kPartsOnly' arguments - both of which are prompted by intellisense if you tried typing it.

 

Anyway - to your specific example, try this. I've adapted it to VBA because I think it will help get you started there.

 

Option Explicit

Sub PlaceBothBOMsOnSheet()

    If Not TypeOf ThisApplication.ActiveEditDocument Is Document Then
        MsgBox ("Please open a drawing document.")
        Exit Sub
    End If
    
    ' MAKE SURE YOUR ACTIVE DOCUMENT IS A DRAWING.
    Dim oD As DrawingDocument
    Set oD = ThisApplication.ActiveEditDocument
    Dim oAS As Sheet
    Set oAS = oD.ActiveSheet
    
    ' Check if a drawing sheet is active
    If oAS Is Nothing Then
        MsgBox ("Please activate a drawing sheet.")
        Exit Sub
    End If
    
    ' Find the associated assembly document
    Dim asmDoc As AssemblyDocument
    Set asmDoc = Nothing
    Dim refDoc As Document
    For Each refDoc In ThisApplication.Documents
        If TypeOf refDoc Is AssemblyDocument Then
            If refDoc.FullFileName = oD.ReferencedDocumentDescriptors(1).FullDocumentName Then
                Set asmDoc = refDoc
                Exit For
            End If
        End If
    Next
    
    ' Check if an assembly document was found
    If asmDoc Is Nothing Then
        MsgBox ("No associated assembly document found.")
        Exit Sub
    End If
    
    ' Get the BOM views from the assembly
    Dim structuredBOMView As BOMView
    Set structuredBOMView = asmDoc.ComponentDefinition.BOM.BOMViews.Item("Structured")
    Dim partsOnlyBOMView As BOMView
    Set partsOnlyBOMView = asmDoc.ComponentDefinition.BOM.BOMViews.Item("Parts Only")
    
    ' Define positions for the BOMs on the drawing sheet
    ' NOTE THAT THESE POSITIONS ARE BASED ON THE TOP RIGHT CORNER OF YOUR PARTSLIST
    Dim structuredBOMPosition As Point2d
    Set structuredBOMPosition = ThisApplication.TransientGeometry.CreatePoint2d(20#, 10#)
    Dim partsOnlyBOMPosition As Point2d
    Set partsOnlyBOMPosition = ThisApplication.TransientGeometry.CreatePoint2d(20#, 25#)
    
    ' Add structured BOM view to the drawing sheet
    oAS.PartsLists.Add asmDoc, structuredBOMPosition, kStructured
    
    ' Add parts-only BOM view to the drawing sheet
    oAS.PartsLists.Add asmDoc, partsOnlyBOMPosition, kPartsOnly

End Sub

 

To use:

  1. Open an assembly document and drawing document.
  2. Make sure the drawing document has at least 1 drawing view referencing the assembly document - if it doesn't, add one.
  3. Make the drawing document active if it isn't already.
  4. Press Alt+F11 to bring up the VBA Editor.
  5. Create a new module and paste this code in.
  6. Press F5 or 'Run' to execute the code.

I hope that helps. Happy coding.

Message 5 of 6

femi_osusa
Observer
Observer

Hi thank you for the advice and information. I tried running that Macro on VBA but it seems to generate two parts only bill of materials tables , instead of two boms made up of parts only and structured parts lists.I have tried to debug the code but i do not see where the issue is. I am abit fixed on time so i would kindly appreciate your help.

0 Likes
Message 6 of 6

A.Acheson
Mentor
Mentor

You cannot have two BOM's of different structures on the same sheet coming from the same drawing view. The reason being is the balloons will have different item references. You may need to rethink the requirements of this. See manual approach with structure level greyed out. Once a partslist structure is selected this is now linked for all partslist. 

AAcheson_0-1693164397930.png

 

There is another workaround which involves copying drawing view to a new drawing and then create a new partslist with different structure then copy back the partslist to existing drawing. Below is the code to carry out this workaround.

Option Explicit

Sub PlaceBothBOMsOnSheet()

    If Not TypeOf ThisApplication.ActiveEditDocument Is Document Then
        MsgBox ("Please open a drawing document.")
        Exit Sub
    End If
    
    ' MAKE SURE YOUR ACTIVE DOCUMENT IS A DRAWING.
    Dim oD As DrawingDocument
    Set oD = ThisApplication.ActiveEditDocument
    Dim oAS As Sheet
    Set oAS = oD.ActiveSheet
    
    ' Check if a drawing sheet is active
    If oAS Is Nothing Then
        MsgBox ("Please activate a drawing sheet.")
        Exit Sub
    End If
    
    ' Find the associated assembly document
    Dim asmDoc As AssemblyDocument
    Set asmDoc = Nothing
    Dim refDoc As Document
    For Each refDoc In ThisApplication.Documents
        If TypeOf refDoc Is AssemblyDocument Then
            If refDoc.FullFileName = oD.ReferencedDocumentDescriptors(1).FullDocumentName Then
                Set asmDoc = refDoc
                Exit For
            End If
        End If
    Next
    
    ' Check if an assembly document was found
    If asmDoc Is Nothing Then
        MsgBox ("No associated assembly document found.")
        Exit Sub
    End If
    
    ' Get the BOM views from the assembly
    Dim structuredBOMView As BOMView
    Set structuredBOMView = asmDoc.ComponentDefinition.BOM.BOMViews.Item("Structured")
    
    Dim partsOnlyBOMView As BOMView
    Set partsOnlyBOMView = asmDoc.ComponentDefinition.BOM.BOMViews.Item("Parts Only")
    
    ' Define positions for the BOMs on the drawing sheet
    ' NOTE THAT THESE POSITIONS ARE BASED ON THE TOP RIGHT CORNER OF YOUR PARTSLIST
    Dim structuredBOMPosition As Point2d
    Set structuredBOMPosition = ThisApplication.TransientGeometry.CreatePoint2d(20#, 10#)
    
    Dim partsOnlyBOMPosition As Point2d
    Set partsOnlyBOMPosition = ThisApplication.TransientGeometry.CreatePoint2d(20#, 25#)
    
    ' Add structured BOM view to the drawing sheet
    oAS.PartsLists.Add asmDoc, structuredBOMPosition, kStructured
    
    ' Create a Temp Drawing.
    Dim oDrawTemp As DrawingDocument
    Set oDrawTemp = ThisApplication.Documents.Add(kDrawingDocumentObject)
    
    Dim tempSheet As Sheet
    Set tempSheet = oDrawTemp.ActiveSheet
    
    ' Create the placement point object.
    Dim oPoint As Point2d
    Set oPoint = ThisApplication.TransientGeometry.CreatePoint2d(25, 25)
    
    ' Create a base view.
    Dim oBaseView As DrawingView
    Set oBaseView = tempSheet.DrawingViews.AddBaseView(asmDoc, oPoint, 0.5, _
                    kIsoTopLeftViewOrientation, kHiddenLineRemovedDrawingViewStyle)
    
    
    ' Add parts-only BOM view to the drawing sheet
    Dim oPartlist_Parts As PartsList
    Set oPartlist_Parts = tempSheet.PartsLists.Add(asmDoc, partsOnlyBOMPosition, kPartsOnly)
    
    ' Copy back to origional drawing.
    Call oPartlist_Parts.CopyTo(oAS)
  
    oDrawTemp.Close (True)
End Sub

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes