Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Change Model State and parameter of assembly occurrence

8 REPLIES 8
SOLVED
Reply
Message 1 of 9
J_Dumont
336 Views, 8 Replies

Change Model State and parameter of assembly occurrence

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

 

8 REPLIES 8
Message 2 of 9

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 !

Message 3 of 9

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

 

 

 

Message 4 of 9
WCrihfield
in reply to: J_Dumont

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)

Message 5 of 9
J_Dumont
in reply to: J_Dumont

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.

Message 6 of 9
WCrihfield
in reply to: J_Dumont

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)

Message 7 of 9
WCrihfield
in reply to: J_Dumont

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)

Message 8 of 9
J_Dumont
in reply to: WCrihfield

Hi @WCrihfield 

 

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

Message 9 of 9
J_Dumont
in reply to: WCrihfield

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. 

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Technology Administrators


Autodesk Design & Make Report