【转】AE中绘制图形元素的方法

转自 http://www.cnblogs.com/gisak/archive/2011/03/16/1985818.html

AE中绘制图形元素的方法

Element元素对象是一个非常庞杂的对象集合,主要分为两大部分:图形元素(Graphic Element)和框架元素(Frame Element);

图形元素包括GroupElement、MarkerElement、LineElement、TextElement、DataElement、PictureElement、和FillShapeElement等对象,它们都是作为图形的形式而存在的。

IElement是所有图形元素和框架元素都实现的接口,它可以确定元素的Geometry属性,Element是一个抽象类,IElement和ILineElement、ITextElement并不是父子关系,后者没有Geometry属性。

   要在视图绘制图形的方法:

1 采用MapControl的DrawShape()方法。

在MapControl控件中通过DrawShape方法绘制的图形,在视图中进行缩放操作之后就会消失,这是因为这些图形本质上都是缓存的,仅仅是暂时存在的。如果要保存这些元素,就需要使用Mxd文件。

    ISimpleFillSymbol pSymbol = new SimpleFillSymbolClass();

    pSymbol.Color = pColor;

    Object symbol = pSymbol;

    IGeometry pGeo=axMapControl1.DrawShape(pGeo,ref symbol);

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

主要步骤:

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

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

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

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

添加IMarkerElement对象和ILineElement对象

   以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);

  添加TextElement对象(用ITextElement接口定义了设置文字元素的属性,例如Text(字符)等

                IMap pMap = axMapControl1.Map;

                IActiveView pActiveView = pMap as IActiveView;

 

                ITextElement pText = new TextElementClass();

                pText.Text = "从今天开始你就是我的了就像驴子一样给你盖个章";

                IElement pEle = pText as IElement;

                IPoint point = new PointClass();

                point.PutCoords(e.mapX-30, e.mapY+10);

 

                IFormattedTextSymbol pTextSymbol =new TextSymbolClass();

                ICallout pCallout = new BalloonCalloutClass();

                pCallout.AnchorPoint = point;

                pTextSymbol.Background = pCallout as ITextBackground;

 

                pText.Symbol = pTextSymbol;

                pEle.Geometry = point;

                IGraphicsContainer pgraphic = axMapControl1.Map as IGraphicsContainer;

                pgraphic.AddElement(pEle,0);

                axMapControl1.Refresh();

添加FillShapeElement对象

     FillShapeElement是一个抽象类,它的子类有CircleElement、EllipseElement、PolygonElement和RectangleElement。这些对象的共同特点是它们的Geometry属性都是一个封闭的二维图形。

     IFillShapeElement接口是所有FillShapeElement类都实现的接口,它定义了用于显示图形元素的Symbol属性,这个Symbol属性必须设置为IFillsymbol对象。

               IMap pMap = axMapControl1.Map;

                IActiveView pActiveView = pMap as IActiveView;

                pGeo = axMapControl1.TrackPolygon();

 

                ISimpleFillSymbol fillsymbol = new SimpleFillSymbolClass();

                fillsymbol.Style = esriSimpleFillStyle.esriSFSDiagonalCross;

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

 

                IFillShapeElement pFillElement = new PolygonElementClass();

                pFillElement.Symbol = fillsymbol;

                IElement pEle = pFillElement as IElement;

                pEle.Geometry = pGeo;

 

                IGraphicsContainer pGraphics = pMap as IGraphicsContainer;

                pGraphics.AddElement(pEle, 0);

原文地址:https://www.cnblogs.com/yuxiuting/p/2245847.html