Counter for Virtual Components - Help

Counter for Virtual Components - Help

munderwood4GRE6
Enthusiast Enthusiast
751 Views
6 Replies
Message 1 of 7

Counter for Virtual Components - Help

munderwood4GRE6
Enthusiast
Enthusiast

I'm trying to write a counter that will go through each occurrence/part in an assembly and count how many of those type of occurrences/parts there are based off the occurrences part # iProperty. 

 

I'm struggling with how to name the counter because I need it to be triggered by the part # iProperty.

 

For example, if I had 3 screws and 5 nails I would need a counter to hold 3 and one to hold 5 and have a way to assign the counters name based off the occurnences part # iProperty so I can pass that into an iLogic generated VC.

 

Any help would be greatly appreciated.

0 Likes
Accepted solutions (1)
752 Views
6 Replies
Replies (6)
Message 2 of 7

WCrihfield
Mentor
Mentor

Hi @munderwood4GRE6.  Your request is a little difficult to understand.  What are you calling a 'virtual component'?  There is a VirtualComponentDefinition that is only found within an assembly's BOM.  I believe that is the type of ComponentDefinition that is returned from a BOM row that was created using the 'Create Virtual Component' tool within the BOM dialog.  Is that what you mean?  Or is the component's BOMStructure set to something like Reference or Phantom or Purchased?  Also, it seems to me like you would need a collection type variable with two factor items inside as a variable to use in the code, to return both a count and part number data of multiple different possible components.  Maybe something like a NameValueMap or Dictionary would work in a situation like that.  With a variable like that, you can have a collection object to gather data into, and each item in that collection can hold 2 pieces of data...like a Part Number and an Integer.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 7

munderwood4GRE6
Enthusiast
Enthusiast

It may be easier for me to add the rule that I have so far:

 

'Delete All Virtual Components
Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition

Dim asmOcc As ComponentOccurrence
For Each asmOcc In oAsmCompDef.Occurrences
If TypeOf asmOcc.Definition Is VirtualComponentDefinition Then
asmOcc.Delete
End If
Next

 

 

'Look for all the Parts that have the Custom Virtual Component iProperty added to them
For Each oOccurrence In oAsmCompDef.Occurrences

Try
iProperties.Value(oOccurrence.Name, "Custom", "VC") = "True"

Try
iProperties.Value(oOccurrence.Name, "Custom", "VC1")= "VC1"


VC1_NAME = iProperties.Value(oOccurrence.Name, "Custom", "VC1_NAME")
VC1_PNUM = iProperties.Value(oOccurrence.Name, "Custom", "VC1_PNUM")
VC1_DESC = iProperties.Value(oOccurrence.Name, "Custom", "VC1_DESC")
VC1_BOMP = iProperties.Value(oOccurrence.Name, "Custom", "VC1_BOMP")
VC1_BCLASS = iProperties.Value(oOccurrence.Name, "Custom", "VC1_BCLASS")

 

'Create a virtual component
Dim oMatrix As Matrix
oMatrix = ThisApplication.TransientGeometry.CreateMatrix
Dim oNewOcc As ComponentOccurrence
oNewOcc = oAsmCompDef.Occurrences.AddVirtual(VC1_NAME, oMatrix)
Dim oCVirtualCompDef As VirtualComponentDefinition
oCVirtualCompDef = oNewOcc.Definition

InventorVb.DocumentUpdate() 

'Pass in the Virtual Component iProperties from the part to the Virtual Component
iProperties.Value(VC1_NAME & ":1", "Project", "Part Number") = VC1_PNUM
iProperties.Value(VC1_NAME & ":1", "Project", "Description") = VC1_DESC
iProperties.Value(VC1_NAME & ":1", "Custom", "BOM Priority") = VC1_BOMP
iProperties.Value(VC1_NAME & ":1", "Custom", "BOM Class") = VC1_BCLASS

Catch EE As Exception
End Try
'Run the code again for a second VC embedded in the part, plan to add the ability to check for 5
Try 
iProperties.Value(oOccurrence.Name, "Custom", "VC2")= "VC2" 

VC2_NAME = iProperties.Value(oOccurrence.Name, "Custom", "VC2_NAME")
VC2_PNUM = iProperties.Value(oOccurrence.Name, "Custom", "VC2_PNUM")
VC2_DESC = iProperties.Value(oOccurrence.Name, "Custom", "VC2_DESC")
VC2_BOMP = iProperties.Value(oOccurrence.Name, "Custom", "VC2_BOMP")
VC2_BCLASS = iProperties.Value(oOccurrence.Name, "Custom", "VC2_BCLASS")


Dim oMatrix As Matrix
oMatrix = ThisApplication.TransientGeometry.CreateMatrix
Dim oNewOcc As ComponentOccurrence
oNewOcc = oAsmCompDef.Occurrences.AddVirtual(VC2_NAME, oMatrix)
Dim oCVirtualCompDef As VirtualComponentDefinition
oCVirtualCompDef = oNewOcc.Definition

InventorVb.DocumentUpdate()
iProperties.Value(VC2_NAME & ":1", "Project", "Part Number") = VC2_PNUM
iProperties.Value(VC2_NAME & ":1", "Project", "Description") = VC2_DESC
iProperties.Value(VC2_NAME & ":1", "Custom", "BOM Priority") = VC2_BOMP
iProperties.Value(VC2_NAME & ":1", "Custom", "BOM Class") = VC2_BCLASS

Catch EE As Exception
End Try
Catch EE As Exception
End Try

Next

0 Likes
Message 4 of 7

munderwood4GRE6
Enthusiast
Enthusiast

This is working great for generating one virtual component in the assembly for the parts that have the iProperties added that trigger them. The issue I'm having is coming up with a way to count them up and then override the BOM with the counted qty 

0 Likes
Message 5 of 7

JelteDeJong
Mentor
Mentor

Maybe this does help you.

Public Class ThisRule
    Sub Main()

        Dim doc As AssemblyDocument = ThisDoc.Document

        Dim virComps = findAllWhere(doc,
            Function(occ) occ.Definition.Type = ObjectTypeEnum.kVirtualComponentDefinitionObject).
            ToList()

        Dim counter As New Dictionary(Of String, Double)
        For Each occ As ComponentOccurrence In virComps
            Dim def As VirtualComponentDefinition = occ.Definition

            Dim iProperty As [Property] = def.PropertySets.
                Item("Design Tracking Properties").
                Item("Part Number")

            Dim key As String = iProperty.Value
            Dim val As Double = def.BOMQuantity.GetEvaluatedBaseQuantity(def.BOMQuantity.UnitQuantity)
            If (counter.ContainsKey(key)) Then
                counter.Item(key) = counter.Item(key) + val
            Else
                counter.Add(key, val)
            End If
        Next

        Dim msg As String = ""
        For Each item In counter
            msg = msg & item.Key & " : " & item.Value & System.Environment.NewLine
        Next

        MsgBox(msg)

    End Sub

    Private Function findAllWhere(doc As AssemblyDocument,
                predicate As Func(Of ComponentOccurrence, Boolean)) As IEnumerable(Of ComponentOccurrence)

        Dim list As IEnumerable(Of ComponentOccurrence) = doc.ComponentDefinition.
            Occurrences.Cast(Of ComponentOccurrence).
            Where(predicate)

        Dim assemblyList As IEnumerable(Of ComponentOccurrence) = doc.ComponentDefinition.
            Occurrences.Cast(Of ComponentOccurrence).
            Where(Function(o) o.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject)

        For Each assOcc As ComponentOccurrence In assemblyList
            If TypeOf assOcc.Definition Is VirtualComponentDefinition Then Continue For
            Dim assDoc As AssemblyDocument = assOcc.ReferencedDocumentDescriptor.ReferencedDocument
            Dim listToAdd As IEnumerable(Of ComponentOccurrence) = findAllWhere(assDoc, predicate)
            list = list.Concat(listToAdd)
        Next
        Return list
    End Function
End Class

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

Message 6 of 7

munderwood4GRE6
Enthusiast
Enthusiast

Is there a simpler method? iLogic and programming are not my strong suite and I don't understand the code that you've shared.

0 Likes
Message 7 of 7

JelteDeJong
Mentor
Mentor
Accepted solution

depending on your exact need maybe there is a simpler method. but I can start by explaining what happens. you will comment in the code.

Dim doc As AssemblyDocument = ThisDoc.Document

' this function finds all VirtualComponent and puts them in a list.
' you can find more about this function on my blog:
' http://www.hjalte.nl/34-universal-occurrence-search-function
Dim virComps = findAllWhere(doc,
    Function(occ) occ.Definition.Type = ObjectTypeEnum.kVirtualComponentDefinitionObject).
    ToList()

' create a Dictionary of key/value pairs
Dim counter As New Dictionary(Of String, Double)

' Loop over all VirtualComponents 
For Each occ As ComponentOccurrence In virComps
    Dim def As VirtualComponentDefinition = occ.Definition

    ' Get the iProperty
    Dim iProperty As [Property] = def.PropertySets.
        Item("Design Tracking Properties").
        Item("Part Number")


    Dim key As String = iProperty.Value
    Dim val As Double = def.BOMQuantity.GetEvaluatedBaseQuantity(def.BOMQuantity.UnitQuantity)

    ' Check ik the counter with sertain key already 
    ' exists in the Dictionary of key/value pairs.
    If (counter.ContainsKey(key)) Then
        ' Yes: add the value of the found VirtualComponent to the item we found before
        counter.Item(key) = counter.Item(key) + val
    Else
        ' No: create create a key/value pair and add it to the Dictionary
        counter.Add(key, val)
    End If
Next

' This is just to show how you can use the information in the Dictionary
Dim msg As String = ""
For Each item In counter
    msg = msg & item.Key & " : " & item.Value & System.Environment.NewLine
Next
MsgBox(msg)

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com