ArcEngine 開發自動完成多邊形功能 [csharp] 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 { www.2cto.com 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 } }