ArcGIS Desktop

  • ArcGIS Pro
  • ArcMap

  • My Profile
  • 帮助
  • Sign Out
ArcGIS Desktop

ArcGIS Online

专为贵组织打造的制图平台

ArcGIS Desktop

全面的专业性 GIS

ArcGIS Enterprise

面向企业的 GIS

ArcGIS for Developers

用于构建位置感知应用程序的工具

ArcGIS Solutions

适用于行业的免费模板地图和应用程序

ArcGIS Marketplace

获取适用于组织的应用程序和数据

  • 文档
  • 支持
Esri
  • 登录
user
  • 我的个人资料
  • 登出

ArcMap

  • 主页
  • 入门
  • 地图
  • 分析
  • 管理数据
  • 工具
  • 扩展模块

添加字段 ToolValidator

本示例对添加字段工具验证进行了模拟。由于添加字段是内置工具,所以不具有 ToolValidator 类。但是,如果它具有 ToolValidator 类,则该类可能如下所示。

以下对添加字段的参数和它们的数据类型进行了回顾:

  • 0 - 输入表:复合数据类型,包括所有表
  • 1 - 字段名称:字符串
  • 2 - 字段类型:字符串(LONG、SHORT、DOUBLE 等)
  • 3 - 字段精度:长整型
  • 4 - 字段小数位数:长整型
  • 5 - 字段长度:长整型
  • 6 - 字段别名:字符串
  • 7 - 字段可为空:布尔型
  • 8 - 字段必填:布尔型
  • 9 - 字段域:字符串
  • 0 - 输出表:派生输出,获取自参数 0

本示例对“添加字段”工具验证进行了模拟

class ToolValidator:

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

    def initializeParameters(self):
        # The derived output is a clone (copy) of the input
        #
        self.params[10].parameterDependencies = [0]
        self.params[10].schema.clone = True

        # Set up the field type list
        #
        self.params[2].filter.list = ["TEXT", "FLOAT", "DOUBLE",
                                      "SHORT", "LONG", "DATE",
                                      "BLOB", "RASTER"]

        # The default field type is LONG
        #
        self.params[2].value = "LONG"

        # Field scale & Length are disabled for LONG types
        #
        self.params[4].enabled = False
        self.params[5].enabled = False

        # Set the Boolean filters for IsNullable and IsRequired and
        #  their default values
        #
        self.params[7].filter.list = ["NULLABLE", "NON_NULLABLE"]
        self.params[7].value = "NULLABLE"
        
        self.params[8].filter.list = ["REQUIRED", "NON_REQUIRED"]
        self.params[8].value = "NON_REQUIRED"

        return

    def updateParameters(self):
        # Set the default field type value unless the user altered it
        #
        if not self.params[2].altered:
            self.params[2].value = "LONG"

        # Enable/Disable parameters based on field type
        #
        fieldType = self.params[2].value.upper()
        if fieldType in ["TEXT", "BLOB"]:
            self.params[3].enabled = False
            self.params[4].enabled = False
            self.params[5].enabled = True
        elif fieldType in ["FLOAT", "DOUBLE"]:
            self.params[3].enabled = True
            self.params[4].enabled = True
            self.params[5].enabled = False
        elif fieldType in ["SHORT", "LONG"]:
            self.params[3].enabled = True
            self.params[4].enabled = False
            self.params[5].enabled = False
        elif fieldType in ["DATE", "RASTER"]:
            self.params[3].enabled = False
            self.params[4].enabled = False
            self.params[5].enabled = False
        else:
            # Unknown field type.  Internal validation will catch this
            #  and show an error. We might as well return here and let
            #  internal validation do its work.
            #
            return
        
        # Update the output schema with the new field. Don't do anything
        #  unless we have an input value and a field name
        #
        if self.params[0].value and self.params[1].value:
            newField = arcpy.Field()
            newField.name = self.params[1].value
            newField.type = self.params[2].value

            # Set up the field properties based on type of field
            #
            if self.params[3].value and self.params[3].enabled:
                newField.precision = self.params[3].value
            if self.params[4].value and self.params[4].enabled:
                newField.scale = self.params[4].value
            if self.params[5].value and self.params[5].enabled:
                newField.length = self.params[5].value
                
            if self.params[6].value:
                newField.aliasName = self.params[6].value
                
            newField.isNullable = self.params[7].value

            # Note: IsRequired is not a property on a field object -- it's
            #  handled internally by the Add Field system tool.
            # 
            if self.params[9].value:
                newField.domain = self.params[9].value

            # Set the additional field on the output schema
            #
            self.params[10].schema.additionalFields = [newField]

    def updateMessages(self):
        return

ArcGIS Desktop

  • 主页
  • 文档
  • 支持

ArcGIS 平台

  • ArcGIS Online
  • ArcGIS Desktop
  • ArcGIS Enterprise
  • ArcGIS for Developers
  • ArcGIS Solutions
  • ArcGIS Marketplace

关于 Esri

  • 关于我们
  • 招贤纳士
  • Esri 博客
  • 用户大会
  • 开发者峰会
Esri
分享您的想法。
Copyright © 2019 Esri. | 隐私政策 | 法律声明