MainForm.cs
上传用户:hucuiqin88
上传日期:2022-06-19
资源大小:132k
文件大小:9k
- using System;
- using System.Drawing;
- using System.Collections;
- using System.ComponentModel;
- using System.Windows.Forms;
- using System.Data;
- using System.IO;
- using System.Runtime.InteropServices;
- using ESRI.ArcGIS.esriSystem;
- using ESRI.ArcGIS.Carto;
- using ESRI.ArcGIS.Controls;
- using ESRI.ArcGIS.ADF;
- using ESRI.ArcGIS.SystemUI;
- using ESRI.ArcGIS.Geodatabase;
- using ESRI.ArcGIS.Geometry;
- using ESRI.ArcGIS.DataSourcesFile;
- using ESRI.ArcGIS.Display;
- namespace addPolylineTest
- {
- public sealed partial class MainForm : Form
- {
- #region class private members
- private IMapControl3 m_mapControl = null;
- private string m_mapDocumentName = string.Empty;
- #endregion
- #region class constructor
- public MainForm()
- {
- InitializeComponent();
- }
- #endregion
- private void MainForm_Load(object sender, EventArgs e)
- {
-
- //get the MapControl
- m_mapControl = (IMapControl3)axMapControl1.Object;
- //disable the Save menu (since there is no document yet)
- menuSaveDoc.Enabled = false;
-
- }
- private void addPolyline()
- {
- string strShapeFolder = @"D:";
- string strShapeFile = "ct.shp";
- string shapeFileFullName = strShapeFolder + strShapeFile;
- IFeatureClass pFeatureClass;
- IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactory();
- IFeatureWorkspace pFeatureWorkspace = (IFeatureWorkspace)pWorkspaceFactory.OpenFromFile(strShapeFolder, 0);
- pFeatureClass = pFeatureWorkspace.OpenFeatureClass(strShapeFile);
- //pFeatureClass = pFeatureWorkspace.OpenFeatureClass(strShapeFile);
- IPolyline polyLine = new PolylineClass();
- IPolyline polyLine1 = new PolylineClass();
-
- IFeature pFeature = pFeatureClass.CreateFeature();
- pFeature.Shape = polyLine;
- IPoint p1 = new PointClass();
- IPoint p2 = new PointClass();
- IPoint p3 = new PointClass();
- IPoint p4 = new PointClass();
- p1.PutCoords(120.306, 31.674);
- p2.PutCoords(120.306, 31.672);
- p3.PutCoords(120.307, 31.8);
- p4.PutCoords(120.308, 31.674);
- polyLine.FromPoint = p1;
- polyLine.ToPoint = p2;
- //IGeometry pGeometry = (IGeometry)polyLine;
- //IPath pPath = new PathClass();
- //pPath.SmoothLocal(2);
-
-
- IFeatureLayer pFeaturelayer = new FeatureLayerClass();
- pFeaturelayer.FeatureClass = pFeatureClass;
- pFeaturelayer.Name = "路况信息";
- pFeatureClass.CreateFeature();
- pFeature.Store();
-
- this.axMapControl1.AddLayer(pFeaturelayer);
- this.axMapControl1.Refresh();
- // if (arrPointAll.Count <= 0)//点坐标数组不能为空
- //{
- // return;
- //}
- ESRI.ArcGIS.Carto.IActiveView activeView;
- //if (activeView == null)
- //{
- activeView = this.axMapControl1.ActiveView.FocusMap as IActiveView;
- //}
- //删除以前的element
- //DeleteOldElement(activeView.GraphicsContainer);
- // 获取IRGBColor接口
- IRgbColor color = new RgbColor();
- // 设置颜色属性
- color.Red = 0;
- color.Blue = 0;
- color.Green=255;
- color.Transparency = 255;
-
- //点
- IPoint pPoint = new PointClass();
- //线样式
- ISimpleLineSymbol lineSymbol = new SimpleLineSymbolClass();
- lineSymbol.Color = color;
- lineSymbol.Style = esriSimpleLineStyle.esriSLSInsideFrame;
- lineSymbol.Width = 2;
- //线元素
- ILineElement lineElement = new LineElementClass();
- lineElement.Symbol = lineSymbol;
- //创建线
- //IPolyline m_Polyline = new PolylineClass();
- //点集合
- //IPointCollection m_PointCollection = new PolylineClass();
- //点数组
- //ArrayList arrPoint = new ArrayList();
- //object missing = Type.Missing;
- //foreach (object o in arrPointAll)
- //{
- // arrPoint = (ArrayList)o;
- // pPoint.PutCoords(int.Parse(arrPoint[0].ToString()), int.Parse(arrPoint[1].ToString()));
- // m_PointCollection.AddPoint(pPoint, ref missing, ref missing);
- //}
- ////QI for IPolyline
- //m_Polyline = m_PointCollection as IPolyline;
- ////放大地图
- //折线范围
- IEnvelope pEnvelope = polyLine.Envelope;
- //折线区域
- //IArea pArea = pEnvelope as IArea;
- //pPoint = pArea.Centroid;
- //this.ChangeEnvelope(pPoint, 0.06, 0.06);
- //QI for IElement
- IElement element = lineElement as IElement;
-
- element.Geometry = polyLine;
- //加载线元素到地图
- activeView.GraphicsContainer.AddElement(element, 0);
- //Refresh the graphics
- activeView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
- }
- #region Main Menu event handlers
- private void menuNewDoc_Click(object sender, EventArgs e)
- {
- //execute New Document command
- ICommand command = new CreateNewDocument();
- command.OnCreate(m_mapControl.Object);
- command.OnClick();
- }
- private void menuOpenDoc_Click(object sender, EventArgs e)
- {
- //execute Open Document command
- ICommand command = new ControlsOpenDocCommandClass();
- command.OnCreate(m_mapControl.Object);
- command.OnClick();
- }
- private void menuSaveDoc_Click(object sender, EventArgs e)
- {
- //execute Save Document command
- if (m_mapControl.CheckMxFile(m_mapDocumentName))
- {
- //create a new instance of a MapDocument
- IMapDocument mapDoc = new MapDocumentClass();
- mapDoc.Open(m_mapDocumentName, string.Empty);
- //Make sure that the MapDocument is not readonly
- if (mapDoc.get_IsReadOnly(m_mapDocumentName))
- {
- MessageBox.Show("Map document is read only!");
- mapDoc.Close();
- return;
- }
- //Replace its contents with the current map
- mapDoc.ReplaceContents((IMxdContents)m_mapControl.Map);
- //save the MapDocument in order to persist it
- mapDoc.Save(mapDoc.UsesRelativePaths, false);
- //close the MapDocument
- mapDoc.Close();
- }
- }
- private void menuSaveAs_Click(object sender, EventArgs e)
- {
- //execute SaveAs Document command
- ICommand command = new ControlsSaveAsDocCommandClass();
- command.OnCreate(m_mapControl.Object);
- command.OnClick();
- }
- private void menuExitApp_Click(object sender, EventArgs e)
- {
- //exit the application
- Application.Exit();
- }
- #endregion
- //listen to MapReplaced evant in order to update the statusbar and the Save menu
- private void axMapControl1_OnMapReplaced(object sender, IMapControlEvents2_OnMapReplacedEvent e)
- {
- //get the current document name from the MapControl
- m_mapDocumentName = m_mapControl.DocumentFilename;
- //if there is no MapDocument, diable the Save menu and clear the statusbar
- if (m_mapDocumentName == string.Empty)
- {
- menuSaveDoc.Enabled = false;
- statusBarXY.Text = string.Empty;
- }
- else
- {
- //enable the Save manu and write the doc name to the statusbar
- menuSaveDoc.Enabled = true;
- statusBarXY.Text = System.IO.Path.GetFileName(m_mapDocumentName);
- }
- }
- private void axMapControl1_OnMouseMove(object sender, IMapControlEvents2_OnMouseMoveEvent e)
- {
- statusBarXY.Text = string.Format("{0}, {1} {2}", e.mapX.ToString("#######.##"), e.mapY.ToString("#######.##"), axMapControl1.MapUnits.ToString().Substring(4));
- }
- private void button1_Click(object sender, EventArgs e)
- {
- addPolyline();
- }
- }
- }