CYCLE THRU MODEL STATES OF PART AND UPDATE CUSTOM PROPERTIES

CYCLE THRU MODEL STATES OF PART AND UPDATE CUSTOM PROPERTIES

tmathieson
Advocate Advocate
1,681 Views
7 Replies
Message 1 of 8

CYCLE THRU MODEL STATES OF PART AND UPDATE CUSTOM PROPERTIES

tmathieson
Advocate
Advocate

Hi All,

 

i am redoing a part from iFactory to a Model State format (yes, we are sure we want to use Model States).  there are custom properties that are unique to each Model State.  how do i cycle thru each Model State in the Part?

 

basically i am looking for the code/snippet that would replace this section i had to access the Ifactory table in the old part

 

Dim oRow As iPartTableRow
For Each oRow In oFactory.TableRows
	' Generate the member and file, (overwrites member file or creates new file)
	'oFactory.CreateMember(oRow)

	oFactory.DefaultRow = oRow
	 t123= iFeature.CurrentRowStringValue("PART_ID")
 t456= iFeature.CurrentRowStringValue("PART NUmber")

MESSAGEBox.SHOW (t123,"PART_ID")
	
Next 

  i have looked for, but can find the statement/function/code to cycle thru the model state but can't find any.any suggestions or leads would be much appreciated!

0 Likes
Accepted solutions (1)
1,682 Views
7 Replies
Replies (7)
Message 2 of 8

Curtis_Waguespack
Consultant
Consultant

Hi @tmathieson ,

 

I've not done much with model states yet, but here is a basic example of accessing the table rows.

 

If you look in the Inventor API help under ModelStateTable you might find more information to help.

 

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

 

Dim oDoc As PartDocument
oDoc = ThisApplication.ActiveDocument
    
Dim oDef As PartComponentDefinition
oDef = oDoc.ComponentDefinition
    
Dim oStates As ModelStates 	
oStates = oDef.ModelStates

Dim oTable As ModelStateTable	
oTable = oStates.ModelStateTable

Dim oRow As ModelStateTableRow 
For Each oRow In oTable.TableRows
    MessageBox.Show(oRow.MemberName)

Next

 

EESignature

0 Likes
Message 3 of 8

Curtis_Waguespack
Consultant
Consultant

Hi @tmathieson 

 

I stopped too quick... on further inspection, this is how we access the part number iproperty for each model state row in a part.

 

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

 

 

 

Dim oDoc As PartDocument
oDoc = ThisApplication.ActiveDocument
    
Dim oDef As PartComponentDefinition
oDef = oDoc.ComponentDefinition
    
Dim oStates As ModelStates 	
oStates = oDef.ModelStates

Dim oTable As ModelStateTable	
oTable = oStates.ModelStateTable

Dim oRow As ModelStateTableRow 
i=1
For Each oRow In oTable.TableRows
    'MessageBox.Show(oRow.MemberName)
	oStates.Item(i).Activate
	 MessageBox.Show(iProperties.Value("Project", "Part Number"))
	i=i+1
		

Next

 

 

 

EESignature

0 Likes
Message 4 of 8

tmathieson
Advocate
Advocate

Thanks, Curtis!!

just what I'm looking for (i think).  never thought to look at the API doc.

 

thanks again!!!

0 Likes
Message 5 of 8

tmathieson
Advocate
Advocate

Well, i might of hit Solved too soon....

..

here is my code snippet. it works, kind of... i can cycle thru the model States, but can't update the custom properties. it seems that the rule is taken the parameters,PLATE_TH_MM * 10 + CStr(PLATE_LENGTH) from the State that i have Active  when starting the rule,and is using those for all the states, even though i change states before updating the custom properties.  is there something i need to do to force the rule to take the parameters from the current Active state?

 

i have tried using this snippet, but can't get the proper type for the 1st argument (oDef)

 

'iProperties.InstanceValue(oDef, "PART_ID")="BPFGL" + CStr(PLATE_TH_MM * 10) + CStr(PLATE_LENGTH) + "N"

 

 

thanks in advance for any insight!!

Dim oDoc As PartDocument
oDoc = ThisApplication.ActiveDocument
    
Dim oDef As PartComponentDefinition
oDef = oDoc.ComponentDefinition
    
Dim oStates As ModelStates 	
oStates = oDef.ModelStates

Dim oTable As ModelStateTable	
oTable = oStates.ModelStateTable

Dim oRow As ModelStateTableRow 
Dim MODEL_NAME As String

' Iterate All Rows of model states

oTable = oStates.ModelStateTable


i=1
For Each oRow In oTable.TableRows
	MODEL_NAME = oStates.Item(i).Name()
	ThisDoc.ActiveModelState = MODEL_NAME
	MessageBox.Show (MODEL_NAME, "STATE")

	 MessageBox.Show(iProperties.Value("Project", "Part Number"))
	
'MessageBox.Show(MODEL_NAME)
iProperties.Value("Project", "Part Number") = "BPFGL" + CStr(PLATE_TH_MM * 10) + CStr(PLATE_LENGTH) + "N"
iProperties.Value("Custom", "PART_ID") = "BPFGL" + CStr(PLATE_TH_MM * 10) + CStr(PLATE_LENGTH) + "N"

'iProperties.InstanceValue(oDef, "PART_ID")="BPFGL" + CStr(PLATE_TH_MM * 10) + CStr(PLATE_LENGTH) + "N"

If Feature.IsActive("HALF-PLATE-CUT") = True Then
	iProperties.Value("Project", "Part Number") = "BPFGL" + CStr(PLATE_TH_MM * 10) + CStr(PLATE_LENGTH) + "NX"
	iProperties.Value("Custom", "PART_ID") = "BPFGL" + CStr(PLATE_TH_MM * 10) + CStr(PLATE_LENGTH) + "NX"
End If	
i=i+1
Next 

 

 

0 Likes
Message 6 of 8

Curtis_Waguespack
Consultant
Consultant

Hi @tmathieson 

 

I marked the previous "solution" unsolved for you.

 

Here's a version of your code... I did a little bit of condensing so it'll be easier for you to see how things are working, but please check it to make sure I haven't goofed up your part numbers, etc.

 

Edit: Also I added a bit to make the model state name match the part number, in case that's of interest... but this could get you in trouble if you have 3 states and two have the same part number so this code adds a prefix to the model state names

 

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

 

 

 

 

 

 

Dim oDoc As PartDocument
oDoc = ThisApplication.ActiveDocument
    
Dim oDef As PartComponentDefinition
oDef = oDoc.ComponentDefinition
    
Dim oStates As ModelStates 	
oStates = oDef.ModelStates

Dim oTable As ModelStateTable	
oTable = oStates.ModelStateTable

Dim oRow As ModelStateTableRow 
Dim MODEL_NAME As String


oTable = oStates.ModelStateTable

'get the current model state, so we can return to it when done
oCurrentState = ThisDoc.ActiveModelState

' Iterate All Rows of model states
i = 1
For Each oRow In oTable.TableRows
	'this activates the row to make it the current state
	oStates.Item(i).Activate 
	
		
	'set the default PN and PID values	
	oPN = "BPFGL" + CStr(PLATE_TH_MM * 10) + CStr(PLATE_LENGTH) + "N"


	'append the value with X if the feature is active
	If Feature.IsActive("HALF-PLATE-CUT") = True Then
		iProperties.Value("Project", "Part Number") =  oPN + "X"
	Else
		iProperties.Value("Project", "Part Number") = oPN 
	End If	
	
	'set ID to match PN
	iProperties.Value("Custom", "PART_ID") = iProperties.Value("Project", "Part Number")

	'set the State name to match the part number
	Try
	oStates.Item(i).Name = "(MS" & i-1 & ") " & iProperties.Value("Project", "Part Number")
	Catch
		'catch error when trying to rename Master
		'or when the state name is already in use
	End Try
	
	oMsg1 = "Model State: " & oStates.Item(i).Name
	oMsg2 = "Part Number: " & iProperties.Value("Project", "Part Number")
	MessageBox.Show(oMsg1 & vbLf & oMsg2)
	
	i=i+1
Next

'set original state active again
oStates.Item(oCurrentState).Activate 

 

 

 

 

 

 

EESignature

0 Likes
Message 7 of 8

tmathieson
Advocate
Advocate

Hi Curtis,

 

thanks for helping out with this.  i copied your code into a new rule and ran it, but i am getting the same results as before...  the attachment MS-1 is the excel spreadsheet after running the program, MS-2 is my original xcel file, and the red cells are how the Part_ID and Part Number should look.  however, they all come out with the same value instead of updating with the individual plate lengths. it is finding if the plate is cut or not, and adding the "X" correctly...

 

should i be using this code... cant tell from the documentation if it for Model States or something completely different

'iProperties.InstanceValue(oDef, "PART_ID")="BPFGL" + CStr(PLATE_TH_MM * 10) + CStr(PLATE_LENGTH) + "N"

 I've also attached a simplified version of my model if you want to have a look.

 

and thanks again!!  I've done some custom programming in the past (FORTRAN,Basic, (giving away my age :^})), but am struggling  with VBA. time for a refresher course..

 

thanks again!

Message 8 of 8

Curtis_Waguespack
Consultant
Consultant
Accepted solution

Hi @tmathieson 

 

ok that helped me see the issue... my test file was just cramming some values into the thickness and length parameters, so I wasn't seeing the issue with those numbers not updating.

 

So your original code was using 

"BPFGL" + CStr(PLATE_TH_MM * 10) + CStr(PLATE_LENGTH) + "N"

 

changing the parameters from this format:

PLATE_TH_MM

to this format, seems to do the trick

Parameter(PLATE_TH_MM)

 

I'm not exactly sure why changing it from a dynamic syntax to a static syntax made a difference here, but that appears to be the case

 

Also I changed the + to & and then Cstr wasn't needed 

 

One more thing that might help is to use the iLogic Log rather than message boxes to return the values... it's just a faster way to debug, see this link for how to turn that on and use it:

To Create Log Statements | Inventor 2019 | Autodesk Knowledge Network

 

You're doing fine with the programming part of it, some of this stuff is just not real clear in ilogic because of the lack of a proper debugger

 

Give this version a try and post back if you're still seeing issues.

 

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

 

Dim oDoc As PartDocument
oDoc = ThisApplication.ActiveDocument
    
Dim oDef As PartComponentDefinition
oDef = oDoc.ComponentDefinition
    
Dim oStates As ModelStates 	
oStates = oDef.ModelStates

Dim oTable As ModelStateTable	
oTable = oStates.ModelStateTable

'get the current model state, so we can return to it when done
oCurrentState = ThisDoc.ActiveModelState

' Iterate All Rows of model states
i = 1
Dim oRow As ModelStateTableRow 
For Each oRow In oTable.TableRows
	'this activates the row to make it the current state
	oStates.Item(i).Activate 
	
	'get the current parameter values
	oTK = Parameter("PLATE_TH_MM") * 10
	oLN = Parameter("PLATE_LENGTH")
	
	'set the default PN 	
	oPN = "BPFGL" & oTK & oLN & "N"

	'append the value with X if the feature is active
	If Feature.IsActive("HALF-PLATE-CUT") = True Then
		iProperties.Value("Project", "Part Number") =  oPN + "X"
	Else
		iProperties.Value("Project", "Part Number") = oPN 
	End If	
	
	'set ID to match PN
	iProperties.Value("Custom", "PART_ID") = iProperties.Value("Project", "Part Number")

	
	oMsg1 = "Model State: " & oStates.Item(i).Name
	oMsg2 = "Part Number: " & iProperties.Value("Project", "Part Number")
	Logger.Info(oMsg1)
	Logger.Info(oMsg2)
	Logger.Info("----------------------")
	'MessageBox.Show(oMsg1 & vbLf & oMsg2)
	
	i=i+1
Next

'set original state active again
oStates.Item(oCurrentState).Activate 

 

 

EESignature