ArcGIS Engine基础(19)之元素标注(以距离标注和面积标注为例)

距离标注主要代码:

带附加箭头属性的线标注。

       private void AddDisLineElement(IActiveView activeView, IPolyline disLine)
        {
            if (activeView != null)
            {
                try
                {
                    IGraphicsContainer graphicsContainer = activeView as IGraphicsContainer;

                    if (disLine != null)
                    {
                        IPolyline geometryLine = new PolylineClass();
                        geometryLine.FromPoint = disLine.FromPoint.X > disLine.ToPoint.X ? disLine.ToPoint : disLine.FromPoint;
                        geometryLine.ToPoint = disLine.FromPoint.X > disLine.ToPoint.X ? disLine.FromPoint : disLine.ToPoint;

                        //构建带箭头的线元素
                        IElement lineWithArrow = GetLineElementWithArrow(geometryLine);
                        //构建文本元素
                        ITextElement areaText = new TextElementClass();
                        double distance = geometryLine.Length;
                        distance = distance > 0 ? distance : (-distance);
                        string unitString = GetUnitsString(m_HookHelper.FocusMap);
                        areaText.Text =distance.ToString("0.0") + unitString;
                        ITextSymbol textSymbol = areaText.Symbol as ITextSymbol;
                        ILine line = new LineClass();
                        line.FromPoint = geometryLine.FromPoint;
                        line.ToPoint = geometryLine.ToPoint;
                        textSymbol.Angle = line.Angle;
                        textSymbol.Size = 10;
                        areaText.Symbol = textSymbol;
                        IElement textElement = areaText as IElement;
                        IObjectCopy objectCopy = new ObjectCopyClass();
                        IPolyline textGeometry = objectCopy.Copy(geometryLine) as IPolyline;
                        (textGeometry as ITransform2D).Move(0, 10);
                        textElement.Geometry = textGeometry;
                        IElementProperties3 textElementPro = textElement as IElementProperties3;
                        textElementPro.Name = "DistanceLabel";
                        textElementPro.CustomProperty = lineWithArrow;
                        // 为了使距离标注中的文字可单独移动,文字标注与箭头元素分开而不组合成一个元素
                        (m_HookHelper.FocusMap as IGraphicsContainer).AddElement(lineWithArrow, 0);
                        (m_HookHelper.FocusMap as IGraphicsContainer).AddElement(textElement, 0);
                    }
                }
                finally
                {
                    activeView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, activeView.Extent);
                }
            }
        }

        /// <summary>
        /// 构建带箭头的线元素
        /// </summary>
        /// <param name="line"></param>
        /// <returns></returns>
        private IElement GetLineElementWithArrow(IPolyline line)
        {
            ICartographicLineSymbol pCartoLineSymbol = new CartographicLineSymbolClass();
            pCartoLineSymbol.Cap = esriLineCapStyle.esriLCSRound;

            ILineProperties pLineProp = pCartoLineSymbol as ILineProperties;
            pLineProp.DecorationOnTop = true;

            ILineDecoration pLineDecoration = new LineDecorationClass();
            ISimpleLineDecorationElement pSimpleLineDecoElem = new SimpleLineDecorationElementClass();
            pSimpleLineDecoElem.AddPosition(0);
            pSimpleLineDecoElem.AddPosition(1);
            IArrowMarkerSymbol pArrowMarkerSym = new ArrowMarkerSymbolClass();
            pArrowMarkerSym.Size = 8;
            pSimpleLineDecoElem.MarkerSymbol = pArrowMarkerSym as IMarkerSymbol;
            pLineDecoration.AddElement(pSimpleLineDecoElem as ILineDecorationElement);
            pLineProp.LineDecoration = pLineDecoration;

            ILineSymbol pLineSymbol = pCartoLineSymbol as ILineSymbol;
            pLineSymbol.Width = 1;

            ILineElement pLineElem = new LineElementClass();
            pLineElem.Symbol = pLineSymbol;
            IElement elementWithArrow = pLineElem as IElement;
            elementWithArrow.Geometry = line;

            return elementWithArrow;
        }

        private string GetUnitsString(IMap map)
        {
            string unitStrig = map.MapUnits.ToString();
            switch (map.MapUnits)
            {
                case esriUnits.esriCentimeters:
                    unitStrig = "厘米"; break;
                case esriUnits.esriDecimeters:
                    unitStrig = "分米"; break;
                case esriUnits.esriMeters:
                    unitStrig = "米"; break;
                case esriUnits.esriKilometers:
                    unitStrig = "千米"; break;
                case esriUnits.esriUnknownUnits:
                    unitStrig = "未知单位"; break;
                default:
                    unitStrig = unitStrig.Substring(4); break;
            }
            return unitStrig;
        }private void AddDisLineElement(IActiveView activeView, IPolyline disLine)
        {
            if (activeView != null)
            {
                try
                {
                    IGraphicsContainer graphicsContainer = activeView as IGraphicsContainer;

                    if (disLine != null)
                    {
                        IPolyline geometryLine = new PolylineClass();
                        geometryLine.FromPoint = disLine.FromPoint.X > disLine.ToPoint.X ? disLine.ToPoint : disLine.FromPoint;
                        geometryLine.ToPoint = disLine.FromPoint.X > disLine.ToPoint.X ? disLine.FromPoint : disLine.ToPoint;

                        //构建带箭头的线元素
                        IElement lineWithArrow = GetLineElementWithArrow(geometryLine);
                        //构建文本元素
                        ITextElement areaText = new TextElementClass();
                        double distance = geometryLine.Length;
                        distance = distance > 0 ? distance : (-distance);
                        string unitString = GetUnitsString(m_HookHelper.FocusMap);
                        areaText.Text =distance.ToString("0.0") + unitString;
                        ITextSymbol textSymbol = areaText.Symbol as ITextSymbol;
                        ILine line = new LineClass();
                        line.FromPoint = geometryLine.FromPoint;
                        line.ToPoint = geometryLine.ToPoint;
                        textSymbol.Angle = line.Angle;
                        textSymbol.Size = 10;
                        areaText.Symbol = textSymbol;
                        IElement textElement = areaText as IElement;
                        IObjectCopy objectCopy = new ObjectCopyClass();
                        IPolyline textGeometry = objectCopy.Copy(geometryLine) as IPolyline;
                        (textGeometry as ITransform2D).Move(0, 10);
                        textElement.Geometry = textGeometry;
                        IElementProperties3 textElementPro = textElement as IElementProperties3;
                        textElementPro.Name = "DistanceLabel";
                        textElementPro.CustomProperty = lineWithArrow;
                        // 为了使距离标注中的文字可单独移动,文字标注与箭头元素分开而不组合成一个元素
                        (m_HookHelper.FocusMap as IGraphicsContainer).AddElement(lineWithArrow, 0);
                        (m_HookHelper.FocusMap as IGraphicsContainer).AddElement(textElement, 0);
                    }
                }
                finally
                {
                    activeView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, activeView.Extent);
                }
            }
        }

        /// <summary>
        /// 构建带箭头的线元素
        /// </summary>
        /// <param name="line"></param>
        /// <returns></returns>
        private IElement GetLineElementWithArrow(IPolyline line)
        {
            ICartographicLineSymbol pCartoLineSymbol = new CartographicLineSymbolClass();
            pCartoLineSymbol.Cap = esriLineCapStyle.esriLCSRound;

            ILineProperties pLineProp = pCartoLineSymbol as ILineProperties;
            pLineProp.DecorationOnTop = true;

            ILineDecoration pLineDecoration = new LineDecorationClass();
            ISimpleLineDecorationElement pSimpleLineDecoElem = new SimpleLineDecorationElementClass();
            pSimpleLineDecoElem.AddPosition(0);
            pSimpleLineDecoElem.AddPosition(1);
            IArrowMarkerSymbol pArrowMarkerSym = new ArrowMarkerSymbolClass();
            pArrowMarkerSym.Size = 8;
            pSimpleLineDecoElem.MarkerSymbol = pArrowMarkerSym as IMarkerSymbol;
            pLineDecoration.AddElement(pSimpleLineDecoElem as ILineDecorationElement);
            pLineProp.LineDecoration = pLineDecoration;

            ILineSymbol pLineSymbol = pCartoLineSymbol as ILineSymbol;
            pLineSymbol.Width = 1;

            ILineElement pLineElem = new LineElementClass();
            pLineElem.Symbol = pLineSymbol;
            IElement elementWithArrow = pLineElem as IElement;
            elementWithArrow.Geometry = line;

            return elementWithArrow;
        }

        private string GetUnitsString(IMap map)
        {
            string unitStrig = map.MapUnits.ToString();
            switch (map.MapUnits)
            {
                case esriUnits.esriCentimeters:
                    unitStrig = "厘米"; break;
                case esriUnits.esriDecimeters:
                    unitStrig = "分米"; break;
                case esriUnits.esriMeters:
                    unitStrig = "米"; break;
                case esriUnits.esriKilometers:
                    unitStrig = "千米"; break;
                case esriUnits.esriUnknownUnits:
                    unitStrig = "未知单位"; break;
                default:
                    unitStrig = unitStrig.Substring(4); break;
            }
            return unitStrig;
        }

面积标注主要代码:

标注文字在面中间。

  IPolygon polygonRegion = _polygonFeedback.Stop();
            if (polygonRegion == null || polygonRegion.IsEmpty)
                return;
            polygonRegion.SpatialReference = m_HookHelper.FocusMap.SpatialReference;
            //构建面元素
            IElement polygonElement = new PolygonElementClass();
            ISimpleFillSymbol fillSymbol = new SimpleFillSymbolClass();
            fillSymbol.Style = esriSimpleFillStyle.esriSFSNull;
            polygonElement.Geometry = polygonRegion;
            (polygonElement as IFillShapeElement).Symbol = fillSymbol;
            //构建文本元素
            ITextElement areaText = new TextElementClass();
            IPoint labelPoint = (polygonRegion as IArea).LabelPoint;
            double area = (polygonRegion as IArea).Area;
            area = area > 0 ? area : (-area);
            string unitString = GetUnitsString(m_HookHelper.FocusMap);
            areaText.Text = area.ToString("0.00") + unitString;
            ITextSymbol textSymbol = areaText.Symbol as ITextSymbol;
            if (LabelOptionGloble.DrawFont != null)
            {
                System.Drawing.Font font = LabelOptionGloble.DrawFont;
                IFontDisp fontDisp = new StdFontClass() as IFontDisp;
                fontDisp.Name = font.Name;
                fontDisp.Bold = font.Bold;
                fontDisp.Italic = font.Italic;
                fontDisp.Underline = font.Underline;
                fontDisp.Strikethrough = font.Strikeout;
                fontDisp.Size = (decimal)font.Size;
                textSymbol = new TextSymbolClass();
                textSymbol.Font = fontDisp;

                textSymbol.Color = RgbColorWrapper.GetRGBColor(LabelOptionGloble.TextColor);
            }
            else
            {
                textSymbol.Size = 10;
            }
            areaText.Symbol = textSymbol;
            IElement textElement = areaText as IElement;
            textElement.Geometry = labelPoint;
            //组成组合元素
            IGroupElement areaLabelElement = new GroupElementClass();
            areaLabelElement.AddElement(polygonElement);
            areaLabelElement.AddElement(textElement);

            IElement addElement = areaLabelElement as IElement;
            IElementProperties3 addElementPro = addElement as IElementProperties3;
            addElementPro.Name = "AreaLabel";

            (m_HookHelper.FocusMap as IGraphicsContainer).AddElement(addElement, 0);
            m_HookHelper.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, m_HookHelper.ActiveView.Extent);

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xizhjxust_GIS

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值