欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

使用 ArcEngine 实现多边形的自动化填充功能

最编程 2024-01-22 07:00:21
...

ArcEngine 开发自动完成多边形功能

using System;
using System.Collections.Generic;
using System.Text;
using GIS.ArcGIS.Commands.SelEx;
using ESRI.ArcGIS.Controls;
using Net.Resource;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Display;
using GIS.ArcGIS.Common;
using GIS.ArcGIS.Carto;
using GIS.ArcGIS.EditEx;
using System.Windows.Forms;

namespace GIS.ArcGIS.Commands.EditEx
{
    /// <summary>
    /// ArcEngine 开发自动完成多边形功能
    /// vp:hsg
    /// create date:2012-07-20
    /// modify date:2013-03-12
    /// </summary>
    public class EditAutoCompletePolygonTool : EditGisBaseTool, ILineSelect
    {
        private EngineEditorClass eedit = new EngineEditorClass();
        public EditAutoCompletePolygonTool()
        {

        }
        ~EditAutoCompletePolygonTool()
        {
            try
            {
                m_Cursor = null;
            }
            catch (Exception ee)
            {
            }
        }


        #region ICommand 成员
        public override int Bitmap
        {
            get
            {
                return 0;
            }
        }
        public  string Description
        {
            get { return "自动完成多边形功能"; }
        }

        public override string Caption
        {
            get { return this.Description; }
        }

        public override string Category
        {
            get { return "AutoCompletePolygon"; }
        }

        public override bool Checked
        {
            get { return false; }
        }

        public override bool Enabled
        {
            get
            {
                if (eedit.TargetLayer != null)
                {
                    return true;
                }
                return false;
            }
        }

        public override int HelpContextID
        {
            get { return 0; }
        }

        public override string HelpFile
        {
            get { return ""; }
        }

        public override string Message
        {
            get { return this.Description; }
        }

        public override string Name
        {
            get { return this.Description; }
        }

        public override void OnClick()
        {
            if (m_Cursor == null)
            {
                m_Cursor = ResourceLib.getInstance().GetCursorResource("Net.Resource.Select.PointSelect.cur");
            }
            this.Step = 0;
            this.IsUsed = false;
            this.LineFeedback = null;
        }

        public override string Tooltip
        {
            get { return this.Description; }
        }

        #endregion

        #region ITool 成员
        private System.Windows.Forms.Cursor m_Cursor = null;
        public override int Cursor
        {
            get
            {
                if (m_Cursor != null)
                {
                    return m_Cursor.Handle.ToInt32();
                }
                else
                {
                    return 0;
                }
            }
        }

        public override bool Deactivate()
        {
            return true;
        }

        public override bool OnContextMenu(int x, int y)
        {
            return false;
        }

        private void CreatePolygonsFromFeatures(IFeatureClass getpolygon_fc)
        {
            IDataset pDataset = getpolygon_fc as IDataset;
            IWorkspace pWorkspace = pDataset.Workspace;
            IWorkspaceEdit pWorkspaceEdit = pWorkspace as IWorkspaceEdit;
            if (pWorkspaceEdit.IsBeingEdited() != true)
            {
                pWorkspaceEdit.StartEditing(true);
                if (pWorkspaceEdit.IsBeingEdited() != true)
                {
                    return;
                }
                pWorkspaceEdit.StartEditOperation();
            }
            IFeatureConstruction pFeatureConstruction = new FeatureConstructionClass();


            IMap pMap = this.HookHelper.FocusMap;// axMapControl1.Map;
            IEnumFeature pEnumFeature = pMap.FeatureSelection as IEnumFeature;
            IFeature pFeature = pEnumFeature.Next();
            if (pFeature == null)
            {
                MessageBox.Show("pEnumFeature为空");
                return;
            }

            pFeatureConstruction.ConstructPolygonsFromFeatures(null, getpolygon_fc, null, false, false, pEnumFeature, null, 0.01, null);
            MessageBox.Show("转化结束");
        }

        /// <summary>
        /// 双击 结束画
        /// </summary>
        public override void OnDblClick()
        {
            try
            {
                object objMiss = Type.Missing;
                //
                if (this.IsUsed == false) return;
                if (this.LineFeedback == null) return;
                if (this.Step > 1)
                {
                    IPolyline tpPolyline = this.LineFeedback.Stop();
                    if (tpPolyline == null)
                    {
                        this.Step = 0;
                        this.IsUsed = false;
                        this.LineFeedback = null;
                        return;
                    }
                    IGeometry tpLine = tpPolyline;
                    //获取构面的线集合
                    GeometryBagClass enumGeoLines = new GeometryBagClass();
                    enumGeoLines.AddGeometry(tpLine, ref objMiss, ref objMiss);                    
                    //
                    if (eedit.TargetLayer != null && eedit.TargetLayer is IFeatureLayer)
                    {
                        IFeatureLayer FeatLayer = eedit.TargetLayer as IFeatureLayer;
                        FeatLayer.Selectable = true;
                        IFeatureClass fc = FeatLayer.FeatureClass;
                        IDataset pDataset = fc as IDataset;
                        IWorkspace pWorkspace = pDataset.Workspace;
                        //
                        IGeoDataset geoDS = fc as IGeoDataset;
                        IEnvelope processingBounds = geoDS.Extent;
                        //
                        IInvalidArea invalidArea = new InvalidAreaClass();

                        //要素构造类
                        IFeatureConstruction pfeatBuild = new FeatureConstructionClass();
                        //从enumGeoLines集合中构面并保存到fc面图层中
                        //pfeatBuild.ConstructPolygonsFromGeometries(null, fc, null, false, enumGeoLines, null, 0.0001);
                        //开始自动完成构面并保存到fc面图层中
                        IFeatureSelection featsel = FeatLayer as IFeatureSelection;
                        ISelectionSet selSet = featsel.SelectionSet;
                        pfeatBuild.AutoCompleteFromGeometries(fc, processingBounds, enumGeoLines, invalidArea, 0.0001, pWorkspace, out selSet);
                        if (selSet != null)
                        {
                            MessageBox.Show(selSet.Count.ToString());
                        }
                        //
                        this.HookHelper.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, this.HookHelper.ActiveView.Extent);
                        //
                    }
                    else
                    {
                        System.Windows.Forms.MessageBox.Show("目标图层为空,请开启编辑会话", "提示");
                    }
                    this.Step = 0;
                    this.IsUsed = false;
                    this.LineFeedback = null;
                }
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message, "提示");
            }
        }

        public virtual IGeometry Erase(IGeometry source, IGeometry other)
        {
            ITopologicalOperator topoOper = source as ITopologicalOperator;
            if (!topoOper.IsSimple)
            {
                topoOper.Simplify();
            }
            IGeometry geo = topoOper.Difference(other);
            topoOper = geo as ITopologicalOperator;
            if (!topoOper.IsSimple)
            {
                topoOper.Simplify();
            }
            return geo;
        }

        public override void OnKeyDown(int keyCode, int shift)
        {
        }

        public override void OnKeyUp(int keyCode, int shift)
        {
        }

        public override void OnMouseDown(int button, int shift, int x, int y)
        {
            if (this.HookHelper.ActiveView == null) return;
            if (button == 1)	//MouseDown左键 开始画点/连续画点
            {
                IPoint tpPoint = null;
                if (this.Step <= 0)
                {
                    //开始画点
                    tpPoint = this.HookHelper.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(x, y);
                    this.LineFeedback = new NewLineFeedbackClass();
                    this.LineFeedback.Start(tpPoint);
                    this.Step += 1;
                    this.LineFeedback.Display = this.HookHelper.ActiveView.ScreenDisplay;
                    this.IsUsed = true;
                }
                else
                {
                    //连续画点
                    tpPoint = this.HookHelper.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(x, y);
                    this.Step += 1;
                    this.LineFeedback.AddPoint(tpPoint);
                }
            }  //--
            else   //MouseDown右键  
            {
            }

        }

        public override void OnMouseMove(int button, int shift, int x, int y)
        {
            if (this.IsUsed == true)
            {
                IPoint mappoint = this.HookHelper.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(x, y);
                this.LineFeedback.MoveTo(mappoint);
            }
        }

        public override void OnMouseUp(int button, int shift, int x, int y)
        {
        }

        public override void Refresh(int hdc)
        {
        }

        #endregion

        #region ILineSelect 成员

        public esriSpatialRelEnum getSpatialRel(esriGeometryType geoType)
        {
            //System.Windows.Forms.MessageBox.Show(geoType.ToString());
            esriSpatialRelEnum SpatialRelEnum = esriSpatialRelEnum.esriSpatialRelIntersects;
            switch (geoType)
            {
                case esriGeometryType.esriGeometryPolygon: //面对面Polygon
                    SpatialRelEnum = esriSpatialRelEnum.esriSpatialRelIntersects;
                    break;
                case esriGeometryType.esriGeometryPoint:   //面对点Point
                    SpatialRelEnum = esriSpatialRelEnum.esriSpatialRelIntersects;
                    break;
                case esriGeometryType.esriGeometryPolyline: //面对线Polyline   
                    SpatialRelEnum = esriSpatialRelEnum.esriSpatialRelIntersects;
                    break;
                case esriGeometryType.esriGeometryLine: //面对线Line
                    SpatialRelEnum = esriSpatialRelEnum.esriSpatialRelIntersects;
                    break;
                default:
                    SpatialRelEnum = esriSpatialRelEnum.esriSpatialRelIntersects;
                    break;
            }
            return SpatialRelEnum;
        }

        public ISymbol getSelectionSymbol(esriGeometryType geoType)
        {
            ISymbol symbol = null;
            switch (geoType)
            {
                case esriGeometryType.esriGeometryPolygon: //点对面Polygon
                    symbol = new SimpleFillSymbolClass();
                    ISimpleFillSymbol fs = symbol as ISimpleFillSymbol;
                    fs.Color = EnvironmentSelectionClass.getInstance.DefaultColor;
                    fs.Outline.Color = EnvironmentSelectionClass.getInstance.DefaultColor;
                    fs.Outline.Width = 1;
                    fs.Style = esriSimpleFillStyle.esriSFSSolid;

                    break;
                case esriGeometryType.esriGeometryPoint:   //点对点Point
                    symbol = new SimpleMarkerSymbolClass();
                    (symbol as ISimpleMarkerSymbol).OutlineColor = EnvironmentSelectionClass.getInstance.DefaultColor;
                    (symbol as ISimpleMarkerSymbol).Outline = true;
                    (symbol as ISimpleMarkerSymbol).OutlineSize = 5;
                    (symbol as ISimpleMarkerSymbol).Style = esriSimpleMarkerStyle.esriSMSCircle;
                    break;
                case esriGeometryType.esriGeometryPolyline: //点对线Polyline   //点作Buffer容差值
                    symbol = new SimpleLineSymbolClass();
                    (symbol as ISimpleLineSymbol).Color = EnvironmentSelectionClass.getInstance.DefaultColor;
                    (symbol as ISimpleLineSymbol).Width = 1;
                    (symbol as ISimpleLineSymbol).Style = esriSimpleLineStyle.esriSLSSolid;
                    break;
                case esriGeometryType.esriGeometryLine: //点对线Line
                    symbol = new SimpleLineSymbolClass();
                    (symbol as ISimpleLineSymbol).Color = EnvironmentSelectionClass.getInstance.DefaultColor;
                    (symbol as ISimpleLineSymbol).Width = 1;
                    (symbol as ISimpleLineSymbol).Style = esriSimpleLineStyle.esriSLSSolid;
                    break;
                default:
                    symbol = new SimpleLineSymbolClass();
                    (symbol as ISimpleLineSymbol).Color = EnvironmentSelectionClass.getInstance.DefaultColor;
                    (symbol as ISimpleLineSymbol).Width = 1;
                    (symbol as ISimpleLineSymbol).Style = esriSimpleLineStyle.esriSLSSolid;
                    break;
            }
            return symbol;
        }

        private INewLineFeedback m_LineFeedback = null;
        public INewLineFeedback LineFeedback
        {
            get { return m_LineFeedback; }
            set { m_LineFeedback = value; }
        }
        private bool m_IsUsed = false;
        public bool IsUsed
        {
            get
            {
                return m_IsUsed;
            }
            set
            {
                m_IsUsed = value;
            }
        }
        private int m_Step = 0;
        public int Step
        {
            get { return m_Step; }
            set { m_Step = value; }
        }

        #endregion
    }
}


 

 

推荐阅读