天天看點

基于C#的AE二次開發之地圖示注

基于C#的AE二次開發之地圖示注

我的開發環境為ArcGIS Engine 10.2與Visual studio2010。主地圖名稱為axMapControl1,建立一個菜單或按鈕,建立一個點選事件,粘貼代碼修改圖層與标注字段即可實作。(注意相關事件的添加與動态連結庫的引入)!

效果預覽:

基于C#的AE二次開發之地圖示注

實作代碼:

private void 标注要素ToolStripMenuItem_Click(object sender, EventArgs e)
{
     try
     {
         IMap pMap = axMapControl1.Map;
         //獲得圖層所有要素(在這裡輸入需要标注的矢量要素類,我這裡預設第一個圖層【高耦合】)
         IFeatureLayer pFeatureLayer = axMapControl1.get_Layer(0) as IFeatureLayer;
         IFeatureCursor pFeatCursor = pFeatureLayer.Search(null, true);
         IFeature pFeature = pFeatCursor.NextFeature();
         while (pFeature != null)
         {
             IFields pFields = pFeature.Fields;
             //找出标注字段的索引号(在這裡填寫标注字段名,我這裡填的是FID字段)
             int index = pFields.FindField("FID");
             //得到要素的Envelope
             IEnvelope pEnve = pFeature.Extent;
             IPoint pPoint = new ESRI.ArcGIS.Geometry.Point();
             pPoint.PutCoords(pEnve.XMin + pEnve.Width / 2, pEnve.YMin + pEnve.Height / 2);
             //建立字型對象
             stdole.IFontDisp pFont;
             pFont = new stdole.StdFont() as stdole.IFontDisp;
             //标注字型為微軟雅黑,可自行調整
             pFont.Name = "微軟雅黑";
             //産生一個文本符号
             ITextSymbol pTextSymbol = new TextSymbol();
             //設定文本符号的大小
             pTextSymbol.Size = 20;
             pTextSymbol.Font = pFont;
             //标注字型的顔色為紅色
             IRgbColor pColor = new RgbColor() as IRgbColor;
             pColor.Red = 255;
             pColor.Green = 0;
             pColor.Blue = 0;
             pTextSymbol.Color = pColor;
             //産生一個文本對象
             ITextElement pTextElement = new TextElement() as ITextElement;
             pTextElement.Text = pFeature.get_Value(index).ToString();
             pTextElement.ScaleText = true;
             pTextElement.Symbol = pTextSymbol;
             IElement pElement = pTextElement as IElement;
             pElement.Geometry = pPoint;
             IActiveView pActiveView = pMap as IActiveView;
             IGraphicsContainer pGraphicsContainer = pMap as IGraphicsContainer;
             //添加元素
             pGraphicsContainer.AddElement(pElement, 0);
             pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
             pPoint = null;
             pElement = null;
             pFeature = pFeatCursor.NextFeature();
         }
     }
     catch (Exception ex){
         MessageBox.Show("标注字段不存在!");
     }
 }