Select to view content in your preferred language

Interactively setting the current workspace using the ToolValidator class

1575
5
11-15-2012 12:53 PM
JimOakleaf
Emerging Contributor
I have a tool which has the user select a workspace in the first parameter.  Once the user selects the appropriate workspace I would then like the tool's environmental settings to be changed so the current workspace be set to that select workspace.  What I am hoping to accomplish is if the user just inputs a name in a feature class output parameter, the new featureclass will be created in the previously selected workspace by default.  I have tried using the ToolValidator however it doesn't seem to change the tool's environment settings.  I have included code from my ToolValidator class showing two ways I have tried to accomplish this.

Thanks for any advice or suggestions,
Jim Oakleaf



class ToolValidator:
  def __init__(self):
    import arcpy
    self.params = arcpy.GetParameterInfo() 

  def initializeParameters(self):
    return

  def updateParameters(self):
    if self.params[0].altered:
      theW = self.params[0].value
      dsW=arcpy.Describe(theW)
      arcpy.env.workspace=dsW.catalogPath  """Tried this method"""
      arcpy.env.scratchWorkspace=theW   """Also tried this method"""

  def updateMessages(self):
    return
Tags (2)
0 Kudos
5 Replies
ArkadiuszMatoszka
Frequent Contributor
Hi,
I don't understand why you want to do this in ToolValidator? Why not in script itself?
Cheers.
Arek
0 Kudos
JimOakleaf
Emerging Contributor
Hi Arek -  I am trying to make it easier when the user is inputting a parameter.  I would like the default location for other named datasets being created point to the same directory.

So for example the user has
1) Project Workspace parameter
2) Analysis Output location 

So the user sets their project workspace and then sets the name of the output location however since I am using a data type of Raster Dataset with an output direction, the parameter wants to use the current workspace set by the tool as the default location if a user just inputs a name.  Additionally the browser dialog which opens points to the last workspace a dataset was written to which can also be different.  I just want them both to point to the users selected workspace by default.

I have attached an image which may make more sense.  So in the image, the user selected test.gdb as their project workspace and then just typed in New which defaulted to creating the dataset in the Default.gdb (current workspace of tool) and then to make things even more convoluted if the user clicks to open a dialog they are being directed to the NewSetup.gdb which was one used by them most recently.  If I click on the Environments button on the tool and then change the current workspace the default output location is the current workspace set.  It just seems like I should be able to do this through code once the user selects their workspace.   I guess I could just set warnings if the location of the output is in a different workspace than what the user selects.

Thanks, jim

[ATTACH=CONFIG]19379[/ATTACH]
0 Kudos
curtvprice
MVP Alum
So the user sets their project workspace and then sets the name of the output location however since I am using a data type of Raster Dataset with an output direction, the parameter wants to use the current workspace set by the tool as the default location if a user just inputs a name. (1)  Additionally the browser dialog which opens points to the last workspace a dataset was written to which can also be different. (2)  I just want them both to point to the users selected workspace by default.



(1) Unfortunately, I think the workspace setting is only local to the validate functions, you can't migrate it "up". So you need to do it this way:

# the first parameter [0] is the (input) workspace parameter
# to make things easy for the user, set this to default to the current workspace
# in the parameter properties dialog (or in the code if pyt toolbox)
# the second [1] is your output dataset.
def updateParameters(self):
 if self.params[0].value and self.params[1].altered:
    outFC = str(self.params[1].value)
    import os
    if os.path.dirname(outFC) == ""
      self.params[1].value = os.path.join(self.params[0].value,outFC)


(2) Bad news, the workspace that appears in the popup when you click the folder is not the current workspace, it's either the home folder, or the last folder opened with an open dialog. This is a Windows UI standard I think, although it is annoying!
0 Kudos
JimOakleaf
Emerging Contributor
Hey Curtis - Thanks for your response!  Unfortunately before moving away from the input box ArcGIS is placing a workspace in front of the inputted name.  I am just going to go with a warning for users and move on.  Your code helped me figure out how to do this, so thanks!

    if self.params[0].value and self.params[6].altered:
      outFC = str(self.params[6].value)
      self.params[6].clearMessage
      import os
      if os.path.dirname(outFC)!= str(self.params[0].value):
        self.params[6].setWarningMessage("Output location not within project folder!")
0 Kudos
curtvprice
MVP Alum
Unfortunately before moving away from the input box ArcGIS is placing a workspace in front of the inputted name.  I am just going to go with a warning for users and move on.  Your code helped me figure out how to do this, so thanks!
    if self.params[0].value and self.params[6].altered:
      outFC = str(self.params[6].value)
      self.params[6].clearMessage
      import os
      if os.path.dirname(outFC)!= str(self.params[0].value):
        self.params[6].setWarningMessage("Output location not within project folder!")


You're right, the internal validation for output filenames gets there before we can alter it (when the output parameter is initialized).

For best results, be sure and update parameter values in the updateParameters function -- and set validation messages (like setWarningMessage above) in the updateMessages function.

Here's the way I'd do it:
  def updateMessages(self):
    """Modify the messages created by internal validation for each tool
    parameter.  This method is called after internal validation."""
    if self.params[0].value and self.params[6].value:
      import os
      inWS = os.path.dirname(str(self.params[0].value))
      outWS = os.path.dirname(str(self.params[6].value))
      if inWS != outWS:
        self.params[6].setWarningMessage(
          "Output will be written to a different workspace than input!")
    return
0 Kudos