Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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?


UPDATE: 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

Unchecked gives me this: ERROR 000714: Error in script ExtentQuery

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.


UPDATE: Apparently if you make a tool with no inputs it has really weird errors like this. As soon as I added an input variable I was able to make the script work as it was supposed to with "In Process" checked. I will still be attempting to do this within the Add-In Wizard, when the finally give me it. Hopefully this afternoon. Thanks for the input and sorry for the confusion on this.

The winning answer is I wasn't running it in the foreground.

share|improve this question
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
2  
On your script tool, is run in-process checked? See Running a script in process. –  blah238 May 19 '13 at 0:40
2  
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

1 Answer 1

up vote 1 down vote accepted

I am still unclear on precisely what you are trying to do, but I think you will need to use ArcGIS Add-Ins for Python which only became available at ArcGIS 10.1.

share|improve this answer
    
That may be what I have to do. Do you happen to know if an add in I create with that wizard can work on 10.0 or is it purely 10.1? Sadly I work in a fairly mixed environment and need to maintain compatibility with both. –  eseglem May 19 '13 at 0:50
2  
Purely 10.1 as far as I know. –  PolyGeo May 19 '13 at 0:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.