0

I want to set the FindLabel function to the labelclass expression using ArcPy. My code is like How To: Label a related table. I don't want to load the function into the expression editor of the label tab but in the toolbox.

I try to load function programmatically but it's not working. What's wrong in the code?

Another problem is using brackets "[]" for field names. In the expression editor the users can write [field name] but in Python I must use string characters. Also FindLabel mainly uses an advanced expression in expression editor but I could not set the advanced expression and parser.

How can I use FindLabel Function programmatically?

import arcpy
mxd = arcpy.mapping.MapDocument("CURRENT")
layer = arcpy.mapping.ListLayers(mxd, "fc")[0]    
def FindLabel ([keyField], [FirstLabel]):
    key1 = [keyField] # Key field in feature class
    key2 = "ID"   # Key field in related table
    L = [FirstLabel] # Label field in feature class
    L2 = "Label2"   # Label field in related table
    myDataTable = r"<path-to-related-table>"   # Path to related table
    cur = arcpy.da.SearchCursor(myDataTable, [key2, L2])
    for row in cur:
        if str(key1) == str(row[0]):
            L = L + " " + str(row[1])
    return L

layer.labelClasses[0].expression = .... ?
print layer.labelClasses[0].expression
layer.labelClasses[0].showClassLabels = True
layer.showLabels = True
arcpy.RefreshActiveView()

del mxd

1 Answer 1

2

You cannot use arcpy to build your tool if it cannot access the Advanced label editor or the parser settings. This is apparently one of the intentional limitations to arcpy that esri has made to keep arcpy as a low powered scripting language and maintain a distintion from ArcObjects to support the need for their programming services and third party programs. Any work around will perform horribly and not be worth the effort you invested.

You must use Visual Studio and ArcObjects to build your tool. It has both the interface capabilities and access to all of the necessary label methods and properties to create a useful tool. There is a free version of Visual Studio available.

2
  • You will also need the ArcObjects SDK from esri to integrate ArcObjects with Visual Studio. Nov 27, 2016 at 18:03
  • I don't know arcobject but my coleages know it . Many thanks.
    – BBG_GIS
    Nov 27, 2016 at 18:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.