0

I am trying to create a .pyt that find and select an object in an specific layer but I have a problem. I'm working on the Desktop version 10.5 and actually the code is this one:

def getParameterInfo(self):
    """Define parameter definitions"""
    Cabina = arcpy.Parameter(
        displayName="Cabina",
        name="cab",
        datatype="string",
        parameterType="Required",
        direction="Input")

    params = [Cabina]
    return params

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

def updateParameters(self, parameters):
    """Modify the values and properties of parameters before internal
    validation is performed.  This method is called whenever a parameter
    has been changed."""
    return

def updateMessages(self, parameters):
    """Modify the messages created by internal validation for each tool
    parameter.  This method is called after internal validation."""
    return

def execute(self, parameters, messages):
    """The source code of the tool."""
    mxd = arcpy.mapping.MapDocument("CURRENT")
    lyr = arcpy.mapping.Layer(r"ESERCIZIO\ELE_ESER_Cab")
    exp = "'" + str(Cabina) + "'"
    arcpy.SelectLayerByAttribute_management(in_layer_or_view= lyr, selection_type="NEW_SELECTION", where_clause= "COD_CABINA LIKE" + exp)
    arcpy.RefreshActiveView()

    return 
    `

the problem is that continue to appear this error message: ERROR 000358: Invalid expression Failed to execute. (I'm working in an Oracle environment) Actually I tried to put a value such as exp = 'A0001' and the script work well. I read almost everything but seems like nothing works. I tried to find some documentation about how to use custom input and SQL but I did not find so much.

  • Try: sql = "{0} LIKE '{1}'".format(arcpy.AddFieldDelimiters(datasource=lyr, field='COD_CABINA'), Cabina) and then arcpy.SelectLayerByAttribute_management(in_layer_or_view= lyr, selection_type="NEW_SELECTION", where_clause=sql) – BERA Apr 8 at 8:55
  • I tried but does not work, appear the ERROR 000358. I tried to run the script on the Python idle and there works perfectly but if i run the Toolbox something go wrong. Actually the results of .format is '"COD_CABINA" LIKE \'A540\'' – vinjazz Apr 8 at 11:31
1

If you change your code to this, it should work:

def getParameterInfo(self):
    """Define parameter definitions"""
    Cabina = arcpy.Parameter(
        displayName="Cabina",
        name="cab",
        datatype="string",
        parameterType="Required",
        direction="Input")

    params = [Cabina]
    return params

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

def updateParameters(self, parameters):
    """Modify the values and properties of parameters before internal
    validation is performed.  This method is called whenever a parameter
    has been changed."""
    return

def updateMessages(self, parameters):
    """Modify the messages created by internal validation for each tool
    parameter.  This method is called after internal validation."""
    return

def execute(self, parameters, messages):
    """The source code of the tool."""
    mxd = arcpy.mapping.MapDocument("CURRENT")
    lyr = arcpy.mapping.Layer(r"ESERCIZIO\ELE_ESER_Cab")
    Cabina = parameters[0].valueAsText
    exp = "{} LIKE '{}'".format(arcpy.AddFieldDelimiters(datasource=lyr, field='COD_CABINA'), Cabina)
    arcpy.SelectLayerByAttribute_management(in_layer_or_view= lyr, selection_type="NEW_SELECTION", where_clause= "COD_CABINA LIKE" + exp)
    arcpy.RefreshActiveView()

    return

The execute method doesn't know anything about the parameter you defined as Cabina, you need to define this again within this function.

0

You are defining Cabina as a Parameter then treating it as if it is a string variable when you build your expression. You need to access the Parameters value and build the sql expression from that.

  • Thank you so much to everyone it is work! now i will go deeper in the parameter Value! – vinjazz Apr 8 at 15:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.