AE二次开发,右键图层,设置图层要素标注

  #region 成员变量
        private string m_zttName = string.Empty;
        private string m_sqlCondition = "";
        private ITool m_tool;

        private ITOCControl2 tocControl;
        private IMapControl3 mapControl;
        private IToolbarMenu toolMenuMap;
        private IToolbarMenu toolMenuLayer;
        private MakeFeature _makeFeature = new MakeFeature();
  #endregion
  
//右键菜单Load方法中写
                tocControl = (ITOCControl2)toc.Object;
              mapControl = (IMapControl3)mcView.Object;
              //创建菜单
                toolMenuMap = new ToolbarMenuClass();
              toolMenuLayer = new ToolbarMenuClass();
              toolMenuLayer.AddItem(_makeFeature, -1, 0, false, esriCommandStyles.esriCommandStyleIconAndText);
              toolMenuLayer.SetHook(mapControl);

 private void toc_OnMouseDown(object sender, ITOCControlEvents_OnMouseDownEvent e)
        {
            try
            {
                #region toolbarmenu
                esriTOCControlItem tocItem = esriTOCControlItem.esriTOCControlItemNone;
                IBasicMap basicMap = null; ;
                ILayer layer = null;
                object legendGroup = null;
                object index = null;
                //获取鼠标点击信息 
                toc.HitTest(e.x, e.y, ref tocItem, ref basicMap, ref layer, ref legendGroup, ref index);
                if (e.button == 2)
                {
                    //设置选中图层标注状态
                    IGeoFeatureLayer curLayer = layer as IGeoFeatureLayer;
                    _makeFeature.SetChecked = curLayer.DisplayAnnotation;

                    if (tocItem == esriTOCControlItem.esriTOCControlItemMap)
                    {
                        toc.SelectItem(basicMap, null);
                    }
                    else
                    {
                        toc.SelectItem(layer, null);
                    }

                    //设置CustomProperty为layer (用于自定义的Layer命令) 
                    mcView.CustomProperty = layer;
                    //弹出右键菜单 
                    if (tocItem == esriTOCControlItem.esriTOCControlItemMap)
                    {
                        toolMenuMap.PopupMenu(e.x, e.y, toc.hWnd);
                    }
                    else
                    {
                        toolMenuLayer.PopupMenu(e.x, e.y, toc.hWnd);
                    }
                }
                #endregion
            }
            catch (Exception ex)
            {
                FrmErrHandle.ErrorLogShow(ex);
            }
        }

   #region 图层要素标注
    /// <summary>
    /// 图层要素标注
    /// </summary>
    public class MakeFeature : BaseCommand
    {
        private IMapControl3 mapControl;
        private ITextSymbol textSymbolTC = null;//图层标注

        /// <summary>
        /// 设置图层选择状态
        /// </summary>
        public bool SetChecked
        {
            get
            {
                return base.m_checked;
            }
            set
            {
                base.m_checked = value;
            }
        }

        //构造函数
        public MakeFeature()
        {
            base.m_caption = "标注要素";
            base.m_checked = false;
            //标注字体初始化
            this.textSymbolTC = new TextSymbolClass();
            IRgbColor color = new RgbColorClass();
            color.Red = 0;
            color.Green = 0;
            color.Blue = 0;
            color.Transparency = 255;
            this.textSymbolTC.Color = color;
            this.textSymbolTC.Size = 9;
            this.textSymbolTC.Font.Bold = false;
            this.textSymbolTC.Font.Name = "宋体";

        }
        //重写BaseCommand基类的虚拟方法OnClick() 
        public override void OnClick()
        {
            ILayer layer = (ILayer)mapControl.CustomProperty;
            IFeatureLayer featureLayer = layer as IFeatureLayer;
            DataRow[] drs = PublicConstant.TC_DRS;
            foreach (DataRow dr in drs)
            {
                string labelField = dr["labelField"] == DBNull.Value ? "" : dr["labelField"].ToString();
                string tcTitle = dr["title"] == DBNull.Value ? "" : dr["title"].ToString();

                if (tcTitle == featureLayer.Name)
                {
                    string[] fieldShow = { labelField };
                    IGeoFeatureLayer layerShow = featureLayer as IGeoFeatureLayer;

                    if (base.m_checked == false && layerShow.DisplayAnnotation == false)
                    {
                        base.m_checked = true;
                        this.DrawLabel(fieldShow, layerShow, textSymbolTC, mapControl);
                    }
                    else if (base.m_checked == true && layerShow.DisplayAnnotation == true)
                    {
                        base.m_checked = false;
                        layerShow.DisplayAnnotation = false;
                    }
                    this.mapControl.Refresh();
                }
            }
        }

        //重写BaseCommand基类的抽象方法OnCreate(object hook) 
        public override void OnCreate(object hook)
        {
            mapControl = (IMapControl3)hook;
        }

        /// <summary>
        /// 设置图层标签
        /// </summary>
        /// <param name="FieldShow">标注字段</param>
        /// <param name="layerShow">标注图层</param>
        /// <param name="textSymbol">标注字体</param>
        /// <param name="MapShow">AxMapControl</param>
        /// <returns></returns>
        private void DrawLabel(string[] FieldShow, IGeoFeatureLayer layerShow, ITextSymbol textSymbol, ESRI.ArcGIS.Controls.IMapControl3 MapShow)
        {
            if (FieldShow.Length == 0) return;
            layerShow.DisplayAnnotation = true;

            string strFieldString = "[" + FieldShow[0] + "]";
            if (FieldShow[0] == "TBBH")
            {
                strFieldString = "CInt(" + strFieldString + ")";
            }
            for (int i = 1; i < FieldShow.Length; i++)
            {
                strFieldString = strFieldString + "+[" + FieldShow[i] + "]";
            }

            IAnnotateLayerPropertiesCollection pannprocol = layerShow.AnnotationProperties;
            ILabelEngineLayerProperties plabelengin;
            IAnnotateLayerProperties pannPro;

            IBasicOverposterLayerProperties pbasicoverposterlayerprops = new BasicOverposterLayerPropertiesClass();
            pbasicoverposterlayerprops.NumLabelsOption = esriBasicNumLabelsOption.esriOneLabelPerShape;
            IElementCollection eleTmp = new ElementCollectionClass();

            for (int j = 0; j < pannprocol.Count; j++)
            {
                pannprocol.QueryItem(j, out pannPro, out eleTmp, out eleTmp);
                plabelengin = (ILabelEngineLayerProperties)pannPro;
                plabelengin.Expression = strFieldString;

                plabelengin.Symbol = textSymbol;
                plabelengin.BasicOverposterLayerProperties = pbasicoverposterlayerprops;
            }

            ITrackCancel pcon = new CancelTrackerClass();
            pcon.Continue();
            layerShow.Draw(esriDrawPhase.esriDPAnnotation, MapShow.ActiveView.ScreenDisplay, pcon);
        }
    }
    #endregion


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值