MainForm.cs
上传用户:hucuiqin88
上传日期:2022-06-19
资源大小:132k
文件大小:9k
源码类别:

按钮控件

开发平台:

C#

  1. using System;
  2. using System.Drawing;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Windows.Forms;
  6. using System.Data;
  7. using System.IO;
  8. using System.Runtime.InteropServices;
  9. using ESRI.ArcGIS.esriSystem;
  10. using ESRI.ArcGIS.Carto;
  11. using ESRI.ArcGIS.Controls;
  12. using ESRI.ArcGIS.ADF;
  13. using ESRI.ArcGIS.SystemUI;
  14. using ESRI.ArcGIS.Geodatabase;
  15. using ESRI.ArcGIS.Geometry;
  16. using ESRI.ArcGIS.DataSourcesFile;
  17. using ESRI.ArcGIS.Display;
  18. namespace addPolylineTest
  19. {
  20.     public sealed partial class MainForm : Form
  21.     {
  22.         #region class private members
  23.         private IMapControl3 m_mapControl = null;
  24.         private string m_mapDocumentName = string.Empty;
  25.         #endregion
  26.         #region class constructor
  27.         public MainForm()
  28.         {
  29.             InitializeComponent();
  30.         }
  31.         #endregion
  32.         private void MainForm_Load(object sender, EventArgs e)
  33.         {
  34.             
  35.             //get the MapControl
  36.             m_mapControl = (IMapControl3)axMapControl1.Object;
  37.             //disable the Save menu (since there is no document yet)
  38.             menuSaveDoc.Enabled = false;
  39.             
  40.         }
  41.         private void addPolyline()
  42.         {
  43.             string strShapeFolder = @"D:";
  44.             string strShapeFile = "ct.shp";
  45.             string shapeFileFullName = strShapeFolder + strShapeFile;
  46.             IFeatureClass pFeatureClass;
  47.             IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactory();
  48.             IFeatureWorkspace pFeatureWorkspace = (IFeatureWorkspace)pWorkspaceFactory.OpenFromFile(strShapeFolder, 0);
  49.             pFeatureClass = pFeatureWorkspace.OpenFeatureClass(strShapeFile);
  50.             //pFeatureClass = pFeatureWorkspace.OpenFeatureClass(strShapeFile);
  51.             IPolyline polyLine = new PolylineClass();
  52.             IPolyline polyLine1 = new PolylineClass();
  53.         
  54.             IFeature pFeature = pFeatureClass.CreateFeature();
  55.             pFeature.Shape = polyLine;
  56.             IPoint p1 = new PointClass();
  57.             IPoint p2 = new PointClass();
  58.             IPoint p3 = new PointClass();
  59.             IPoint p4 = new PointClass();
  60.             p1.PutCoords(120.306, 31.674);
  61.             p2.PutCoords(120.306, 31.672);
  62.             p3.PutCoords(120.307, 31.8);
  63.             p4.PutCoords(120.308, 31.674);
  64.             polyLine.FromPoint = p1;
  65.             polyLine.ToPoint = p2;
  66.             //IGeometry pGeometry = (IGeometry)polyLine;
  67.             //IPath pPath = new PathClass();
  68.             //pPath.SmoothLocal(2);
  69.  
  70.          
  71.             IFeatureLayer pFeaturelayer = new FeatureLayerClass();
  72.             pFeaturelayer.FeatureClass = pFeatureClass;
  73.             pFeaturelayer.Name = "路况信息";
  74.             pFeatureClass.CreateFeature();
  75.             pFeature.Store();
  76.           
  77.             this.axMapControl1.AddLayer(pFeaturelayer);
  78.             this.axMapControl1.Refresh();
  79.             // if (arrPointAll.Count <= 0)//点坐标数组不能为空
  80.             //{
  81.             //    return;
  82.             //}
  83.              ESRI.ArcGIS.Carto.IActiveView activeView;
  84.             //if (activeView == null)
  85.             //{
  86.              activeView = this.axMapControl1.ActiveView.FocusMap as IActiveView;
  87.             //}
  88.             //删除以前的element
  89.             //DeleteOldElement(activeView.GraphicsContainer);
  90.             // 获取IRGBColor接口
  91.             IRgbColor color = new RgbColor();
  92.             // 设置颜色属性
  93.             color.Red = 0;
  94.             color.Blue = 0;
  95.                 color.Green=255;
  96.             color.Transparency = 255;
  97.             
  98.             //点
  99.             IPoint pPoint = new PointClass();
  100.             //线样式
  101.             ISimpleLineSymbol lineSymbol = new SimpleLineSymbolClass();
  102.             lineSymbol.Color = color;
  103.             lineSymbol.Style = esriSimpleLineStyle.esriSLSInsideFrame;
  104.             lineSymbol.Width = 2;
  105.             //线元素
  106.             ILineElement lineElement = new LineElementClass();
  107.             lineElement.Symbol = lineSymbol;
  108.             //创建线
  109.             //IPolyline m_Polyline = new PolylineClass();
  110.             //点集合
  111.             //IPointCollection m_PointCollection = new PolylineClass();
  112.             //点数组
  113.             //ArrayList arrPoint = new ArrayList();
  114.             //object missing = Type.Missing;
  115.             //foreach (object o in arrPointAll)
  116.             //{
  117.             //    arrPoint = (ArrayList)o;
  118.             //    pPoint.PutCoords(int.Parse(arrPoint[0].ToString()), int.Parse(arrPoint[1].ToString()));
  119.             //    m_PointCollection.AddPoint(pPoint, ref missing, ref missing);
  120.             //}
  121.             ////QI for IPolyline
  122.             //m_Polyline = m_PointCollection as IPolyline;
  123.             ////放大地图
  124.             //折线范围
  125.             IEnvelope pEnvelope = polyLine.Envelope;
  126.             //折线区域
  127.             //IArea pArea = pEnvelope as IArea;
  128.             //pPoint = pArea.Centroid;
  129.             //this.ChangeEnvelope(pPoint, 0.06, 0.06);
  130.             //QI for IElement
  131.             IElement element = lineElement as IElement;
  132.     
  133.             element.Geometry = polyLine;
  134.             //加载线元素到地图
  135.             activeView.GraphicsContainer.AddElement(element, 0);
  136.             //Refresh the graphics
  137.             activeView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
  138.         }
  139.         #region Main Menu event handlers
  140.         private void menuNewDoc_Click(object sender, EventArgs e)
  141.         {
  142.             //execute New Document command
  143.             ICommand command = new CreateNewDocument();
  144.             command.OnCreate(m_mapControl.Object);
  145.             command.OnClick();
  146.         }
  147.         private void menuOpenDoc_Click(object sender, EventArgs e)
  148.         {
  149.             //execute Open Document command
  150.             ICommand command = new ControlsOpenDocCommandClass();
  151.             command.OnCreate(m_mapControl.Object);
  152.             command.OnClick();
  153.         }
  154.         private void menuSaveDoc_Click(object sender, EventArgs e)
  155.         {
  156.             //execute Save Document command
  157.             if (m_mapControl.CheckMxFile(m_mapDocumentName))
  158.             {
  159.                 //create a new instance of a MapDocument
  160.                 IMapDocument mapDoc = new MapDocumentClass();
  161.                 mapDoc.Open(m_mapDocumentName, string.Empty);
  162.                 //Make sure that the MapDocument is not readonly
  163.                 if (mapDoc.get_IsReadOnly(m_mapDocumentName))
  164.                 {
  165.                     MessageBox.Show("Map document is read only!");
  166.                     mapDoc.Close();
  167.                     return;
  168.                 }
  169.                 //Replace its contents with the current map
  170.                 mapDoc.ReplaceContents((IMxdContents)m_mapControl.Map);
  171.                 //save the MapDocument in order to persist it
  172.                 mapDoc.Save(mapDoc.UsesRelativePaths, false);
  173.                 //close the MapDocument
  174.                 mapDoc.Close();
  175.             }
  176.         }
  177.         private void menuSaveAs_Click(object sender, EventArgs e)
  178.         {
  179.             //execute SaveAs Document command
  180.             ICommand command = new ControlsSaveAsDocCommandClass();
  181.             command.OnCreate(m_mapControl.Object);
  182.             command.OnClick();
  183.         }
  184.         private void menuExitApp_Click(object sender, EventArgs e)
  185.         {
  186.             //exit the application
  187.             Application.Exit();
  188.         }
  189.         #endregion
  190.         //listen to MapReplaced evant in order to update the statusbar and the Save menu
  191.         private void axMapControl1_OnMapReplaced(object sender, IMapControlEvents2_OnMapReplacedEvent e)
  192.         {
  193.             //get the current document name from the MapControl
  194.             m_mapDocumentName = m_mapControl.DocumentFilename;
  195.             //if there is no MapDocument, diable the Save menu and clear the statusbar
  196.             if (m_mapDocumentName == string.Empty)
  197.             {
  198.                 menuSaveDoc.Enabled = false;
  199.                 statusBarXY.Text = string.Empty;
  200.             }
  201.             else
  202.             {
  203.                 //enable the Save manu and write the doc name to the statusbar
  204.                 menuSaveDoc.Enabled = true;
  205.                 statusBarXY.Text = System.IO.Path.GetFileName(m_mapDocumentName);
  206.             }
  207.         }
  208.         private void axMapControl1_OnMouseMove(object sender, IMapControlEvents2_OnMouseMoveEvent e)
  209.         {
  210.             statusBarXY.Text = string.Format("{0}, {1}  {2}", e.mapX.ToString("#######.##"), e.mapY.ToString("#######.##"), axMapControl1.MapUnits.ToString().Substring(4));
  211.         }
  212.         private void button1_Click(object sender, EventArgs e)
  213.         {
  214.             addPolyline();
  215.         }
  216.     }
  217. }