public void OnFinishSketch() { //get reference to featurelayer being edited IFeatureLayer featureLayer = m_editLayer.TargetLayer as IFeatureLayer; //get reference to the sketch geometry IGeometry reshapeGeom = m_editSketch.Geometry; if (reshapeGeom.IsEmpty == false) { //get the currently selected feature IFeatureSelection featureSelection = featureLayer as IFeatureSelection; ISelectionSet selectionSet = featureSelection.SelectionSet; ICursor cursor; selectionSet.Search(null, false, out cursor); IFeatureCursor featureCursor = cursor as IFeatureCursor; //the PerformSketchToolEnabledChecks property has already checked that only 1 feature is selected IFeature feature = featureCursor.NextFeature(); //Take a copy of geometry for the selected feature IGeometry editShape = feature.ShapeCopy; //create a path from the editsketch geometry IPointCollection reshapePath = new PathClass(); reshapePath.AddPointCollection(reshapeGeom as IPointCollection); //reshape the selected feature IPolyline polyline = editShape as IPolyline; polyline.Reshape(reshapePath as IPath); #region Perform an edit operation to store the new geometry for selected feature try { m_engineEditor.StartOperation(); feature.Shape = editShape; feature.Store(); m_engineEditor.StopOperation("Reshape Feature"); } catch (Exception ex) { m_engineEditor.AbortOperation(); System.Diagnostics.Trace.WriteLine(ex.Message, "Reshape Geometry Failed"); } #endregion } //refresh the display IActiveView activeView = m_engineEditor.Map as IActiveView; activeView.PartialRefresh(esriViewDrawPhase.esriViewGeography, (object)featureLayer, activeView.Extent); }
private void CutSelectedPolygon() { bool isSuccess = false; if (m_selectedFeature == null) { MessageBox.Show("请先选择要分割的面要素!!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information); m_toolPhase = ToolPhase.SelectFeature; return; } //在屏幕上绘制用于分割的线要素 IScreenDisplay screenDisplay = m_activeView.ScreenDisplay; ISimpleLineSymbol sym = new SimpleLineSymbolClass(); IRgbColor color = new RgbColorClass(); color.Red = 255; color.Green = 128; color.Blue = 128; sym.Color = color; sym.Style = esriSimpleLineStyle.esriSLSSolid; sym.Width = 2; IRubberBand cutBand = new RubberLineClass(); IGeometry reshaprGeometry = cutBand.TrackNew(screenDisplay, sym as ISymbol); screenDisplay.StartDrawing(screenDisplay.hDC, (short)esriScreenCache.esriNoScreenCache); screenDisplay.SetSymbol(sym as ISymbol); screenDisplay.DrawPolyline(reshaprGeometry); screenDisplay.FinishDrawing(); IFeatureClass featureClass = m_selectedFeature.Class as IFeatureClass; IDataset dataset = featureClass as IDataset; IWorkspaceEdit workspaceEdit = dataset.Workspace as IWorkspaceEdit; if (!(workspaceEdit.IsBeingEdited())) { return; } //分割选择的面要素 if (reshaprGeometry.IsEmpty == true) { return; } try { IPolyline reshapePolyline, sourcePolyline = null; IPolygon4 polygon = null; IRing ring = null, innerRing = null, outRing = null; IPath reshapePath = null; IGeometryCollection pathCollection = null; IGeometry geometry = m_selectedFeature.Shape; switch (geometry.GeometryType) { case esriGeometryType.esriGeometryPolygon: bool outerSuccess = false, innerSuccess = false; IGeometryBag innerRingGeometryBag = null; IGeometryCollection innerRingGeometryCollection = null; reshapePolyline = reshaprGeometry as IPolyline; pathCollection = reshapePolyline as IGeometryCollection; //只可能产生一条polyline,直接写死 reshapePath = pathCollection.Geometry[0] as IPath; polygon = geometry as IPolygon4; IGeometryBag exteriorRingGeometryBag = polygon.ExteriorRingBag; IGeometryCollection exteriorRingGeometryCollection = exteriorRingGeometryBag as IGeometryCollection; for (int i = 0; i < exteriorRingGeometryCollection.GeometryCount; i++) { outRing = exteriorRingGeometryCollection.Geometry[i] as IRing; bool a = outRing.Reshape(reshapePath); outerSuccess = outerSuccess || a; innerRingGeometryBag = polygon.get_InteriorRingBag(outRing); innerRingGeometryCollection = innerRingGeometryBag as IGeometryCollection; for (int j = 0; j < innerRingGeometryCollection.GeometryCount; j++) { innerRing = innerRingGeometryCollection.Geometry[j] as IRing; bool b = innerRing.Reshape(reshapePath); innerSuccess = innerSuccess || b; } } isSuccess = innerSuccess || outerSuccess; break; case esriGeometryType.esriGeometryPolyline: sourcePolyline = geometry as IPolyline; reshapePolyline = reshaprGeometry as IPolyline; pathCollection = reshapePolyline as IGeometryCollection; //只可能产生一条polyline,直接写死 reshapePath = pathCollection.Geometry[0] as IPath; isSuccess = sourcePolyline.Reshape(reshapePath); break; } if (isSuccess) { workspaceEdit.StartEditOperation(); m_selectedFeature.Shape = geometry;//如果没加这句gdb无法编辑 m_selectedFeature.Store(); m_activeView.Refresh(); workspaceEdit.StopEditOperation(); } else { throw new Exception("重塑要素失败!"); } m_activeView.Refresh(); } catch (Exception ex) { workspaceEdit.AbortEditOperation(); MessageBox.Show("分割面要素失败!!" + ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } m_activeView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, m_activeView.Extent); m_toolPhase = ToolPhase.SelectFeature; }