Create model states from list

Create model states from list

creggo
Enthusiast Enthusiast
1,079 Views
5 Replies
Message 1 of 6

Create model states from list

creggo
Enthusiast
Enthusiast

Hi folks,

 

Looking for some help to add predefined model states to part files using an ilogic rule.

 

Example here, model states with different materials, the Member name is used as the suffix in the part number, the Primary model is by default always Generic with /XX in the part number.

 

MemberMaterial [Physical]<material></material>Part Number [Project]
[Primary]GenericMS Test Part/XX
ALAluminum 6061MS Test Part/AL
COPCopperMS Test Part/COP
IRONIron, CastMS Test Part/IRON
NINickel-Copper Alloy 400MS Test Part/NI
SSStainless SteelMS Test Part/SS
TITitaniumMS Test Part/TI

 

We use the same material options for a huge range of components, having an all in one rule to create the model state options would save so much time.

 

Hope that makes sense, thanks!

Craig

 

 

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

CattabianiI
Collaborator
Collaborator
Accepted solution

It's something like that
you need to add the other model states data in the list of tuples.

 

Dim msRows As New List(Of Tuple(Of String, String, String)) ()
' Define model states: name, material, part number
msRows.Add(New tuple(Of String, String, String)("AL", "Aluminum 6061", "MS Test Part/AL"))
'msRows.Add(New tuple(Of String, String, String)("ms2", "Aluminum 6061", "MS2"))

Dim doc As Document = ThisApplication.ActiveDocument
Dim mss As ModelStates = doc.componentdefinition.modelstates

' Assume active model state is the [Primary]
' Set part number in the active model state
iProperties.Value("Project", "Part Number") = "MS Test Part/XX"

For Each msRow As Tuple(Of String, String, String) In msRows
	' Create model state
	mss.Add(msRow.Item1)
	
	' the newly created model state is now the active one
	
	' Set iProperties in the active model state
	iProperties.Material = msRow.Item2
	iProperties.Value("Project", "Part Number") = msRow.Item3
Next

 

Message 3 of 6

creggo
Enthusiast
Enthusiast

Hi @CattabianiI, thank you so much, it works great! Not seen tuple before in ilogic, very intrigued 🤓

 

I've added the existing part no. as a string and then added the material suffix after, seems to be working okay. 

 

I'm wondering if you could help with 1 further tweak though. I've added in the last line to go back to Primary once done. I'd ideally like to ensure it starts on Primary as well, due to line 19. However if I add that same line higher in the code, it wont work on a part that has no existing mode states already. In other words, how can I ensure it always starts with Primary, whether there is existing model states or not?

Dim msRows As New List(Of Tuple(Of String, String, String))()
' Define model states: name, material, part number
msRows.Add(New tuple(Of String, String, String)("AL", "Aluminum 6061", "/AL"))
msRows.Add(New tuple(Of String, String, String)("COP", "Copper", "/COP"))
msRows.Add(New tuple(Of String, String, String)("IRON", "Iron, Cast", "/IRON"))
msRows.Add(New tuple(Of String, String, String)("NI", "Nickel-Copper Alloy 400", "/NI"))
msRows.Add(New tuple(Of String, String, String)("SS", "Stainless Steel", "/SS"))
msRows.Add(New tuple(Of String, String, String)("TI", "Titanium", "/TI"))

Dim doc As Document = ThisApplication.ActiveDocument
Dim mss As ModelStates = doc.componentdefinition.modelstates
Dim PartNo As String = iProperties.Value("Project", "Part Number")

'Start from Primary ### Wont work if part has no existing model states??? ####
ThisDoc.ActiveModelState = ThisServer.LanguageTools.CurrentPrimaryModelStateString

' Assume active model state is the [Primary] ### hopefully solved with above ###
' Set part number in the active model state
iProperties.Value("Project", "Part Number") = PartNo & "/XX"

For Each msRow As Tuple(Of String, String, String) In msRows
	' Create model state
	mss.Add(msRow.Item1)
	
	' the newly created model state is now the active one
	
	' Set iProperties in the active model state
	iProperties.Material = msRow.Item2
	iProperties.Value("Project", "Part Number") = PartNo & msRow.Item3
Next

'Go back to Primary once complete
ThisDoc.ActiveModelState = ThisServer.LanguageTools.CurrentPrimaryModelStateString

  

Thanks again, appreciate the help!

Craig

0 Likes
Message 4 of 6

CattabianiI
Collaborator
Collaborator

It's perfect 👌
The primary model state IS a model state and will always been there. You can just add this to make the code cleaner:

'Start from Primary. It it's not the active model state I'll activate it. 
If ThisDoc.ActiveModelState <> ThisServer.LanguageTools.CurrentPrimaryModelStateString then
  ThisDoc.ActiveModelState = ThisServer.LanguageTools.CurrentPrimaryModelStateString
end if

Another useful thing is to check if a model state with that name already exist before creating it.
Otherwise this is something the user who runs the rule have to know.

Message 5 of 6

creggo
Enthusiast
Enthusiast

Spot on, working great now! Thank you again @CattabianiI, appreciate the help! 

0 Likes
Message 6 of 6

WCrihfield
Mentor
Mentor

Also, in my experience so far, the first ModelState in the ModelStates collection is always the original (Master/Primary), because it is always the first one to exist in the document.  You can test this if you want by looking at the FullDocumentName of the document objects returned by the ModelState properties, because it will always contain the name of the ModelState right after the FullFileName portion of the String, within "<" & ">" characters, when it is set to a non-original ModelState.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes