• GIS应用开发AO(1)_普通几何图形绘制


         啥都不说先上代码,首先来一波规则面与不规则面的绘制,所有的面要素都是用IPolygon的接口,唯一不同的是IRubberBand, 后面细讲

     protected override void OnMouseDown(ESRI.ArcGIS.Desktop.AddIns.Tool.MouseEventArgs e)
            {
                IMap map = ArcMap.Document.FocusMap;
                IActiveView view = ArcMap.Document.ActiveView;
                IScreenDisplay screenDisplay = ArcMap.Document.ActiveView.ScreenDisplay;
                IRubberBand prubberpolygon = new RubberRectangularPolygonClass();
                IPolygon pPolygon = prubberpolygon.TrackNew(screenDisplay, null) as IPolygon;
                ISimpleFillSymbol pFillSymbol = new SimpleFillSymbolClass();
               
                pFillSymbol.Style = esriSimpleFillStyle.esriSFSDiagonalCross;
              
                pFillSymbol.Color = getRGB(0, 0, 255);
              
                IFillShapeElement pPoFillEle = new PolygonElementClass();
                pPoFillEle.Symbol = pFillSymbol;
              
                IElement pEle = pPoFillEle as IElement;
                pEle.Geometry = pPolygon;
                //用IGraphicsContainer::AddElement把图形元素添加到视图并显示
                IGraphicsContainer pGraphicsContalner = map as IGraphicsContainer;
                //将元素对象添加到Map中
                pGraphicsContalner.AddElement(pEle, 0);
                //刷新图形元素
                view.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
    
    
            }
    
            public IColor getRGB(int yourRed, int yourGreen, int yourBlue)
            {
                IRgbColor pRGB = new RgbColorClass();
                pRGB.Red = yourRed;
                pRGB.Green = yourGreen;
                pRGB.Blue = yourBlue;
                pRGB.UseWindowsDithering = true;
                return pRGB;
            }

    2.代码注解

      2.1IMap接口

       它是操作任务的起点,用于管理Map对象的Layer对象,要素选择集,Mapsurround对象,Map是一个存放对象的容器,IMap定义了大量的管理对象的方法

     IMap map = ArcMap.Document.FocusMap;
                IActiveView view = ArcMap.Document.ActiveView;
    获取ArcMap文档的map对象,IActiveView按照当前视图的不同来获取不同的视窗——数据视图,专题图视图.

    
    

      2.2  IActiveView接口

          ArcMap.Document.ActiveView.ScreenDisplay指向当前视图的ScreenDisplay,每一个视图对象都有一个ScreenDisplay,来控制图形绘制工作。

      2.3 IRubberBand接口

       IRUbberBand接口有两个方法:1,TrackExisting方法;2,TrackNew方法;
        我们经常需要在项目中画地理要素(Feature)时或者画元素(Element),其实IRUbberBand接口就实现了绘制几何形体(Geometry)的方法TrackNew,以及移动一个一个几何形体的方法TrackExisting

     IRubberBand prubberpolygon = new RubberRectangularPolygonClass();
                IPolygon pPolygon = prubberpolygon.TrackNew(screenDisplay, null) as IPolygo

      2.3 主要代码


    通过IRubberBand创建一个Retangular对象,并通过TrackNew方法将绘制的对象赋予polygon,如果你要画圆就把RubberRectanglarPolygonClass改为RubberCircleClass,多边形RubberPolygonClass,画线段不能仅仅改这个函数。
        ISimpleFillSymbol pFillSymbol = new SimpleFillSymbolClass();
               
                pFillSymbol.Style = esriSimpleFillStyle.esriSFSDiagonalCross;
              
                pFillSymbol.Color = getRGB(0, 0, 255);
              
                IFillShapeElement pPoFillEle = new PolygonElementClass();
                pPoFillEle.Symbol = pFillSymbol;
              
                IElement pEle = pPoFillEle as IElement;
                pEle.Geometry = pPolygon;
    这段代码主要定义了填充符号类型,
    esriSimpleFillStyle.esriSFSDiagonalCross;这个多边形填充的样式,有以下几种

     
    IFillShapeElement定义了Geometry的Symbol,它可实现如下几类几何或文本

    要实现线状几何的绘制,只需要改为ILineElement
    IElement定义Geometry的类别,Point.Polygon Polyline.......
    IElement是所有图形元素和框架元素都实现的接口,它可以确定元素的Geometry属性,Element是一个抽象类,IElement和ILineElement、ITextElement并不是父子关系,后者没有Geometry属性。

      //用IGraphicsContainer::AddElement把图形元素添加到视图并显示
                IGraphicsContainer pGraphicsContalner = map as IGraphicsContainer;
                //将元素对象添加到Map中
                pGraphicsContalner.AddElement(pEle, 0);
                //刷新图形元素
                view.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);

    用IGraphicsContainer::AddElement把图形元素添加到视图并显示

       主要步骤:

       1 产生一个新的元素对象;

       2 确定元素显示时使用的Symbol(符号)和Geometry(几何对象);

       3用IGraphicsContainer::AddElement把图形元素添加到视图并显示

       4 刷新视图,让添加的元素可以显示出来。

    3.实例

     以LineElement为例添加它到视图中需要两个接口:IElement和ILineElement,前者用于确定线元素的Geometry,后者确定Symbol。需要注意的什么类型的元素配对什么类型的Symbol,例如LineElement元素只能用修饰LineElement对象的符号,也只能用Line或者Polyline作为Geometry,MarkerElement也是一样的,使用的是Marker类型的Symbol和点作为它的Geometry。具体例子:

                    IMap pMap = axMapControl1.Map;

                    IActiveView pActive = pMap as IActiveView;

     

                    ISimpleLineSymbol pLineSymbol = new SimpleLineSymbolClass();  //设置Symbol属性

                    pLineSymbol.Color = GetRGB(0, 255, 0);

                    pLineSymbol.Width = 3;

                    pGeo = axMapControl1.TrackLine();

                 

                    ILineElement pLineElement = new LineElementClass();    

                    IElement pEle = pLineElement as IElement;

                    pLineElement.Symbol = pLineSymbol;

                    pEle.Geometry = pGeo;

     

                    IGraphicsContainer pContainer = pMap as IGraphicsContainer;

                    pContainer.AddElement(pEle, 0);

                   pActive.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);







    CV_小羊
  • 相关阅读:
    兼容利器之X-UA-Compatible
    SQL Server之游标的基础知识
    SQL 按特定字段值排序
    NodeJS 开篇 牛刀小试
    临时表之IF-ELSE
    css中如何设置字体
    怎么做到不加班?
    C# .net中cookie值为中文时的乱码解决方法
    HTML5商城开发一 楼层滚动加载数据
    QQ浏览器X5内核问题汇总
  • 原文地址:https://www.cnblogs.com/Mathsion811/p/6972452.html
Copyright © 2020-2023  润新知