Inventor Customization

Reply
Mentor
mslosar
Posts: 227
Registered: ‎08-11-2004
Message 11 of 16 (132 Views)

Re: Searching whole assembly stack

09-26-2013 06:38 AM in reply to: Gruff

That's pretty much what i've done. I'm taking a BOM output and search it for row's that meet the criteria and then only exporting those rows. Seeing as i'll have to deal with old pieces that won't necessarily have the field in question in them (i.e. library parts usually), i'm having to use error handling to deal with those row.

 

 On Error Resume Next
           ' oTagProperty.Value = ""
            Set oTagProperty = oCompDef.Document.PropertySets _
                .Item("User Defined Properties").Item("TAG_NO")
                
            If Len(oTagProperty.Value) > 0 Then
               sText = sText + items to export here & vbcrlf
            end if

 

There's probably a more elegant way of handling whether or not the property is present on the object or not, though :smileyhappy:

Valued Mentor
Gruff
Posts: 431
Registered: ‎11-24-2003
Message 12 of 16 (127 Views)

Re: Searching whole assembly stack

09-26-2013 10:39 AM in reply to: mslosar

I think you are missing what I am getting at.  

 

Virtual Parts show up in the BOM.

Instead of looping through the assembly parts and looking at properties,

programmically grab the BOM object (Assuming you have added your property to the BOM output.

You can even add it with visibility turned off if you need to.)

 

Do one pass through the actual BOM to get your filenames and property values.  I do not know

if Virtual parts can have a property, but if they can then you can get the output you want.

 

Mentor
mslosar
Posts: 227
Registered: ‎08-11-2004
Message 13 of 16 (123 Views)

Re: Searching whole assembly stack

09-26-2013 11:24 AM in reply to: Gruff

I've switched to this at the moment. It's from the API Samples/BOM

 

Public Sub BOMQuery()
    ' Set a reference to the assembly document.
    ' This assumes an assembly document is active.
    Dim oDoc As AssemblyDocument
    Set oDoc = ThisApplication.ActiveDocument

    Dim FirstLevelOnly As Boolean
    If MsgBox("First level only?", vbYesNo) = vbYes Then
        FirstLevelOnly = True
    Else
        FirstLevelOnly = False
    End If
    
    ' Set a reference to the BOM
    Dim oBOM As BOM
    Set oBOM = oDoc.ComponentDefinition.BOM
    
    ' Set whether first level only or all levels.
    If FirstLevelOnly Then
        oBOM.StructuredViewFirstLevelOnly = True
    Else
        oBOM.StructuredViewFirstLevelOnly = False
    End If
    
    ' Make sure that the structured view is enabled.
    oBOM.StructuredViewEnabled = True
    
    'Set a reference to the "Structured" BOMView
    Dim oBOMView As BOMView
    Set oBOMView = oBOM.BOMViews.Item("Structured")
        
    Debug.Print "Item"; Tab(15); "Quantity"; Tab(30); "Part Number"; Tab(70); "Description"
    Debug.Print "----------------------------------------------------------------------------------"

    'Initialize the tab for ItemNumber
    Dim ItemTab As Long
    ItemTab = -3
    Call QueryBOMRowProperties(oBOMView.BOMRows, ItemTab)
End Sub

Private Sub QueryBOMRowProperties(oBOMRows As BOMRowsEnumerator, ItemTab As Long)
    ItemTab = ItemTab + 3
    ' Iterate through the contents of the BOM Rows.
    Dim i As Long
    For i = 1 To oBOMRows.Count
        ' Get the current row.
        Dim oRow As BOMRow
        Set oRow = oBOMRows.Item(i)

        'Set a reference to the primary ComponentDefinition of the row
        Dim oCompDef As ComponentDefinition
        Set oCompDef = oRow.ComponentDefinitions.Item(1)

        Dim oPartNumProperty As Property
        Dim oDescripProperty As Property

        If Typeof oCompDef Is VirtualComponentDefinition Then
            'Get the file property that contains the "Part Number"
            'The file property is obtained from the virtual component definition
            Set oPartNumProperty = oCompDef.PropertySets _
                .Item("Design Tracking Properties").Item("Part Number")

            'Get the file property that contains the "Description"
            Set oDescripProperty = oCompDef.PropertySets _
                .Item("Design Tracking Properties").Item("Description")

            Debug.Print Tab(ItemTab); oRow.ItemNumber; Tab(17); oRow.ItemQuantity; Tab(30); _
                oPartNumProperty.Value; Tab(70); oDescripProperty.Value
        Else
            'Get the file property that contains the "Part Number"
            'The file property is obtained from the parent
            'document of the associated ComponentDefinition.
            Set oPartNumProperty = oCompDef.Document.PropertySets _
                .Item("Design Tracking Properties").Item("Part Number")

            'Get the file property that contains the "Description"
            Set oDescripProperty = oCompDef.Document.PropertySets _
                .Item("Design Tracking Properties").Item("Description")

            Debug.Print Tab(ItemTab); oRow.ItemNumber; Tab(17); oRow.ItemQuantity; Tab(30); _
                oPartNumProperty.Value; Tab(70); oDescripProperty.Value
            
            'Recursively iterate child rows if present.
            If Not oRow.ChildRows Is Nothing Then
                Call QueryBOMRowProperties(oRow.ChildRows, ItemTab)
            End If
        End If
    Next
    ItemTab = ItemTab - 3
End Sub

 For whateve reason they're not reading all values from the BOM, though. They are reaching outside the bom for a couple, but i'm not sure why. Part Number and Description are both standard BOM fields.

Valued Mentor
Gruff
Posts: 431
Registered: ‎11-24-2003
Message 14 of 16 (117 Views)

Re: Searching whole assembly stack

09-26-2013 12:36 PM in reply to: mslosar
Have you in Inventor Files set the custom property you want to get as "Exported"? I believe you have to do so to get it to show up in the BOM.
Mentor
mslosar
Posts: 227
Registered: ‎08-11-2004
Message 15 of 16 (113 Views)

Re: Searching whole assembly stack

09-26-2013 12:56 PM in reply to: Gruff

It's not physically resident in every ipt file, but it resident in every iam file.

 

Plus, if i use the straight export to bom sample, the entire thing exports.with  all the columns present in the top level iam file.

 

I have modifited the above to kick out the entries I want and using debug.print get the output i'm looking for. So, it's nearly a success.

 

At the present, though, i'm trying to send the qualifying entries to excel - and just the columns i'm after. Only problem i have is that no matter what I do, it doesn't seem to move down to the second level. I'm fairly sure the problem is that in order to go down to the next level it re-call's the function it's presently in. As the second part stands now, i can't get the lower level parts/assemblies to make it to excel - meanwhile, though, all the debug.print lines work perfectly.

 

Private Sub QueryBOMRowProperties(oBOMRows As BOMRowsEnumerator, ItemTab As Long)

    If ExcelSession <> True Then
    Dim fExcel As Excel.Application
    Dim fWB As Workbook
    Set fExcel = New Excel.Application
    Set fWB = fExcel.Workbooks.Open("R:\Inventor\VBA\blank.xlsx")
    ExcelSession = True
    fExcel.Visible = True
    
    End If

    'Dim MyCount As Integer
    
    ItemTab = ItemTab + 3
    ' Iterate through the contents of the BOM Rows.
    Dim i As Long
    For i = 1 To oBOMRows.Count
        ' Get the current row.
        Dim oRow As BOMRow
        Set oRow = oBOMRows.Item(i)
        
        'Set a reference to the primary ComponentDefinition of the row
        Dim oCompDef As ComponentDefinition
        Set oCompDef = oRow.ComponentDefinitions.Item(1)

        Dim oPartNumProperty As Property
        Dim oDescripProperty As Property
        Dim oTagProperty As Property
        
        If TypeOf oCompDef Is VirtualComponentDefinition Then
            'Get the file property that contains the "Part Number"
            'The file property is obtained from the virtual component definition
            Set oPartNumProperty = oCompDef.PropertySets _
                .Item("Design Tracking Properties").Item("Part Number")

            'Get the file property that contains the "Description"
            Set oDescripProperty = oCompDef.PropertySets _
                .Item("Design Tracking Properties").Item("Description")
                
            On Error Resume Next
            Set oTagProperty = oCompDef.PropertySets _
                .Item("User Defined Properties").Item("TAG_NO")
            
            If Len(oTagProperty.Value) > 0 Then
            
            Debug.Print Tab(0); oTagProperty.Value; Tab(20); oDescripProperty.Value; Tab(90); oPartNumProperty.Value; Tab(110); oRow.ItemQuantity; Tab(120); MyCount;
            
            fExcel.Range("B" & MyCount + 9).Select
            fExcel.ActiveCell.Value = oTagProperty.Value
            fExcel.Range("C" & MyCount + 9).Select
            fExcel.ActiveCell.Value = oDescripProperty.Value
            fExcel.Range("F" & MyCount + 9).Select
            fExcel.ActiveCell.Value = oPartNumProperty.Value
            fExcel.Range("G" & MyCount + 9).Select
            fExcel.ActiveCell.Value = oRow.ItemQuantity
           ' MsgBox (oDescripProperty.Value)
             MyCount = MyCount + 1
             End If
             
             Else
                
            'Get the file property that contains the "Part Number"
            'The file property is obtained from the parent
            'document of the associated ComponentDefinition.
            Set oPartNumProperty = oCompDef.Document.PropertySets _
                .Item("Design Tracking Properties").Item("Part Number")

            'Get the file property that contains the "Description"
            Set oDescripProperty = oCompDef.Document.PropertySets _
                .Item("Design Tracking Properties").Item("Description")
            
            On Error Resume Next
           ' oTagProperty.Value = ""
            Set oTagProperty = oCompDef.Document.PropertySets _
                .Item("User Defined Properties").Item("TAG_NO")
                
            If Len(oTagProperty.Value) > 0 Then
            
            Debug.Print Tab(0); oTagProperty.Value; Tab(20); oDescripProperty.Value; Tab(90); oPartNumProperty.Value; Tab(110); oRow.ItemQuantity; Tab(120); MyCount;
            
            
            fExcel.Range("B" & MyCount + 9).Select
            fExcel.ActiveCell.Value = oTagProperty.Value
            fExcel.Range("C" & MyCount + 9).Select
            fExcel.ActiveCell.Value = oDescripProperty.Value
            fExcel.Range("F" & MyCount + 9).Select
            fExcel.ActiveCell.Value = oPartNumProperty.Value
            fExcel.Range("G" & MyCount + 9).Select
            fExcel.ActiveCell.Value = oRow.ItemQuantity
            'MsgBox (oDescripProperty.Value)
            MyCount = MyCount + 1
            End If


            'Recursively iterate child rows if present.
            If Not oRow.ChildRows Is Nothing Then
                Call QueryBOMRowProperties(oRow.ChildRows, ItemTab)
            End If
        End If
    Next
    ItemTab = ItemTab - 3
    'ActiveWorkbook.SaveAs ("R:\drafting\bom\SL-JZ-FABs1r0.xlsx")
    'ActiveWorkbook.Close savechanges:=False, FileName:="R:\Inventor\VBA\blank.xlsx"
    'activework.Close savechanges:=True, FileName:="R:\\drafting\bom\SL-JZ-FABs1r0.xlsx"
    
    'fExcel.Quit
    
    End Sub

 

I commented out the file saving in hopes of seeing where the lower level info was going, but I can't tell. Seems to go nowhere.

 

My guess is that re-calling the queryBOMRowProperties function is messing something up in regards to the excel writing. hence My ExcelSession flag. Still doesn't work with that. Seems like the file should still be open and work, butit's not seeing a file to write to.

 

I've been thinking i just nned to make a string for the whole thing then send it to another function to export to excel, but converting multiple lines of delimited text is proving frustrating. I know i have to split it into lines, then in each line split up the entries, but it's kicking my butt right now.

Mentor
mslosar
Posts: 227
Registered: ‎08-11-2004
Message 16 of 16 (74 Views)

Re: Searching whole assembly stack

10-22-2013 10:07 AM in reply to: mslosar

This was working just great, but i've run into a bizarre issue.

As long as the first part in the main assembly is a normal part or virtual part it works just fine. If it's a library part, it generates a blank line of text in the QueryBOMRowProperties() for debug.print.

Not sure why that would be an issue as reading the information is fine otherwise and doesn't require write access or anything.

 

 

Ad Gruff, do you have something you can point me to that grabs the whole BOM as you were talking about? I can't seem to find anything, but i may not be looking in the right spot :smileyhappy:

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