Solved! Go to Solution.
from subprocess import check_call # format is check_call([ConsoleApp_Path_and_Name, MXD_Name, Data_Frame_Name, Layer_Name]) check_call(["SetFrameClipGeometry.exe", "Collision_Segment_Diagram2.mxd", "Bottom_Split", "Bottom Clip Shape Left"]) # perform a refresh of the data frame using arcpy after setting the data frame geometry.
using System; using System.Collections.Generic; using System.Text; using ESRI.ArcGIS.esriSystem; using ESRI.ArcGIS.Framework; using ESRI.ArcGIS.ArcMapUI; using ESRI.ArcGIS.Carto; using ESRI.ArcGIS.Geodatabase; using ESRI.ArcGIS.ADF; using ESRI.ArcGIS.Geometry; namespace SetFrameClipGeometry { class Program { private static LicenseInitializer m_AOLicenseInitializer = new SetFrameClipGeometry.LicenseInitializer(); [STAThread()] static int Main(string[] args) { //ESRI License Initializer generated code. if (!m_AOLicenseInitializer.InitializeApplication(new esriLicenseProductCode[] { esriLicenseProductCode.esriLicenseProductCodeArcView, esriLicenseProductCode.esriLicenseProductCodeArcEditor, esriLicenseProductCode.esriLicenseProductCodeArcInfo }, new esriLicenseExtensionCode[] { })) { System.Console.WriteLine(m_AOLicenseInitializer.LicenseMessage()); System.Console.WriteLine("This application could not initialize with the correct ArcGIS license and will shutdown."); m_AOLicenseInitializer.ShutdownApplication(); return 1; } //ESRI License Initializer generated code. String mxdName = null; String dfName = null; String layerName = null; if (args.Length < 3) { System.Console.WriteLine("Insufficient parameters. Expected Three Strings"); return 1; } else { mxdName = args[0]; dfName = args[1]; layerName = args[2]; } try { IAppROT aprot = new AppROT(); if (aprot.Count == 0) { System.Console.WriteLine("No ArcMap application is open"); return 1; } IApplication application = null; IMxDocument mxd = null; for (int a = 0; a < aprot.Count; a++) { application = aprot.get_Item(a); System.Console.WriteLine("Search Application Title: " + application.Document.Title); if (application.Document.Title == mxdName) { System.Console.WriteLine("Found Open ArcMap Document Named: " + mxdName); mxd = application.Document as IMxDocument; break; } } IMap pMap = null; IMaps pMaps = null; if (mxd == null) { System.Console.WriteLine("No ArcMap Document Named " + mxdName + " Was Found"); return 1; } pMaps = mxd.Maps; for (int i = 0; i <= pMaps.Count - 1; i++) { pMap = pMaps.get_Item(i); if (pMap.Name == dfName) { Console.WriteLine("Found Data Frame Named: " + pMap.Name); break; } } if (pMap == null) { System.Console.WriteLine("No DataFrame Exists In " + mxdName); return 1; } if (pMap.Name != dfName) { System.Console.WriteLine("No Data Frame Named " + dfName + " Was Found"); return 1; } if (pMap.LayerCount == 0) { return 1; } // Fetch all the feature layers in the focus map // to determine if at least one is selectable. UIDClass uid = new UIDClass(); uid.Value = "{40A9E885-5533-11d0-98BE-00805F7CED21}"; IEnumLayer pEnumLayer = pMap.get_Layers(uid, true); pEnumLayer.Reset(); ILayer pLayer = pEnumLayer.Next(); while (pLayer != null) { if (pLayer.Name == layerName) { Console.WriteLine("Found Layer Named: " + pLayer.Name); break; } pLayer = pEnumLayer.Next(); } if (pLayer == null) { System.Console.WriteLine("No Layer Named " + layerName + " Was Found"); return 1; } using (ComReleaser comReleaser = new ComReleaser()) { IFeatureLayer pFLayer = pLayer as IFeatureLayer; IFeatureCursor pFCursor = pFLayer.Search(null, false); comReleaser.ManageLifetime(pFCursor); IFeature pFeat = null; IGeometry pGeom = null; while ((pFeat = pFCursor.NextFeature()) != null) { pGeom = (IGeometry)pFeat.Shape; } if (pGeom == null) { System.Console.WriteLine("No Feature Geometry Was Found!"); return 1; } pMap.ClipGeometry = pGeom; System.Console.WriteLine("Feature Geometry Set!"); } } catch (Exception e) { Console.WriteLine("{0} Exception caught.", e); System.Console.ReadLine(); return 1; } //System.Console.ReadLine(); // Uncomment this line if you want to pause the Console application before it closes. //Do not make any call to ArcObjects after ShutDownApplication() m_AOLicenseInitializer.ShutdownApplication(); return 0; } } }
#ClipToExtent.py #Clips selected feature layers to the extent of your current mxd #written by Bruce Bacia, 8/12/2012 import arcpy,os,gc,time #define layer to clip exportLayer = arcpy.GetParameterAsText(0) exportLayer = exportLayer.split(";") #set map document to the current open mxd mxd = arcpy.mapping.MapDocument("CURRENT") path = mxd.filePath #get the root path of the mxd path = path.replace(os.path.basename(path),"") #set the tool to overwrite output arcpy.env.workspace = path arcpy.env.overwriteOutput = True #set the dataframe as a polygon df = arcpy.mapping.ListDataFrames(mxd)[0] dfAsFeature = arcpy.Polygon(arcpy.Array([df.extent.lowerLeft, df.extent.lowerRight, df.extent.upperRight, df.extent.upperLeft]),df.spatialReference) #clip the layer to the dataframe extent polygon #send the output feature class to the same folder as the mxd for layer in exportLayer: outFC = path + layer + "_Clip_" + time.strftime('%m_%d_%Y_%H_%M') + ".shp" gc.collect() arcpy.AddMessage("Clipping..... " + outFC) arcpy.Clip_analysis(layer,dfAsFeature,outFC) gc.collect() del mxd del df