Do arcpy support highlight field?

154
3
2 weeks ago
向宁焦
New Contributor

In ArcGIS Pro,we can highligh field in fied view。But how wo highlight field by arcpy?

0 Kudos
3 Replies
TonyAlmeida
Occasional Contributor II

What are you trying to do with the highlighted field? Are you trying to do something with the selected features?

0 Kudos
BlakeTerhune
MVP Regular Contributor

This is possible, but only through manipulating the CIM, which is a bit tedious.

Python CIM access—ArcGIS Pro | Documentation

def highlightField(map, layer_name, highlight_field_name):
    lyr = map.listLayers(layer_name)[0]

    # Get the layer's CIM definition
    cim_lyr = lyr.getDefinition("V3")

    # Ensure the featureTable has fieldDescriptions to modify
    # https://pro.arcgis.com/en/pro-app/latest/arcpy/mapping/python-cim-access.htm#GUID-C80C9CF4-67C8-418E-BEB5-386B18EE6097
    if len(cim_lyr.featureTable.fieldDescriptions) == 0:
        # No CIM field descriptions class; create one.
        fDesc = arcpy.cim.CreateCIMObjectFromClassName('CIMFieldDescription', 'V3')
        # Get field info and populate the new field description.
        highlight_field_props = [f for f in arcpy.ListFields(lyr) if f.name == highlight_field_name][0]
        fDesc.alias = highlight_field_props.aliasName
        fDesc.fieldName = highlight_field_props.baseName
        fDesc.highlight = True
        fDesc.visible = True
        fDesc.searchMode = "Exact"
        # Add new field description to the CIM.
        cim_lyr.featureTable.fieldDescriptions.append(fDesc)
    else:
        # Make changes to field description in CIM.
        for f in cim_lyr.featureTable.fieldDescriptions:
            if f.fieldName == highlight_field_name:
                f.highlight = True

    # Push the CIM changes back to the layer object
    lyr.setDefinition(cim_lyr)


aprx = arcpy.mp.ArcGISProject("current")
highlightField(aprx.activeMap, "MY_LAYER_NAME", "MY_FIELD_NAME")

In my testing, the fieldDescriptions was indeed empty (as the documentation notes) so I created a CIMFieldDescription only for the field being highlighted. This has the effect of moving the highlighted field to the beginning of the attribute table view. If that is not acceptable, you will have to create a CIMFieldDescription for each field in the order you want.

0 Kudos
BlakeTerhune
MVP Regular Contributor

The CIM is a weird and wonderful world. I highly recommend watching the session from the 2024 Esri Developer Summit.

ArcPy: Beyond the Basics of arcpy.mp

 

0 Kudos