Using System.Drawing;Using System.Text;Using System.Windows.Forms;Using System.Runtime.InteropServices;Using ESRI. ArcGIS.ADF.BaseClasses;Using ESRI. ArcGIS.ADF.CATIDs;Using ESRI. Arcgis.controls;Using ESRI. Arcgis.carto;Using ESRI. Arcgis.esrisystem;Using ESRI. Arcgis.geoprocessor;Using ESRI. arcgis.geoprocessing;Using ESRI. Arcgis.analysistools;Namespace gpbufferlayer{PublicPartialClass Bufferdlg:form {In order to scroll the messages textbox to the bottom we must import this Win32 call [DllImport ("User32.dll")]PrivateStaticexternint PostMessage (IntPtr wnd,UINT MSG, IntPtr WParam, IntPtr lParam);Private Ihookhelper M_hookhelper =NullPrivateConstUINT Wm_vscroll = 0x0115;PrivateConstUINT Sb_bottom = 7;Public Bufferdlg (Ihookhelper hookhelper) {InitializeComponent (); m_hookhelper = Hookhelper;}Privatevoid Bufferdlg_load (Object sender, EventArgs e) {if (Null = = M_hookhelper | |Null = = M_hookhelper.hook | | 0 = = M_hookHelper.FocusMap.LayerCount)ReturnLoad all the feature layers in the map to the layers combo Ienumlayer layers = getlayers (); Layers. Reset (); ILayer layer =Nullwhile (layer = layers. Next ())! =NULL) {CBOLAYERS.ITEMS.ADD (layer. Name); }Select the first layerif (CboLayers.Items.Count > 0) cbolayers.selectedindex = 0;String tempdir = System.IO.Path.GetTempPath (); Txtoutputpath.text = System.IO.Path.Combine (TempDir, (String) Cbolayers.selecteditem +"_buffer.shp"));Set the default units of the bufferint units = Convert.ToInt32 (m_hookHelper.FocusMap.MapUnits); Cbounits.selectedindex = units; }Privatevoid Btnoutputlayer_click (Object sender, EventArgs e) {Set the output layer SaveFileDialog Savedlg =New SaveFileDialog (); Savedlg.checkpathexists =True Savedlg.filter ="Shapefiles (*.SHP) |*.shp"; Savedlg.overwriteprompt =True Savedlg.title ="Output Layer"; Savedlg.restoredirectory =True Savedlg.filename = (String) Cbolayers.selecteditem +"_buffer.shp"; DialogResult dr = Savedlg.showdialog ();if (dr = = DialogResult.OK) Txtoutputpath.text = Savedlg.filename; }Privatevoid Btnbuffer_click (Object sender, EventArgs e) {Make sure, all parameters is okayDouble bufferdistance;Double. TryParse (Txtbufferdistance.text,Out bufferdistance);if (0.0 = = bufferdistance) {MessageBox.Show ("Bad buffer distance!");Return }if (! System.IO.Directory.Exists (System.IO.Path.GetDirectoryName (txtoutputpath.text)) | |". shp"! = System.IO.Path.GetExtension (Txtoutputpath.text)) {MessageBox.Show ("Bad output filename!");Return }if (M_hookHelper.FocusMap.LayerCount = = 0)ReturnGet the layer from the map ifeaturelayer layer = Getfeaturelayer (string) cbolayers.selecteditem);if (NULL = = Layer) {Txtmessages.text + ="Layer" + (String) Cbolayers.selecteditem +"Cannot be found!\r\n";Return }Scroll the textbox to the bottom Scrolltobottom ();Add message to the messages box Txtmessages.text + ="Buffering layer:" + layer. Name +"\ r \ n"; Txtmessages.text + ="\r\nget the Geoprocessor. This might take a few seconds...\r\n "; Txtmessages.update ();Get an instance of the Geoprocessor geoprocessor GP =New Geoprocessor (); Gp. Overwriteoutput =True Txtmessages.text + ="Buffering...\r\n"; Txtmessages.update ();Create a new instance of a buffer tool ESRI. ArcGIS.AnalysisTools.Buffer Buffer =New ESRI. ArcGIS.AnalysisTools.Buffer (layer, Txtoutputpath.text, convert.tostring (bufferdistance) +" " + (string) cbounits.selecteditem);Execute the Buffer tool (very easy:-)) igeoprocessorresult results = (igeoprocessorresult) GP. Execute (Buffer,NULL);if (results. Status! = esrijobstatus.esrijobsucceeded) {Txtmessages.text + ="Failed to Buffer layer:" + layer. Name +"\ r \ n"; } Txtmessages.text + = Returnmessages (GP);Scroll the textbox to the bottom Scrolltobottom (); Txtmessages.text + ="\r\ndone.\r\n"; Txtmessages.text + ="-----------------------------------------------------------------------------------------\ r \ n";Scroll the textbox to the bottom Scrolltobottom (); }PrivateString Returnmessages (Geoprocessor gp) {StringBuilder SB =New StringBuilder ();if (GP. Messagecount > 0) {for (int Count = 0; Count <= GP. MessageCount-1; count++) {System.Diagnostics.Trace.WriteLine (GP). GetMessage (Count)); Sb. AppendFormat ("{0}\n", GP. GetMessage (Count)); } }Return SB. ToString (); }Private Ifeaturelayer Getfeaturelayer (String layername) {Get the layers from the maps ienumlayer layers = getlayers (); Layers. Reset (); ILayer layer =Nullwhile (layer = layers. Next ())! = null) {if (layer. Name = = layername) return layer as Ifeaturelayer;} returnnull;} private ienumlayer getlayers () {uid uid = new uidclass (); uid. Value = "{40a9e885-5533-11d0-98be-00805f7ced21}"; Ienumlayer layers = m_hookHelper.FocusMap.get_Layers (uid, true); return layers;} privatevoid Scrolltobottom () {PostMessage ((IntPtr) Txtmessages.handle, WM_ VScroll, (INTPTR) Sb_bottom, (INTPTR) IntPtr.Zero); } privatevoid Btncancel_click (object sender, EventArgs e) { this. Close (); } }}
Create a geoprocessing tool to buffer a layer and retrieve Messages____sync