ArcGIS AddIn开发之:属性刷工具

插件地址,相关细节已更新

利用AddIn开发,实现类似PS中仿制图章的工具。即通过鼠标点选的方式,选择一个要素作为源,然后将属性赋值给其他要素。

Tool代码:

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Framework;
using System.Windows.Forms;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Editor;
using System.Drawing;

namespace Brush_Attributes
{
    public class Brush_Attributes : ESRI.ArcGIS.Desktop.AddIns.Tool
    {
        private static AttributeBrushTool_SetPanel m_pbp = null;

        IMap pMap = null;
        //IFeatureLayer pFtlyr = null; //待操作的图层
        IEditor _Editor;

        public Brush_Attributes()
        {
            UID eUID = new UIDClass();
            eUID.Value = "esriEditor.Editor";
            _Editor = ArcMap.Application.FindExtensionByCLSID(eUID) as IEditor;

            if (_Editor.EditState != esriEditState.esriStateEditing)
            {
                Enabled = false;
                return;
            }
            else
            {
                Enabled = true;
            }
        }

        protected override void OnUpdate()
        {
            if (ArcMap.Application != null)
            {
                UID eUID = new UIDClass();
                eUID.Value = "esriEditor.Editor";
                _Editor = ArcMap.Application.FindExtensionByCLSID(eUID) as IEditor;

                if (_Editor.EditState != esriEditState.esriStateEditing)
                {
                    Enabled = false;
                    return;
                }
                else
                {
                    Enabled = true;
                }
            }
        }

        protected override void OnActivate()
        {
            if (Brush_Attributes.m_pbp == null)
            {
                UID windID = new UIDClass();
                windID.Value = ThisAddIn.IDs.AttributeBrushTool_SetPanel;
                IDockableWindow pdw = ArcMap.DockableWindowManager.GetDockableWindow(windID);
                if (!pdw.IsVisible())
                {
                    pdw.Show(true);
                }
            }
        }

        protected override void OnKeyUp(KeyEventArgs arg)
        {
            if (arg.KeyCode == System.Windows.Forms.Keys.Escape)
                ArcMap.Application.CurrentTool = null;
        }

        protected override void OnMouseDown(MouseEventArgs arg)
        {
            if (AttributeBrushTool_SetPanel.cpidLyrId == -1)
                return;

            pMap = ArcMap.Document.FocusMap;
            IActiveView pAv = pMap as IActiveView;
            try
            {
                IPoint tmppnt = new PointClass();
                tmppnt = pAv.ScreenDisplay.DisplayTransformation.ToMapPoint(arg.X, arg.Y);

                ITopologicalOperator ptop = tmppnt as ITopologicalOperator;
                IPolygon tmpPln = ptop.Buffer(0.5) as IPolygon;
                if (AttributeBrushTool_SetPanel.cpidLyrId == -1)
                    return;

                IFeatureClass copiedFtCls = ((IFeatureLayer)pMap.get_Layer(AttributeBrushTool_SetPanel.cpidLyrId)).FeatureClass;

                if (arg.Control && arg.Button == MouseButtons.Left)
                {
                    //设置鼠标样式
                    this.Cursor = System.Windows.Forms.Cursors.Cross;
                    //选择源
                    ISpatialFilter psf = new SpatialFilterClass();
                    psf.Geometry = tmpPln;
                    psf.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;

                    IFeatureCursor pFtCursor = copiedFtCls.Search(psf, false); ;
                    IFeature pTmpFt = pFtCursor.NextFeature();
                    if (pTmpFt != null)
                    {
                        //将选择到的要素添加至前端
                        List<string> ss = new List<string>();
                        for (int i = 0; i < pTmpFt.Fields.FieldCount; i++)
                        {
                            switch (pTmpFt.Fields.get_Field(i).Type)
                            {
                                case esriFieldType.esriFieldTypeBlob:
                                case esriFieldType.esriFieldTypeGeometry:
                                case esriFieldType.esriFieldTypeGlobalID:
                                case esriFieldType.esriFieldTypeGUID:
                                case esriFieldType.esriFieldTypeOID:
                                case esriFieldType.esriFieldTypeRaster:
                                    ss.Add("");
                                    break;
                                default:
                                    ss.Add(pTmpFt.get_Value(i).ToString());
                                    break;
                            }
                        }
                        //m_pbp.fillDataGridView3(ss);
                        AttributeBrushTool_SetPanel.fillDataGridView3(ss);
                    }
                }

                else if (arg.Button == MouseButtons.Left)
                {
                    //刷属性
                    //设置鼠标样式
                    Stream sm = this.GetType().Assembly.GetManifestResourceStream("Brush_Attributes.Images.AttributeBrushTool.cur");
                    this.Cursor = new System.Windows.Forms.Cursor(sm);
                    //获取DataGridView中的属性
                    List<string> str = AttributeBrushTool_SetPanel.getDataGridViewValue3();
                    if (str.Count == 0)
                        return;

                    //获取所选择的要素
                    ISpatialFilter psf = new SpatialFilterClass();
                    psf.Geometry = tmpPln;
                    psf.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;

                    IFeatureCursor pFtCursor = copiedFtCls.Update(psf, true); ;
                    IFeature pTmpFt = pFtCursor.NextFeature();
                    if (pTmpFt != null)
                    {
                        for (int i = 0; i < pTmpFt.Fields.FieldCount; i++)
                        {
                            if (str[i] != "")
                            {
                                //字段类型转换试试
                                bool goon = true;
                                double d;
                                int ii;

                                switch (pTmpFt.Fields.get_Field(i).Type)
                                {
                                    case esriFieldType.esriFieldTypeDouble:
                                        goon = double.TryParse(str[i], out d);
                                        break;
                                    case esriFieldType.esriFieldTypeInteger:
                                        goon = int.TryParse(str[i], out ii);
                                        break;
                                    default:
                                        break;
                                }
                                if (goon)
                                {
                                    pTmpFt.set_Value(i, str[i]);
                                    pFtCursor.UpdateFeature(pTmpFt);
                                    pFtCursor.Flush();
                                }
                                else
                                {
                                    MessageBox.Show(pTmpFt.Fields.get_Field(i).Name + "字段所输入的值 类型不正确");
                                    return;
                                }
                            }

                        }

                        //刷完属性闪烁要素
                        //FlashShape(pTmpFt);

                        //保存交由编辑器执行
                        //pTmpFt.Store();
                        IFeatureSelection pFtSelection = (IFeatureSelection)((IFeatureLayer)pMap.get_Layer(AttributeBrushTool_SetPanel.cpidLyrId));
                        pFtSelection.Clear();
                        pFtSelection.Add(pTmpFt);
                        pAv.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, null, null);
                        System.Threading.Thread.Sleep(100);
                        pFtSelection.Clear();
                        System.Threading.Thread.Sleep(100);
                        pAv.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);
                        System.Threading.Thread.Sleep(100);
                        pFtSelection.Add(pTmpFt);
                        pAv.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);
                    }
                }
                else
                {
                    return;
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("发生未知异常_"+ex.Message);
                return;
            }
        }

        /// <summary>
        /// 要素闪烁
        /// </summary>
        /// <param name="pTmpFt"></param>
        private void FlashShape(IFeature pTmpFt)
        {
            switch (pTmpFt.Shape.GeometryType)
            {
                case esriGeometryType.esriGeometryPoint:
                case esriGeometryType.esriGeometryPolygon:
                case esriGeometryType.esriGeometryPolyline:
                    break;
                default:
                    break;
            }
        }

        protected override void OnMouseMove(MouseEventArgs arg)
        {
            try
            {
                if (arg.Control)
                {
                    //设置鼠标样式
                    this.Cursor = System.Windows.Forms.Cursors.Cross;
                }
                else
                {
                    Stream sm = this.GetType().Assembly.GetManifestResourceStream("Brush_Attributes.Images.AttributeBrushTool.cur");
                    this.Cursor = new System.Windows.Forms.Cursor(sm);
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("发生未知异常_"+ex.Message);
            }
            
        }

        protected override void OnKeyDown(KeyEventArgs arg)
        {
            if (arg.Control)
            {
                //设置鼠标样式
                this.Cursor = System.Windows.Forms.Cursors.Cross;
            }
        }
    }

}

其中,上述代码也实现了在AddIn中自定义鼠标样式。


交互式窗体代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Geodatabase;

namespace Brush_Attributes
{
    /// <summary>
    /// Designer class of the dockable window add-in. It contains user interfaces that
    /// make up the dockable window.
    /// </summary>
    public partial class AttributeBrushTool_SetPanel : UserControl
    {
        IMap pMap = null;
        public static int cpidLyrId=-1;
        internal static AttributeBrushTool_SetPanel _Bsp;

        public AttributeBrushTool_SetPanel(object hook)
        {
            InitializeComponent();
            this.Hook = hook;

            _Bsp = this;
        }

        /// <summary>
        /// Host object of the dockable window
        /// </summary>
        private object Hook
        {
            get;
            set;
        }

        /// <summary>
        /// Implementation class of the dockable window add-in. It is responsible for 
        /// creating and disposing the user interface class of the dockable window.
        /// </summary>
        public class AddinImpl : ESRI.ArcGIS.Desktop.AddIns.DockableWindow
        {
            private AttributeBrushTool_SetPanel m_windowUI;

            public AddinImpl()
            {
            }

            protected override IntPtr OnCreateChild()
            {
                m_windowUI = new AttributeBrushTool_SetPanel(this.Hook);
                return m_windowUI.Handle;
            }

            protected override void Dispose(bool disposing)
            {
                if (m_windowUI != null)
                    m_windowUI.Dispose(disposing);

                base.Dispose(disposing);
            }

        }

        private void cmbLayer_SelectedIndexChanged(object sender, EventArgs e)
        {
            dataGridView1.Rows.Clear();
            pMap = ArcMap.Document.FocusMap;
            //根据选择图层的不同,DataGridView中显示不同的字段名
            if (cmbLayer.SelectedIndex != -1)
            {
                string tmplyrname = cmbLayer.SelectedItem.ToString();
                int tmplyrIndex = getlyridByName(tmplyrname,pMap);
                IFeatureLayer tmpLyr = pMap.get_Layer(tmplyrIndex) as IFeatureLayer;
                cpidLyrId = tmplyrIndex;
                dataGridView1.Rows.Add(tmpLyr.FeatureClass.Fields.FieldCount);
                for (int i = 0; i < tmpLyr.FeatureClass.Fields.FieldCount; i++)
                {
                    dataGridView1[0, i].Value = tmpLyr.FeatureClass.Fields.get_Field(i).Name;
                    switch (tmpLyr.FeatureClass.Fields.get_Field(i).Type)
                    {
                        case esriFieldType.esriFieldTypeGeometry:
                        case esriFieldType.esriFieldTypeRaster:
                        case esriFieldType.esriFieldTypeOID:
                        case esriFieldType.esriFieldTypeGUID:
                        case esriFieldType.esriFieldTypeGlobalID:
                        case esriFieldType.esriFieldTypeBlob:
                            dataGridView1[1, i].ReadOnly = true;
                            dataGridView1[2, i].ReadOnly = true;
                            break;
                        default:
                            DataGridViewCheckBoxCell dgc = ((DataGridViewCheckBoxCell)dataGridView1[1, i]);
                            dgc.Value = true;
                            break;
                    }
                }
            }
        }

        private int getlyridByName(string nm, IMap pMap)
        {
            int id = -1;
            for (int i = 0; i < pMap.LayerCount; i++)
            {
                if (nm == pMap.get_Layer(i).Name)
                {
                    id = i;
                    break;
                }
            }
            return id;
        }

        /// <summary>
        /// 填充第三列,没有则为空
        /// </summary>
        /// <param name="vls"></param>
        public static void fillDataGridView3(List<string> vls)
        {
            try
            {
                for (int i = 0; i < _Bsp.dataGridView1.Rows.Count; i++)
                {
                    DataGridViewCheckBoxCell dgc = (DataGridViewCheckBoxCell)_Bsp.dataGridView1[1, i];
                    if (dgc.Value == null)
                        continue;
                    if ((bool)dgc.Value) //可修改的则赋值
                        _Bsp.dataGridView1[2, i].Value = vls[i];
                }
            }
            catch (System.Exception ex)
            {
                throw new Exception(ex.Message);
            } 
        }

        /// <summary>
        /// 获取第三列的值
        /// </summary>
        /// <returns></returns>
        public static List<string> getDataGridViewValue3()
        {
            List<string> sre = new List<string>();
            try
            {
                for (int i = 0; i < _Bsp.dataGridView1.Rows.Count; i++)
                {
                    DataGridViewCheckBoxCell dgc = (DataGridViewCheckBoxCell)_Bsp.dataGridView1[1, i];
                    if (dgc.Value == null)
                    {
                        sre.Add("");
                        continue;
                    }
                    if ((bool)dgc.Value) //可修改的则赋值
                    {
                        if (_Bsp.dataGridView1[2, i].Value != null)
                            sre.Add(_Bsp.dataGridView1[2, i].Value.ToString());
                        else
                            sre.Add("");
                    }
                }
                return sre;
            }
            catch (System.Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        private void cmbLayer_DropDown(object sender, EventArgs e)
        {
            try
            {
                cmbLayer.Items.Clear();

                IMap tmpMap = ArcMap.Document.FocusMap;
                for (int i = 0; i < tmpMap.LayerCount; i++)
                {
                    IFeatureLayer tmpFtLyr = tmpMap.get_Layer(i) as IFeatureLayer;
                    if (tmpFtLyr != null)
                    {
                        cmbLayer.Items.Add(tmpFtLyr.Name);
                    }
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("发生未知异常_"+ex.Message);
            }
        }

        private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
        {
            this.dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
        }
    }
}

工具界面如下。选择待操作图层之后,通过Ctrl+单击,实现属性源的选择。刷否checkebox可以设置属性是否写入目标要素。然后通过单击实现对目标要素属性的写入。



里面有就是多种数据处理工具,非常便利。 1.文本文件操作 1.1征地部标准坐标导出... 2 1.2征地部标准坐标导入... 6 1.3线封闭... 6 1.4点集转面... 7 2.     数据转换... 8 2.1        SHP转数据库... 8 2.2        批量数据库转数据库... 9 2.3        栅格彩色转黑白... 9 3.     数据检查和数据信息获得... 10 3.1        锐角检查... 10 3.2        获得内角并判断是否凸多边形... 11 3.3        获得线(面)两个折点方向... 12 3.4        四至和范围获得... 14 3.4.1获得数据的XY范围... 14 3.4.2获得数据的经纬度范围... 15 3.4.3四邻信息获得... 15 3.4.4地块四至点获得... 17 3.4.5地块四至点坐标获得... 18 3.4.6获得相对四至(适合大比例小地块) 19 3.4.7获得绝对四至(根据四至点坐标)... 20 3.5        属性赋值... 22 3.5.1比例分析... 22 3.5.2加权平均... 23 3.6        椭球面积计算... 23 3.7        计算点到线的距离... 24 3.8        道路河流依次经过的地方... 24 4.     裁剪和合并... 25 4.1        按属性裁剪... 25 4.2        矢量数据批量裁剪... 25 4.3        矢量数据批量合库... 26 4.4        影像批量裁剪... 27 4.5        影像批量合并... 28 5.     MXD文档处理... 29 5.1        MXD批量裁剪... 29 5.2        MXD批量导出图片... 30 5.3        mxd压缩和版本另存... 31 5.4        mxd文档相对路径和无效数据检查... 31 6.     制图... 32 6.1        梯形接幅表的创建... 32 6.2        矩形接幅表的创建... 33 6.3        公里网或方里网制作... 34 6.4        经纬网制作... 35 6.5        色带制作... 35 7.     拓扑错误处理... 36 7.1        删除线面直线上的点... 36 7.2        点不在线面上处理... 37 7.3        线部分或完全重叠处理... 38 7.4        删除完全重复的点线面处理... 38 7.5        面线边界不重合... 39 7.6        面缝隙处理... 39 7.7        面重叠处理... 40 7.8        删除线面上重复点... 40 7.9        检查多部件要素... 41 7.10          删除伪节点... 42 8.     编号工具... 42 8.1        整库更新BSM.. 42 8.2        更新BSM.. 43 8.3        字符串前补零... 43 8.4        按图形自动编号... 44 9.     数据处理... 45 9.1        两个图层按重叠度赋属性... 45 9.2        分区域消除... 46 9.3        批量压缩数据库... 47 9.4        批量修复几何(修复前一定备份数据) 47 9.5        按长度分割线... 48 9.6        线分割面保留属性... 48 9.7        融合时字段连接... 49 9.8        要素移动... 51 10.            业务相关... 51 10.1          上级行政区和下级行政区图形不一致处理... 51 10.2          修改面左上角点为第一个点... 52 10.3          修改面左上角点为第一个点根据点层... 53 10.4          地类符号生成... 53 10.5          宗地获得界址点顺序号... 54 10.6          村级行政区生成行政界线... 55 10.7          生成上级行政区... 56 10.8          查询节点距离小于指定距离的点... 56 11.            其他工具... 57 11.1          获得工具箱的工具数... 57 11.2          飞行网络路线制作... 57 12.            版本说明... 59 12.1          个人版... 59 12.2          企业版... 59 ———————————————— 版权声明:本文为CSDN博主「gisoracle」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/gisoracle/article/details/98242703
GisTool4.0说明: 一、功能说明: MAPGIS6.5实用工具,集成若干MAPGIS6.5不具有的实用功能,简单易用。 本软件有以下特色功能: 1、精确制图功能(类似于AutoCAD)。画线、移动、复制图元时,都可以输入距离和角度,还可以捕捉端点、垂足等,F2为正交开关。 2、支持鼠标滚轮窗口缩放操作。向前滚动图形放大,向后滚动图形缩小,按下滚轮图形平移。 3、格式功能。点、线、区都可以运用格式进行修改和编辑。 4、快速制表功能。利用画水平线、竖直线以及捕足功能,输入距离移动、复制、拉伸线条功能,可以快速绘制各种表格。 5、多种测量功能。测量面积不需要造区,且有多种简单快速的方法,带图上面积与实际面积转换的功能。可以直接测量线图元的方位角。 6、线段拉伸功能。能够将线图元进行拉伸操作,所有选中的线节点将同时拉伸,同时可以输入距离和角度。 7、属性编辑功能。采用电子表格的方式对图元属性进编辑,与excel、acsses能够直接互相复制、粘贴,并且能够实现图形联动、属性联动的操作。 8、条件删除图元功能。可以根据图元ID能及图元属性有条件地对图元批量删除,还可以自动删除重叠图元。 9、整图栽剪功能。不需要生成单独的栽剪框文件,只要选择工程文件中任一条封闭的线就会自动对工程中的所有可见文件进行栽剪,栽剪后的文件自动保存在新建的目录中,每次栽剪都会生成一个新的目录,栽剪后的文件名与原文件名一致。 10、CAD转MapGis。本功能可以将AutoCAD格式的图形文件转换成MapGis格式的图形文件,能将CAD中的充填图案直接转换成MapGis中的区图元,而且能将CAD中的内部块、多行文本直接转出,不需要在CAD中分解。完全按照CAD中的图层分层转出所有可见图元,颜色、线型自动转换,不需要对照表,所有操作全自动,速度快,是目前比较完美的转换程序。可打开安装目录中的 CADdata.dxf 文件试用。 11、Excel转MapGis。本功能可以将 Microsoft Office Excel 电子表格文件中的各种表格直接转换成MapGis格式的点、线、面文件。可打开安装目录中的 bookdata.xls 文件试用。 12、自动绘制钻孔柱状图。本功能可以将保存在 Microsoft Office Excel 电子表格文件中的钻孔数据绘制成钻孔柱状图,数据输入格式请参考软件安装目录中的 zkdata.xls,也可打开此文件试用。 13、自动绘等值线、等高线。将测量点数据录入到文本文件或电子表格中,然后打开点数据文件便可自动绘制等值线、等高线。 14、等值线、等高线加密。在绘制等值线、等高线时,可以先画计曲线,然后利用此功能在计曲线之间自动生成首曲线,可节省大量的绘图时间。 15、坐标转换功能。经纬度坐标、六度带坐标、三度带坐标都可以批量相互转换。 16、自动标注功能。等高线、等值线其值可以根据其线的方向自动调整角度标注。 17、坐标成图功能。可以根据坐标及比例尺生成点、生成线,可以在图上采集实际坐标,批量导入文本注释等。 18、图例排列功能。输入指定的间距,点或图例都可以进行横向、纵向的等距离自动排列。 19、显示顺序功能。点、线、区都可以调整其显示顺序,即图元置顶、图元置底。 20、常用注释功能。可以将常用的文本注释、子图等事先设置好,使用时直接从工具栏中调出。 21、图元搜索功能。可以根据图元的属性、颜色、注释、子图号在图面上将符合条件的图元逐个查找出来。 22、属性提取功能。输入属性提取条件,能够将点、线、区中的符合条件的图元提取到新的文件中。 23、储量计算功能。用来计算分段平均品位、剖面面积、体积公式判别、块段体积、单矿石类型体积等。 24、线头沿边处理。所有线头自动靠近线上(端点自动落到线上,用于拓朴前期处理)。 25、自动节点平差。周围线头自动相聚于一点。(端点自动相聚于一点,用于拓朴前期处理)。 26、编辑属性结构。用电子表格的方式编辑属性结构,整个文件的属性结构可以任意复制和粘贴。 注:拉框选点时,从左到右拉框,需要包住整个点才能选中;从右到左拉框,只要框压住点的任何部位都可选中(与AutoCAD类似)。 二、安装说明: 1、安装前,请先关闭XP的数据执行保护功能(我的电脑→右键→属性→高级→启动和故障恢复→设置→编辑→optin改为alwaysoff→保存→重启电脑),并重新启动计算机。 2、安装MapGis65,需要原版光盘提供的安装程序,版本号为021010,日期是2002-10-25,其它版本的可能不太好用。 3、先
评论 18
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值