How can I access a components model state table from the assembly

How can I access a components model state table from the assembly

ASklarEKLL3
Contributor Contributor
369 Views
5 Replies
Message 1 of 6

How can I access a components model state table from the assembly

ASklarEKLL3
Contributor
Contributor

I'm fairly new to ilogic so please bare with me on this.

 

I need code that will look at 2 parameters from a specific component in my assembly and test to see if they match a condition, and do that for each model state in that component. If it finds a match it should then change the component to that model state. If it doesn't find a match, it should display a message. 

 

Now, I know I could keep changing the model state of the component then check the parameters until it does or doesn't match, but it takes a while to change the model state of this component and there's a lot of them, so I don't want to do it that way if I don't have to.

 

But either way, I don't know how to deal with model states without having to specify the name of the model states, which is what I want to avoid since there will be more model states added later I don't want to have to modify my code every time to include the new ones.

 

Any help is appreciated.

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

dalton_northwind
Enthusiast
Enthusiast

Hello,

 

It sounds like you are looking for the 'ModelStateTable' object. Heres a quick example that looks at each column then row:

Dim oPDoc As PartDocument = ThisDoc.Document

Dim oModelStateTable As ModelStateTable = oPDoc.ComponentDefinition.ModelStates.ModelStateTable

For Each oColumn As ModelStateTableColumn In oModelStateTable.TableColumns
	MessageBox.Show(oColumn.Heading, oColumn.ReferencedDataType.ToString)
	For i = 1 To oModelStateTable.TableRows.Count
		MessageBox.Show(oColumn.Item(i).Value)
	Next
Next

 

I don't like dealing with model states unless it's a perfect use case. This sounds like it could get complicated w/ searching for matching values. Anything that doesn't use just the model state name and the 'Link Model States' button in the assembly should just be managed by ilogic rules instead of model states imo.

0 Likes
Message 3 of 6

ASklarEKLL3
Contributor
Contributor

Thank you for the reply.

 

That does seem to be on the right track, but I can only get that code to work within the component file itself, not the top level assembly where I need it. Is there any way of accessing the component's model state table without having to be in the component? Or how would I go into the component and access the table from the rule?

 

Thank you.

0 Likes
Message 4 of 6

dalton_northwind
Enthusiast
Enthusiast
Accepted solution

You'll need to access the ComponentOccurrence first, then it works mostly the same. ex:

Dim oADoc As AssemblyDocument = ThisDoc.Document

Dim oOcc As ComponentOccurrence = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kAssemblyOccurrenceFilter, "select component")

'For Each oSubOcc As ComponentOccurrence In oOcc.SubOccurrences
'	MessageBox.Show(oSubOcc.Name)
'Next

Dim oPartCompDef As PartComponentDefinition = oOcc.Definition

'Dim oFactoryDocument As PartDocument = oPartCompDef.FactoryDocument.ComponentDefinition.FactoryDocument

'Dim oModelStateTable As ModelStateTable = oFactoryDocument.ComponentDefinition.ModelStates.ModelStateTable

Dim oModelStateTable As ModelStateTable = oPartCompDef.ModelStates.ModelStateTable

For Each oColumn As ModelStateTableColumn In oModelStateTable.TableColumns
	MessageBox.Show(oColumn.Heading, oColumn.ReferencedDataType.ToString)
	For i = 1 To oModelStateTable.TableRows.Count
		MessageBox.Show(oColumn.Item(i).Value)
	Next
Next

 

Message 5 of 6

ASklarEKLL3
Contributor
Contributor

Thank you! this is what I needed. I got my code to work using your example.

Btw, is there a quick way to get it to return the numeric values of the parameters instead of strings? It was kind of awkward having to cut off the unit string and convert them to doubles.

0 Likes
Message 6 of 6

dalton_northwind
Enthusiast
Enthusiast

I think you'll always have to convert them from strings using the table values. The unit string is always after a " " space so you could split them first. ex:

If oColumn.ReferencedDataType = kParameterValueColumn
			MessageBox.Show(CDbl(oColumn.Item(i).Value.ToString.Split(" ")(0)))

		End If