7

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.

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
eseglem
  • 800
  • 1
  • 5
  • 18
  • 1
    I'm not clear on what you are trying to achieve but if it is to work out what map you currently have open try mxd.filePath. – PolyGeo May 18 '13 at 20:46
  • It would work if I wanted to do something on a specific mxd, and not keep the output later in_memory, but I want to be able to open a new arc map and run the tool. Instead of having a specific mxd. – eseglem May 18 '13 at 22:50
  • I think you will need to include more code (and pseudo code) in your question but my understanding is that your in_memory workspace is gone when you close ArcMap or complete a Python script which imports ArcPy. – PolyGeo May 18 '13 at 22:59
  • That is how in_memory works, but as long as ArcMap is open the user can see the results and choose to save them if they would like to.

    Like I said, the code works great if I paste it into the python window and run it that way. I just need to know how to manipulate the currently opened map with a tool. Or make a button that runs the script within the current map. End users will not understand pasting a script into the python window.

    – eseglem May 18 '13 at 23:08
  • 2
    On your script tool, is run in-process checked? See Running a script in process. – blah238 May 19 '13 at 00:40
  • I have tried with it checked an unchecked, both appeared to have the same issue. Said something along the lines of current map doesn't exist. I am not on my work machine to get the exact error, but that is roughly what it said. – eseglem May 19 '13 at 00:48
  • 3
    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) – KHibma May 22 '13 at 16:32
  • Wow, that was the answer, not the inputs. I wasn't running it in the foreground. I just inadvertantly fixed it by making a new tool. – eseglem May 22 '13 at 18:54

1 Answers1

2

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.

PolyGeo
  • 65,136
  • 29
  • 109
  • 338