Just a simple command:
arcpy.analysis.Buffer('Target_Layer', 'C:/Buffer/Buffer.gdb/buffer', '100 Feet', 'FULL', 'ROUND', 'NONE')
It works in the Python Window, created a new layer in the gdb file. However, when I change this to Python Tools:
def execute(self, params, messages):
arcpy.analysis.Buffer('Target_Layer', 'C:/Buffer/Buffer.gdb/buffer', '100 Feet', 'FULL', 'ROUND', 'NONE')
It shows 'Tool completed', but nothing happen. However, when I change the output to 'C:/Buffer/buffer' only, it can create a .shp file named 'buffer'. What is the different between the script in Python Window and Python Toolbox.
Also, when I want to add the output layer in the gdb file to Contents, I type in the Python Window:
arcpy.management.MakeFeatureLayer('buffer', 'buffer_lyr')
It works. However, when I put it into Python Toolbox:
def execute(self, params, messages):
arcpy.management.MakeFeatureLayer('buffer', 'buffer_lyr')
It shows:
Traceback (most recent call last):
File "<string>", line 80, in execute
I found some solution said that the layer in gdb need to change to .lyr file and then use the .addLayer() function. Can I have a way need not to create a .lyr file just like I did in the Python Window.
'C:/Buffer/buffer'
will assume you want to create the output in a folder since the workspace isn't a geodatabase like
'C:/Buffer/buffer.gdb'
I find the solution:
The new feature class is created and saved to the gdb file, but it is hidden in catalog and a refresh is needed to show it.
I can directly add the feature class to map, even it is hidden in the catalog.:
Buffer_lyr = 'C:/Buffer/Buffer.gdb/buffer"' aprx = arcpy.mp.ArcGISProject('CURRENT') aprxMap = aprx.listMaps()[0] aprxMap.addDataFromPath(Buffer_lyr)