ArcGIS Engine开发Geodatabase代码(六)——TopologyEdit

时间:2013-4-25    作者:悬浮的青春    分类: gis二次开发


/**/

  ESRI Developer Summit 2009

 
Developer's Guide to the Geodatabase

  Code Samples

 
6 April 2009

/**/




开发环境:

  • ArcGIS Engine9.3/9.3.1
  • VS2008



<p style="margin-top:0px;margin-bottom:0px;padding:0px;font-family:Arial;font-size:14px;line-itFormBoundaryGdRn5qhD7nx0T4Q7
Content-Disposition: form-data; name=" content"="" /**/

  ESRI Developer Summit 2009

 
Developer's Guide to the Geodatabase

  Code Samples

 
6 April 2009

/**/



开发环境:

  • ArcGIS Engine9.3/9.3.1
  • VS2008



关于版本的接口差别参考:

http://help.arcgis.com/en/sdk/10.0/arcobjects_net/conceptualhelp/index.html#/Type_changes_between_9_3_and_10/000100000408000000/


以下代码主要是对拓扑编辑做相关介绍

  1. using System;  
  2. using System.ComponentModel;  
  3. using System.Diagnostics;  
  4. using System.Drawing;  
  5. using System.Windows.Forms;  
  6. using System.Runtime.InteropServices;  
  7. using ESRI.ArcGIS.ADF.BaseClasses;  
  8. using ESRI.ArcGIS.ADF.CATIDs;  
  9. using ESRI.ArcGIS.Carto;  
  10. using ESRI.ArcGIS.Controls;  
  11. using ESRI.ArcGIS.Display;  
  12. using ESRI.ArcGIS.Editor;  
  13. using ESRI.ArcGIS.esriSystem;  
  14. using ESRI.ArcGIS.Framework;  
  15. using ESRI.ArcGIS.Geodatabase;  
  16. using ESRI.ArcGIS.Geometry;  
  17.   
  18. namespace TopologyEditDemo  
  19. {  
  20.   [Guid("35d59a9d-997c-4536-a003-3c6ed77abb92")]  
  21.   [ClassInterface(ClassInterfaceType.None)]  
  22.   [ProgId("TopologyEditDemo.TopologyEditCommand")]  
  23.   public sealed class TopologyEditCommand : BaseCommand  
  24.   {  
  25.     #region COM Registration Function(s)  
  26.     [ComRegisterFunction()]  
  27.     [ComVisible(false)]  
  28.     static void RegisterFunction(Type registerType)  
  29.     {  
  30.             String regKey = String.Format(@"HKEY_CLASSES_ROOT\CLSID\{{{0}}}", registerType.GUID);  
  31.             MxCommands.Register(regKey);  
  32.     }  
  33.   
  34.     [ComUnregisterFunction()]  
  35.     [ComVisible(false)]  
  36.     static void UnregisterFunction(Type registerType)  
  37.     {  
  38.             String regKey = String.Format(@"HKEY_CLASSES_ROOT\CLSID\{{{0}}}", registerType.GUID);  
  39.             MxCommands.Unregister(regKey);  
  40.     }  
  41.     #endregion  
  42.  
  43.         #region Instance Variables  
  44.         /// <summary>  
  45.         /// A helper for working with ArcMap.  
  46.         /// </summary>  
  47.         private IHookHelper hookHelper = null;  
  48.   
  49.         /// <summary>  
  50.         /// A reference to the application (ArcMap).  
  51.         /// </summary>  
  52.         private IApplication application = null;  
  53.         #endregion  
  54.  
  55.         #region Constructors  
  56.         /// <summary>  
  57.         /// The default constructor.  
  58.         /// </summary>  
  59.         public TopologyEditCommand()  
  60.     {  
  61.             // Set the command's properties.  
  62.       m_category = "2009 DevSummit";  
  63.             m_caption = "TopologyEditCommand";  
  64.       m_message = "Editing using the Topology Graph";  
  65.             m_toolTip = "TopologyEditCommand";  
  66.             m_name = String.Format("{0}_{1}", m_category, m_caption);  
  67.   
  68.             // Set the command's image.  
  69.       try  
  70.       {  
  71.         String bitmapResourceName = "TopologyEditCommand.bmp";  
  72.         m_bitmap = new Bitmap(GetType(), bitmapResourceName);  
  73.       }  
  74.       catch (Exception exc)  
  75.       {  
  76.         Trace.WriteLine(exc.Message, "Invalid Bitmap");  
  77.       }  
  78.         }  
  79.         #endregion  
  80.  
  81.         #region Overridden Class Methods  
  82.         /// <summary>  
  83.         /// Occurs when the command is created.  
  84.         /// </summary>  
  85.         /// <param name="hook">A reference to the application.</param>  
  86.         public override void OnCreate(object hook)  
  87.     {  
  88.       // Setup the HookHelper and get a reference to application.  
  89.             hookHelper = new HookHelperClass { Hook = hook };  
  90.       application = (IApplication)hook;  
  91.     }  
  92.   
  93.     /// <summary>  
  94.     /// Indicates whether the command is enabled. True if features are selected.  
  95.     /// </summary>  
  96.     public override Boolean Enabled  
  97.     {  
  98.       get  
  99.       {  
  100.         IMap map = hookHelper.FocusMap;  
  101.         IEnumFeature enumFeature = (IEnumFeature)map.FeatureSelection;  
  102.                 return enumFeature.Next() != null;  
  103.       }  
  104.     }  
  105.   
  106.         /// <summary>  
  107.         /// Occurs when the command is clicked.  
  108.         /// </summary>  
  109.         public override void OnClick()  
  110.     {  
  111.       try  
  112.       {  
  113.                 // Get a reference to the focus map, the active view, and the Editor.  
  114.         IMap map = hookHelper.FocusMap;  
  115.                 IActiveView activeView = hookHelper.ActiveView;  
  116.         IEditor editor = (IEditor)application.FindExtensionByName("ESRI Object Editor");  
  117.   
  118.                 // Get the first selected feature.  
  119.         IEnumFeature enumFeature = (IEnumFeature)map.FeatureSelection;  
  120.         IEnumFeatureSetup enumFeatureSetup = (IEnumFeatureSetup)enumFeature;  
  121.         enumFeatureSetup.AllFields = true;  
  122.         IFeature feature = enumFeature.Next();  
  123.   
  124.                 // Request a move distance from the user.  
  125.         Double yMoveDistance = Double.Parse(InputBox.ShowInputBox());  
  126.   
  127.         // Assume that there is a Topology layer in position 0 in the TOC.  
  128.                 ITopologyLayer topologyLayer = map.get_Layer(0) as ITopologyLayer;  
  129.         if (topologyLayer != null)  
  130.         {  
  131.                     // Open the topology graph.  
  132.           ITopology topology = topologyLayer.Topology;            
  133.           ITopologyGraph topologyGraph = topology.Cache;  
  134.   
  135.           // Check to see if an existing graph is built for the area of interest.  
  136.           IEnvelope existingGraphEnv = topologyGraph.BuildExtent;  
  137.                     IEnvelope activeViewEnv = activeView.Extent;  
  138.           if (!existingGraphEnv.IsEmpty)  
  139.           {  
  140.                         IClone graphEnvClone = (IClone)existingGraphEnv;  
  141.                         IClone activeViewExtentClone = (IClone)activeViewEnv;  
  142.                         if (!graphEnvClone.IsEqual(activeViewExtentClone))  
  143.             {  
  144.               // We could build the cache using the Geodatasets extent but   
  145.               // Cook County is large. This demo uses the ArcMap extent,   
  146.               // but any envelope could be passed in.  
  147.                             topologyGraph.Build(activeViewEnv, false);  
  148.             }  
  149.           }  
  150.           else  
  151.           {  
  152.                         topologyGraph.Build(activeViewEnv, false);  
  153.           }  
  154.   
  155.                     // Copy the first selected feature's geometry and use it to  
  156.                     // make a selection in the topology graph.  
  157.           IGeometry geometry = feature.ShapeCopy;  
  158.           topologyGraph.SelectByGeometry(  
  159.               (int)esriTopologyElementType.esriTopologyNode,  
  160.                esriTopologySelectionResultEnum.esriTopologySelectionResultNew,  
  161.                geometry);  
  162.   
  163.                     // Create a transformation.  
  164.           IAffineTransformation2D affine = new AffineTransformation2DClass();  
  165.           affine.Move(0, yMoveDistance);  
  166.   
  167.           // Start editing.  
  168.           IDataset dataset = (IDataset)topology.FeatureDataset;  
  169.           editor.StartEditing(dataset.Workspace);  
  170.           editor.StartOperation();  
  171.   
  172.           topologyGraph.TransformSelection(esriTransformDirection.esriTransformForward, affine, false);  
  173.   
  174.           // Commit the topology edit in the database.  
  175.           // The post will tell you the envelope of the area which has changed.  
  176.                     IEnvelope invalidArea = null;  
  177.           topologyGraph.Post(out invalidArea);  
  178.   
  179.           editor.StopOperation("Stop operation");  
  180.           editor.StopEditing(true);  
  181.   
  182.           activeView.Refresh();  
  183.         }  
  184.         else  
  185.         {  
  186.           MessageBox.Show("Topology is not the first layer in TOC""TopologyGraphEditCommand");  
  187.           return;  
  188.         }  
  189.       }  
  190.       catch (COMException comExc)  
  191.       {  
  192.         MessageBox.Show(String.Format("Error ({0}): {1}", comExc.ErrorCode, comExc.Message));  
  193.       }  
  194.       catch (Exception exc)  
  195.       {  
  196.         MessageBox.Show(String.Format("Error: {0}", exc.Message));  
  197.       }  
  198.     }  
  199.     #endregion  
  200.  
  201.     #region Inputbox Class  
  202.         /// <summary>  
  203.         /// A form for inputting a simple numeric value.   
  204.         /// </summary>  
  205.     public class InputBox : Form  
  206.         {  
  207.             #region Instance Variables  
  208.             /// <summary>  
  209.             /// The form's text box.  
  210.             /// </summary>  
  211.             private TextBox textBox = null;  
  212.             #endregion  
  213.  
  214.             #region Constructors and Initialization  
  215.             /// <summary>  
  216.             /// The default constructor.  
  217.             /// </summary>  
  218.             private InputBox()  
  219.       {  
  220.         InitializeComponent();  
  221.             }  
  222.   
  223.             /// <summary>  
  224.             /// Initializes the form and its components.  
  225.             /// </summary>  
  226.             private void InitializeComponent()  
  227.             {  
  228.                 // Create the text box.  
  229.                 textBox = new TextBox()  
  230.                 {  
  231.                     Location = new System.Drawing.Point(16, 16),  
  232.                     Name = "TextBox",  
  233.                     Size = new Size(256, 20),  
  234.                     TabIndex = 0,  
  235.                     Text = "30"  
  236.                 };  
  237.                 textBox.KeyDown += new KeyEventHandler(OnKeyDown);  
  238.   
  239.                 SuspendLayout();  
  240.   
  241.                 // Set this object's inherited properties.  
  242.                 AutoScaleBaseSize = new Size(5, 13);  
  243.                 ClientSize = new Size(292, 53);  
  244.                 ControlBox = false;  
  245.                 Controls.AddRange(new Control[] { textBox });  
  246.                 FormBorderStyle = FormBorderStyle.FixedDialog;  
  247.                 Name = "InputBox";  
  248.                 Text = "Enter 2D Transform Vertical Distance";  
  249.   
  250.                 ResumeLayout(false);  
  251.             }  
  252.             #endregion  
  253.  
  254.             #region Public Methods  
  255.             /// <summary>  
  256.             /// Gets the value entered in the text box.  
  257.             /// </summary>  
  258.             public String Value  
  259.             {  
  260.                 get { return textBox.Text; }  
  261.             }  
  262.             #endregion  
  263.  
  264.             #region Event Handlers  
  265.             /// <summary>  
  266.             /// Occurs when a key is pressed in the text box.  
  267.             /// </summary>  
  268.             /// <param name="sender">The source of the event.</param>  
  269.             /// <param name="e">Event arguments.</param>  
  270.             private void OnKeyDown(object sender, KeyEventArgs e)  
  271.       {  
  272.         if (e.KeyCode == Keys.Enter)  
  273.           this.Close();  
  274.             }  
  275.             #endregion  
  276.  
  277.             #region Static Methods  
  278.             /// <summary>  
  279.             /// Displays a new instance of the InputBox and returns the value entered.  
  280.             /// </summary>  
  281.             /// <returns>The value entered by the user.</returns>  
  282.             public static String ShowInputBox()  
  283.       {  
  284.         InputBox box = new InputBox();  
  285.         box.ShowDialog();  
  286.         return box.Value;  
  287.             }  
  288.             #endregion  
  289.         }  
  290.     #endregion  
  291.   }  
  292. }  




标签: arcgis二次开发 arcgis Geodatabase

WRITTEN BY

avatar