Thursday, 14 April 2016

arcpy - Changing Parameter Type from Optional to Required in Python Toolbox?


I am creating a Python toolbox with 5 parameters. One of the parameters is a Boolean/checkbox, which checks if the user wants to edit the labels of a shapefile. What I would hope to do is if the user enables this checkbox, then change the parameter type of two of my other parameters to be required.


Is there a simple way to do this?


For the time being, my toolbox looks like the following:


enter image description here


If "Edit Labels?" is checked, I would like Label Font Size and Bold Font to be changed to be required inputs. I tried to do this within the updateParameters() method, but it seems like that is only used once the "OK" button is hit.




Here is the code that I have been working on:


import arcpy


class Toolbox(object):
def __init__(self):
"""Define the toolbox (the name of the toolbox is the name
the.pyt file).
"""
self.label = "CCDT Toolbox"
self.alias = ""

# List of tool classes associated with this toolbox

self.tools = [applyMultipleLayerFeatures]

class applyMultipleLayerFeatures(object):
def __init__(self):
"""Define the tool (tool name is the name of the class)."""
self.label = "Apply Multiple Layer Features"
self.description = "Applies features to multiple layers. Customize a single layer and use it as a source layer in the tool then select layers to be modified by the tool"
self.canRunInBackground = False

def getParameterInfo(self):

"""Define parameter definitions"""

# First parameter = layer with customized features
param0 = arcpy.Parameter(
displayName = "Source layer with customized features",
name = "source",
datatype = "Feature Layer",
parameterType = "Required",
direction = "Input")


# Second parameter = layers to which customized features will be applied
param1 = arcpy.Parameter(
displayName = "Layers with features to be updated",
name = "updlayers",
datatype = "GPValueTable",
parameterType = "Required",
direction = "Input")

param2 = arcpy.Parameter(
displayName = "Edit labels?",

name = "enableLabels",
datatype = "GPBoolean",
parameterType = "Required",
direction = "Input")

param3 = arcpy.Parameter(
displayName = "Label Font Size",
name = "labelFontSize",
datatype = "GPDouble",
parameterType = "Required",

direction = "Input")

param4 = arcpy.Parameter(
displayName = "Bold Font:",
name = "labelBoldFont",
datatype = "GPBoolean",
parameterType = "Required",
direction = "Input")

param1.columns = [['Feature Layer', 'Features']]


params = [param0, param1, param2, param3, param4]

return params

def isLicensed(self):
"""Set whether tool is licensed to execute."""
return True

def updateParameters(self, parameters):

if parameters[2].value:
parameters[3].parameterType = "Required"
parameters[4].parameterType = "Required"
else:
parameters[3].parameterType = "Optional"
parameters[4].parameterType = "Optional"
return

def execute(self, parameters, messages):
"""The source code of the tool."""


#Get Inputs

for input in parameters:
arcpy.AddMessage("This is part of the parameters list:" + input.name)

updlayers = parameters[1].values
enableLabels = parameters[2].value
labelFontSize = parameters[3].value
labelBoldFont = parameters[4].value


if enableLabels == True:
for layers in updlayers:
for labels in layers:
labels.showLabels = True
if labelBoldFont == True:
for a in labels.labelClasses:
a.expression = '"%s" + [Name] + "%s"' % ("","")
else:
for a in labels.labelClasses:

a.expression = '"%s" + [Name] + "%s"' % ("","")

source = parameters[0].valueAsText
updlayers = parameters[1].valueAsText.split(";")

for app in updlayers:
arcpy.ApplySymbologyFromLayer_management(app.strip("'"),source)

arcpy.RefreshTOC
arcpy.RefreshActiveView

return

Answer



updateParameters() is called whenever any parameter's value is changed. You should be able to use a function like the following:


def updateParameters(self, parameters):
if parameters[2].value:
parameters[3].parameterType = 'Required'
parameters[4].parameterType = 'Required'
else:
parameters[3].parameterType = 'Optional'
parameters[4].parameterType = 'Optional'

return



EDIT: It turns out that parameterType is a read-only property. Given that, I'm not sure that what you're asking is actually possible. Perhaps it's best to initialize them with parameterType='Required', set default values for the parameters after initialization (e.g. param3.value=0 and param4.value=False), and then simply ignore them if the box is unchecked.


Another option would be to initialize the parameters as required, then enable/disable them based on the checkbox value. I haven't tested for myself whether an unspecified disabled required parameter will give you an error, but the code to try would be:


def updateParameters(self, parameters):
if parameters[2].value:
parameters[3].enabled = True
parameters[4].enabled = True
else:

parameters[3].enabled = False
parameters[4].enabled = False
return

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...