Occurrence Quantity for specific Model state

Occurrence Quantity for specific Model state

floccipier
Advocate Advocate
1,366 Views
14 Replies
Message 1 of 15

Occurrence Quantity for specific Model state

floccipier
Advocate
Advocate

Hi All, 

I have this really basic VBA code to get quantity for each model state of the parts which is doing what I wanted to do. I was just wondering if someone has an idea to do it better way. The idea is to get the quantity of each model state of each part. 

Sub QtyCount()

Dim oAsmCompDef As AssemblyComponentDefinition
Set oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition

Dim i As Integer
i = 0
Dim j As Integer
j = 0
Dim k As Integer
k = 0
Dim l As Integer
l = 0
Dim n As Integer
n = 0

'Iterate through all of the occurrences
Dim oOccurrence As ComponentOccurrence
For Each oOccurrence In oAsmCompDef.Occurrences

If oOccurrence.ActiveModelState = "Master" Then
n = n + 1

ElseIf oOccurrence.ActiveModelState = "One" Then
i = i + 1

ElseIf oOccurrence.ActiveModelState = "Two" Then
j = j + 1

ElseIf oOccurrence.ActiveModelState = "Three" Then
k = k + 1

ElseIf oOccurrence.ActiveModelState = "Four" Then
l = l + 1


End If
Next
MsgBox ("Qty for Master model State is " & n & vbNewLine & "Qty for One model State is " & i _
& vbNewLine & "Qty for Two model State is " & j & vbNewLine & "Qty for Three model State is " & k _
& vbNewLine & "Qty for Four model State is " & l)

End Sub

Best regards, 

Flo

 

0 Likes
Accepted solutions (1)
1,367 Views
14 Replies
Replies (14)
Message 2 of 15

floccipier
Advocate
Advocate

apology for additional question:
I am trying to save quantity of each model state in a iProperty in the part and iproperties are playing up and they become part of model state work sheet. Any idea how quantity of each model state could be reliably be stored in an iproperty?

0 Likes
Message 3 of 15

floccipier
Advocate
Advocate

Hi, 

This is my updated code and I am really lost as I can'g get much expamples regarding model states. After code I am stating the issues which I am facing.

Sub QtyCount()

Dim oAsmCompDef As AssemblyComponentDefinition
Set oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition

Dim i As Integer
i = 0
Dim j As Integer
j = 0
Dim k As Integer
k = 0
Dim l As Integer
l = 0
Dim n As Integer
n = 0

'Iterate through all of the occurrences
Dim oOccurrence As ComponentOccurrence
For Each oOccurrence In oAsmCompDef.Occurrences

If oOccurrence.ActiveModelState = "Master" Then
n = n + 1

ElseIf oOccurrence.ActiveModelState = "One" Then
i = i + 1

ElseIf oOccurrence.ActiveModelState = "Two" Then
j = j + 1

ElseIf oOccurrence.ActiveModelState = "Three" Then
k = k + 1

ElseIf oOccurrence.ActiveModelState = "Four" Then
l = l + 1


End If
Next
MsgBox ("Qty for Master model State is " & n & vbNewLine & "Qty for One model State is " & i _
& vbNewLine & "Qty for Two model State is " & j & vbNewLine & "Qty for Three model State is " & k _
& vbNewLine & "Qty for Four model State is " & l)

End Sub

 

  • ipropeties are being populated to model state configuration table (when i opened it in excel) and it seems like I don't have control over these. 
  • This code is pretty much hard coded for the specific part - I will also like if I could get some pointer to lead to create more generic code which reads all the (if there are 2 or more) model states in each occ (part) in assembly (and sub assemblies) and add qty in part for each part. 
    sorry tagging you guys shamelessly @WCrihfield , @JhoelForshav, if you guys can assit in any way, it would be really appreciated. 

    Best regards, 
    Sam

0 Likes
Message 4 of 15

floccipier
Advocate
Advocate
Dim CurrentDoc As Document
CurrentDoc = oOccurrence.Definition.Document
Dim strCustProperty As String
'Get the custom property set.
Dim invCustomPropertySet As PropertySet
invCustomPropertySet = CurrentDoc.PropertySets.Item("Inventor User Defined Properties")
'Dim invVolumeProperty As Property
invCustomPropertySet.Item("00 Master Qty").Value = n
invCustomPropertySet.Item("01 One Qty").Value = i
invCustomPropertySet.Item("02 Two Qty").Value = j
invCustomPropertySet.Item("03 Three Qty").Value = k
invCustomPropertySet.Item("04 Four Qty").Value = l
0 Likes
Message 5 of 15

WCrihfield
Mentor
Mentor

Hi @floccipier.  Unfortunately, I'm still using Inventor 2021.2.2, due to some sort of corporate delay, so I can't do any trial & error testing on code that would involve Model States, because they became available in 2022 release.  Although, I have attended multiple webinars, and read other resources on the subject that seem to suggest that now, when there are Model States present in a model file, you now have to specify whether changes to things like parameters & iProperties will effect just the 'active' model state, or all model states.  So, if you want these iProperties to be stored at the document level, instead of just within model states, I believe you need to now specify that these changes should effect all model states.  I believe this setting is under the PartDocument.ComponentDefinition.ModelStates.MemberEditScope, and as far as I know it only has two possible settings from the MemberEditScopeEnum, which only contains options for editing the active member or all members.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 6 of 15

floccipier
Advocate
Advocate

thanks for reply. What about attributes, do they come under same umbrella?

0 Likes
Message 7 of 15

WCrihfield
Mentor
Mentor

I don't believe that any Attributes are specific to, or owned by a model state exactly, other than the ones specifically assigned to that model state object itself.

The following things can be recorded/saved individually to model states: iProperties, Parameters, Features, Dimensions, Components/Bill Of Materials (in an assembly).  So, since attributes can be attached to a great many things, I would have to say that as long as the attribute isn't attached to geometry, a feature, or component that only exist in one of the model states, then the attribute should be totally independent of any model states.  Does that make sense to you?  Is that the answer you were looking for?

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 8 of 15

floccipier
Advocate
Advocate

I think if I am able to save an attribute that would help me definitely. But apart from that I was wondering as I said in my first post that if you could guide me with bit of logic to make a generic rule that would find quantity for each part's each design state (rather than a hard coded rule describing each design state of a specific part). I understand that you don't have the latest version and you have no way of testing so even if it is bit raw I can take it from there. 

Thank you. 

0 Likes
Message 9 of 15

WCrihfield
Mentor
Mentor

So, the 'hard coded' element of your earlier code that you are trying to avoid, would be specifying model state names to check for within the rule, right?  If so, then maybe all you really need is a list of all the 'unique' names of the active model states of the assemblies components, and how many times each of those unique names is found?  For instance, I could have a Dictionary object (each entry has a Key and a Value) created before the loop begins, for collecting unique names and the count of how many times each name is found.  Then while looping through all the components, I could get the components 'active model state' name, check to see if it is already in the our list (Name would be a Key).  If it is not found, then add it to the Dictionary with the name as the Key, and a quantity of 1 as the value.  And if it is already in the list, get that entry by its Key, and add 1 to its Value.  This would get us a list of Key/Value pairs (Name, Quantity) being used in the assembly.  Does that sound like a plan?  Are you familiar with the Dictionary object?

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 10 of 15

floccipier
Advocate
Advocate

Sounds really promising. I have not used a dictionary before but with bit of lead from you I can get going and if stuck I do bit of search as well. 

I have another question though, once we would have dictionary with modelstate names and corresponding quantity then we can assign those set of information to say attribute (attribute name and value), isn't it?

0 Likes
Message 11 of 15

WCrihfield
Mentor
Mentor

I'm not 100% sure where I would be attaching the attribute after the fact, but maybe you could loop back through all the components again, then attempt to attach an attribute to them.  I'm not sure how I would set those attributes up though.  Name of Model State as name of Attribute, then quantity as its value?

 

Anyways, here is the main part of the code that I created for you, that uses the Dictionary to collect the data, then reports the finding to the user, similarly to what you were doing before, but without specifying any names directly.  Again, untested, so be cautious.

Sub QtyCount()
    Dim oAsmCompDef As AssemblyComponentDefinition
    Set oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition
    
    'create a Dictionary to store names and quantities in
    Dim oMSNames As New Dictionary
    'or
    'Dim oMSNames As Object
    'Set oMSNames = CreateObject("Scripting.Dictionary")
    
    oMSNames.CompareMode = TextCompare 'capilalization differences won't matter
    
    Dim oName As String
    Dim oOccurrence As ComponentOccurrence
    'Iterate through all of the occurrences
    For Each oOccurrence In oAsmCompDef.Occurrences
        'get the name of the active model state
        oName = oOccurrence.ActiveModelState
        'check to see if it is in our Dictionary yet or not
        If oMSNames.Exists(oName) Then
            oMSNames.Item(oName) = oMSNames.Item(oName) + 1
        Else
            Call oMSNames.Add(oName, 1)
        End If
    Next
    Dim oReport As String
    Dim oKey As String
    For Each oKey In oMSNames.Keys
        oReport = oReport & vbCrLf & "The Model State name '" & oKey & "' was found " & oMSNames.Item(oKey) & " times."
    Next
    Call MsgBox(oReport, , "")
End Sub

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 12 of 15

floccipier
Advocate
Advocate

@WCrihfield 

 

Thanks for this. 

I am getting this error:

 

floccipier_1-1628094514261.png

and for alternate you provide this error

floccipier_2-1628094552355.png

Any idea if I am doing something wrong?

0 Likes
Message 13 of 15

WCrihfield
Mentor
Mentor
Accepted solution

You aren't doing anything wrong. The first error is understandable, it just doesn't recognize that type of object directly. Not a problem, because the second method of creating and working with that object worked. The second error I also understand. I forgot that the Dictionary.Keys is an Array, and you're not supposed to specify the type of object you are looping through ahead of time, because its Type is already hardcoded by the type of Array. Try just commenting out, or deleting that line of code above that loop "Dim oKey As String", and see if that fixes it.

For some reason I'm having a lot of difficulty staying signed into my Autodesk Account. Web page keeps blinking and signing me out, and kicking me back out to generic autodesk page. Extremely frustrating! Seems to take 10 minutes to do a 10 second job.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 14 of 15

floccipier
Advocate
Advocate

This has fixed and code is running to give me instance quantity for each model state in smaller assembly. I would be testing and tweaking it for larger assemblies with subassemblies as right now it is giving few errors. Unfortunately I have been pulled to do something else but I will be back on this in couple of weeks maximum. 
@WCrihfield Thank you so much for your help and patience, appreciated big time. 

0 Likes
Message 15 of 15

grant_jGAR8X
Contributor
Contributor

@floccipier Did you end up getting this towrk?

0 Likes