Using Ilogic to update model states

Using Ilogic to update model states

aberningR89Y3
Explorer Explorer
786 Views
5 Replies
Message 1 of 6

Using Ilogic to update model states

aberningR89Y3
Explorer
Explorer

I am looking for a way to update every model state in a part file without having to manually select each model state and click 'local update'.

Ideally, I would like to do this without having to know the name of every model state beforehand.

 

Something like: find each model state name, put it into an array, and then loop through and use

ThisApplication.ActiveView.Update()

I am not well versed on VBA/iLogic syntax so maybe there is a 'For Each' loop that can do it?  

 

Thanks in advance,

Alex

0 Likes
Accepted solutions (1)
787 Views
5 Replies
Replies (5)
Message 2 of 6

WCrihfield
Mentor
Mentor
Accepted solution

Hi @aberningR89Y3.  I am not sure if this is really needed, or if one update / rebuild would be enough, but here is an example iLogic rule that should work similarly to what you seem to be asking for.  This is just a quickie rough draft, and could be improved upon, if needed though.

 

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

 

If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 6

aberningR89Y3
Explorer
Explorer

Thank you for this, your code cycles through each model state successfully, I added some other lines to run inside the loop as well and it works nicely. I appreciate your help. 

0 Likes
Message 4 of 6

KaynePellegrino
Enthusiast
Enthusiast

I tried copying and pasting this code into a rule and its orange all over. Are there aspects to the rule that I need to change based on my part?

I'm new to this and it's very confusing 

0 Likes
Message 5 of 6

WCrihfield
Mentor
Mentor

The image looks relatively normal to me.  The color of the text is controlled by a setting within the iLogic rule editor dialog.  When you have the iLogic rule editor dialog open, go to the 'Options' tab, then on the upper-right area is usually a setting named 'Syntax Coloring', with a drop-down list under it, for choosing a 'Theme', of sorts.  And below that is a button for letting us choose which 'Font' we want the code to be in.

Which version / year of Inventor are you using?  ModelStates became available in the Inventor 2022 release.  Before that (in earlier year versions of Inventor), we had LODs (level of detail representations).  So if you have an earlier version of Inventor, that code may not work for you.  It also does not provide any 'feedback', as far as telling you what is going on as it is doing its task, or explaining what problems it may have encountered along the way.  For example, if you ran that rule, but had a drawing open on your screen at that moment in time, then the rule would not have done anything, because it is expecting a part or assembly type document to be active when it starts.  Instead of throwing an error, it avoids the error, then just exits the rule.  Then if it did not find any ModelStates in that document it would just exit the rule also, because there is nothing for it to do.  If there was only the one, original ModelState in the document, then it might also seem like it did not do anything, because that one ModelState is already the active one, and there are no others to activate or update.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 6 of 6

KaynePellegrino
Enthusiast
Enthusiast

I'm on 2025

I've found previously that orange text means it's missing something when writing other things. 

When I run the rule, it runs through all model states (like 600+). I see my forms change to what they should as it moves from one model state to the next. But even when it's finished it still shows I need to click 'Local Update' when I activate any new model states made after editing the Model States spreadsheet

 

A little context.

Example:

I want a board that can be different lengths, widths, and thicknesses

Thickness Options: 2", 4"

Widths Options: 4", 6", 8", 10", 12"

Length Options: 6', 8', 10', 12', 16'

 

So I made Model states for all combos that could be 2" Thick

1. 2" x 4" x 6'

2. 2" x 6" x 6'

3. 2" x 8" x 6'

4. 2" x 10" x 6'

5. 2" x 12" x 6'

6. 2" x 4" x 8'

7. 2" x 6" x 8'

8. 2" x 8" x 8'

9. 2" x 10" x 8'

10. 2" x 12" x 8'

11. 2" x 4" x 10'

12. 2" x 6" x 10'

13. 2" x 8" x 10'

14. 2" x 10" x 10'

15. 2" x 12" x 10'

16. 2" x 4" x 12'

17. 2" x 6" x 12'

18. 2" x 8" x 12'

19. 2" x 10" x 12'

20. 2" x 12" x 12'

21. 2" x 4" x 16'

22. 2" x 6" x 16'

23. 2" x 8" x 16'

24. 2" x 10" x 16'

25. 2" x 12" x 16'

 

I then Edited the Model States via spreadsheet by copying Row 1-25, and changing the thickness to 4in in order to make new model states that are 4" Thick (Model States 26-50)

 

When I save and exit the spreadsheet, it makes the new model states that I added. Thought this was great, I need to make literally 30'000 model states for my actual part. But the model states made from editing the spreadsheet still show I need to manually click and update them individually, even after running the rule you wrote.

I click on Model State '1.' and the form shows '2" Thick, 4" Wide, 6' Long' and it doesn't need updated. But then I click on the new Model State '26.' and the form shows '4" Thick, 4" Wide, 6' Long' like it should, but the model itself is still actually showing as if it's Model State '1.' until I manually update it.

Is there anything you can suggest I change to make it work?

0 Likes