I am attempting to use this code in a tool that I am working on:
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
XMIN = df.extent.XMin
YMIN = df.extent.YMin
XMAX = df.extent.XMax
YMAX = df.extent.YMax
I then go on to do other calculations with the extent. The code works perfectly in the Python window, but because Toolbox tools are run in separate processes I am unable to use "CURRENT".
I think I could probably add a derived input variable and pass the coordinates through the validation script, but that doesn't work for what I am doing. I need to be able to do this later on in the script and add an additional temporary feature to the current map:
points = arcpy.CreateFeatureclass_management("in_memory", "NewFeature", "POINT")
arcpy.AddField_management(points, "NewField", "TEXT", "", "", 20)
I know all of my code works right, because I can paste the entire .py into the Python window and then call Main()
and run through it, but I need to share it with others in an easier way.
I would prefer the solution be compatible with 10.0 and 10.1 if at all possible, because some of the people I work with do not have 10.1 yet. I will accept a 10.1 only solution if I have to though.
What options do I have to do this?
I still can not get this to work, they have not gotten me Python Add-In Wizard yet so I am unable to test that approach. I have again tried the "Run in process" box checked and unchecked. Neither are working.
Checked gives me this: RuntimeError: CreateObject cannot open map document
Handled that exception: Object: CreateObject cannot open map document
From my testing looks like it is having the same error running in process or not.
I will still be attempting to do this within the Add-In Wizard, when they finally give me it.
The winning answer is I wasn't running it in the foreground.
Answer
As commented by @KHibma:
You should be running this tool in process, and in the foreground. ("current" doesnt work in the background as there isn't a map there to operate on)
However, I think you will be better using ArcGIS Add-Ins for Python which only became available at ArcGIS 10.1.
No comments:
Post a Comment