ArcEngine开发:IElement.Geometry 值不在预期范围内 + 元素绘制代码

IElement pEle = pLineEle as IElement;

pEle.Geometry = pLn;

 

pLn为一个ILine对象,想当然的以为它是IGeometry对象,可以赋值,结果爆出异常值不在预期范围内。

解决方法是将ILine转换为IPolyline对象,涉及到一些Geometry的基本操作。贴出一些可用的绘制元素的代码

 

#region 获取RgbColor

        /// <summary>

        /// 获取RgbColor颜色对象

        /// </summary>

        /// <param name="r"></param>

        /// <param name="g"></param>

        /// <param name="b"></param>

        /// <param name="alpa">可选参数,透明度.0代表透明</param>

        /// <returns></returns>

        public static IRgbColor getRgbColor(int r, int g, int b, byte alpa = 255)

        {

            IRgbColor pColor = new RgbColorClass();

            pColor.Red = r;

            pColor.Green = g;

            pColor.Blue = b;

            pColor.Transparency = alpa;



            return pColor;

        }

        #endregion



        #region 获取随机颜色

        /// <summary>

        /// 获取随机颜色

        /// </summary>

        /// <returns></returns>

        public static IRgbColor getRandColor()

        {

            Random rd = new Random();

            IRgbColor myColor = new RgbColorClass();

            myColor.Red = rd.Next(0, 255);

            myColor.Blue = rd.Next(0, 255);

            myColor.Green = rd.Next(0, 255);



            return myColor;

        }

        #endregion



        #region 绘制线

        /// <summary>

        /// 绘制线

        /// </summary>

        /// <param name="pLine"></param>

        /// <param name="pMapCtrl"></param>

        /// <param name="pLineSymbol">线符号</param>

        /// <param name="refresh">刷新</param>

        public static void DrawLine(ILine pLine, IMapControl2 pMapCtrl, ISimpleLineSymbol pLineSymbol

            , bool refresh)

        {

            ILineElement pLineEle = new LineElementClass();

            pLineEle.Symbol = pLineSymbol;

            IGeometryCollection pPolyline = new PolylineClass();

            ISegmentCollection pPath = new PathClass();

            object m1 = Type.Missing;

            object m2 = Type.Missing;

            pPath.AddSegment(pLine as ISegment, ref m1, ref m2);

            pPolyline.AddGeometry(pPath as IGeometry, ref m1, ref m2);

            IElement pEle = pLineEle as IElement;

            pEle.Geometry = pPolyline as IGeometry;

            IGraphicsContainer pGC = pMapCtrl.Map as IGraphicsContainer;

            pGC.AddElement(pEle, 0);

            if (refresh)

            {

                IActiveView pAv = pMapCtrl.ActiveView;

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

            }

        }

        #endregion



        #region 绘制文字注记

        /// <summary>

        /// 绘制文字

        /// </summary>

        /// <param name="pnt"></param>

        /// <param name="value"></param>

        /// <param name="ptxtSym"></param>

        /// <param name="pMapCtrl"></param>

        /// <param name="refresh"></param>

        public static void DrawTextEle(IPoint pnt, string strvalue, ITextSymbol ptxtSym,

            IMapControl2 pMapCtrl, bool refresh)

        {

            ITextElement pTextEle = new TextElementClass();

            pTextEle.Text = strvalue;

            pTextEle.Symbol = ptxtSym;



            IElement pEle = pTextEle as IElement;

            pEle.Geometry = pnt;

            IGraphicsContainer pGc = pMapCtrl.Map as IGraphicsContainer;

            pGc.AddElement(pEle, 0);

            if (refresh)

            {

                IActiveView pAv = pMapCtrl.ActiveView;

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

            }

        }

        #endregion



        #region 绘制polyline

        /// <summary>

        /// 

        /// </summary>

        /// <param name="pPolyline"></param>

        /// <param name="pLineSym"></param>

        /// <param name="pMapCtrl"></param>

        /// <param name="refresh"></param>

        public static void DrawPolyline(IPolyline pPolyline, ISimpleLineSymbol pLineSym, IMapControl2 pMapCtrl,

            bool refresh)

        {

            ILineElement pLineEle = new LineElementClass();

            IElement pEle = pLineEle as IElement;

            pLineEle.Symbol = pLineSym;

            pEle.Geometry = pPolyline;



            IGraphicsContainer pGc = pMapCtrl.Map as IGraphicsContainer;

            pGc.AddElement(pEle, 0);

            if (refresh)

            {

                IActiveView pAv = pMapCtrl.ActiveView;

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

            }

        }

        #endregion



        #region 绘制点

        /// <summary>

        /// 绘制点

        /// </summary>

        /// <param name="pnt"></param>

        /// <param name="pMapCtrl"></param>

        /// <param name="pColor">颜色</param>

        /// <param name="refresh">是否刷新</param>

        public static void DrawPoint(IPoint pnt, IMapControl2 pMapCtrl, IRgbColor pColor, bool refresh)

        {

            ISimpleMarkerSymbol pMarkerSymbol = new SimpleMarkerSymbolClass();



            pMarkerSymbol.Color = pColor;

            pMarkerSymbol.Style = esriSimpleMarkerStyle.esriSMSCircle;

            pMarkerSymbol.Size = 3;

            IElement pEle;

            IMarkerElement pMe = new MarkerElementClass();

            pMe.Symbol = pMarkerSymbol;

            pEle = pMe as IElement;

            pEle.Geometry = pnt;



            IActiveView pav = pMapCtrl.ActiveView;

            IGraphicsContainer pGc = pMapCtrl.Map as IGraphicsContainer;

            pGc.AddElement(pEle, 0);

            if (refresh)

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

        }

        #endregion        

 

 

 

你可能感兴趣的:(ArcEngine)