send arcpy command to python window

1022
9
07-20-2020 08:25 PM
LeandraGordon
Occasional Contributor II

This may seem a strange request but I am trying to get a workaround for this bug BUG-000119907: The Apply Symbology From Layer tool does not work if.. 
Is there any way in a python toolbox that I can construct a command and send it to execute in the python window.

Something like this: sendcmd Python "arcpy.management.ApplySymbologyFromLayer("OUTLIER_LYR", r"C:\Data\7A_MAPPING\LayerFiles_Pro\OUTLIER.lyrx", None, "UPDATE")"

I have tried

0 Kudos
9 Replies
DanPatterson
MVP Esteemed Contributor

Leandra Gordon

The procedure described in the topic describes how to create the script and assign it to a tool for use in a toolbox.

Since it has been corrected in subsequent versions and 2.3 is due to retire in about 18 months, I wouldn't expect a backport to exist beyond what is described in the workaround in your link

re support

Esri Support ArcGIS Pro 2.5.2 (covers previous versions again


... sort of retired...
0 Kudos
LeandraGordon
Occasional Contributor II

Hi Dan - I'm not sure I'm clear on what you are describing. Maybe I have missed a link. The current workaround for the bug (creating a symbology parameter) isn't working in a Python Toolbox e.g.

    def getParameterInfo(self):
 
        # Spatial Data Layer
        param0 = arcpy.Parameter(
            displayName="Spatial Data Layer (SPATIALDATA_LYR)",
            name ="SPATIALDATA_LYR",
            datatype="GPFeatureLayer",
            parameterType="Derived",
            direction="Output")
        param0.value = "SPATIALDATA_LYR"
        param0.symbology = "C:\\Data\\7A_MAPPING\\\LayerFiles_Pro\\SMG_NAME.lyrx"
    
        # SMG (line) Layer
        param1 = arcpy.Parameter(
            displayName="SMG_LINE_LYR",
            name ="SMG_LINE_LYR",
            datatype="GPFeatureLayer",
            parameterType="Derived",
            direction="Output")
        param1.value = "SMG_LINE_LYR"
        param1.symbology = os.path.join(SEVENA_LYRX_DIR, "SMG_NAME.lyrx") #set as a global var at the top of the script

The symbology field of the toolbox properties is empty. The script itself, which is just layer styling runs fine, but nothing is updated:

e.g.
        CurrentProject  = arcpy.mp.ArcGISProject("CURRENT")
        CurrentMap = CurrentProject.listMaps()[0]  

        SD_SVCh_LYR = CurrentMap.listLayers("SD_SVCh_LYR")[0]
        
        # Layer Styling----------------------
        #https://community.esri.com/thread/198403-pro-20-bug-apply-symbology-from-layer-geoprocessing-tool#comment-706275
        #https://support.esri.com/en/bugs/nimbus/QlVHLTAwMDExOTkwNw==
        
        messages.addMessage("Styling Layers")
        
        arcpy.management.ApplySymbologyFromLayer(SMG_LINE_LYR, SMG_LYRX, None, "UPDATE")
        
        TOP_10_LYR = CurrentMap.listLayers("TOP_10_LYR")[0]
        arcpy.management.ApplySymbologyFromLayer(TOP_10_LYR, TOP10_LYRX, None, "UPDATE")
        arcpy.management.ApplySymbologyFromLayer(OUTLIER_LYR, OUTLIER_LYRX, None, "UPDATE")
        arcpy.management.ApplySymbologyFromLayer(SALE_USE_LYR, SALE_USE_LYRX, "VALUE_FIELD AllData_Sale_Use_GIS AllData_Sale_Use_GIS", "UPDATE")  
        arcpy.management.ApplySymbologyFromLayer(SD_LV_LYR, LV_LYRX)
        
        SD_SVCh_LYR = CurrentMap.listLayers("SD_SVCh_LYR")[0]
        arcpy.management.ApplySymbologyFromLayer(SD_SVCh_LYR, SVCh_LYRX)

I'm using a few different approaches in the script in case I hit on the right one but nothing is happening!!

0 Kudos
LeandraGordon
Occasional Contributor II

Sorry - forgot to mention - I'm using ARCGis Pro version 2.4.0

0 Kudos
DanPatterson
MVP Esteemed Contributor

The alternate solution in the link you posted showed how to edit the script and the parameters of the toolbox.  Is that what you were following?


... sort of retired...
0 Kudos
LeandraGordon
Occasional Contributor II

Hi Dan - Python Toolboxes do not allow you to edit Parameters via the interface, they are set programmatically in the python script.
I had a another go last night and added arcpy.SetParameterAsText after each ApplySymbologyFromLayer

and I am starting to get some results although they are inconsistent as some layers are being cloned. My code looks like this:

       #Top 10 Layer - getParameterInfo incl only symb as os.path.join
        TOP_10_LYR = CurrentMap.listLayers("TOP_10_LYR")[0]
        arcpy.management.ApplySymbologyFromLayer(TOP_10_LYR, TOP10_LYRX, None, "UPDATE")
        arcpy.SetParameterAsText(2, "TOP_10_LYR")
        
        #Outlier - getParameterInfo incl value, symb as os.path.join
        arcpy.management.ApplySymbologyFromLayer(OUTLIER_LYR, OUTLIER_LYRX, None, "UPDATE")
        arcpy.SetParameterAsText(3, OUTLIER_LYR)
        
        #Sales Layer - getParameterInfo incl value only
        arcpy.management.ApplySymbologyFromLayer(SALE_USE_LYR, SALE_USE_LYRX, "VALUE_FIELD AllData_Sale_Use_GIS AllData_Sale_Use_GIS", "UPDATE")  
        arcpy.SetParameterAsText(4, SALE_USE_LYR)
        
        # Level of Value Layer - getParameterInfo incl value only
        arcpy.management.ApplySymbologyFromLayer(SD_LV_LYR, LV_LYRX)
        arcpy.SetParameterAsText(5, SD_LV_LYR)
        
        # Site Value Change - getParameterInfo incl value only
        SD_SVCh_LYR = CurrentMap.listLayers("SD_SVCh_LYR")[0]
        arcpy.management.ApplySymbologyFromLayer(SD_SVCh_LYR, SVCh_LYRX)
        arcpy.SetParameterAsText(6, "SD_SVCh_LYR")

Anyway I'm working through it although I don't really understand what arcpy.SetParameterAsText is actually doing but it seems to be making ApplySymbologyFromLayer work
       

0 Kudos
DanPatterson
MVP Esteemed Contributor

Leandra Gordon‌ I use custom toolboxes and specify everything in the dialog (defaults included).  If I need special validation then you can edit the validation code within there.  I prefer this since I can use screen grabs for documentation.

SetParameterAsText basically does that.  If you have a parameter and you want a specific value for it, the you 'set' it.  Just like getting input parameters (with GetParameterAsText).

In custom toolboxes scripts you can set default symbology within the dialog.  You just need to keep everything relative to the toolbox location.


... sort of retired...
0 Kudos
LeandraGordon
Occasional Contributor II

Hi Dan - I'm still wondering whether to go this way but Python Toolboxes are allowing me to get some parameters via large combo dropdowns etc so I'm sticking with them for the moment.

e.g.         param3.filter.type = 'ValueList'
               param3.filter.list = MList # Long list set as global

........

               LGA_NUM = str(MDict.get(PROP_LGA))

I'm also going to expand the toolbox with a lot of prompts so that users can predefine some of the thematics (e.g. for rural areas vs inner city) so I'm trying to stick with the python toolbox.

Also I am very stubborn 🙂

0 Kudos
DanPatterson
MVP Esteemed Contributor

You can use value lists within custom toolboxes... just like in python toolboxes

Don't forget visual documentation is often better than textual.

/blogs/dan_patterson/2016/05/19/toolbox-creation-in-arcgis-pro 


... sort of retired...
LeandraGordon
Occasional Contributor II

I've worked out how to get around BUG-000119907: The Apply Symbology From Layer tool does not work if.. 

  # Spatial Data Layer
        param0 = arcpy.Parameter(
            displayName="Spatial Data Layer (SPATIALDATA_LYR)",
            name ="SPATIALDATA_LYR",
            datatype="GPFeatureLayer",
            parameterType="Derived",
            direction="Output")
        param0.value = "SPATIALDATA_LYR"

in the execute bit:

         arcpy.management.ApplySymbologyFromLayer(SPATIALDATA_LYR, SPATIALDATA_LYRX)
        arcpy.SetParameterAsText(0, SPATIALDATA_LYR)

0 Kudos