0

I have a Python ToolBox with two Custom Python Scripts. The first one works well, but when I try to run the second, there is no errors, but the operation do not run.

   import arcpy

class Toolbox(object):
    def __init__(self):
        """Define the toolbox (the name of the toolbox is the name of the
        .pyt file)."""
        self.label = "Agro Toolbox"
        self.alias = ""
        self.tools = [ConfereChave, ConectarTabelas]

class ConfereChave(object):
    def __init__(self):
        """Define the tool (tool name is the name of the class)."""
        self.label = "ConfereChave"
        self.description = ""
        self.canRunInBackground = False
        self.category = "Banco de Dados" 

    def getParameterInfo(self):
        param0 = arcpy.Parameter(
            displayName="Tabela (FC)",
            name="Tabela_Base",
            datatype="DEFeatureClass",
            parameterType="Required",
            direction="Input")

        param1 = arcpy.Parameter(
            displayName="Tipo Chave (Banco de Dados Externo)",
            name="TipoChave",
            datatype="GPString",
            parameterType="Required",
            direction="Input")

        param2 = arcpy.Parameter(
            displayName="Campo Confere",
            name="CampoConfere",
            datatype="Field",
            parameterType="Required",
            direction="Input")

        param3 = arcpy.Parameter(
            displayName="Nome Campo",
            name="Nome_Campo",
            datatype="GPString",
            parameterType="Required",
            direction="Input")

        param4 = arcpy.Parameter(
            displayName="Tipo Campo",
            name="Tipo_Campo",
            datatype="GPString",
            parameterType="Required",
            direction="Input")

    params = [param0, param1, param2, param3, param4]
        param1.value = "Numerico, Alfabetico ou Alfanumerico"   param2.parameterDependencies = [param0.name]    param4.value = 'String, Float, Double'  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."""
        Tabela_Base = parameters[0].valueAsText
        TipoChave = parameters[1].valueAsText
        CampoConfere = parameters[2].valueAsText
        Nome_Campo = parameters[3].valueAsText
        Tipo_Campo = parameters[4].valueAsText
        fields = arcpy.ListFields(Tabela_Base)
        Saida1 = Tabela_Base
        arcpy.AddMessage('Tabela de Entrada: '+Tabela_Base)
        arcpy.AddMessage('Campo a ser Conferido: '+CampoConfere)

        if TipoChave == "Numerico":

            ValidaCampo = sum(field.name == CampoConfere and field.type == 'Single' for field in fields) 

            if ValidaCampo == True:

                arcpy.AddMessage('O campo selecionado possui o tipo adequado.')

            if not ValidaCampo:

                arcpy.AddField_management(Tabela_Base, Nome_Campo, Tipo_Campo, "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
                arcpy.AddMessage('Campo '+CampoConfere+' Nao Atende o Tipo Adequado, Adicionando Novo Campo Chave')
                arcpy.AddMessage("Campo "+Nome_Campo+" Adicionado com Sucesso!")
                arcpy.AddMessage('Campo: {0}'.format(CampoConfere) +' Sendo Recalculado...')
                arcpy.CalculateField_management(Saida1, Nome_Campo, "!{0}!".format(CampoConfere), "PYTHON_9.3", "")
                arcpy.AddMessage('Campo '+Nome_Campo+' Adicionado e Recalculado com Sucesso!')

        elif TipoChave == "Alfabetico" or TipoChave == "Alfanumerico":

            ValidaCampo = sum(field.name == CampoConfere and field.type == 'String' for field in fields) 

            if ValidaCampo == True:

                    arcpy.AddMessage('O campo selecionado possui o tipo adequado.')

            if not ValidaCampo:

                arcpy.AddField_management(Tabela_Base, Nome_Campo, Tipo_Campo, "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
                arcpy.AddMessage('Campo '+CampoConfere+' Nao Atende o Tipo Adequado, Adicionando Novo Campo Chave')
                arcpy.AddMessage("Campo "+Nome_Campo+" Adicionado com Sucesso!")
                arcpy.AddMessage('Campo: {0}'.format(CampoConfere) +' Sendo Recalculado...')
                arcpy.CalculateField_management(Saida1, Nome_Campo, "!{0}!".format(CampoConfere), "PYTHON_9.3", "")
                arcpy.AddMessage('Campo '+Nome_Campo+' Adicionado e Recalculado com Sucesso!')

        arcpy.RefreshActiveView()
        arcpy.RefreshTOC()
        return

class ConectarTabelas(object):

    def __init__(self):
        """Define the tool (tool name is the name of the class)."""
        self.label = "ConectarTabelas"
        self.description = "ConectarTabelas"
        self.canRunInBackground = False
        self.category = "Banco de Dados"

    def getParameterInfo(self):
        param0 = arcpy.Parameter(
            displayName="Tabela (FC)",
            name="TabelaP1",
            datatype="DEFeatureClass",
            parameterType="Required",
            direction="Input")

        param1 = arcpy.Parameter(
            displayName="Tabela 1",
            name="TabelaE1",
            datatype="DETable",
            parameterType="Required",
            direction="Input")

        param2 = arcpy.Parameter(
            displayName="Tabela 2",
            name="TabelaE2",
            datatype="DETable",
            parameterType="Optional",
            direction="Input")

        param3 = arcpy.Parameter(
            displayName="Tabela 3",
            name="TabelaE3",
            datatype="DETable",
            parameterType="Optional",
            direction="Input")

        param4 = arcpy.Parameter(
            displayName="Campo Chave - Tabela (FC)",
            name="CampoChave1",
            datatype="Field",
            parameterType="Required",
            direction="Input")

        param5 = arcpy.Parameter(
            displayName="Campo Chave - Tabelas",
            name="CampoSaida",
            datatype="Field",
            parameterType="Required",
            direction="Input")
            params = [param0, param1, param2, param3, param4, param5]   param4.parameterDependencies = [param0.name]    param5.parameterDependencies = [param1.name]    #param5.parameterDependencies = [param2.name]   #param5.parameterDependencies = [param3.name]   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):

        TabelaP1 = parameters[0].valueAsText
        TabelaE1 = parameters[1].valueAsText
        TabelaE2 = parameters[2].valueAsText
        TabelaE3 = parameters[3].valueAsText
        CampoChave1 = parameters[4].valueAsText
        CampoSaida = parameters[5].valueAsText

        arcpy.AddMessage('Iniciando Operacao de Juncao das Tabelas')

        if TabelaE2 == "" and TabelaE3 == "":

            arcpy.AddMessage("Tabelas 2 e 3 ausentes. Executando Join com " +TabelaE1+'.')
            arcpy.JoinField_management(TabelaP1, CampoChave1, TabelaE1, CampoSaida, "")
            arcpy.AddMessage('Juncao Concluida')

        elif TabelaE2 == "":

            arcpy.AddMessage("Tabelas 2 ausente. Executando Join com " +TabelaE1+" e "+TabelaE3)
            arcpy.JoinField_management(TabelaP1, CampoChave1, TabelaE1, CampoSaida, "")
            arcpy.JoinField_management(TabelaP1, CampoChave1, TabelaE3, CampoSaida, "")
            arcpy.AddMessage('Juncao Concluida')

        elif TabelaE3 == "":

            arcpy.JoinField_management(TabelaP1, CampoChave1, TabelaE1, CampoSaida, "")
            arcpy.JoinField_management(TabelaP1, CampoChave1, TabelaE2, CampoSaida, "")
            arcpy.AddMessage('Juncao Concluida')

        arcpy.RefreshActiveView()
        arcpy.RefreshTOC()
        return

The syntax is ok and I really do not understand why the second script "ConectarTabelas" runs, show no errors and do not work.

  • Did you ever find an answer to your question? I'm experiencing the same thing where the code looks good, I'm not getting an error message, but also not getting any results in the field either. – Becky Colwell-Ongenae Jan 25 '18 at 22:28
  • Hey Becky. Yes, I did found the answer to my question. Can you explain better your problem? Maybe I can help you! – Vinicius Cruvinel Jan 27 '18 at 2:50
  • Since you found the answer, would you please post it (and mark it as accepted-you can accept your own answer)? This would help other visitors to this question. – dav Oct 16 '18 at 12:34

Your Answer

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

Browse other questions tagged or ask your own question.