Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.

model states iproperties

GosponZ
Collaborator

model states iproperties

GosponZ
Collaborator
Collaborator

I found this rule believe created by  WCrihfield.  Rule is working just perfect as is intent to work. I do have frame which is sheet metal parts and has fold and unfold. As i said rule is giving me what i need, but i have to change for every part . in original rule that is named as Testing.

I do have questions;

1. Can this rule be adjusted to run from assembly?

2. If not run from assembly then Can MsgBox be changed with input box. 

Thanks

 

Dim oPDoc As PartDocument = ThisDoc.Document

Dim oPDef = oPDoc.ComponentDefinition

Dim oMSs As ModelStates = oPDef.ModelStates

Dim oActiveMS As ModelState = oMSs.ActiveModelState

Dim oCProps As PropertySet = oPDoc.PropertySets.Item("Inventor User Defined Properties")

Dim oCProp As Inventor.Property = oCProps.Item("CProp1")

oMSs.MemberEditScope = MemberEditScopeEnum.kEditActiveMember

For Each oMS As ModelState In oMSs

        oMS.Activate

        Dim oVal As String = ""

        oVal = oCProp.Value

        MsgBox("While " & oMS.Name & " is active, CProp1 = " & oVal, , "")

        oCProp.Value = "Testing - " & oMS.Name

        oVal = oCProp.Value

        MsgBox("While " & oMS.Name & " is active, CProp1 changed to: " & oVal, , "")

Next

oActiveMS.Activate

 

 

 

0 Likes
Reply
Accepted solutions (2)
819 Views
8 Replies
Replies (8)

WCrihfield
Mentor
Mentor

Maybe give this version a try.  The active document just needs to be saved first, so it's file name can be checked in the first line.

If ThisApplication.FileManager.IsInventorComponent(ThisDoc.PathAndFileName(True)) = False Then Exit Sub
Dim oDoc As Document = ThisDoc.FactoryDocument
Dim oDef As ComponentDefinition = oDoc.ComponentDefinition
Dim oMSs As ModelStates = oDef.ModelStates
Dim oActiveMS As ModelState = oMSs.ActiveModelState
Dim oCProps As PropertySet = oDoc.PropertySets.Item("Inventor User Defined Properties")
Dim oCProp As Inventor.Property = oCProps.Item("CProp1")
oMSs.MemberEditScope = MemberEditScopeEnum.kEditActiveMember
For Each oMS As ModelState In oMSs
	oMS.Activate
	Dim oVal As String = ""
	oVal = oCProp.Value
	MsgBox("While " & oMS.Name & " is active, CProp1 = " & oVal, , "")
	oCProp.Value = "Testing - " & oMS.Name
	oVal = oCProp.Value
	MsgBox("While " & oMS.Name & " is active, CProp1 changed to: " & oVal, , "")
Next
oActiveMS.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)

0 Likes

GosponZ
Collaborator
Collaborator

Error on line 7

0 Likes

WCrihfield
Mentor
Mentor

Sorry for the delay.  I left right after posting that.  Line 7 is trying to access a custom iProperty named "CProp1".  I just assumed that you had already created that property manually, just for testing purposes.  If that custom iProperty does not already exist, that line of code would throw an error.  In this new version I included a Try...Catch block at that position that will first try to find that property, and if not found, it will try to create it with an empty String as its value.  Then I also added ".ToString" at the end of the "oVal = oCProp.Value" line, just in case its Value is not already understood as a String.  If it creates the iProperty, then that first message in each loop will likely show it having an empty value.  When you create something like a custom property or user parameter in a file with multiple ModelStates, the new property or parameter will exist for all ModelStates, and it will have that same initial value recorded in all ModelStates, no matter how you have the edit scope set.  After that they can be changed however you want.

If ThisApplication.FileManager.IsInventorComponent(ThisDoc.PathAndFileName(True)) = False Then Exit Sub
Dim oDoc As Document = ThisDoc.FactoryDocument
Dim oDef As ComponentDefinition = oDoc.ComponentDefinition
Dim oMSs As ModelStates = oDef.ModelStates
Dim oActiveMS As ModelState = oMSs.ActiveModelState
Dim oCProps As PropertySet = oDoc.PropertySets.Item("Inventor User Defined Properties")
Dim oCProp As Inventor.Property = Nothing
Try
	oCProp = oCProps.Item("CProp1")
Catch
	oCProp = oCProps.Add("", "CProp1")
End Try
If oCProp Is Nothing Then Exit Sub
oMSs.MemberEditScope = MemberEditScopeEnum.kEditActiveMember
For Each oMS As ModelState In oMSs
	oMS.Activate
	Dim oVal As String = ""
	oVal = oCProp.Value.ToString
	MsgBox("While " & oMS.Name & " is active, CProp1 = " & oVal, , "")
	oCProp.Value = "Testing - " & oMS.Name
	oVal = oCProp.Value
	MsgBox("While " & oMS.Name & " is active, CProp1 changed to: " & oVal, , "")
Next
oActiveMS.Activate

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes

GosponZ
Collaborator
Collaborator

Thank you for looking into this . Just run rule in assembly and in assembly iproperties create custom CProp1  and value for that one but nothing happening at part level. There is no error either.

0 Likes

GosponZ
Collaborator
Collaborator

OK on part level working fantastic, but can you change MsgBox with input box. I need to enter number for that custom ipropertie.

0 Likes

WCrihfield
Mentor
Mentor
Accepted solution

Sure.  I simply changed these lines:

Dim oVal As String = ""
oVal = oCProp.Value.ToString
MsgBox("While " & oMS.Name & " is active, CProp1 = " & oVal, , "")
oCProp.Value = "Testing - " & oMS.Name
oVal = oCProp.Value
MsgBox("While " & oMS.Name & " is active, CProp1 changed to: " & oVal, , "")

...like this:

Dim sPrompt As String = "While " & oMS.Name & " is active, CProp1 = "
Dim oNewVal As String = InputBox(sPrompt, "CProp1 Value Per ModelState", oCProp.Value.ToString)
oCProp.Value = oNewVal
MsgBox("While " & oMS.Name & " is active, CProp1 changed to: " & oNewVal, , "")

...and here is the result.

If ThisApplication.FileManager.IsInventorComponent(ThisDoc.PathAndFileName(True)) = False Then Exit Sub
Dim oDoc As Document = ThisDoc.FactoryDocument
Dim oDef As ComponentDefinition = oDoc.ComponentDefinition
Dim oMSs As ModelStates = oDef.ModelStates
Dim oActiveMS As ModelState = oMSs.ActiveModelState
Dim oCProps As PropertySet = oDoc.PropertySets.Item("Inventor User Defined Properties")
Dim oCProp As Inventor.Property = Nothing
Try
	oCProp = oCProps.Item("CProp1")
Catch
	oCProp = oCProps.Add("", "CProp1")
End Try
If oCProp Is Nothing Then Exit Sub
oMSs.MemberEditScope = MemberEditScopeEnum.kEditActiveMember
For Each oMS As ModelState In oMSs
	oMS.Activate
	Dim sPrompt As String = "While " & oMS.Name & " is active, CProp1 = "
	Dim oNewVal As String = InputBox(sPrompt, "CProp1 Value Per ModelState", oCProp.Value.ToString)
	oCProp.Value = oNewVal
	MsgBox("While " & oMS.Name & " is active, CProp1 changed to: " & oNewVal, , "")
Next
oActiveMS.Activate

If you need the rule to dig down into all the referenced documents (or components [one level or all levels]) of the assembly then just let me know.  When both the main assembly, and some of the components within the assembly have multiple ModelStates involved, that's when things start getting pretty complicated, and a lot more code is necessary.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

GosponZ
Collaborator
Collaborator

You are the Man. Thank you so much.

From assembly would be great but this is also fantastic and working as charm.

0 Likes

WCrihfield
Mentor
Mentor
Accepted solution

Also, just in case you were not already aware, if you want to manually change the values of things like parameters or properties for multiple ModelStates quickly, and see all the data in one place, you can right-click on the ModelStates folder in your model browser tree, then choose 'Edit via spreadsheet...'.  That will open Microsoft Excel showing all that data laid out in a table.  Each row (other than the top one) is for another ModelState, and each column is for things like parameters and properties.  There is also API code access to all the ModelStates data in table or spreadsheet format.

ModelStates.ModelStateTable 

ModelStates.ExcelWorkSheet 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)