Hi,
I have a geoprocessing tool (thanks to the generous help of @SSWoodward) that uses a domain table to to define subtypes in a feature class. It uses the integer code and description from the domain table to create the subtypes, so the domain needs to be coded value and not range. The tool populates a list of available domains from the nominated workspace, and available subtype columns from the target feature class. Subtypes can only be integers, so I want to only have coded value integer domains and integer fields in my dropdown selections.
I have successfully limited the fields dropdowns to integers from the target feature class using a validation script, but my attempts with the domain list have not been successful.
Main script and validator scripts below.
import arcpy
def add_subtypes_from_domain(workspace: str, domain_name: str, target_table: str, subtype_field: str):
"""
Adds subtypes to a feature class or table based on a coded value domain.
Parameters:
workspace (str): Path to the geodatabase containing the domain.
domain_name (str): Name of the domain to extract codes from.
target_table (str): Path to the feature class or table to update.
subtype_field (str): Name of the integer field to use for subtypes.
"""
arcpy.env.workspace = workspace
temp_table = "memory\\temp_out_tbl"
try:
# Convert domain to table
st_tbl = arcpy.management.DomainToTable(workspace, domain_name, temp_table, 'code', 'desc')
# Set the subtype field
arcpy.management.SetSubtypeField(target_table, subtype_field)
# Add subtypes from the domain table
with arcpy.da.SearchCursor(st_tbl, ['code', 'desc']) as cursor:
for code, desc in cursor:
arcpy.management.AddSubtype(target_table, code, desc)
finally:
# Clean up
if 'cursor' in locals():
del cursor
if arcpy.Exists(temp_table):
arcpy.management.Delete(temp_table)
if __name__ == "__main__":
workspace = arcpy.GetParameterAsText(0)
domain_name = arcpy.GetParameterAsText(1)
target_table = arcpy.GetParameterAsText(2)
subtype_field = arcpy.GetParameterAsText(3)
add_subtypes_from_domain(workspace, domain_name, target_table, subtype_field)
Below is a working validation script that lists all domains as dropdowns, and only integer type fields from the target feature class (does not filter to only display coded value integer domains, but at least has dropdowns for easy selection):
import arcpy
class ToolValidator:
def __init__(self):
self.params = arcpy.GetParameterInfo()
def initializeParameters(self):
return
def updateParameters(self):
# Populate domain names from workspace (no type filtering)
if self.params[0].altered and self.params[0].value:
workspace = self.params[0].valueAsText
arcpy.env.workspace = workspace
try:
domains = arcpy.da.ListDomains(workspace)
domain_names = [d.name for d in domains]
self.params[1].filter.list = domain_names
except Exception:
self.params[1].filter.list = []
# Populate subtype field list with only Integer or SmallInteger fields
if self.params[2].altered and self.params[2].value:
target_table = self.params[2].valueAsText
try:
fields = arcpy.ListFields(target_table)
int_fields = [
f.name for f in fields
if f.type in ["Integer", "SmallInteger"]
]
self.params[3].filter.list = int_fields
except Exception:
self.params[3].filter.list = []
return
def updateMessages(self):
return
Can anyone suggest some changes to the domain validator or main script to get this to limit domain dropdowns to coded value with integer field type?
Thanks for reading through to the end.