public void OnMouseDown(int button, int shift, int x, int y) { if (button != 2) { try { IFeatureLayer targetLayer = Editor.UniqueInstance.TargetLayer; if (targetLayer != null) { IFeatureClass featureClass = targetLayer.FeatureClass; if (featureClass != null) { IPoint pGeometry = this.m_hookHelper.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(x, y); ISpatialFilter queryFilter = new SpatialFilterClass { Geometry = pGeometry, GeometryField = featureClass.ShapeFieldName, SubFields = featureClass.ShapeFieldName, SpatialRel = esriSpatialRelEnum.esriSpatialRelWithin }; IFeatureCursor o = targetLayer.Search(queryFilter, false); IFeature feature = o.NextFeature(); IFeature feature2 = o.NextFeature(); Marshal.ReleaseComObject(o); o = null; if ((feature != null) && (feature2 != null)) { feature = featureClass.GetFeature(feature.OID); feature2 = featureClass.GetFeature(feature2.OID); ITopologicalOperator2 shape = feature.Shape as ITopologicalOperator2; IGeometry geometry1 = feature2.Shape; IGeometry other = shape.Intersect(feature2.Shape, esriGeometryDimension.esriGeometry2Dimension); if (!other.IsEmpty) { pGeometry = GISFunFactory.UnitFun.ConvertPoject(pGeometry, other.SpatialReference) as IPoint; IGeometryCollection geometrys = other as IGeometryCollection; if (geometrys.GeometryCount > 1) { for (int i = 0; i < geometrys.GeometryCount; i++) { IGeometry inGeometry = geometrys.get_Geometry(i); if (!inGeometry.IsEmpty) { if (inGeometry.GeometryType == esriGeometryType.esriGeometryRing) { object missing = System.Type.Missing; IGeometryCollection geometrys2 = new PolygonClass(); geometrys2.AddGeometry(inGeometry, ref missing, ref missing); IPolygon polygon = geometrys2 as IPolygon; inGeometry = polygon; } IRelationalOperator operator2 = inGeometry as IRelationalOperator; if ((operator2 != null) && operator2.Contains(pGeometry)) { other = inGeometry; break; } } } } else { IRelationalOperator operator3 = other as IRelationalOperator; if (!operator3.Contains(pGeometry)) { return; } } if (other != null) { feature = Editor.UniqueInstance.TargetLayer.FeatureClass.GetFeature(feature.OID); feature2 = Editor.UniqueInstance.TargetLayer.FeatureClass.GetFeature(feature2.OID); IList <IFeature> pList = new List <IFeature> { feature, feature2 }; OverlapSublot sublot = new OverlapSublot(this.m_hookHelper.Hook, pList) { Text = "合并", Tip = "请选择要保留重叠部分的小班ID" }; if (sublot.ShowDialog() == DialogResult.OK) { int selectedIndex = sublot.SelectedIndex; IFeature feature3 = null; if (selectedIndex == 0) { feature3 = feature2; } else { feature3 = feature; } if (other.GeometryType != esriGeometryType.esriGeometryPolygon) { IPolygon polygon2 = new PolygonClass { SpatialReference = other.SpatialReference }; IPointCollection newPoints = other as IPointCollection; (polygon2 as IPointCollection).AddPointCollection(newPoints); other = polygon2; } IGeometry geometry3 = (feature3.Shape as ITopologicalOperator2).Difference(other); if (geometry3.IsEmpty) { Editor.UniqueInstance.StartEditOperation(); feature3.Delete(); Editor.UniqueInstance.StopEditOperation(); } else { Editor.UniqueInstance.StartEditOperation(); feature3.Shape = geometry3; feature3.Store(); Editor.UniqueInstance.StopEditOperation(); IFeatureSelection selection = Editor.UniqueInstance.TargetLayer as IFeatureSelection; selection.Add(feature); selection.Add(feature2); } this.m_hookHelper.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection | esriViewDrawPhase.esriViewGeography, null, this.m_hookHelper.ActiveView.Extent); } sublot = null; MessageBox.Show("修改完成!", "提示"); } } } } } } catch (Exception exception) { Editor.UniqueInstance.AbortEditOperation(); this.mErrOpt.ErrorOperate(this.mSubSysName, "ShapeEdit.OverlapCombine", "OnMouseDown", exception.GetHashCode().ToString(), exception.Source, exception.Message, "", "", ""); MessageBox.Show("修改出错!", "提示"); } } }
public override void OnMouseUp(int Button, int Shift, int X, int Y) { IPoint point = this.m_hookHelper.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y); try { IFeatureLayer targetLayer = Editor.UniqueInstance.TargetLayer; IFeatureClass featureClass = targetLayer.FeatureClass; ISpatialFilter queryFilter = null; queryFilter = new SpatialFilterClass { Geometry = point, GeometryField = featureClass.ShapeFieldName, SpatialRel = esriSpatialRelEnum.esriSpatialRelWithin }; IFeature feature = null; IFeatureCursor cursor = targetLayer.Search(queryFilter, false); IFeature item = cursor.NextFeature(); int num = 0; IList <IFeature> pList = new List <IFeature>(); while (item != null) { if ((this.m_Feature1 != null) && (item.OID == this.m_Feature1.OID)) { item = cursor.NextFeature(); } else { feature = item; num++; pList.Add(item); item = cursor.NextFeature(); } } if (num < 1) { MessageBox.Show("当前无要素可进行裁切!", "提示"); } else { if (num > 1) { OverlapSublot sublot = new OverlapSublot(this.m_hookHelper.Hook, pList) { Text = "裁切", Tip = "超过一个要素被选中,请选择需要的小班ID" }; if (sublot.ShowDialog() == DialogResult.OK) { int selectedIndex = sublot.SelectedIndex; feature = pList[selectedIndex]; } else { MessageBox.Show("超过一个要素被选中,无法进行裁切!", "提示"); return; } } IFeatureSelection selection = Editor.UniqueInstance.TargetLayer as IFeatureSelection; if (this.m_Feature1 == null) { this.m_Feature1 = feature; selection.Add(this.m_Feature1); this.m_hookHelper.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, null, null); this._cursor = ToolCursor.Erase22; } else { this.m_Feature2 = feature; selection.Add(this.m_Feature2); this.m_hookHelper.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, null, null); IGeometry shapeCopy = this.m_Feature1.ShapeCopy; IGeometry other = this.m_Feature2.ShapeCopy; ITopologicalOperator2 @operator = shapeCopy as ITopologicalOperator2; @operator.IsKnownSimple_2 = false; @operator.Simplify(); IGeometry geometry3 = @operator.Intersect(other, esriGeometryDimension.esriGeometry2Dimension); if (geometry3.IsEmpty) { MessageBox.Show("选中两个要素无相交部分,无法进行裁切!", "提示"); this.m_Feature1 = null; this.m_Feature2 = null; this._cursor = ToolCursor.Erase2; } else { ITopologicalOperator2 operator2 = geometry3 as ITopologicalOperator2; operator2.IsKnownSimple_2 = false; operator2.Simplify(); IGeometry geometry4 = (other as ITopologicalOperator2).Difference(geometry3); if (geometry4.IsEmpty) { MessageBox.Show("选中两个要素完全重合,无法进行裁切!", "提示"); this.m_Feature1 = null; this.m_Feature2 = null; this._cursor = ToolCursor.Erase2; } else { Editor.UniqueInstance.CheckOverlap = false; Editor.UniqueInstance.StartEditOperation(); this.m_Feature2.Shape = geometry4; this.m_Feature2.Store(); Editor.UniqueInstance.StopEditOperation(); Editor.UniqueInstance.CheckOverlap = true; this.m_Feature1 = null; this.m_Feature2 = null; this._cursor = ToolCursor.Erase2; (Editor.UniqueInstance.TargetLayer as IFeatureSelection).Clear(); this.m_hookHelper.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection | esriViewDrawPhase.esriViewGeography, null, null); } } } } } catch (Exception exception) { Editor.UniqueInstance.AbortEditOperation(); this.m_Feature1 = null; this.m_Feature2 = null; this._cursor = ToolCursor.Erase2; this.mErrOpt.ErrorOperate(this.mSubSysName, "ShapeEdit.Erase2", "OnMouseUp", exception.GetHashCode().ToString(), exception.Source, exception.Message, "", "", ""); } }