ArcGIS二次开发课程总结(附上代码)vs2010(C#)+AE

很久没有写了,原因很简单没有想起来,够简单粗暴吧,好吧承认都是借口

—————————————————我是分界线哦————————————————————————

    昨天GIS程序与开发课结束了它的完整生命周期,老师让我们每个人做个总结。学号最后一个,便最后一个总结。

课程上完,最大感受,嗯,我又学会了怎么拼代码!老师给我说了我的优缺点,可能当时没反应过来,今天早上才反应过来

,所以写下该文。工作做的很多,成果别人看不到,自己学到的帮助其他同学,自己却不能从他们那获得有用的知识,这也是要求我多总结

,并且是阶段性的总结。那就列一下自己课程代码吧,项目名称:《道路分析系统》,基于vs2010(C#开发)+ArcEngine10.2+ArcGIS10.2

虽然我是代码搬运工,但我不在乎啊。

-----------------------------------------------

主窗体代码

  1 // This is the main form of the application.
  2 
  3 namespace NAEngine
  4 {
  5     /// 
  6     /// Summary description for Form1.
  7     /// 
  8     public class frmMain : System.Windows.Forms.Form
  9     {
 10         /// 
 11         /// Required designer variable.
 12         /// 
 13         private System.ComponentModel.Container components = null;
 14         private System.Windows.Forms.Splitter splitter1;
 15 
 16         // Context menu objects for NAWindow's context menu
 17         private System.Windows.Forms.ContextMenu contextMenu1;
 18         private System.Windows.Forms.MenuItem miLoadLocations;
 19         private System.Windows.Forms.MenuItem miClearLocations;
 20         private System.Windows.Forms.MenuItem miAddItem;
 21 
 22         // ArcGIS Controls on the form
 23         private ESRI.ArcGIS.Controls.AxMapControl axMapControl1;
 24         private ESRI.ArcGIS.Controls.AxLicenseControl axLicenseControl1;
 25         private ESRI.ArcGIS.Controls.AxToolbarControl axToolbarControl1;
 26         private ESRI.ArcGIS.Controls.AxTOCControl axTOCControl1;
 27 
 28         // Listen for context menu on NAWindow
 29         private IEngineNAWindowEventsEx_OnContextMenuEventHandler m_onContextMenu;
 30 
 31         // Reference to Network Analyst Environment
 32         private IEngineNetworkAnalystEnvironment m_naEnv;
 33 
 34         // Reference to NAWindow.  Need to hold on to reference for events to work.
 35         private IEngineNAWindow m_naWindow;
 36 
 37         // Menu for our commands on the TOC context menu
 38         private IToolbarMenu m_menuLayer;
 39         private MenuStrip menuStrip1;
 40         private ToolStripMenuItem 文件ToolStripMenuItem;
 41         private ToolStripMenuItem ToolStripMenuItemQueryByAttribute;
 42         private ToolStripMenuItem ToolStripMenuItemQueryBySpatial;
 43         private ToolStripMenuItem ToolStripMenuItemOpen;
 44         private StatusStrip statusStrip1;
 45         private ToolStripStatusLabel toolStripStatusMessage;
 46         private ToolStripStatusLabel toolStripStatusBlank;
 47         private ToolStripStatusLabel toolStripStatusScale;
 48         private ToolStripStatusLabel toolStripStatusCoordinates;
 49 
 50         // incrementor for auto generated names
 51         private static int autogenInt = 0;
 52 
 53         public frmMain()
 54         {
 55             //
 56             // Required for Windows Form Designer support
 57             //
 58             InitializeComponent();
 59         }
 60 
 61         /// 
 62         /// Clean up any resources being used.
 63         /// 
 64         protected override void Dispose(bool disposing)
 65         {
 66             ESRI.ArcGIS.ADF.COMSupport.AOUninitialize.Shutdown();
 67 
 68             if (disposing)
 69             {
 70                 if (components != null)
 71                 {
 72                     components.Dispose();
 73                 }
 74             }
 75             base.Dispose(disposing);
 76         }
 77 
 78         #region Windows Form Designer generated code
 79         /// 
 80         /// Required method for Designer support - do not modify
 81         /// the contents of this method with the code editor.
 82         /// 
 83         private void InitializeComponent()
 84         {
 85             System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(frmMain));
 86             this.axMapControl1 = new ESRI.ArcGIS.Controls.AxMapControl();
 87             this.axLicenseControl1 = new ESRI.ArcGIS.Controls.AxLicenseControl();
 88             this.axToolbarControl1 = new ESRI.ArcGIS.Controls.AxToolbarControl();
 89             this.splitter1 = new System.Windows.Forms.Splitter();
 90             this.axTOCControl1 = new ESRI.ArcGIS.Controls.AxTOCControl();
 91             this.contextMenu1 = new System.Windows.Forms.ContextMenu();
 92             this.miLoadLocations = new System.Windows.Forms.MenuItem();
 93             this.miClearLocations = new System.Windows.Forms.MenuItem();
 94             this.miAddItem = new System.Windows.Forms.MenuItem();
 95             this.menuStrip1 = new System.Windows.Forms.MenuStrip();
 96             this.文件ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
 97             this.ToolStripMenuItemOpen = new System.Windows.Forms.ToolStripMenuItem();
 98             this.ToolStripMenuItemQueryByAttribute = new System.Windows.Forms.ToolStripMenuItem();
 99             this.ToolStripMenuItemQueryBySpatial = new System.Windows.Forms.ToolStripMenuItem();
100             this.statusStrip1 = new System.Windows.Forms.StatusStrip();
101             this.toolStripStatusMessage = new System.Windows.Forms.ToolStripStatusLabel();
102             this.toolStripStatusBlank = new System.Windows.Forms.ToolStripStatusLabel();
103             this.toolStripStatusScale = new System.Windows.Forms.ToolStripStatusLabel();
104             this.toolStripStatusCoordinates = new System.Windows.Forms.ToolStripStatusLabel();
105             ((System.ComponentModel.ISupportInitialize)(this.axMapControl1)).BeginInit();
106             ((System.ComponentModel.ISupportInitialize)(this.axLicenseControl1)).BeginInit();
107             ((System.ComponentModel.ISupportInitialize)(this.axToolbarControl1)).BeginInit();
108             ((System.ComponentModel.ISupportInitialize)(this.axTOCControl1)).BeginInit();
109             this.menuStrip1.SuspendLayout();
110             this.statusStrip1.SuspendLayout();
111             this.SuspendLayout();
112             // 
113             // axMapControl1
114             // 
115             this.axMapControl1.Dock = System.Windows.Forms.DockStyle.Fill;
116             this.axMapControl1.Location = new System.Drawing.Point(173, 53);
117             this.axMapControl1.Name = "axMapControl1";
118             this.axMapControl1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axMapControl1.OcxState")));
119             this.axMapControl1.Size = new System.Drawing.Size(706, 454);
120             this.axMapControl1.TabIndex = 2;
121             this.axMapControl1.OnMouseMove += new ESRI.ArcGIS.Controls.IMapControlEvents2_Ax_OnMouseMoveEventHandler(this.axMapControl1_OnMouseMove);
122             // 
123             // axLicenseControl1
124             // 
125             this.axLicenseControl1.Enabled = true;
126             this.axLicenseControl1.Location = new System.Drawing.Point(797, 0);
127             this.axLicenseControl1.Name = "axLicenseControl1";
128             this.axLicenseControl1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axLicenseControl1.OcxState")));
129             this.axLicenseControl1.Size = new System.Drawing.Size(32, 32);
130             this.axLicenseControl1.TabIndex = 1;
131             // 
132             // axToolbarControl1
133             // 
134             this.axToolbarControl1.Dock = System.Windows.Forms.DockStyle.Top;
135             this.axToolbarControl1.Location = new System.Drawing.Point(0, 25);
136             this.axToolbarControl1.Name = "axToolbarControl1";
137             this.axToolbarControl1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axToolbarControl1.OcxState")));
138             this.axToolbarControl1.Size = new System.Drawing.Size(879, 28);
139             this.axToolbarControl1.TabIndex = 0;
140             // 
141             // splitter1
142             // 
143             this.splitter1.Location = new System.Drawing.Point(170, 53);
144             this.splitter1.Name = "splitter1";
145             this.splitter1.Size = new System.Drawing.Size(3, 454);
146             this.splitter1.TabIndex = 4;
147             this.splitter1.TabStop = false;
148             // 
149             // axTOCControl1
150             // 
151             this.axTOCControl1.Dock = System.Windows.Forms.DockStyle.Left;
152             this.axTOCControl1.Location = new System.Drawing.Point(0, 53);
153             this.axTOCControl1.Name = "axTOCControl1";
154             this.axTOCControl1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axTOCControl1.OcxState")));
155             this.axTOCControl1.Size = new System.Drawing.Size(170, 454);
156             this.axTOCControl1.TabIndex = 1;
157             this.axTOCControl1.OnMouseDown += new ESRI.ArcGIS.Controls.ITOCControlEvents_Ax_OnMouseDownEventHandler(this.axTOCControl1_OnMouseDown);
158             // 
159             // contextMenu1
160             // 
161             this.contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
162             this.miLoadLocations,
163             this.miClearLocations});
164             // 
165             // miLoadLocations
166             // 
167             this.miLoadLocations.Index = 0;
168             this.miLoadLocations.Text = "Load Locations...";
169             this.miLoadLocations.Click += new System.EventHandler(this.miLoadLocations_Click);
170             // 
171             // miClearLocations
172             // 
173             this.miClearLocations.Index = 1;
174             this.miClearLocations.Text = "Clear Locations";
175             this.miClearLocations.Click += new System.EventHandler(this.miClearLocations_Click);
176             // 
177             // miAddItem
178             // 
179             this.miAddItem.Index = -1;
180             this.miAddItem.Text = "Add Item";
181             this.miAddItem.Click += new System.EventHandler(this.miAddItem_Click);
182             // 
183             // menuStrip1
184             // 
185             this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
186             this.文件ToolStripMenuItem,
187             this.ToolStripMenuItemQueryByAttribute,
188             this.ToolStripMenuItemQueryBySpatial});
189             this.menuStrip1.Location = new System.Drawing.Point(0, 0);
190             this.menuStrip1.Name = "menuStrip1";
191             this.menuStrip1.Size = new System.Drawing.Size(879, 25);
192             this.menuStrip1.TabIndex = 5;
193             this.menuStrip1.Text = "menuStrip1";
194             // 
195             // 文件ToolStripMenuItem
196             // 
197             this.文件ToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
198             this.ToolStripMenuItemOpen});
199             this.文件ToolStripMenuItem.Name = "文件ToolStripMenuItem";
200             this.文件ToolStripMenuItem.Size = new System.Drawing.Size(44, 21);
201             this.文件ToolStripMenuItem.Text = "文件";
202             // 
203             // ToolStripMenuItemOpen
204             // 
205             this.ToolStripMenuItemOpen.Name = "ToolStripMenuItemOpen";
206             this.ToolStripMenuItemOpen.Size = new System.Drawing.Size(100, 22);
207             this.ToolStripMenuItemOpen.Text = "打开";
208             this.ToolStripMenuItemOpen.Click += new System.EventHandler(this.ToolStripMenuItemOpen_Click);
209             // 
210             // ToolStripMenuItemQueryByAttribute
211             // 
212             this.ToolStripMenuItemQueryByAttribute.Name = "ToolStripMenuItemQueryByAttribute";
213             this.ToolStripMenuItemQueryByAttribute.Size = new System.Drawing.Size(68, 21);
214             this.ToolStripMenuItemQueryByAttribute.Text = "属性查询";
215             this.ToolStripMenuItemQueryByAttribute.Click += new System.EventHandler(this.ToolStripMenuItemQueryByAttribute_Click);
216             // 
217             // ToolStripMenuItemQueryBySpatial
218             // 
219             this.ToolStripMenuItemQueryBySpatial.Name = "ToolStripMenuItemQueryBySpatial";
220             this.ToolStripMenuItemQueryBySpatial.Size = new System.Drawing.Size(68, 21);
221             this.ToolStripMenuItemQueryBySpatial.Text = "空间查询";
222             this.ToolStripMenuItemQueryBySpatial.Click += new System.EventHandler(this.ToolStripMenuItemQueryBySpatial_Click);
223             // 
224             // statusStrip1
225             // 
226             this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
227             this.toolStripStatusMessage,
228             this.toolStripStatusBlank,
229             this.toolStripStatusScale,
230             this.toolStripStatusCoordinates});
231             this.statusStrip1.Location = new System.Drawing.Point(173, 485);
232             this.statusStrip1.Name = "statusStrip1";
233             this.statusStrip1.Size = new System.Drawing.Size(706, 22);
234             this.statusStrip1.TabIndex = 10;
235             this.statusStrip1.Text = "statusStrip1";
236             // 
237             // toolStripStatusMessage
238             // 
239             this.toolStripStatusMessage.Name = "toolStripStatusMessage";
240             this.toolStripStatusMessage.Size = new System.Drawing.Size(32, 17);
241             this.toolStripStatusMessage.Text = "就绪";
242             // 
243             // toolStripStatusBlank
244             // 
245             this.toolStripStatusBlank.Name = "toolStripStatusBlank";
246             this.toolStripStatusBlank.Size = new System.Drawing.Size(535, 17);
247             this.toolStripStatusBlank.Spring = true;
248             // 
249             // toolStripStatusScale
250             // 
251             this.toolStripStatusScale.Name = "toolStripStatusScale";
252             this.toolStripStatusScale.Size = new System.Drawing.Size(56, 17);
253             this.toolStripStatusScale.Text = "当前坐标";
254             // 
255             // toolStripStatusCoordinates
256             // 
257             this.toolStripStatusCoordinates.Name = "toolStripStatusCoordinates";
258             this.toolStripStatusCoordinates.Size = new System.Drawing.Size(68, 17);
259             this.toolStripStatusCoordinates.Text = "当前比例尺";
260             this.toolStripStatusCoordinates.Click += new System.EventHandler(this.toolStripStatusCoordinates_Click);
261             // 
262             // frmMain
263             // 
264             this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
265             this.ClientSize = new System.Drawing.Size(879, 507);
266             this.Controls.Add(this.statusStrip1);
267             this.Controls.Add(this.axLicenseControl1);
268             this.Controls.Add(this.axMapControl1);
269             this.Controls.Add(this.splitter1);
270             this.Controls.Add(this.axTOCControl1);
271             this.Controls.Add(this.axToolbarControl1);
272             this.Controls.Add(this.menuStrip1);
273             this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
274             this.MainMenuStrip = this.menuStrip1;
275             this.Name = "frmMain";
276             this.Text = "Road analysis System";
277             this.Load += new System.EventHandler(this.frmMain_Load);
278             ((System.ComponentModel.ISupportInitialize)(this.axMapControl1)).EndInit();
279             ((System.ComponentModel.ISupportInitialize)(this.axLicenseControl1)).EndInit();
280             ((System.ComponentModel.ISupportInitialize)(this.axToolbarControl1)).EndInit();
281             ((System.ComponentModel.ISupportInitialize)(this.axTOCControl1)).EndInit();
282             this.menuStrip1.ResumeLayout(false);
283             this.menuStrip1.PerformLayout();
284             this.statusStrip1.ResumeLayout(false);
285             this.statusStrip1.PerformLayout();
286             this.ResumeLayout(false);
287             this.PerformLayout();
288 
289         }
290         #endregion
291 
292         /// 
293         /// The main entry point for the application.
294         /// 
295         [STAThread]
296         static void Main()
297         {
298             if (!ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.Engine))
299             {
300                 if (!ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.Desktop))
301                 {
302                     if (ESRI.ArcGIS.RuntimeManager.ActiveRuntime.Version == "") // "" is less than 10
303                     {
304                         System.Windows.Forms.MessageBox.Show("Loading ArcGIS Version Failed");
305                         return;
306                     }
307                 }
308             }
309 
310             Application.Run(new frmMain());
311         }
312 
313         private void frmMain_Load(object sender, System.EventArgs e)
314         {
315             // Add commands to the NALayer context menu
316             m_menuLayer = new ToolbarMenuClass();
317 
318             int nItem = -1;
319             m_menuLayer.AddItem(new cmdLoadLocations(), -1, ++nItem, false, esriCommandStyles.esriCommandStyleTextOnly);
320             m_menuLayer.AddItem(new cmdRemoveLayer(), -1, ++nItem, false, esriCommandStyles.esriCommandStyleTextOnly);
321             m_menuLayer.AddItem(new cmdClearAnalysisLayer(), -1, ++nItem, true, esriCommandStyles.esriCommandStyleTextOnly);
322             m_menuLayer.AddItem(new cmdNALayerProperties(), -1, ++nItem, true, esriCommandStyles.esriCommandStyleTextOnly);
323             m_menuLayer.SetHook(axMapControl1);
324 
325             // Add command for Network Analyst env properties to end of "Network Analyst" dropdown menu
326             nItem = -1;
327             for (int i = 0; i < axToolbarControl1.Count; ++i)
328             {
329                 IToolbarItem item = axToolbarControl1.GetItem(i);
330                 IToolbarMenu mnu = item.Menu;
331 
332                 if (mnu == null)
333                     continue;
334 
335                 IMenuDef mnudef = mnu.GetMenuDef();
336                 string name = mnudef.Name;
337                 if (name == "ControlToolsNetworkAnalyst_SolverMenu")
338                 {
339                     nItem = i;
340                     break;
341                 }
342             }
343 
344             if (nItem >= 0)
345             {
346                 IToolbarItem item = axToolbarControl1.GetItem(nItem);
347                 IToolbarMenu mnu = item.Menu;
348                 if (mnu != null)
349                     mnu.AddItem(new cmdNAProperties(), -1, mnu.Count, true, esriCommandStyles.esriCommandStyleTextOnly);
350             }
351 
352             // Initialize naEnv variables
353             m_naEnv = new EngineNetworkAnalystEnvironmentClass();
354             m_naEnv.ZoomToResultAfterSolve = false;
355             m_naEnv.ShowAnalysisMessagesAfterSolve = (int)(esriEngineNAMessageType.esriEngineNAMessageTypeInformative | esriEngineNAMessageType.esriEngineNAMessageTypeWarning);
356 
357             // Explicitly setup buddy control and initialize NA extension 
358             // so we can get to NAWindow to listen to window events
359             // This is necessary the various controls are not yet setup and they
360             // need to be in order to get the NAWindow's events.
361             axToolbarControl1.SetBuddyControl(axMapControl1);
362             IExtension ext = m_naEnv as IExtension;
363             object obj = axToolbarControl1.Object;
364             ext.Startup(ref obj);
365             m_naWindow = m_naEnv.NAWindow;
366             m_onContextMenu = new IEngineNAWindowEventsEx_OnContextMenuEventHandler(OnContextMenu);
367             ((IEngineNAWindowEventsEx_Event)m_naWindow).OnContextMenu += m_onContextMenu;
368         }
369 
370         //  Show the TOC context menu when an NALayer is right-clicked on
371         private void axTOCControl1_OnMouseDown(object sender, ESRI.ArcGIS.Controls.ITOCControlEvents_OnMouseDownEvent e)
372         {
373             if (e.button != 2) return;
374 
375             esriTOCControlItem item = esriTOCControlItem.esriTOCControlItemNone;
376             IBasicMap map = null;
377             ILayer layer = null;
378             object other = null;
379             object index = null;
380 
381             //Determine what kind of item has been clicked on
382             axTOCControl1.HitTest(e.x, e.y, ref item, ref map, ref layer, ref other, ref index);
383 
384             // Only implemented a context menu for NALayers.  Exit if the layer is anything else.
385             if ((layer as INALayer) == null)
386                 return;
387 
388             axTOCControl1.SelectItem(layer);
389 
390             // Set the layer into the CustomProperty.
391             // This is used by the other commands to know what layer was right-clicked on
392             // in the table of contents.            
393             axMapControl1.CustomProperty = layer;
394 
395             //Popup the correct context menu and update the TOC when it's done.
396             if (item == esriTOCControlItem.esriTOCControlItemLayer)
397             {
398                 m_menuLayer.PopupMenu(e.x, e.y, axTOCControl1.hWnd);
399                 ITOCControl toc = axTOCControl1.Object as ITOCControl;
400                 toc.Update();
401             }
402         }
403 
404         public bool OnContextMenu(int x, int y)
405         {
406             System.Drawing.Point pt = this.PointToClient(System.Windows.Forms.Cursor.Position);
407 
408             //Get the active category
409             IEngineNAWindowCategory2 activeCategory = m_naWindow.ActiveCategory as IEngineNAWindowCategory2;
410             if (activeCategory == null)
411                 return false;
412 
413             MenuItem separator = new MenuItem("-");
414 
415             miLoadLocations.Enabled = false;
416             miClearLocations.Enabled = false;
417 
418             // in order for the AddItem choice to appear in the context menu, the class
419             // should be an input class, and it should not be editable
420             INAClassDefinition pNAClassDefinition = activeCategory.NAClass.ClassDefinition;
421             if (pNAClassDefinition.IsInput)
422             {
423 
424                 miLoadLocations.Enabled = true;
425                 miClearLocations.Enabled = true;
426 
427                 // canEditShape should be false for AddItem to Apply (default is false)
428                 // if it's a StandaloneTable canEditShape is implicitly false (there's no shape to edit)
429                 bool canEditShape = false;
430                 IFields pFields = pNAClassDefinition.Fields;
431                 int nField = -1;
432                 nField = pFields.FindField("Shape");
433                 if (nField >= 0)
434                 {
435                     int naFieldType = 0;
436                     naFieldType = pNAClassDefinition.get_FieldType("Shape");
437 
438                     // determining whether or not the shape field can be edited consists of running a bitwise comparison
439                     // on the FieldType of the shape field.  See the online help for a list of the possible field types.
440                     // For our case, we want to verify that the shape field is an input field.  If it is an input field, 
441                     // then we do NOT want to display the Add Item menu option.
442                     canEditShape = ((naFieldType & (int)esriNAFieldType.esriNAFieldTypeInput) == (int)esriNAFieldType.esriNAFieldTypeInput) ? true : false;
443                 }
444 
445                 if (!canEditShape)
446                 {
447                     contextMenu1.MenuItems.Add(separator);
448                     contextMenu1.MenuItems.Add(miAddItem);
449                 }
450             }
451 
452             contextMenu1.Show(this, pt);
453 
454             // even if the miAddItem menu item has not been added, Remove() won't crash.
455             contextMenu1.MenuItems.Remove(separator);
456             contextMenu1.MenuItems.Remove(miAddItem);
457 
458             return true;
459         }
460 
461         private void miLoadLocations_Click(object sender, System.EventArgs e)
462         {
463             IMapControl3 mapControl = (IMapControl3)axMapControl1.Object;
464 
465             // Show the Property Page form for Network Analyst
466             frmLoadLocations loadLocations = new frmLoadLocations();
467             if (loadLocations.ShowModal(mapControl, m_naEnv))
468             {
469                 // notify that the context has changed because we have added locations to a NAClass within it
470                 INAContextEdit contextEdit = m_naEnv.NAWindow.ActiveAnalysis.Context as INAContextEdit;
471                 contextEdit.ContextChanged();
472 
473                 // If loaded locations, refresh the NAWindow and the Screen
474                 INALayer naLayer = m_naWindow.ActiveAnalysis;
475                 mapControl.Refresh(esriViewDrawPhase.esriViewGeography, naLayer, mapControl.Extent);
476                 m_naWindow.UpdateContent(m_naWindow.ActiveCategory);
477             }
478         }
479 
480         private void miClearLocations_Click(object sender, System.EventArgs e)
481         {
482             IMapControl3 mapControl = (IMapControl3)axMapControl1.Object;
483 
484             IEngineNetworkAnalystHelper naHelper = m_naEnv as IEngineNetworkAnalystHelper;
485             IEngineNAWindow naWindow = m_naWindow;
486             INALayer naLayer = naWindow.ActiveAnalysis;
487 
488             // we do not have to run ContextChanged() as with adding an item and loading locations,
489             // because that is done by the DeleteAllNetworkLocations method.
490             naHelper.DeleteAllNetworkLocations();
491 
492             mapControl.Refresh(esriViewDrawPhase.esriViewGeography, naLayer, mapControl.Extent);
493         }
494 
495         private void miAddItem_Click(object sender, System.EventArgs e)
496         {
497             // Developers Note:
498             // Once an item has been added, the user can double click on the item to edit the properties
499             // of the item.  For the purposes of this sample, only the default values from the InitDefaultValues method
500             // and an auto generated Name value are populated initially for the new item.
501 
502             IMapControl3 mapControl = (IMapControl3)axMapControl1.Object;
503 
504             IEngineNAWindowCategory2 activeCategory = m_naWindow.ActiveCategory as IEngineNAWindowCategory2;
505             IDataLayer pDataLayer = activeCategory.DataLayer;
506 
507             // In order to add an item, we need to create a new row in the class and populate it 
508             // with the initial default values for that class.
509             ITable table = pDataLayer as ITable;
510             IRow row = table.CreateRow();
511             IRowSubtypes rowSubtypes = row as IRowSubtypes;
512             rowSubtypes.InitDefaultValues();
513 
514             // we need to auto generate a display name for the newly added item.
515             // In some cases (depending on how the schema is set up) InitDefaultValues may result in a nonempty name string 
516             // in these cases do not override the preexisting non-empty name string with our auto generated one.
517             IFeatureLayer ipFeatureLayer = activeCategory.Layer as IFeatureLayer;
518             IStandaloneTable ipStandaloneTable = pDataLayer as IStandaloneTable;
519             string name = "";
520             if (ipFeatureLayer != null)
521                 name = ipFeatureLayer.DisplayField;
522             else if (ipStandaloneTable != null)
523                 name = ipStandaloneTable.DisplayField;
524 
525             //If the display field is an empty string or does not represent an actual field on the NAClass just skip the auto generation.  
526             // (Some custom solvers may not have set the DisplayField for example).
527             // Note:  The name we are auto generating does not have any spaces in it.  This is to ensure that any classes 
528             // that are space sensitive will be able to handle the name (ex Specialties).
529             string currentName = "";
530             int fieldIndex = row.Fields.FindField(name);
531             if (fieldIndex >= 0)
532             {
533                 currentName = row.get_Value(fieldIndex) as string;
534                 if (currentName.Length <= 0)
535                     row.set_Value(fieldIndex, "Item" + ++autogenInt);
536             }
537 
538             // A special case is OrderPairs NAClass because that effectively has a combined 2 field display field.  
539             // You will have to hard code to look for that NAClassName and create a default name for 
540             // both first order and second order field names so the name will display correctly 
541             // (look for the NAClass Name and NOT the layer name).
542             INAClassDefinition naClassDef = activeCategory.NAClass.ClassDefinition;
543             if (naClassDef.Name == "OrderPairs")
544             {
545                 fieldIndex = row.Fields.FindField("SecondOrderName");
546                 if (fieldIndex >= 0)
547                 {
548                     string secondName = row.get_Value(fieldIndex) as string;
549                     if (secondName.Length <= 0)
550                         row.set_Value(fieldIndex, "Item" + ++autogenInt);
551                 }
552             }
553 
554             row.Store();
555 
556             // notify that the context has changed because we have added an item to a NAClass within it
557             INAContextEdit contextEdit = m_naEnv.NAWindow.ActiveAnalysis.Context as INAContextEdit;
558             contextEdit.ContextChanged();
559 
560             // refresh the NAWindow and the Screen
561             INALayer naLayer = m_naWindow.ActiveAnalysis;
562             mapControl.Refresh(esriViewDrawPhase.esriViewGeography, naLayer, mapControl.Extent);
563             m_naWindow.UpdateContent(m_naWindow.ActiveCategory);
564         }
565 
566 
567         //打开文档
568         private void ToolStripMenuItemOpen_Click(object sender, EventArgs e)
569         {
570             //使用对话框选择要打开的mxd文档
571             using (OpenFileDialog openFileDialog = new OpenFileDialog())
572             {
573                 openFileDialog.Filter = "ArcMap Documents (*.mxd)|*.mxd";
574                 if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
575                 {
576                     string filePath = openFileDialog.FileName;
577                     axMapControl1.LoadMxFile(filePath);
578                 }
579             }
580         }        
581 
582         //属性查询&空间查询
583 
584         private void ToolStripMenuItemQueryBySpatial_Click(object sender, EventArgs e)
585         {
586             //新创建空间查询窗体
587             FormQueryBySpatial formQueryBySpatial = new FormQueryBySpatial();
588             //将当前主窗体中MapControl控件中的Map对象赋值给FormSelection窗体的CurrentMap属性
589             formQueryBySpatial.CurrentMap = axMapControl1.Map;
590             //显示空间查询窗体
591             formQueryBySpatial.Show();
592         }
593 
594         private void ToolStripMenuItemQueryByAttribute_Click(object sender, EventArgs e)
595         {
596             //新创建属性查询窗体
597             FormQueryByAttribute formQueryByAttribute = new FormQueryByAttribute();
598             //将当前主窗体中MapControl控件中的Map对象赋值给FormQueryByAttribute窗体的CurrentMap属性
599             formQueryByAttribute.CurrentMap = axMapControl1.Map;
600             //显示属性查询窗体
601             formQueryByAttribute.Show();
602         }                   
603 
604         //脚标 坐标系等信息  
605        
606         private void toolStripStatusCoordinates_Click(object sender, EventArgs e)
607         {
608 
609         }
610 
611         private void axMapControl1_OnMouseMove(object sender, IMapControlEvents2_OnMouseMoveEvent e)
612         {
613             // 显示当前比例尺,整数
614             toolStripStatusScale.Text = " 比例尺 1:" + axMapControl1.MapScale.ToString("f0");
615 
616             // 显示当前坐标,保留小数点后四位
617             toolStripStatusCoordinates.Text = String.Format(" 当前坐标 X = {0}, Y={1} {2}",
618                                                                                                 e.mapX.ToString("f4"),
619                                                                                                 e.mapY.ToString("f4"),
620                                                                                                 YCMap.Utils.SystemHelper.ConvertEsriUnit(axMapControl1.MapUnits));
621         }
622 
623         private void axToolbarControl_OnMouseMove(object sender, IToolbarControlEvents_OnMouseMoveEvent e)
624         {
625             // 取得鼠标所在工具的索引号
626             int index = axToolbarControl1.HitTest(e.x, e.y, false);
627             if (index != -1)
628             {
629                 // 取得鼠标所在工具的 ToolbarItem
630                 IToolbarItem toolbarItem = axToolbarControl1.GetItem(index);
631                 // 设置状态栏信息
632                 toolStripStatusMessage.Text = toolbarItem.Command.Message;
633             }
634             else
635             {
636                 toolStripStatusMessage.Text = " 就绪 ";
637             }
638         }
639     }
640 }
View Code

空间查询

  1 namespace NAEngine
  2 {
  3     public partial class FormQueryBySpatial : Form
  4     {
  5         private IMap currentMap;    //当前MapControl控件中的Map对象        
  6 
  7         /// 
  8         /// 获得当前MapControl控件中的Map对象。
  9         /// 
 10         public IMap CurrentMap
 11         {
 12             set
 13             {
 14                 currentMap = value;
 15             }
 16         }
 17 
 18         public FormQueryBySpatial()
 19         {
 20             InitializeComponent();
 21         }
 22 
 23         //窗体加载时触发事件,执行本函数
 24         private void FormQueryBySpatial_Load(object sender, EventArgs e)
 25         {
 26             try
 27             {
 28                 //清空目标图层列表
 29                 checkedListBoxTargetLayers.Items.Clear();
 30 
 31                 string layerName;   //设置临时变量存储图层名称
 32 
 33                 //对Map中的每个图层进行判断并添加图层名称
 34                 for (int i = 0; i < currentMap.LayerCount; i++)
 35                 {
 36                     //如果该图层为图层组类型,则分别对所包含的每个图层进行操作
 37                     if (currentMap.get_Layer(i) is GroupLayer)
 38                     {
 39                         //使用ICompositeLayer接口进行遍历操作
 40                         ICompositeLayer compositeLayer = currentMap.get_Layer(i) as ICompositeLayer;
 41                         for (int j = 0; j < compositeLayer.Count; j++)
 42                         {
 43                             //将图层的名称添加到checkedListBoxTargetLayers控件和comboBoxMethods控件中
 44                             layerName = compositeLayer.get_Layer(j).Name;
 45                             checkedListBoxTargetLayers.Items.Add(layerName);
 46                             comboBoxSourceLayer.Items.Add(layerName);
 47                         }
 48                     }
 49                     //如果图层不是图层组类型,则直接添加名称
 50                     else
 51                     {
 52                         layerName = currentMap.get_Layer(i).Name;
 53                         checkedListBoxTargetLayers.Items.Add(layerName);
 54                         comboBoxSourceLayer.Items.Add(layerName);
 55                     }
 56                 }
 57 
 58                 //将comboBoxSourceLayer控件的默认选项设置为第一个图层的名称
 59                 comboBoxSourceLayer.SelectedIndex = 0;
 60                 //将comboBoxMethods控件的默认选项设置为第一种空间选择方法
 61                 comboBoxMethods.SelectedIndex = 0;
 62             }
 63             catch { }
 64         }
 65 
 66         private void buttonClose_Click(object sender, EventArgs e)
 67         {
 68             this.Close();
 69         }
 70 
 71         private void buttonApply_Click(object sender, EventArgs e)
 72         {
 73             try
 74             {
 75                 SelectFeaturesBySpatial();
 76             }
 77             catch 
 78             { }
 79         }
 80 
 81         private void buttonOK_Click(object sender, EventArgs e)
 82         {
 83             try
 84             {
 85                 SelectFeaturesBySpatial();
 86                 this.Close();
 87             }
 88             catch
 89             { }
 90         }
 91 
 92         /// 
 93         /// 在地图中根据图层名称获得矢量图层。
 94         /// 
 95         /// 当前地图
 96         /// 图层名称
 97         /// IFeatureLayer接口的矢量图层对象
 98         private IFeatureLayer GetFeatureLayerByName(IMap map, string layerName)
 99         {
100             //对地图中的图层进行遍历
101             for (int i = 0; i < map.LayerCount; i++)
102             {
103                 //如果该图层为图层组类型,则分别对所包含的每个图层进行操作
104                 if (map.get_Layer(i) is GroupLayer)
105                 {
106                     //使用ICompositeLayer接口进行遍历操作
107                     ICompositeLayer compositeLayer = map.get_Layer(i) as ICompositeLayer;
108                     for (int j = 0; j < compositeLayer.Count; j++)
109                     {
110                         //如果图层名称为所要查询的图层名称,则返回IFeatureLayer接口的矢量图层对象
111                         if (compositeLayer.get_Layer(j).Name == layerName)
112                             return (IFeatureLayer)compositeLayer.get_Layer(j);
113                     }
114                 }
115                 //如果图层不是图层组类型,则直接进行判断
116                 else
117                 {
118                     if (map.get_Layer(i).Name == layerName)
119                         return (IFeatureLayer)map.get_Layer(i);
120                 }
121             }
122             return null;
123         }
124 
125         /// 
126         /// 将矢量图层中所有要素的几何体进行Union操作得到一个合并后的新几何体。
127         /// 
128         /// 进行操作的矢量图层
129         /// 合并后的新几何体
130         private IGeometry GetFeatureLayerGeometryUnion(IFeatureLayer featureLayer)
131         {
132             //定义IGeometry接口的对象,存储每一步拓扑操作后得到的几何体
133             IGeometry geometry = null;
134             //使用ITopologicalOperator接口进行几何体的拓扑操作
135             ITopologicalOperator topologicalOperator;
136             //使用null作为查询过滤器得到图层中所有要素的游标
137             IFeatureCursor featureCursor = featureLayer.Search(null, false);
138             //获取IFeature接口的游标中的第一个元素
139             IFeature feature = featureCursor.NextFeature();
140             //当游标不为空时
141             while (feature != null)
142             {
143                 //如果几何体不为空
144                 if (geometry != null)
145                 {
146                     //进行接口转换,使用当前几何体的ITopologicalOperator接口进行拓扑操作
147                     topologicalOperator = geometry as ITopologicalOperator;
148                     //执行拓扑合并操作,将当前要素的几何体与已有几何体进行Union,返回新的合并后的几何体
149                     geometry = topologicalOperator.Union(feature.Shape);
150                 }
151                 else
152                     geometry = feature.Shape;
153                 //移动游标到下一个要素
154                 feature = featureCursor.NextFeature();
155             }
156             //返回最新合并后的几何体
157             return geometry;
158         }
159 
160         /// 
161         /// 根据已配置的查询条件来执行空间查询操作。
162         /// 
163         private void SelectFeaturesBySpatial()
164         {
165             //定义和创建用于空间查询的ISpatialFilter接口的对象
166             ISpatialFilter spatialFilter = new SpatialFilterClass();
167             //默认设定用于查询的空间几何体为当前地图源图层中所有要素几何体的集合
168             spatialFilter.Geometry = GetFeatureLayerGeometryUnion
169                 (GetFeatureLayerByName(currentMap, comboBoxSourceLayer.SelectedItem.ToString()));
170             //根据对空间选择方法的选择采用相应的空间选择方法
171             switch (comboBoxMethods.SelectedIndex)
172             {
173                 case 0:
174                     spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
175                     break;
176                 case 1:
177                     spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelWithin;
178                     break;
179                 case 2:
180                     spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelContains;
181                     break;
182                 case 3:
183                     spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelWithin;
184                     break;
185                 case 4:
186                     spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelTouches;
187                     break;
188                 case 5:
189                     spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelCrosses;
190                     break;
191                 default:
192                     spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
193                     break;
194             }
195 
196             //对所选择的目标图层进行遍历,并对每一个图层进行空间查询操作,查询结果将放在选择集中
197             IFeatureLayer featureLayer;
198             //对所有被选择的目标图层进行遍历
199             for (int i = 0; i < checkedListBoxTargetLayers.CheckedItems.Count; i++)
200             {
201                 //根据选择的目标图层名称获得对应的矢量图层
202                 featureLayer = GetFeatureLayerByName(currentMap, (string)checkedListBoxTargetLayers.CheckedItems[i]);
203                 //进行接口转换,使用IFeatureSelection接口选择要素
204                 IFeatureSelection featureSelection = featureLayer as IFeatureSelection;
205                 //使用IFeatureSelection接口的SelectFeatures方法根据空间查询过滤器选择要素,将其放在新的选择集中
206                 featureSelection.SelectFeatures((IQueryFilter)spatialFilter, esriSelectionResultEnum.esriSelectionResultAdd, false);
207             }
208 
209             //进行接口转换,使用IActiveView接口进行视图操作
210             IActiveView activeView = currentMap as IActiveView;
211             //部分刷新操作,只刷新地理选择集的内容
212             activeView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, null, activeView.Extent);
213         }
214     }
215 }
View Code

属性查询

  1 namespace NAEngine
  2 {
  3     public partial class FormQueryByAttribute : Form
  4     {
  5         private IMap currentMap;    //当前MapControl控件中的Map对象
  6         private IFeatureLayer currentFeatureLayer;  //设置临时类变量来使用IFeatureLayer接口的当前图层对象
  7         private string currentFieldName;    //设置临时类变量来存储字段名称
  8 
  9         /// 
 10         /// 获得当前MapControl控件中的Map对象。
 11         /// 
 12         public IMap CurrentMap
 13         {
 14             set
 15             {
 16                 currentMap = value;
 17             }
 18         }
 19 
 20         public FormQueryByAttribute()
 21         {
 22             InitializeComponent();
 23         }
 24 
 25         //窗体加载时触发事件,执行本函数
 26         private void FormQueryByAttribute_Load(object sender, EventArgs e)
 27         {
 28             try
 29             {
 30                 //将当前图层列表清空
 31                 comboBoxLayerName.Items.Clear();
 32 
 33                 string layerName;   //设置临时变量存储图层名称
 34 
 35                 //对Map中的每个图层进行判断并加载名称
 36                 for (int i = 0; i < currentMap.LayerCount; i++)
 37                 {
 38                     //如果该图层为图层组类型,则分别对所包含的每个图层进行操作
 39                     if (currentMap.get_Layer(i) is GroupLayer)
 40                     {
 41                         //使用ICompositeLayer接口进行遍历操作
 42                         ICompositeLayer compositeLayer = currentMap.get_Layer(i) as ICompositeLayer;
 43                         for (int j = 0; j < compositeLayer.Count; j++)
 44                         {
 45                             //将图层的名称添加到comboBoxLayerName控件中
 46                             layerName = compositeLayer.get_Layer(j).Name;
 47                             comboBoxLayerName.Items.Add(layerName);
 48                         }
 49                     }
 50                     //如果图层不是图层组类型,则直接添加名称
 51                     else
 52                     {
 53                         layerName = currentMap.get_Layer(i).Name;
 54                         comboBoxLayerName.Items.Add(layerName);
 55                     }
 56                 }
 57 
 58                 //将comboBoxLayerName控件的默认选项设置为第一个图层的名称
 59                 comboBoxLayerName.SelectedIndex = 0;
 60                 //将comboBoxSelectMethod控件的默认选项设置为第一种选择方式
 61                 comboBoxSelectMethod.SelectedIndex = 0;
 62             }
 63             catch { }
 64         }
 65 
 66         //在图层名称下拉框控件中所选择图层发生改变时触发事件,执行本函数
 67         private void comboBoxLayerName_SelectedIndexChanged(object sender, EventArgs e)
 68         {
 69             //首先将字段列表和字段值列表清空
 70             listBoxFields.Items.Clear();
 71             listBoxValues.Items.Clear();
 72 
 73             IField field;   //设置临时变量存储使用IField接口的对象
 74 
 75             for (int i = 0; i < currentMap.LayerCount; i++)
 76             {
 77                 if (currentMap.get_Layer(i) is GroupLayer)
 78                 {
 79                     ICompositeLayer compositeLayer = currentMap.get_Layer(i) as ICompositeLayer;
 80                     for (int j = 0; j < compositeLayer.Count; j++)
 81                     {
 82                         //判断图层的名称是否与comboBoxLayerName控件中选择的图层名称相同
 83                         if (compositeLayer.get_Layer(j).Name == comboBoxLayerName.SelectedItem.ToString())
 84                         {
 85                             //如果相同则设置为整个窗体所使用的IFeatureLayer接口对象
 86                             currentFeatureLayer = compositeLayer.get_Layer(j) as IFeatureLayer;
 87                             break;
 88                         }
 89                     }
 90                 }
 91                 else
 92                 {
 93                     //判断图层的名称是否与comboBoxLayerName中选择的图层名称相同
 94                     if (currentMap.get_Layer(i).Name == comboBoxLayerName.SelectedItem.ToString())
 95                     {
 96                         //如果相同则设置为整个窗体所使用的IFeatureLayer接口对象
 97                         currentFeatureLayer = currentMap.get_Layer(i) as IFeatureLayer;
 98                         break;
 99                     }
100                 }
101             }
102 
103             //使用IFeatureClass接口对该图层的所有属性字段进行遍历,并填充listBoxFields控件
104             for (int i = 0; i < currentFeatureLayer.FeatureClass.Fields.FieldCount; i++)
105             {
106                 //根据索引值获取图层的字段
107                 field = currentFeatureLayer.FeatureClass.Fields.get_Field(i);
108                 //排除SHAPE字段,并在其它字段名称前后添加字符"和字符"
109                 if (field.Name.ToUpper() != "SHAPE")
110                     listBoxFields.Items.Add("\"" + field.Name + "\"");
111             }
112 
113             //更新labelwhere控件中的图层名称信息
114             labelwhere.Text = currentFeatureLayer.Name + " WHERE:";
115 
116             //将显示where语句的文本框内容清空
117             textBoxWhere.Clear();
118         }
119 
120         //在字段名称列表控件中所选择的字段发生改变时触发事件,执行本函数
121         private void listBoxFields_SelectedIndexChanged(object sender, EventArgs e)
122         {
123             //首先将listBoxValues控件中的字段属性值清空
124             listBoxValues.Items.Clear();
125             //将buttonGetUniqeValue按钮控件置为可用状态
126             if (buttonGetUniqeValue.Enabled == false)
127                 buttonGetUniqeValue.Enabled = true;
128 
129             //设置整个窗体可用的字段名称
130             string str = listBoxFields.SelectedItem.ToString();
131             //使用string类中的方法将字段名称中的两个"字符去掉
132             str = str.Substring(1);
133             str = str.Substring(0, str.Length - 1);
134             currentFieldName = str;
135         }
136 
137         //在点击“获取唯一属性值”按钮时触发事件,执行本函数
138         //对图层的某个字段进行唯一值获取操作,并将所有的唯一值显示在listBoxValues控件中
139         private void buttonGetUniqeValue_Click(object sender, EventArgs e)
140         {
141             try
142             {
143                 //使用FeatureClass对象的IDataset接口来获取dataset和workspace的信息
144                 IDataset dataset = (IDataset)currentFeatureLayer.FeatureClass;
145                 //使用IQueryDef接口的对象来定义和查询属性信息。通过IWorkspace接口的CreateQueryDef()方法创建该对象。
146                 IQueryDef queryDef = ((IFeatureWorkspace)dataset.Workspace).CreateQueryDef();
147                 //设置所需查询的表格名称为dataset的名称
148                 queryDef.Tables = dataset.Name;
149                 //设置查询的字段名称。可以联合使用SQL语言的关键字,如查询唯一值可以使用DISTINCT关键字。
150                 queryDef.SubFields = "DISTINCT (" + currentFieldName + ")";
151                 //执行查询并返回ICursor接口的对象来访问整个结果的集合
152                 ICursor cursor = queryDef.Evaluate();
153                 //使用IField接口获取当前所需要使用的字段的信息
154                 IFields fields = currentFeatureLayer.FeatureClass.Fields;
155                 IField field = fields.get_Field(fields.FindField(currentFieldName));
156 
157                 //对整个结果集合进行遍历,从而添加所有的唯一值
158                 //使用IRow接口来操作结果集合。首先定位到第一个查询结果。
159                 IRow row = cursor.NextRow();
160                 //如果查询结果非空,则一直进行添加操作
161                 while (row != null)
162                 {
163                     //对String类型的字段,唯一值的前后添加'和',以符合SQL语句的要求
164                     if (field.Type == esriFieldType.esriFieldTypeString)
165                     {
166                         listBoxValues.Items.Add("\'" + row.get_Value(0).ToString() + "\'");
167                     }
168                     else
169                     {
170                         listBoxValues.Items.Add(row.get_Value(0).ToString());
171                     }
172                     //继续执行下一个结果的添加
173                     row = cursor.NextRow();
174                 }
175             }
176             catch(Exception ex)
177             {
178                
179             }
180         
181 }
182         //关闭窗体
183         private void buttonClose_Click(object sender, EventArgs e)
184         {            
185             this.Close();
186         }
187 
188         //在点击“清除”按钮时发生
189         private void buttonClear_Click(object sender, EventArgs e)
190         {
191             //清空textBoxWhere控件中的文本
192             textBoxWhere.Clear();
193         }        
194 
195         //在字段列表中双击属性字段名称时发生
196         private void listBoxFields_MouseDoubleClick(object sender, MouseEventArgs e)
197         {
198             //在where语句中加入字段名称信息
199             textBoxWhere.Text += listBoxFields.SelectedItem.ToString();
200         }
201 
202         //在字段值列表中双击属性唯一值时发生
203         private void listBoxValues_MouseDoubleClick(object sender, MouseEventArgs e)
204         {
205             //在where语句中加入字段值信息
206             textBoxWhere.Text += listBoxValues.SelectedItem.ToString();
207         }
208 
209         //在点击“=”运算符时发生
210         private void buttonEqual_Click(object sender, EventArgs e)
211         {
212             //在where语句中加入运算符信息
213             textBoxWhere.Text += " " + buttonEqual.Text + " ";
214         }
215         //在点击“And”运算符时发生
216         private void buttonAnd_Click(object sender, EventArgs e)
217         {
218             textBoxWhere.Text += " " + buttonAnd.Text + " ";
219         }
220         //在点击“<>”运算符时发生
221         private void buttonNotEqual_Click(object sender, EventArgs e)
222         {
223             textBoxWhere.Text += " " + buttonNotEqual.Text + " ";
224         }
225         //在点击“Like”运算符时发生
226         private void buttonLike_Click(object sender, EventArgs e)
227         {
228             textBoxWhere.Text += " " + buttonLike.Text + " ";
229         }
230         //在点击“>”运算符时发生
231         private void buttonGreater_Click(object sender, EventArgs e)
232         {
233             textBoxWhere.Text += " " + buttonGreater.Text + " ";
234         }
235         //在点击“>=”运算符时发生
236         private void buttonGeaterEqual_Click(object sender, EventArgs e)
237         {
238             textBoxWhere.Text += " " + buttonGeaterEqual.Text + " ";
239         }
240         //在点击“<”运算符时发生
241         private void buttonLess_Click(object sender, EventArgs e)
242         {
243             textBoxWhere.Text += " " + buttonLess.Text + " ";
244         }
245         //在点击“<=”运算符时发生
246         private void buttonLessEqual_Click(object sender, EventArgs e)
247         {
248             textBoxWhere.Text += " " + buttonLessEqual.Text + " ";
249         }
250         //在点击“Or”运算符时发生
251         private void buttonOr_Click(object sender, EventArgs e)
252         {
253             textBoxWhere.Text += " " + buttonOr.Text + " ";
254         }
255         //在点击“_”运算符时发生
256         private void buttonUnderLine_Click(object sender, EventArgs e)
257         {
258             textBoxWhere.Text += " " + buttonUnderLine.Text + " ";
259         }
260         //在点击“%”运算符时发生
261         private void buttonPercent_Click(object sender, EventArgs e)
262         {
263             textBoxWhere.Text += " " + buttonPercent.Text + " ";
264         }
265         //在点击“()”运算符时发生
266         private void buttonBrackets_Click(object sender, EventArgs e)
267         {
268             textBoxWhere.Text += " " + buttonBrackets.Text + " ";
269         }
270         //在点击“Not”运算符时发生
271         private void buttonNot_Click(object sender, EventArgs e)
272         {
273             textBoxWhere.Text += " " + buttonNot.Text + " ";
274         }
275         //在点击“Is”运算符时发生
276         private void buttonIs_Click(object sender, EventArgs e)
277         {
278             textBoxWhere.Text += " " + buttonIs.Text + " ";
279         }
280         //在点击“确定”按钮时发生
281         private void buttonOK_Click(object sender, EventArgs e)
282         {
283             try
284             {
285                 //执行属性查询操作,并关闭窗体
286                 SelectFeaturesByAttribute();
287                 this.Close();
288             }
289             catch { }
290         }
291 
292         //在点击“应用”按钮时发生
293         private void buttonApply_Click(object sender, EventArgs e)
294         {
295             try
296             {
297                 //执行属性查询操作
298                 SelectFeaturesByAttribute();
299             }
300             catch
301             { }
302         }
303 
304         /// 
305         /// 根据已配置的查询条件来执行属性查询操作。
306         /// 
307         private void SelectFeaturesByAttribute()
308         {
309             //使用FeatureLayer对象的IFeatureSelection接口来执行查询操作。这里有一个接口转换操作。
310             IFeatureSelection featureSelection = currentFeatureLayer as IFeatureSelection;
311             //新建IQueryFilter接口的对象来进行where语句的定义
312             IQueryFilter queryFilter = new QueryFilterClass();
313             //设置where语句内容
314             queryFilter.WhereClause = textBoxWhere.Text;
315             //通过接口转换使用Map对象的IActiveView接口来部分刷新地图窗口,从而高亮显示查询的结果
316             IActiveView activeView = currentMap as IActiveView;
317 
318             //根据查询选择方式的不同,得到不同的选择集
319             switch (comboBoxSelectMethod.SelectedIndex)
320             {
321                 //在新建选择集的情况下
322                 case 0:
323                     //首先使用IMap接口的ClearSelection()方法清空地图选择集
324                     currentMap.ClearSelection();
325                     //根据定义的where语句使用IFeatureSelection接口的SelectFeatures方法选择要素,并将其添加到选择集中
326                     featureSelection.SelectFeatures(queryFilter, esriSelectionResultEnum.esriSelectionResultNew, false); 
327                     break;
328                 //添加到当前选择集的情况
329                 case 1:
330                     featureSelection.SelectFeatures(queryFilter, esriSelectionResultEnum.esriSelectionResultAdd, false);
331                     break;
332                 //从当前选择集中删除的情况
333                 case 2:
334                     featureSelection.SelectFeatures(queryFilter, esriSelectionResultEnum.esriSelectionResultXOR, false);
335                     break;
336                 //从当前选择集中选择的情况
337                 case 3:
338                     featureSelection.SelectFeatures(queryFilter, esriSelectionResultEnum.esriSelectionResultAnd, false);
339                     break;
340                 //默认为新建选择集的情况
341                 default:
342                     currentMap.ClearSelection();
343                     featureSelection.SelectFeatures(queryFilter, esriSelectionResultEnum.esriSelectionResultNew, false);
344                     break;
345             }
346 
347             //部分刷新操作,只刷新地理选择集的内容
348             activeView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, null, activeView.Extent);
349         }
350 
351     }
352 }
View Code

以上部分参考《ArcGIS+Engine地理信息系统开发教程+基于C#.NET+,牟乃夏编著+,P544》,书还配有源码,美滋滋!

--------------------------------------------------------------

路径分析

 1 // This command allows users to load locations from another point feature layer into the selected NALayer and active category.
 2 
 3 namespace NAEngine
 4 {
 5     [Guid("72BDDCB7-03E8-4777-BECA-11DC47EFEDBA")]
 6     [ClassInterface(ClassInterfaceType.None)]
 7     [ProgId("NAEngine.LoadLocations")]
 8     public sealed class cmdLoadLocations : BaseCommand
 9     {
10         private IMapControl3 m_mapControl;
11         private IEngineNetworkAnalystEnvironment m_naEnv;
12 
13         public cmdLoadLocations()
14         {
15             base.m_caption = "Load Locations...";
16         }
17 
18         public override bool Enabled
19         {
20             get
21             {
22                 // Enabled if the active category is an input NAClass
23 
24                 IEngineNAWindowCategory naWindowCategory = m_naEnv.NAWindow.ActiveCategory;
25                 if (naWindowCategory == null)
26                     return false;
27 
28                 INAClass naClass = naWindowCategory.NAClass;
29                 if (naClass == null)
30                     return false;
31 
32                 INAClassDefinition naClassDefinition = naClass.ClassDefinition;
33                 if (naClassDefinition == null)
34                     return false;
35 
36                 return naClassDefinition.IsInput;
37             }
38         }
39 
40         public override void OnClick()
41         {
42             // Get the NALayer and corresponding NAContext of the layer that
43             // was right-clicked on in the table of contents
44             // m_MapControl.CustomProperty was set in frmMain.axTOCControl1_OnMouseDown
45             INALayer naLayer = (INALayer)m_mapControl.CustomProperty;
46 
47             // Set the Active Analysis layer to be the layer right-clicked on
48             m_naEnv.NAWindow.ActiveAnalysis = naLayer;
49 
50             if (!Enabled)
51                 return;
52 
53             // Show the Property Page form for Network Analyst
54             frmLoadLocations loadLocations = new frmLoadLocations();
55             if (loadLocations.ShowModal(m_mapControl, m_naEnv))
56             {
57                 // notify that the context has changed because we have added locations to a NAClass within it
58                 INAContextEdit contextEdit = m_naEnv.NAWindow.ActiveAnalysis.Context as INAContextEdit;
59                 contextEdit.ContextChanged();
60 
61                 // If loaded locations, refresh the NAWindow and the Screen
62                 m_mapControl.Refresh(esriViewDrawPhase.esriViewGeography, naLayer, m_mapControl.Extent);
63                 m_naEnv.NAWindow.UpdateContent(m_naEnv.NAWindow.ActiveCategory);
64             }
65         }
66 
67         public override void OnCreate(object hook)
68         {
69             m_mapControl = (IMapControl3)hook;
70 
71             // Get the Network Analyst Env
72             m_naEnv = new EngineNetworkAnalystEnvironmentClass();
73         }
74     }
75 }
View Code
 1 // This command brings up the property pages for the NALayer.
 2 
 3 namespace NAEngine
 4 {
 5     [Guid("04B67C95-96DD-4afe-AF62-942255ACBA71")]
 6     [ClassInterface(ClassInterfaceType.None)]
 7     [ProgId("NAEngine.NALayerProperties")]
 8     public sealed class cmdNALayerProperties : BaseCommand
 9     {
10         private IMapControl3 m_mapControl;
11 
12         public cmdNALayerProperties()
13         {
14             base.m_caption = "Properties...";
15         }
16 
17         public override void OnClick()
18         {
19             // Show the Property Page form for the NALayer
20 
21             // Get the NALayer that was right-clicked on in the table of contents
22             // m_MapControl.CustomProperty was set in frmMain.axTOCControl1_OnMouseDown
23             INALayer naLayer = (INALayer)m_mapControl.CustomProperty;
24             frmNALayerProperties props = new frmNALayerProperties();
25             if (props.ShowModal(naLayer))
26             {
27                 // Notify the ActiveView that the contents have changed so the TOC and NAWindow know to refresh themselves.
28                 m_mapControl.ActiveView.ContentsChanged();
29             }
30         }
31 
32         public override void OnCreate(object hook)
33         {
34             m_mapControl = (IMapControl3)hook;
35         }
36     }
37 }
View Code
 1 namespace NAEngine
 2 {
 3     [Guid("53399A29-2B65-48d5-930F-804B88B85A34")]
 4     [ClassInterface(ClassInterfaceType.None)]
 5     [ProgId("NAEngine.RemoveLayer")]
 6     public sealed class cmdRemoveLayer : BaseCommand
 7     {
 8         private IMapControl3 m_mapControl;
 9 
10         public cmdRemoveLayer()
11         {
12             base.m_caption = "Remove Layer";
13         }
14 
15         public override void OnClick()
16         {
17             // Get the NALayer that was right-clicked on in the table of contents
18             // m_MapControl.CustomProperty was set in frmMain.axTOCControl1_OnMouseDown
19             ILayer layer = (ILayer)m_mapControl.CustomProperty;
20             m_mapControl.Map.DeleteLayer(layer);
21             m_mapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, layer.AreaOfInterest);
22         }
23 
24         public override void OnCreate(object hook)
25         {
26             m_mapControl = (IMapControl3)hook;
27         }
28     }
29 }
View Code
 1 namespace NAEngine
 2 {
 3     [Guid("7E98FE97-DA7A-4069-BC85-091D75B1AF65")]
 4     [ClassInterface(ClassInterfaceType.None)]
 5     [ProgId("NAEngine.NAProperties")]
 6     public sealed class cmdNAProperties : BaseCommand
 7     {
 8         private IMapControl3 m_mapControl;
 9 
10         public cmdNAProperties()
11         {
12             base.m_caption = "Properties...";
13         }
14 
15         public override void OnClick()
16         {
17             // Get the network analyst environment
18             IEngineNetworkAnalystEnvironment naEnv = new EngineNetworkAnalystEnvironmentClass();
19 
20             // Show the Property Page form for Network Analyst
21             frmNAProperties props = new frmNAProperties();
22             props.ShowModal(naEnv);
23         }
24 
25         public override void OnCreate(object hook)
26         {
27             //m_mapControl = (IMapControl3)hook;
28         }
29     }
30 }
View Code
 1 // This command deletes all the network locations and analysis results from the selected NALayer.
 2 
 3 namespace NAEngine
 4 {
 5     [Guid("773CCD44-C46A-42eb-A1B2-E00C7B765783")]
 6     [ClassInterface(ClassInterfaceType.None)]
 7     [ProgId("NAEngine.ClearAnalysisLayer")]
 8     public sealed class cmdClearAnalysisLayer : BaseCommand
 9     {
10         private IMapControl3 m_mapControl;
11 
12         public cmdClearAnalysisLayer()
13         {
14             base.m_caption = "Clear Analysis Layer";
15         }
16 
17         public override void OnClick()
18         {
19             IEngineNetworkAnalystEnvironment naEnv = new EngineNetworkAnalystEnvironmentClass();
20             IEngineNetworkAnalystHelper naHelper = naEnv as IEngineNetworkAnalystHelper;
21             IEngineNAWindow naWindow = naEnv.NAWindow;
22 
23             // Get the NALayer and corresponding NAContext of the layer that
24             // was right-clicked on in the table of contents
25             // m_MapControl.CustomProperty was set in frmMain.axTOCControl1_OnMouseDown
26             INALayer naLayer = (INALayer)m_mapControl.CustomProperty;
27             INAContext naContext = naLayer.Context;
28 
29             // Set the active Analysis layer
30             if (naWindow.ActiveAnalysis != naLayer)
31                 naWindow.ActiveAnalysis = naLayer;
32 
33             // Remember what the current category is
34             IEngineNAWindowCategory currentCategory = naWindow.ActiveCategory;
35 
36             // Loop through deleting all the items from all the categories
37             INamedSet naClasses = naContext.NAClasses;
38             for (int i = 0; i < naClasses.Count; i++)
39             {
40                 IEngineNAWindowCategory category = naWindow.get_CategoryByNAClassName(naClasses.get_Name(i));
41                 naWindow.ActiveCategory = category;
42                 naHelper.DeleteAllNetworkLocations();
43             }
44 
45             //Reset to the original category
46             naWindow.ActiveCategory = currentCategory;
47 
48             m_mapControl.Refresh(esriViewDrawPhase.esriViewGeography, naLayer, m_mapControl.Extent);
49         }
50 
51         public override void OnCreate(object hook)
52         {
53             m_mapControl = (IMapControl3)hook;
54         }
55     }
56 }
View Code

该部分代码参考自ArcGIS的网络分析代码

------------------------

贴张图吧

ArcGIS二次开发课程总结(附上代码)vs2010(C#)+AE_第1张图片

 

 

最后,谈结课感受,多调BUG世界才能在我们手中!

 

转载于:https://www.cnblogs.com/zzg-home/p/9103941.html

你可能感兴趣的:(ArcGIS二次开发课程总结(附上代码)vs2010(C#)+AE)