iLogic Code part iproperties to custom iproperties

iLogic Code part iproperties to custom iproperties

Anonymous
Not applicable
3,344 Views
9 Replies
Message 1 of 10

iLogic Code part iproperties to custom iproperties

Anonymous
Not applicable

I was looking for some iLogic Code for Inventor 2013 that would cycle through all the parts in an assembly grabbing the quantity of each part and setting it equal to the custom property "Qty" of each part.

 

Having a little trouble creating the for each loop and setting reference to each part without referencing each part by specific name. Any help would be appreciated. Thanks.

 

0 Likes
Accepted solutions (1)
3,345 Views
9 Replies
Replies (9)
Message 2 of 10

Vladimir.Ananyev
Alumni
Alumni

There is a good alternative workflow.  You may consider BOM view “Parts Only” as a way to get all desired quantities. In this case you do not need to traverse assembly levels.  Each BOMRow represents some part.  BOMRow.TotalQuantity property returns the quantity for this part taking into account all settings like BOMStructure.

Inventor API help includes the sample “BOMQuery” illustrating BOM API functionality in assemblies. 


Vladimir Ananyev
Developer Technical Services
Autodesk Developer Network

0 Likes
Message 3 of 10

Anonymous
Not applicable

Sounds great. I am ok with that method as well. What would the code look like for that task?

0 Likes
Message 4 of 10

Vladimir.Ananyev
Alumni
Alumni

Here is a sample VBA code that gets components quantities from BOMView “Parts Only”  and prints them in the immediate window.

Sub GetQuantities()

  ' a reference to the assembly document.
  ' This assumes an assembly document is active.
  Dim oAssyDoc As AssemblyDocument
  Set oAssyDoc = ThisApplication.ActiveDocument
  Dim oAssyDef As AssemblyComponentDefinition
  Set oAssyDef = oAssyDoc.ComponentDefinition

  ' Get the Representations Manager object.
  Dim repMgr As RepresentationsManager
  Set repMgr = oAssyDef.RepresentationsManager
  
  ' activate LOD Master - necessary to get BOM object !!!
  Dim oMasterLOD As LevelOfDetailRepresentation
  Set oMasterLOD = repMgr.LevelOfDetailRepresentations.Item(1)

  'set  a reference to the BOM
  Dim oBOM As BOM
  Set oBOM = oAssyDef.BOM
  
  ' Make sure that the "Parts Only" view is enabled.
  oBOM.PartsOnlyViewEnabled = True
  
  'set a reference to the "Parts Only" BOMView
  Dim oBOMView As BOMView
  Set oBOMView = oBOM.BOMViews.Item("Parts Only")

  Dim oBOMRow As BOMRow
  
  For Each oBOMRow In oBOMView.BOMRows
  
      'Set a reference to the primary ComponentDefinition of the row
      Dim oCompDef As ComponentDefinition
      Set oCompDef = oBOMRow.ComponentDefinitions.Item(1)

      Dim oPartNumProperty As Property
      Dim oDescripProperty As Property
      Dim Qty As String

      If TypeOf oCompDef Is VirtualComponentDefinition Then
          'Get the Virtual Component property that contains the "Part Number"
          Set oPartNumProperty = oCompDef.PropertySets _
              .Item("Design Tracking Properties").Item("Part Number")

          'Get the  Virtual Component property that contains the "Description"
          Set oDescripProperty = oCompDef.PropertySets _
              .Item("Design Tracking Properties").Item("Description")
              
          Qty = oBOMRow.TotalQuantity
             
          Debug.Print oBOMRow.ItemNumber; "  "; oPartNumProperty.value; "  Qty="; _
                      Qty; "  "; 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")
          
          Qty = oBOMRow.TotalQuantity
          
          Debug.Print oBOMRow.ItemNumber; "  "; oPartNumProperty.value; "  Qty="; _
                      Qty; "  "; oDescripProperty.value
      End If
  Next
  
  Beep
End Sub

Here is the iLogic version:

'gets components quantities from BOMView "Parts Only"

' a reference to the assembly document.
' This assumes an assembly document is active.
Dim oAssyDoc As AssemblyDocument = ThisDoc.Document
Dim oAssyDef As AssemblyComponentDefinition = oAssyDoc.ComponentDefinition

' Get the Representations Manager object.
Dim repMgr As RepresentationsManager = oAssyDef.RepresentationsManager

' activate LOD Master - necessary to get BOM object !!!
Dim oMasterLOD As LevelOfDetailRepresentation = repMgr.LevelOfDetailRepresentations.Item(1)

'set  a reference to the BOM
Dim oBOM As BOM = oAssyDef.BOM

' Make sure that the "Parts Only" view is enabled.
oBOM.PartsOnlyViewEnabled = True

'set a reference to the "Parts Only" BOMView
Dim oBOMView As BOMView = oBOM.BOMViews.Item("Parts Only")

For Each oBOMRow As BOMRow In oBOMView.BOMRows

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


  Dim oPartNumProperty As Inventor.Property
  Dim oDescripProperty As Inventor.Property
  Dim Qty As String

  If TypeOf oCompDef Is VirtualComponentDefinition Then
      'Get the Virtual Component property that contains the "Part Number"
      oPartNumProperty = oCompDef.PropertySets _
          .Item("Design Tracking Properties").Item("Part Number")

      'Get the  Virtual Component property that contains the "Description"
      oDescripProperty = oCompDef.PropertySets _
          .Item("Design Tracking Properties").Item("Description")
          
      Qty = oBOMRow.TotalQuantity
         
'      Debug.Print oBOMRow.ItemNumber; "  "; oPartNumProperty.Value; "  Qty="; _
'                  Qty; "  "; 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.
      oPartNumProperty = oCompDef.Document.PropertySets _
          .Item("Design Tracking Properties").Item("Part Number")

      'Get the file property that contains the "Description"
      oDescripProperty = oCompDef.Document.PropertySets _
          .Item("Design Tracking Properties").Item("Description")
      
      Qty = oBOMRow.TotalQuantity
      
'      Debug.Print oBOMRow.ItemNumber; "  "; oPartNumProperty.Value; "  Qty="; _
'                  Qty; "  "; oDescripProperty.Value
  End If
Next

Beep

MsgBox("Done")

 It might not be exactly what you're after, but should get you started.

Cheers,


Vladimir Ananyev
Developer Technical Services
Autodesk Developer Network

0 Likes
Message 5 of 10

Anonymous
Not applicable

Thanks so much for the help so far. This gets me pretty close. I see in the code that the "Qty" string variable holds what I am looking for. Now I just need to referance the custom iproperty of each part called "Qty" and set it equal to this current "Qty" prpperty.

 

All my attempts have failed at this time. I tried using the iproperties custom object but can't get it to work. Again thanks so much.

0 Likes
Message 6 of 10

Vladimir.Ananyev
Alumni
Alumni
Accepted solution

Could you try the following rule. It uses Inventor API to save Qty string in component's iProperty "QUANTITY".  If this property doesn't exist, it will be created.  Hope this helps.

'This iLogic rule gets components quantities from BOMView "Parts Only"
Sub Main()

    ' a reference to the assembly document.
    ' This assumes an assembly document is active.
    Dim oAssyDoc As AssemblyDocument = ThisDoc.Document
    Dim oAssyDef As AssemblyComponentDefinition = oAssyDoc.ComponentDefinition
    
    ' Get the Representations Manager object.
    Dim repMgr As RepresentationsManager = oAssyDef.RepresentationsManager
    
    ' activate LOD Master - necessary to get BOM object !!!
    Dim oMasterLOD As LevelOfDetailRepresentation = repMgr.LevelOfDetailRepresentations.Item(1)
    
    'set  a reference to the BOM
    Dim oBOM As BOM = oAssyDef.BOM
    
    ' Make sure that the "Parts Only" view is enabled.
    oBOM.PartsOnlyViewEnabled = True
    
    'set a reference to the "Parts Only" BOMView
    Dim oBOMView As BOMView = oBOM.BOMViews.Item("Parts Only")
    
    For Each oBOMRow As BOMRow In oBOMView.BOMRows
    
        'Set a reference to the primary ComponentDefinition of the row
        Dim oCompDef As ComponentDefinition = oBOMRow.ComponentDefinitions.Item(1)        
        
        Dim oPartNumProperty As Inventor.Property
        Dim oDescripProperty As Inventor.Property
        Dim Qty As String
        
        If TypeOf oCompDef Is VirtualComponentDefinition Then
            'Get the Virtual Component property that contains the "Part Number"
            oPartNumProperty = oCompDef.PropertySets _
                .Item("Design Tracking Properties").Item("Part Number")
        
            'Get the  Virtual Component property that contains the "Description"
            oDescripProperty = oCompDef.PropertySets _
                .Item("Design Tracking Properties").Item("Description")
                
            Qty = oBOMRow.TotalQuantity
            SetCustomProperty(oCompDef, "QUANTITY", Qty)
                
        Else
            'Get the file property that contains the "Part Number"
            'The file property is obtained from the parent
            'document of the associated ComponentDefinition.
            oPartNumProperty = oCompDef.Document.PropertySets _
                .Item("Design Tracking Properties").Item("Part Number")
        
            'Get the file property that contains the "Description"
            oDescripProperty = oCompDef.Document.PropertySets _
                .Item("Design Tracking Properties").Item("Description")
            
            Qty = oBOMRow.TotalQuantity
            
            SetCustomProperty(oCompDef, "QUANTITY", Qty)

        End If
    Next
    
    Beep    
    MsgBox("Done")
End Sub 'Main



Private Sub SetCustomProperty( _
                ByVal oCompDef as ComponentDefinition, _
                ByVal PropName As String, _
                ByVal Qty As String)

    Dim PropSetNAme As String = "Inventor User Defined Properties"
    Dim oCustomPropertySet As PropertySet
    
    If TypeOf oCompDef Is VirtualComponentDefinition Then
        ' Get the user defined (custom) property set.
        oCustomPropertySet = oCompDef.PropertySets.Item(PropSetNAme)    
    Else
        ' Get the active document.
        Dim oDoc as Inventor.PartDocument = oCompDef.Document    
        ' Get the user defined (custom) property set.
        oCustomPropertySet = oDoc.PropertySets.Item(PropSetNAme)    
    End If

    Dim oProperty As Inventor.Property    
    Try    
        'property exists, set new value
           oProperty = oCustomPropertySet.Item(PropName)
        oProperty.Value = Qty
    Catch
        ' Create the properties.
        oProperty = oCustomPropertySet.Add(Qty, PropName)
    End Try
End Sub  'SetCustomProperty
Cheers,

Vladimir Ananyev
Developer Technical Services
Autodesk Developer Network

0 Likes
Message 7 of 10

Anonymous
Not applicable

That Works! Thanks so much for your help.

 

I am going to play with the code a little though. It seems there is allot of unecessary code in there.

0 Likes
Message 8 of 10

Anonymous
Not applicable

Hi

I use this ilogic code to write part number, description, and quantity to Excel in structured way.

I write data about all components containing in assembly parts and subassemblies.

Therefore I made change in illogic rule: BOMViews.Item("Parts Only") is changed to BOMViews.Item("Structured”).

 

Now I will that all components from subassemblies will be listed under the assembly.

It will require that the code “stops” if it meet a subassembly and set reference to structured BOM View of the subassembly, read data from the sub-BOM and return focus to the main assembly for to continue the main job.

Is it possible to make such change in the existing code?  Would you help with this?

 

Thanks.

Robert Wojciechowski

0 Likes
Message 9 of 10

Anonymous
Not applicable

Here you have similar problem only in this case not only the parts but also the subassemblies get their quantity from BOM

http://forums.autodesk.com/t5/Inventor-Customization/Qty-in-bom/td-p/4528995

 

0 Likes
Message 10 of 10

Anonymous
Not applicable

Unfortunately i don’t understand this code (from your link) after some hours of considerations.

I use the code from this topic with some changes.

-          I use the Structured BOM for catch info about sub-assemblies in the assembly.

-          I write data to Excel at Debug section.

See these steps:

 …

'      Debug.Print oBOMRow.ItemNumber; "  "; oPartNumProperty.Value; "  Qty="; _

'                  Qty; "  "; oDescripProperty.Value

 

 ' write parts data to Excel

    GoExcel.CellValue("B" & RowNum) = "+"

    GoExcel.CellValue("C" & RowNum) = oPartNumProperty.Value

    GoExcel.CellValue("D" & RowNum) = oDescripProperty.Value

    GoExcel.CellValue("E" & RowNum) = Qty

    RowNum = RowNum + 1

    If TypeOf oCompDef Is AssemblyComponentDefinition Then

          RowNum = RowNum + 1

        ComponentNum = oPartNumProperty.Value

        ' MsgBox ("A Sub-assembly found " & ComponentNum )

    End If

   

  End If

Next

This code work very well, and produces output like on picture.
Excel spreadsheet has same name as  the main assembly.

You can see that the code perform check of the current component and register if it is a assembly.

After the line with MsgBox shall be a jump to sub- code that write data about the sub-assembly components to the spreadsheet. (I think). What if the sub-assembly contains a sub-sub-assembly?

The Sub-code should jump in such case to itself with “start data” about this sub-sub-assembly. And so on.

First after no assembly in sub code the sub-code should return to the main code.

 

0 Likes