Custom Text Parameter To iProperty

Custom Text Parameter To iProperty

mvilleneuve
Contributor Contributor
3,086 Views
6 Replies
Message 1 of 7

Custom Text Parameter To iProperty

mvilleneuve
Contributor
Contributor

Frist, I am not a programmer or even a designer in any way shape or form.

 

I have, like many, just copied and pasted after google searching to make various rules work for me. Currently I am trying to make a external rule that will have a button on the bar to activate it for either a part or sheet metal:

 

1. Create a custom parameter with a Multivalue text list of material types

2. Run a global form based on that parameter so the user selects the material type

3. Set that to the Parameters Value

4. Take the selected value and set it as the iProperty.Description field

 

After much searching and frankenstiening I have a code that is 90% there. It will create the parameter, run the form, update the parameter value based on the form selection...BUT it won't update it into the iproperty field. If it is run "Fresh" (no parameter listed) the iproperty field will end up being blank OR have the generic selection listed (depending on lines of code are active). If I run a second time, it will update with the selection from the first run and put it into the iproperty field.

 

I assume it is something simple I am missing. i have tried to make a 2nd rule that would activate the form and then grab the value with no success as well. Any help would be greatly appreciated.

 

 

								
'Sets rule to work in open part file
Dim oPDoc As PartDocument = ThisDoc.Document 											
Dim oUParams As UserParameters = oPDoc.ComponentDefinition.Parameters.UserParameters 						'Sets up customer paramter
Dim oUParam As UserParameter													'defines user parameter

					' Creates Custom Parameter if not done.
	Try
	
		oUParam = oUParams.Item("MaterialType")
		oUParam.IsKey = True 'MARK KEY AS ACTIVE-OK

	Catch
	
		oUParam = oUParams.AddByValue("MaterialType", "", UnitsTypeEnum.kTextUnits)
		oUParam.IsKey = True 'MARK KEY AS ACTIVE-OK
		'MultiValue.SetValueOptions(True, DefaultIndex := 0)
		MultiValue.SetList("MaterialType", "Generic", "Commercial Brass", "Commercial Brass & Fixtures", "Commercial Faucet", "Corrosive Waste")
		MultiValue.UpdateAfterChange = True 'Update MultiValue List			
	
	End Try
Dim oMaterialType As String = oUParam.Value

					'End of Paramter Setup
			
'Section to create custom iProperty removed as doesn't appear to be needed for rule to function correctly

iLogicForm.ShowGlobal("Material Selector")
oMaterialType = oUParam.Value

iProperties.Value("Project", "Description") = oMaterialType 'iProperties.Value("Custom", propertyName1)

RuleParametersOutput()
InventorVb.DocumentUpdate()							
iLogicVb.UpdateWhenDone = True 'Update When Complete

 

 

0 Likes
Accepted solutions (1)
3,087 Views
6 Replies
Replies (6)
Message 2 of 7

WCrihfield
Mentor
Mentor

Hi @mvilleneuve.  Trying to keep that iProperty's value up to date with the value of the parameter may require a second, much simpler iLogic rule.  Since text and boolean type user parameters do not have the 'export' or 'expose as iProperty' setting, like the numerical parameters do, if we want to have a custom iProperty that stays up do date with one of those types of parameters, we need to use a technique that will 'trigger' an iLogic rule to run every time the value of that "MaterialType" parameter changes.  Then the iLogic rule that is triggered to run will set the value of the iProperty based on the value of the parameter, after that change.  One technique to do that, is to use a 'local' iLogic rule (saved within the same document as the parameter), and include the unquoted name of that local parameter within that iLogic rule.  That will cause that local rule to be triggered to run, when the value of that parameter changes.  Here is a rule like that:

oTrigger = MaterialType
InventorVb.DocumentUpdate()
iProperties.Value("Project", "Description") = MaterialType
InventorVb.DocumentUpdate()

The first line is an example of how you can set this type of trigger up, if the rest of the code did not contain that unquoted parameter name.  But in this case, we do have the unquoted name of the parameter later within the code, so that line would technically not be needed, but it will not hurt anything.  I am updating the document both before and after the parameter value change event happens, just to be sure that the value is correct when we write it to the iProperty.  Give this a try.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 7

mvilleneuve
Contributor
Contributor

Thanks for the information @WCrihfield .

I made a local rule with your code and here are the results:

 

1. If I have this line in my external rule active "MultiValue.SetValueOptions(True, DefaultIndex := 0)" on the first run of the external rule and form the iproperty with list Generic, regardless of the selected material (which I can understand based on the command). Each change after the initial run will then result in the iproperty being the previous selection not the newest choice.

 

i.e 1st time I pick corrosive waste, the iproperty = Generic, 2nd time I select Commercial Brass and iproperty = corrosive waste

 

2: If I leave the comment out the SetValueOption and run the rule fresh then on the first run the iproperty field will still be blank, on subsequent runs of the rule still show the previous selection in the iproperty field

 

I see the ilogic adds this in to the top of the local rule after it is run 

MaterialType = "Corrosive Waste"' deleted parameter

 

I also tried to activate the local rule from the external rule as the last line of code but it just failed to move anything into the iproperty field

iLogicVb.RunRule("Rule0")

 

Should I even continue to try this using the MultiVale.Setlist? Would some form of array work easier? Or calling in the variables from a external spread sheet?

 

To my uneducated mind I think the flow of my original code makes sense but just cant get it to update.

 

Any other thoughts or workflows to achieve this? 

 

0 Likes
Message 4 of 7

WCrihfield
Mentor
Mentor
Accepted solution

Is your form 'Modal'?  Do you know what 'Modal' means?  If the form is set to modal, then when it shows, it will receive top focus while it is being shown, and you can not do other things while it remains showing.  If it is not set to modal, after it shows, you can leave it showing while you do other things.  I believe this is going to be an important setting if you intend to keep the bit of code in your main rule that writes the changed parameter's value to the custom iProperty.  The reason is that, if the form is not set to modal, or you don't call it to be shown modal by the line of code, then your other code after that point will go ahead and process while the form is still showing, which means you haven't finished using the form yet, but that other code that writes the parameter value to the iProperty has already ran.  But if the form is modal, or is called to be shown modal when called to show, then the form will show, it will wait for you to finish with the form, then it will finish processing the rest of the rule.  We want that last process in your situation.  So, you can either make the Form modal by editing the Form, then changing the setting within the editor to True, or you can change the line of code that calls the form to show, and add the second input to it, which specifies if you want the form to show modal or non-modal.

 

We should only need one code writing the parameter's value to the iProperty.  So if you want to leave that in your main external rule, then you should get rid of the new local rule you made above.  Or, you can get rid of that part from the end of your main rule, then only use that local rule to keep the iProperty up do date.  It's up to you.  If you only have it in your main rule, and get rid of the local rule, then the iProperty will only be updated when you run that main rule.  But if that parameter's value changes by other means, lets say you use the form to change it, or the parameters dialog to change it, that iProperty will not be updated, because the only way to update it is to use that main code.  However, if you leave the local rule, then every time the parameter's value changes, no matter how it changes, the iProperty will automatically be updated by that local rule.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 5 of 7

mvilleneuve
Contributor
Contributor

@WCrihfield  thank you so much, this was causing me to pull out the little bit of hair I have out...

 

It was the modal setting. This was my first foray into using forms and had no clue about that setting. I have tested it multiple times now and it appears to work perfectly 

 

Thank you for help

0 Likes
Message 6 of 7

WCrihfield
Mentor
Mentor

I created a simple new part file just to test this functionality out on, as an example.  I created a 'local' form and local rules though, instead of a global form and external rule, to keep it simple and all in one place for testing.  And I chose the 2 rule route, because I believe that will be the best option to keep the iProperty accurate.  I gave the form the same name as yours, and just included the one parameter, as a drop-down list.  I created the first local iLogic rule and put the code into it to create the multi-value text type user parameter, followed by the line of code to show the form, followed by some code to either find or create the local rule we need.  If it is found, it runs it, but if it is created, it is automatically ran when it is created.  When I run the first rule, it creates the parameter just fine, then shows the form, then allows be to take my time choosing a value, then proceeds to either run, or create than run the local rule, which updates (and keeps updated) the iProperty.

 

Now to use this, first get rid of the other local rule in the document for keeping this iProperty up to date, if you have one.  When you have a local rule which contains the unquoted name of a known local parameter, then you delete that parameter, the local rule immediately knows about it, and adds that little note at the top of it, which tells you that the parameter has been deleted.

Here is the new main external rule code you should be able to use to make all of this work.

 

Dim oPDoc As PartDocument = ThisDoc.Document 											
Dim oUParams As UserParameters = oPDoc.ComponentDefinition.Parameters.UserParameters
Dim oUParam As UserParameter
For Each oP As Inventor.Parameter In oUParams
	If oP.Name = "MaterialType" Then
		oUParam = oP
		Exit For
	End If
Next
If oUParam Is Nothing Then
	oUParam = oUParams.AddByValue("MaterialType", "", UnitsTypeEnum.kTextUnits)
	Dim oVals() As String = {"Generic", "Commercial Brass", "Commercial Brass & Fixtures", "Commercial Faucet", "Corrosive Waste"}
	oUParam.ExpressionList.SetExpressionList(oVals)
End If
oUParam.IsKey = True
oPDoc.Update
'<<<< CHANGE THIS...I WAS USING A LOCAL FORM >>>>
iLogicForm.ShowGlobal("Material Selector", FormMode.Modal)
'iLogicForm.Show("Material Selector", FormMode.Modal)
oAuto = iLogicVb.Automation
Dim oRule As iLogicRule
oRule = oAuto.GetRule(oPDoc, "Update MaterialType")
If oRule Is Nothing Then
	oRuleText = "ThisDoc.Document.PropertySets.Item(""Design Tracking Properties"").Item(""Description"").Value = MaterialType"
	oRule = oAuto.AddRule(oPDoc, "Update MaterialType", oRuleText)
Else
	oAuto.RunRuleDirect(oRule)
End If

 

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

If you want and have time, I would appreciate your Vote(s) for My IDEAS 💡 or you can Explore My CONTRIBUTIONS

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 7 of 7

mvilleneuve
Contributor
Contributor

@WCrihfield 

 

I will use your latest post as an experiment and play around with it when I have some free time. I normally use external rules only just due to how I structure my work and my lack of experience/understanding regarding illogic/VBA

 

That being said, since you last post regarding my form and the "modal" issue I have been able to get the rule to run the way I wanted it to, and now it uses an excel sheet to pull the material list from, and this material rule is now activated when I run other rules inside  a part or sheet metal.

 

As this point I am going to get back to some "real" work, but I appreciate all the help you gave me on this issue.

 

Also a thanks to everyone who helps others on this board. Even though this was my first post I have learned a lot from reading other peoples problems and solutions

0 Likes