[Tutor] ArcPro Add Field Toolbox

K. Lunzner mambafrutty at gmail.com
Sun Apr 5 18:31:22 EDT 2020


Hello everybody,



I've been attending a Python course for a few weeks now. In this course we
briefly went through the basics. Now we have to make an Add Field toolbox,
similar to the original one (AddField_management).





>      import arcpy

>      arcpy.env.workspace “C:…”

>      inputFC = arcpy.GetParameterAsText(0)

>      inputString = arcpy.GetParameterAsText(1)

>      fieldList =inputString.split(";")

>      inputString = arcpy.ValidateFieldName(inputString,
arcpy.env.workspace)

>      fieldType = arcpy.GetParameterAsText(2)

>      fieldLength = arcpy.GetParameterAsText(2)

>      for fieldName in fieldList:

>       arcpy.AddField_management(inputFC , fieldName, fieldType)

>

>       arcpy.AddMessage("Field created:" + fieldName)

>      arcpy.AddMessage ("Process completed")



Problem 1: fieldLength



The field should only be selectable if you enter "text" in the fieldtype. I
tried:



>  def updateParameters(self):

>         if self.params[2].value: # check that parameter has a value

>               if self.params[2].value == "TEXT":

>                    self.params[3].enabled = True

>         else:

>                    self.params[3].enabled = False



but it is not working



Problem 2: ValidateField

Entry with special characters or spacing is still possible. It appears in
the table: ‘A !’



Problem 3:



>   existingFields = [f.name for f in arcpy.ListFields(inputFC)]

>   for fieldName in fieldList:

>    if fieldName in existingFields:

>     arcpy.AddMessage('Feld mit folgendem Name existiert bereits:
%s'%(fieldName))



If the name of a field type already exists, a message should appear and
inform the user. It still shows that the field was created. Tried a
listfield function (iflen ..) but it didn't work either...



I've been trying for days now and have gone through all Esri aids in the
meantime, but haven't been able to solve any of these problems so far.
Thank you very much for any help!


More information about the Tutor mailing list