Update all model states

Update all model states

KaynePellegrino
Enthusiast Enthusiast
210 Views
9 Replies
Message 1 of 10

Update all model states

KaynePellegrino
Enthusiast
Enthusiast

I'm making a part that has roughly 31'000 combinations.

 

I'm slowly learning how to make rules and adjust things via the Model States spreadsheet in order to make tens and hundreds of model states at once. But when I make a new model state using the spreadsheet I need to manually activate the new model states and click 'local update' for every single one. I'm at model state like 500 and it's exhausting.

 

I got code from a different forum thread ⬇️

 

Dim oDoc As Inventor.Document = ThisDoc.FactoryDocument
If oDoc Is Nothing Then Return
Dim oMSs As ModelStates = oDoc.ComponentDefinition.ModelStates
If oMSs Is Nothing OrElse oMSs.Count = 0 Then Return
Dim oAMS As ModelState = oMSs.ActiveModelState
For Each oMS As ModelState In oMSs
	oMS.Activate
	Dim oMSDoc As Inventor.Document = TryCast(oMS.Document, Inventor.Document)
	If oMSDoc Is Nothing Then Continue For
	oMSDoc.Update2(True)
Next oMS
If oMSs.ActiveModelState IsNot oAMS Then oAMS.Activate

 

When I run this as a rule I can see it correctly cycle through each model state. I can watch a form I made select the correct combos of radial options compared to the model state name. But even after its run, with the scope on both options, it still doesn't update each model state like it should. When I run it and click on the new model states I just made, and 'updated' using the code above, it still shows that I need to manually click 'local update' for it to actually update the individual model states.

 

Is there some way I'm not seeing the update all model states at once to correctly adjust based on the parameters set for each model state? Or something in the code above that I need to change

 

I'm on inventor 2025 in case that helps.

0 Likes
Accepted solutions (2)
211 Views
9 Replies
Replies (9)
Message 2 of 10

hollypapp65
Advocate
Advocate

I just use oDoc, Activate each MS, oDoc.Update.

oDoc.Save at the end.

It'll go by fast and won't see each MS on screen.

 

Some file need to be in assembly and activate and save each MS to get properly updated.

0 Likes
Message 3 of 10

KaynePellegrino
Enthusiast
Enthusiast

what does that look like written out in code

 

I have a very very very basic understanding of how to write code in inventor so far

 

But thank you very much!

0 Likes
Message 4 of 10

WCrihfield
Mentor
Mentor
Accepted solution

Sorry for not getting back on the other forum reply sooner, things have been pretty crazy at work lately.

That older code example was relying on each ModelState having its own Document, but that is not always the case.  So, this updated code simply gets the 'active' document after activating each ModelState, then updates that Document.  I changed how it gets the initial document, since this process needs to always be acting on the 'active' document, and not some other document being referenced in the background.  It can be ran when any type of document is active, but will only work when either an assembly or part is active.  I also added in a check to make sure it is working with the 'factory' document, because that is the only version we can make changes to.  There is only a factory document when there are 2 or more ModelStates present in the file.  The factory document is just the one associated with the currently 'active' ModelState, whichever one that may be.

Give this version a try.

Dim oDoc As Inventor.Document = ThisApplication.ActiveDocument
If oDoc Is Nothing Then Return
If (Not TypeOf oDoc Is AssemblyDocument) AndAlso (Not TypeOf oDoc Is PartDocument) Then Return
If oDoc.ComponentDefinition.IsModelStateMember Then
	oDoc = oDoc.ComponentDefinition.FactoryDocument
End If
Dim oMSs As ModelStates = oDoc.ComponentDefinition.ModelStates
If (oMSs Is Nothing) OrElse (oMSs.Count < 2) Then Return
Dim oAMS As ModelState = oMSs.ActiveModelState
For Each oMS As ModelState In oMSs
	oMS.Activate()
	Dim oFDoc As Inventor.Document = ThisApplication.ActiveDocument
	oFDoc.Update2(True)
Next oMS
If oMSs.ActiveModelState IsNot oAMS Then oAMS.Activate()

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 5 of 10

KaynePellegrino
Enthusiast
Enthusiast

No Worries!

I found that if I deleted the last 2 lines and ran it, it would show I needed to manually update the last model state and then they would all be updated. Idk if it's a visual glitch that I need to update the last one. But I'm running this new code right now and will get back in a moment. I currently have 2,625 model states, so hopefully it doesn't run for too long 😂

0 Likes
Message 6 of 10

KaynePellegrino
Enthusiast
Enthusiast

Thank you for you help!

0 Likes
Message 7 of 10

WCrihfield
Mentor
Mentor

By the way, just so you know, when we place an instance of a new ModelState into an assembly, as an assembly component, and maybe when we switch an existing assembly component to a new ModelState, that action will 'generate' a Document for that new ModelState.  But before that point, the new ModelState will usually not have its own Document (ModelState.Document property value) yet.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 8 of 10

KaynePellegrino
Enthusiast
Enthusiast

It just finished loading, and it worked! None of the new model states required a manual 'local update'. Thank you so much. Only 27,000 to go.

 

When you say 'it's own document', what is document referring too? Like what is a document exactly?

0 Likes
Message 9 of 10

WCrihfield
Mentor
Mentor
Accepted solution

To help explain that, I think I should probably explain that there is also an Inventor.File object.  The File object is a specifically organized collection of data that has already been written to something like a hard drive or other long term data storage medium.  The Inventor.Document object is a specifically organized collection of data that is currently being held within Inventor's temporary 'session memory', which is a portion of your computer's RAM that Inventor is using while it is running.  A Document can exist without being associated with a File, and a File can exist without being associated with a Document.  For example, when we click on the 'New' button in Inventor, that creates a new Document.  You can then start designing something within that document, but there will not be a File for that document until you 'Save' it.  But you could have also just closed the document, without ever saving it, then there would have never been a File created for it.  When you save a Document for the first time, that creates a File that will then be associated with that Document, as long as that Document remains open in Inventor.  If you continued to make changes to that Document after saving it, then the contents of the File would be different from the contents of the Document, so if you closed that document without saving again, those changes made since the last save would be lost, because they would not get written into the File.  This is because the document is just temporary.  When you use Inventor to 'Open' an Inventor file, Inventor creates a Document to represent the data to the user in a way that the user can recognize and interact with.

 

Traditionally, it seemed like there has always been just one Document per open file, but with the introduction of ModelStates in the 2022 release of Inventor, it seems like one file can now store the necessary data for defining either just one document, or many documents, because each ModelState can potentially represent a different document that is based on a different set of data.  Each ModelState can potentially have different values for the same Parameters and iProperties, and can potentially have different components &/or features suppressed or unsuppressed.  So, each ModelState can potentially represent a model that is very different in size, shape, and information.  Not only that, but we can potentially place many assembly components into an assembly, where each of them reference the same source file, but each one can be set to a different ModelState that was defined in that source file, which can generate many different documents (one for each different ModelState that the components are set to), all pointing back to that one source file.  In situations like that, making changes to those documents by code can get pretty complicated.

 

Next, we need to be aware of the difference between 'member' documents, and the 'factory' document, when ModelStates are involved.  When you have a file open in Inventor which has multiple ModelStates in it, the document associated with the 'active' ModelState is understood as the 'factory' document, and the documents associated with the 'other' (not active) ModelStates are known as 'member' documents.  Usually, we will be able to (Read/Write) freely make changes to the factory document, but the member documents will be (ReadOnly) readable, but not changeable.  There are exceptions, but that is where some of the complexity comes in.  One of the exceptions is what is called the 'edit scope'.  This edit scope dictates whether our current and near future changes made to the active document will only effect the active ModelState, or will effect (multiple or all) ModelStates.  Manually, that edit scope an be accessed/changed on the Manage tab of the ribbon, on the Author panel, where there is a drop-down control for it, or from the ModelStates nodes in the model browser tree, where will be small indicator controls next to each ModelState.  Another exception is where we have the ability to edit the contents of the ModelState table, or editing it through an Excel spreadsheet, where those edits can effect multiple ModelStates at the same time.

Whew. 😅 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 10 of 10

KaynePellegrino
Enthusiast
Enthusiast

That was a lot, but definitely gave me a better understanding. Thank you so much!

0 Likes