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        

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM