Text String as input parameter

2136
7
Jump to solution
04-01-2013 05:59 AM
EricStarn2
New Contributor
Hello All,

I have a model that runs a two different processes and I would like to have which process to be run based on a user input.
I have done this before by using a feature dataset allowing the user to select a point on the map to run model however, for this I would like to have the user select a text string from a list if possible (i.e. Ammonia, pH, VSS, COD). Then pass that text value to the model and have it run the desired process based on that selection.

So I am guessing that somehow I need to have the model take the value and test it and run the right process. But, I am not sure if this can be done or exactly how.
I have attached a graphic of the model.

Thanks

Eric
0 Kudos
1 Solution

Accepted Solutions
curtvprice
MVP Esteemed Contributor
"%Ammonia%" I think this is correct.


Not exactly. The value of your string variable (let's call it Compound) should be set to Ammonia (no quotes). Then you can refer to it elsewhere in the model as %Compound%, this will be replaced by the string Ammonia at runtime. So a CV  expression "%Compound%" == "Ammonia" (quotes included) becomes "Ammonia" == "Ammonia" when CV is executed.

Our example, just worries about true and false. However, you can also use model variables in pathnames and other tool input expressions. You can see how CV is very handy to create strings that are ready to stick into a tool input parameter or a path. CV's output can be set to a multitude of data types; this make it possible to connect its output to a tool that expects that particular data type.

The way you set up preconditions is to grab the magic wand tool, click on a data element/variable you want set or created before a tool runs, then drag to the tool, let go, and pick "Precondition" from the list. Preconditions can also be set by right-clicking on a tool and going to the Precondition tab and adding an element there.

View solution in original post

0 Kudos
7 Replies
JakeSkinner
Esri Esteemed Contributor
Hi Eric,

This can be accomplished through python.  You can easily export your model out to python in model builder by clicking Model > Export > To Python.  Then you would just need to update the python script.  For example:

import arcpy
string = arcpy.GetParameterAsText(0)
if string == "Ammonia":
    <code>

if string == "pH":
    <code>

if string == "VSS":
    <code>

if string == "COD":
    <code>


You would want to replace <code> with the exported python code from model builder that you would like executed when the user selects the string value.  After you are finished updating the python file, you can add the script back to your toolbox (right-click on your toolbox > Add > Script). 

Once the script is added, right-click on the script > Properties > Parameters tab.  Set up a parameter with a data type of 'String' and set the 'Filter' property to 'Value List'.  You can then add the required string values:

[ATTACH=CONFIG]23114[/ATTACH]

Once this is set up, when user's execute the script they will be presented with a drop down of values:

[ATTACH=CONFIG]23115[/ATTACH]
0 Kudos
EricStarn2
New Contributor
Thanks that helps

However maybe just a little more help.

I export the model into python and it is group a little different then I expected.
So if you can help me in where I need to put your suggested text.

I attached the file

Thanks again

Eric
0 Kudos
curtvprice
MVP Esteemed Contributor
Eric, Python scripting makes sense when things get complicated, epecially if you need more control over error messages and tool validation.

However, there are several approaches within Model Builder, some involving a script tool, some just using Calculate Value. Here's my favorite method, using the "model-only" Calculate Value tool, to calculate a Boolean (true/false) value and connecting its output as preconditions to the processing chains. In this example, I use the Merge Branch tool to provide the same output whether I use one option or the other.

[ATTACH=CONFIG]23118[/ATTACH]

I highly recommend this set of blog posts:

ArcGIS blog: if you are stuck at "if"  (5 parts)

Using Calculate Value, filters, and preconditions, you can build some pretty sophisticated tools within ModelBuilder.
0 Kudos
EricStarn2
New Contributor
Curtis,

That looks like just what I am looking for.

I read through the blog but am still unsure about this:

Using your attachment as reference I can see you take a string variable and check to see what the string is using the "Calculate Value" tool and then set the data type to Boolean
But I am not quite sure what to put in the code block or expression field and string variable a precondition to the "Calculate Value" tool?

Thanks

Eric
0 Kudos
curtvprice
MVP Esteemed Contributor
Using your attachment as reference I can see you take a string variable and check to see what the string is using the "Calculate Value" (CV) tool and then set the data type to Boolean. But I am not quite sure what to put in the code block or expression field and string variable a precondition to the "Calculate Value" tool?


In the CV tool, you fill in the expression with the one I put in the label:

"%String%" == "A"

Leave the CV code block blank. If the test were complicated, you would put a function in the code block. but in this case you do not need it.

String is the name of a data element in your model. The value of String gets put there before Python sees it, so it evaluates at run time to:

"A" == "A"

which returns 1 which is returned as True if you set the output type to Boolean.

Then grab the magic wand tool and connect the output of Calculate Value to the first tool in a process chain you want to run when "%String%" == "A". Select "Precondition" from the context menu.

Do this the same with the other one ("%String%" == "B"). Set the output as a precondition to processing chain B. If this evaulates to false ("B" == "A" - is false), the second processing chain will not run.

Neat, isn't it?

Hope this helps.
0 Kudos
EricStarn2
New Contributor
Curtis,

That is perfect
I have now built most of the model

but I do still need maybe a little more clarification on the data element for the model.

I created a String Variable and then set it to "%Ammonia%" I think this is correct.

Then what is the correct way to connect it to the CV tool?

Environments
Precondition
etc.

Please let me know if I am way off track

Thanks immensely for your help

Eric
0 Kudos
curtvprice
MVP Esteemed Contributor
"%Ammonia%" I think this is correct.


Not exactly. The value of your string variable (let's call it Compound) should be set to Ammonia (no quotes). Then you can refer to it elsewhere in the model as %Compound%, this will be replaced by the string Ammonia at runtime. So a CV  expression "%Compound%" == "Ammonia" (quotes included) becomes "Ammonia" == "Ammonia" when CV is executed.

Our example, just worries about true and false. However, you can also use model variables in pathnames and other tool input expressions. You can see how CV is very handy to create strings that are ready to stick into a tool input parameter or a path. CV's output can be set to a multitude of data types; this make it possible to connect its output to a tool that expects that particular data type.

The way you set up preconditions is to grab the magic wand tool, click on a data element/variable you want set or created before a tool runs, then drag to the tool, let go, and pick "Precondition" from the list. Preconditions can also be set by right-clicking on a tool and going to the Precondition tab and adding an element there.
0 Kudos