ArcGIS Engine移动图层顺序

要在TOC控件中移动顺序,其实就是这二个操作,选择要移动的图层;拖动要放置的位置,但是这两个操作牵扯到三个函数,分别是mousedown,mounsemove,mounseup。而在这三个操作里面牵扯一个重要的方法HitTest这里只做了对图层的移动,其它的没有考虑,现在直接上代码:

  //保存选择的图层
        public ILayer pSeletLayer = null;
        public IBasicMap pBasicMap = null;
        System.Object pOther = new object();
        System.Object pIndex = new object();

        private void axTOCControl1_OnMouseDown(object sender, ITOCControlEvents_OnMouseDownEvent e)
        {
            ILayer pLayer = null;
           
           
            //鼠标左击操作,对层的选择
            if (e.button == 1)
            {
                this.axTOCControl1.HitTest(e.x, e.y, ref pItem, ref pBasicMap, ref pLayer, ref pOther, ref pIndex);
                if (pItem == esriTOCControlItem.esriTOCControlItemLayer)
                {
                    pSeletLayer = pLayer;
                    this.axTOCControl1.SelectItem(pLayer, null);
                }

                //刷新SelectLayer控件

            }
           
           
            //鼠标右击操作
            if (e.button == 2)
            {
                this.axTOCControl1.HitTest(e.x, e.y, ref pItem, ref pBasicMap, ref pLayer, ref pOther, ref pIndex);
                /
                //显示右击事件窗口
                if (pItem == esriTOCControlItem.esriTOCControlItemLayer)
                {
                    pSeletLayer = pLayer;
                    this.axTOCControl1.SelectItem(pLayer, null);

                    if (pLayer is IFeatureLayer)
                    {

                        this.pContextMenuStrip.Show(this.axTOCControl1, new System.Drawing.Point(e.x, e.y));
                    }
                }
                else if (pItem == esriTOCControlItem.esriTOCControlItemMap)
                {
                    this.pMapMenu.Show(this.axTOCControl1, new System.Drawing.Point(e.x, e.y));
                }


            }
        }
        private void axTOCControl1_OnMouseMove(object sender, ITOCControlEvents_OnMouseMoveEvent e)
        {
            IBasicMap pTemBasicMap = null;
            System.Object pTemOther = new object();
            System.Object pTemIndex = new object();


            esriTOCControlItem pTemItem = new esriTOCControlItem();   //选择的TOC的项目名

            ILayer pLayer = null;

            if (e.button == 1)
            {
                this.axTOCControl1.HitTest(e.x, e.y, ref pTemItem, ref pTemBasicMap, ref pLayer, ref pTemOther, ref pTemIndex);

                if (pTemItem != esriTOCControlItem.esriTOCControlItemNone)
                {
                   
                    axTOCControl1.MousePointer = esriControlsMousePointer.esriPointerCustom;
                }
                //刷新SelectLayer控件

            }
        }

        private void axTOCControl1_OnMouseUp(object sender, ITOCControlEvents_OnMouseUpEvent e)
        {
             
         IBasicMap pTemBasicMap = null;
         System.Object pTemOther = new object();
         System.Object pTemIndex = new object();

    
         esriTOCControlItem pTemItem = new esriTOCControlItem();      //选择的TOC的项目名

            ILayer pLayer = null;
   
            //鼠标左击操作,对层的选择
            if (e.button == 1)
            {
                this.axTOCControl1.HitTest(e.x, e.y, ref pTemItem, ref pTemBasicMap, ref pLayer, ref pTemOther, ref pTemIndex);
                if (pItem == esriTOCControlItem.esriTOCControlItemLayer)
                {
                    if (pSeletLayer != pLayer && pLayer!=null)
                    {
                        this.axTOCControl1.SelectItem(pLayer, null);

                        this.globe.Globe.GlobeDisplay.Scene.MoveLayer(pSeletLayer, CommonHelper.GetIndexByLayer(this.globe.Globe, pLayer));

                        this.globe.Globe.GlobeDisplay.RefreshViewers();
                        this.axTOCControl1.Refresh();

                        this.axTOCControl1.SelectItem(pSeletLayer, null);
                    }
                    
                }


            }

        }

其中GetIndexByLayer 方法是用来获取图层对象的索引值,代码如下:

  public static int GetIndexByLayer(IGlobe _pGlobe,ILayer _pLayer)
        {
            IScene pScene = _pGlobe.GlobeDisplay.Scene;
            int pIndex = 0;

            for (int i = 0; i < pScene.LayerCount; i++)
            {
                if (pScene.get_Layer(i).Name == _pLayer.Name)
                {
                    pIndex = i;
                    return pIndex;
                }

            }

            return pIndex;
        }

ARCGIS Engine 中保存图层文件,你可以使用以下代码示例: ```csharp // 创建一个地图文档 IMapDocument mapDoc = new MapDocument(); // 打开地图文档 mapDoc.Open("C:\\path\\to\\mapfile.mxd"); // 获取地图 IMap map = mapDoc.Map[0]; // 获取图层 ILayer layer = map.Layer[0]; // 创建图层文件保存对象 IWorkspaceFactory workspaceFactory = new ShapefileWorkspaceFactoryClass(); IWorkspace workspace = workspaceFactory.OpenFromFile("C:\\path\\to\\output\\folder", 0); IFeatureClass featureClass = ((IFeatureLayer)layer).FeatureClass; IDataset dataset = (IDataset)featureClass; IWorkspaceEdit workspaceEdit = (IWorkspaceEdit)workspace; workspaceEdit.StartEditing(true); workspaceEdit.StartEditOperation(); IWorkspaceDomains workspaceDomains = (IWorkspaceDomains)workspace; IDomain domain = workspaceDomains.get_DomainByName("字段名称"); IField field = featureClass.Fields.get_Field(featureClass.Fields.FindField("字段名称")); IFieldEdit fieldEdit = (IFieldEdit)field; fieldEdit.Domain_2 = domain; featureClass.AddField(field); featureClass.Store(); // 保存图层文件 IDataset layerDataset = (IDataset)layer; layerDataset.Copy("newLayerFileName"); // 关闭地图文档 mapDoc.Close(); ``` 这段代码打开一个地图文档,获取第一个图层,然后使用 `Copy` 方法将图层保存为一个新的文件。你需要将代码中的路径和文件名替换为你自己的路径和文件名。请确保你有足够的权限来保存文件到指定的文件夹中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值