Change Model State and parameter of assembly occurrence

J_Dumont
Advocate
Advocate

Change Model State and parameter of assembly occurrence

J_Dumont
Advocate
Advocate

Hello,

I'd appreciate any help with the following.

 

I've gone through many earlier posts, and I can't get the code to change the Model State and Parameter of an assembly occurrence to work.

 

I've gotten it to work in iLogic, but I need to move to VB.Net for various reasons to make an Add-in.

 

I prompt the user for input, which gives me the information for the needed model state and the value of the parameter that needs to change. I also need to set the occurrence to Global Edit. Below is the snippet for iLogic.

 

 

Dim AssDoc As AssemblyDocument = ThisApplication.ActiveDocument

'Set Grille style using Model States
Dim Occ As Inventor.ComponentOccurrence = AssDoc.ComponentDefinition.Occurrences.ItemByName("Linear Bar") 
Dim partdoc As PartDocument = Occ.Definition.FactoryDocument
Dim MSGSS As New ModelStatesGlobalScope(partdoc)
Occ.ActiveModelState=BarGrilleStyle
InventorVb.DocumentUpdate()
partdoc.ComponentDefinition.Parameters.ModelParameters.Item("Length").Value = LinearBarLength*2.54 'convert from cm to in

Here's the VB code

 

 

        Dim oDoc As AssemblyDocument
        oDoc = g_inventorApplication.ActiveDocument

        Dim oAssemblyCompDef As AssemblyComponentDefinition
        oAssemblyCompDef = oDoc.ComponentDefinition

        ' Specify an occurrence to change its active model state.
        Dim oOccu As ComponentOccurrence
        oOccu = oAssemblyCompDef.Occurrences(1)

        ' Get native document ComponentDefinition of occurrence.
        Dim oOccuDef As ComponentDefinition
        oOccuDef = oOccu.Definition

        oOccu.ActiveModelState = "CA400"

 

I referenced code from the following:

https://help.autodesk.com/view/INVNTOR/2022/ENU/?guid=GUID-045E78DB-DBE3-4220-8EBC-29EB53890E1A

 

Here's the message I get.

J_Dumont_0-1716224118772.png

 

0 Likes
Reply
Accepted solutions (1)
545 Views
8 Replies
Replies (8)

bradeneuropeArthur
Mentor
Mentor

For me this code works correct.

It wil set the Model State of the first occurrence to "CA400" if defined correctly in the (sub)Assembly Occurrence

Regards,

Arthur Knoors

Autodesk Affiliations:

Autodesk Software:Inventor Professional 2024 | Vault Professional 2022 | Autocad Mechanical 2022
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:Drawing List!|Toggle Drawing Sheet!|Workplane Resize!|Drawing View Locker!|Multi Sheet to Mono Sheet!|Drawing Weld Symbols!|Drawing View Label Align!|Open From Balloon!|Model State Lock!
Posts and Ideas:Dimension Component!|Partlist Export!|Derive I-properties!|Vault Prompts Via API!|Vault Handbook/Manual!|Drawing Toggle Sheets!|Vault Defer Update!


! For administrative reasons, please mark a "Solution as solved" when the issue is solved !

0 Likes

J_Dumont
Advocate
Advocate

Hi @bradeneuropeArthur 

Thank you for the quick reply.

 

My apologies; I did not proofread my code before I copied it to the forum.

 

I need to specify the occurrence name and then change its Model State.

 

Here is the code that is not working.

        Dim oDoc As AssemblyDocument
        oDoc = g_inventorApplication.ActiveDocument

        Dim oAssemblyCompDef As AssemblyComponentDefinition
        oAssemblyCompDef = oDoc.ComponentDefinition

        ' Specify an occurrence to change its active model state.
        Dim oOccu As ComponentOccurrence
        oOccu = oAssemblyCompDef.Occurrences.Item("Linear Bar")

        ' Get native document ComponentDefinition of occurrence.
        Dim oOccuDef As ComponentDefinition
        oOccuDef = oOccu.Definition

        oOccu.ActiveModelState = "CA400"

 

 

J_Dumont_0-1716234183323.png

 

 

 

0 Likes

WCrihfield
Mentor
Mentor
Accepted solution

Looks like you may just need to switch from using the regular Item property (only accepts Integer/Long), to using the ItemByName property for that one line of code.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes

J_Dumont
Advocate
Advocate

Hi @bradeneuropeArthur and @WCrihfield 

 

Thank you for your assistance with the issue.

 

Can either of you help me with my second issue? After I change the Model State, I need to adjust the Length parameter.

The Length parameter is not defined in each State, so I will need to change the edit scope to Global and then change the length parameter.

If I need to, I can start another message.

0 Likes

WCrihfield
Mentor
Mentor

Hi @J_Dumont.  There is no vb.net equivalent of that iLogic 'ModelStatesGlobalScope' resource.  When we want a change to be applied to all ModelStates the same way, we either iterate through each of the ModelStates, activate them, then make the same change, or we make the change to each row in the ModelStateTable, or to each row in the ModelStates Excel worksheet.  Working with the ModelStateTable is not too difficult, but nearly everything in it is ReadOnly, except for the data cell values.

 

Correction:  If we can access the ModelStates collection of that component, then we can control its MemberEditScope property value, which can then be set to 'kEditAllMembers' (a variation of the https://help.autodesk.com/view/INVNTOR/2024/ENU/?guid=MemberEditScopeEnum).  Then we can make the change just once, and it will effect all ModelStates of that file.  But you must start from the 'factory' document version of that file.  One shortcut way to do that from an assembly perspective, is to use the 'Open' method, specify the true FullDocumentName (not just FullFileName) of that component, and capture the Document that this method returns, then use that to access the ModelStates, and further interactions / edits to that document.  The document you get back from that Open method will effectively always be the 'factory', due to the way you are opening it with that method.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes

WCrihfield
Mentor
Mentor

Also, have you read this help page about the assembly needing to be updated when multiple component instances are included for models that have multiple ModelStates.

To Modify an Occurrence Parameter with a Model State 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes

J_Dumont
Advocate
Advocate

Hi @WCrihfield 

 

Thank you for the info. I will see if I can dig deeper into what you sent me.

0 Likes

J_Dumont
Advocate
Advocate

Hi @WCrihfield 

 

Yes, this is where I first started, but I had no luck changing the occurrence parameters with the information within this link. The link seems to be focused on iLogic or VBA. 

 

0 Likes