Passing Variable to Nested Iterator

1404
6
10-17-2017 12:07 AM
SimonGee
New Contributor II

I am processing a large set of data to do least cost path analysis from a lot of point sources.  To speed things up I am chopping up all of the source data into a series of geodatabases.  My LCP model iterates through each point in the gdb and solves the path.

What I want to do is iterate through the gdbs (244 No.) and store the results just in that gdb (I'll put the data back together later).  I may just be being a bit dim, but I can't work out how to pass the name of the current workspace into my sub-model for it to pick up the inputs it needs.

I have exposed my sub-model inputs so that they show up in my main model, 

And made them like this,

But this clearly isn't correct as it doesn't validate.  If I run just the iterator in the main model and then set these inputs manually to exactly the same as above (but don't validate it), the sub-model does run for the last workspace.

I've thought about simplifying it so that I only need to get my first input parameters from the main model and can then get the workspace for this in the sub-model, and whilst this would look neater but I still have the same initial problem of getting the workspace variable into that parameter.

I suspect I am missing something really obvious!!

Simon

0 Kudos
6 Replies
DuncanHornby
MVP Notable Contributor

So the Parameter from your sub-model called PathResults is used as a workspace in the sub-model? If you want to use the current workspace that the iterator is spitting out then you need only type into PathResults the inline substitution %Workspace%. Currently it appears you have the output of the iterator as a precondition to your sub-model (dashed line), you do not need that.

0 Kudos
SimonGee
New Contributor II

I have tried to simplify this, but still not quite got it working.

I have created a Workspace variable in my sub-model as a Parameter and referred everything else in that model to %workspace%

If I add the sub-model to the main model I can connect the Iterator output to it as Workspace.  If my main model has already been run (so the GDB_Name has a value) before I add in the sub-model, it is all OK, and the sub-model will run, but obviously only on the last Workspace the iterator found.

If I then Validate the main model, if breaks the sub-model.  Is this because there is no longer a value in GDB_Name to act as the Workspace variable for the sub-model?  If so, how do I get around this?  I still think I'm missing something blindingly obvious!!

Simon

0 Kudos
DuncanHornby
MVP Notable Contributor

In your original model the iterator is creating an output named Workspace so you use %Workpace% to pass this value into the sub model parameter. You have now renamed that to GDB_Name so to pass that you would use %GDB_Name% as that is what you are now calling it. Worth reading the Help file on inline substitution.

SimonGee
New Contributor II

Duncan

Thanks for the swift response. I started with that, and have now changed it back to Workspace, but it doesn’t make any difference. The behaviour is exactly the same.

This is what the start of the sub model looks like…..

Simon

0 Kudos
curtvprice
MVP Esteemed Contributor

This is a "feature" of Model Builder that validating an iterator breaks its output. I suggest getting it all ready to go and then do not validate, just run it.

When the validating system totally keeps me from getting things done, I have worked around it by running the submodel in a Python script inside Calculate Value.  (Import toolbox, pass parameters to the model tool, return result with data type set). Yes, painful, but sometimes worth not going all the way to a full Python script.

Just a suggestion: %workspace% is a built-in model variable that references the current environment workspace. You should use another variable name that will not conflict, like "Working GDB"

DuncanHornby
MVP Notable Contributor

In your sub-model you have created a variable called workspace and exposed it as a parameter but it is not connected to anything in your model, so will do nothing. The other variable (tm_segment_centers) which is connected to the iterator is not exposed as a parameter so won't get exposed to the master model. Suggest you delete the variable workspace and make the input to the iterator the parameter.