Monday, 2 November 2015

python addin - How to get the editor's current layer in ArcPy?


In ArcObjects, it is possible to retrieve the currently selected editable layer using IEditor3.CurrentTemplate.Layer, or IEditLayers.CurrentLayer.


Is it possible to do the same thing in ArcPy?


In Python add-in extensions, there is an onCurrentLayerChanged method, but no corresponding property as far as I can tell to determine the current layer, which begs another question -- what is that method intended to be used for?




Answer



I think you are probably seeing the key difference between ArcPy and ArcObjects and later the Python Add-in.


Arcpy is designed to access the data itself, but doesn't have ties to the user interface. This is why, if you want to access a layer in the table of contents, you have to return the list of layers, then iterate through until you get to the one you want. From what I can tell, Arcpy doesn't know anything about a user interface. Even in the case of a selection of features, they are seen as an object collection, but there is no reference to how this selection may have been created.


On the other hand, ArcObjects, and now the Add-ins, have a definite tie-in to the user interface. As you listed with ArcObjects, the IEditLayers.CurrentLayer class sees that a user has selected a particular layer in the editor.


For the Python add-in, you found the onCurrentLayerchanged method, which returns when the layer has been changed.
If you look in the python addins module, there is a function for GetSelectedTOCLayerOrDataFrame. This would at least return what has been selected in the table of contents.
I think the way you would use these two methods is in combination. Knowing the selected layer to start with will let you perform actions based on that layer name, or feature type, etc. If you then are checking to see when the current layer changes during an edit session, you would be able to enable or disable tools, or raise warnings based on if said tools were relevant to the characteristics of the new layer that was selected.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...