コード例 #1
0
        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);
        }
        public void OnFinishSketch()
        {
            if (m_editSketch == null)
            {
                return;
            }

            bool hasCutPolygons = false;

            //Change the cursor to be hourglass shape.
            System.Windows.Forms.Cursor.Current = Cursors.WaitCursor;

            try
            {
                //Get the geometry that performs the cut from the edit sketch.
                IGeometry cutGeometry = m_editSketch.Geometry;

                //The sketch geometry is simplified to deal with a multi-part sketch as well
                //as the case where the sketch loops back over itself.
                ITopologicalOperator2 topoOperator = cutGeometry as ITopologicalOperator2;
                topoOperator.IsKnownSimple_2 = false;
                topoOperator.Simplify();

                //Create the spatial filter to search for features in the target feature class.
                //The spatial relationship we care about is whether the interior of the line
                //intersects the interior of the polygon.
                ISpatialFilter spatialFilter = new SpatialFilterClass();
                spatialFilter.Geometry   = m_editSketch.Geometry;
                spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;

                //Find the polygon features that cross the sketch.
                IFeatureClass  featureClass  = m_editLayer.TargetLayer.FeatureClass;
                IFeatureCursor featureCursor = featureClass.Search(spatialFilter, false);

                //Only do work if there are features that intersect the edit sketch.
                IFeature origFeature = featureCursor.NextFeature();
                if (origFeature != null)
                {
                    //Check the first feature to see if it is ZAware and if it needs to make the
                    //cut geometry ZAware.
                    IZAware zAware = origFeature.Shape as IZAware;
                    if (zAware.ZAware)
                    {
                        zAware        = cutGeometry as IZAware;
                        zAware.ZAware = true;
                    }

                    ArrayList comErrors = new ArrayList();

                    //Start an edit operation so we can have undo/redo.
                    m_engineEditor.StartOperation();

                    //Cycle through the features, cutting with the sketch.
                    while (origFeature != null)
                    {
                        try
                        {
                            //Split the feature. Use the IFeatureEdit::Split method which ensures
                            //the attributes are correctly dealt with.
                            IFeatureEdit featureEdit = origFeature as IFeatureEdit;
                            //Set to hold the new features that are created by the Split.
                            ISet newFeaturesSet = featureEdit.Split(cutGeometry);

                            //New features have been created.
                            if (newFeaturesSet != null)
                            {
                                newFeaturesSet.Reset();
                                hasCutPolygons = true;
                            }
                        }
                        catch (COMException comExc)
                        {
                            comErrors.Add(String.Format("OID: {0}, Error: {1} , {2}", origFeature.OID.ToString(), comExc.ErrorCode, comExc.Message));
                        }
                        finally
                        {
                            //Continue to work on the next feature if it fails to split the current one.
                            origFeature = featureCursor.NextFeature();
                        }
                    }
                    //If any polygons were cut, refresh the display and stop the edit operation.
                    if (hasCutPolygons)
                    {
                        //Clear the map's selection.
                        m_engineEditor.Map.ClearSelection();

                        //Refresh the display including modified layer and any previously selected component.
                        IActiveView activeView = m_engineEditor.Map as IActiveView;
                        activeView.PartialRefresh(esriViewDrawPhase.esriViewGeography | esriViewDrawPhase.esriViewGeoSelection, null, activeView.Extent);

                        //Complete the edit operation.
                        m_engineEditor.StopOperation("Cut Polygons Without Selection");
                    }
                    else
                    {
                        m_engineEditor.AbortOperation();
                    }

                    //report any errors that have arisen while splitting features
                    if (comErrors.Count > 0)
                    {
                        StringBuilder stringBuilder = new StringBuilder("The following features could not be split: \n", 200);
                        foreach (string comError in comErrors)
                        {
                            stringBuilder.AppendLine(comError);
                        }

                        MessageBox.Show(stringBuilder.ToString(), "Cut Errors");
                    }
                }
            }
            catch (Exception e)
            {
                MessageBox.Show("Unable to perform the cut task.\n" + e.Message);
                m_engineEditor.AbortOperation();
            }
            finally
            {
                //Change the cursor shape to default.
                System.Windows.Forms.Cursor.Current = Cursors.Default;
            }
        }
コード例 #3
0
        public override void OnMouseUp(int Button, int Shift, int X, int Y)
        {
            if (Button != 1)
            {
                return;
            }
            //if (m_engineEditor.SelectionCount < 1) return;
            if (m_rotateTracker == null)
            {
                return;
            }

            IEnumFeature pEnumFeature;
            IFeature     pFeature;
            ITransform2D pTrans2D;
            bool         bChanged = m_rotateTracker.OnMouseUp();

            if (bChanged == false)
            {
                return;
            }

            try
            {
                if (this.DoubleCheck(m_rotateTracker) == false)
                {
                    return;
                }
                IEnvelope pSelEnvelope = null;
                pEnumFeature = m_hookHelper.ActiveView.Selection as IEnumFeature;
                pEnumFeature.Reset();
                pFeature = pEnumFeature.Next();
                m_engineEditor.StartOperation();
                while (pFeature != null)
                {
                    if (pSelEnvelope == null)
                    {
                        pSelEnvelope = pFeature.Extent;
                    }
                    else if (pFeature.Extent.IsEmpty == false)
                    {
                        pSelEnvelope.Union(pFeature.Extent);
                    }
                    //如果选中的是地物
                    if (pFeature.FeatureType == esriFeatureType.esriFTSimple || pFeature.FeatureType == esriFeatureType.esriFTDimension ||
                        pFeature.FeatureType == esriFeatureType.esriFTSimpleEdge || pFeature.FeatureType == esriFeatureType.esriFTSimpleJunction)
                    {
                        pTrans2D = pFeature.ShapeCopy as ITransform2D;
                        //旋转要素
                        pTrans2D.Rotate(m_rotateTracker.Origin, m_rotateTracker.Angle);
                        pFeature.Shape = pTrans2D as IGeometry;
                        pFeature.Store();
                    }
                    if (pFeature.FeatureType == esriFeatureType.esriFTAnnotation)
                    {
                        double angle    = m_rotateTracker.Angle / Math.PI * 180;
                        int    i        = pFeature.Fields.FindField("Angle");
                        double oldAngle = (double)pFeature.get_Value(i);
                        double newAngle = oldAngle + angle;
                        if (newAngle > 360)
                        {
                            newAngle = angle - 360;
                        }
                        if (newAngle < -360)
                        {
                            newAngle = angle + 360;
                        }
                        pFeature.set_Value(i, newAngle);
                        pFeature.Store();
                    }
                    pFeature = pEnumFeature.Next();
                }
                m_engineEditor.StopOperation("Rotate Features");
                m_mapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, null, null);
                m_mapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, pSelEnvelope);
            }
            catch (Exception ex)
            {
                m_engineEditor.AbortOperation();
            }
            finally
            {
                pEnumFeature = null;
                pFeature     = null;
                pTrans2D     = null;
            }
        }
コード例 #4
0
ファイル: ToolSplitLine.cs プロジェクト: eglrp/TESTPROJECT-1
        public override void OnMouseDown(int Button, int Shift, int X, int Y)
        {
            try
            {
                pt = ((m_hookHelper.ActiveView.ScreenDisplay).DisplayTransformation).ToMapPoint(X, Y);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            bool ddd = snapEnvironment.SnapPoint(pt);

            if (pMPfeedback != null)
            {
                pMPfeedback.Stop();
            }
            pMPfeedback = null;

            bool hasCut         = false;
            ISet newFeaturesSet = null;

            try
            {
                IFeatureSelection featureSelection = m_editLayer.TargetLayer as IFeatureSelection;
                ISelectionSet     selectionSet     = featureSelection.SelectionSet;
                IEnumIDs          enumIDs          = selectionSet.IDs;
                int iD = enumIDs.Next();
                while (iD != -1) //-1 is reutned after the last valid ID has been reached
                {
                    m_engineEditor.StartOperation();
                    IFeatureClass featureClass = m_editLayer.TargetLayer.FeatureClass;
                    IFeature      feature      = featureClass.GetFeature(iD);
                    // 判断点是否在线上,不是则跳过
                    ITopologicalOperator pto       = feature.Shape as ITopologicalOperator;
                    IGeometry            pgeometry = pto.Intersect(pt, esriGeometryDimension.esriGeometry0Dimension);
                    if (pgeometry.IsEmpty == true)
                    {
                        iD = enumIDs.Next();
                        continue;
                    }
                    IFeatureEdit featureedit = feature as IFeatureEdit;
                    /*ISet*/
                    newFeaturesSet = featureedit.Split(pgeometry);
                    if (newFeaturesSet != null)
                    {
                        newFeaturesSet.Reset();
                        hasCut = true;
                        break;
                    }
                    iD = enumIDs.Next();
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            if (hasCut)
            {
                //如果操作成功,选中切割的两条线
                //IFeatureSelection featureSelection = m_editLayer.TargetLayer as IFeatureSelection;
                //ISelectionSet selectionSet = featureSelection.SelectionSet;
                //for (int i = 0; i < newFeaturesSet.Count; i++)
                //{
                //    selectionSet.Add(((IFeature)newFeaturesSet.Next()).OID);
                //}
                //selectionSet.Refresh();

                //Refresh the display including modified layer and any previously selected component.
                IActiveView activeView = m_engineEditor.Map as IActiveView;
                activeView.PartialRefresh(esriViewDrawPhase.esriViewGeography | esriViewDrawPhase.esriViewGeoSelection, null, activeView.Extent);
                activeView.Refresh();
                //Complete the edit operation.
                m_engineEditor.StopOperation("Split a line");
                // 将当前工具设置为选择工具
                IToolbarControl toolbarctl = m_hookHelper.Hook as IToolbarControl;
                //ICommand com = new ControlsEditingEditToolClass();
                //com.OnCreate(m_mapControl.Object);
                //this.m_mapControl.CurrentTool = com as ITool;
                for (int i = 0; i < toolbarctl.Count; i++)
                {
                    IToolbarItem tbi = toolbarctl.GetItem(i);
                    if (tbi.Command != null && tbi.Command.Name.Equals("ControlToolsEditing_Edit"))
                    {
                        tbi.Command.OnClick();// = true;
                        toolbarctl.CurrentTool = tbi.Command as ITool;
                        IToolbarBuddy toolbarbuddy = (IToolbarBuddy)((IToolbarControl)m_hookHelper.Hook).Buddy;
                        break;
                    }
                }
                //  操作成功后将当前工具置为以前的工具
                //IToolbarBuddy toolbarbuddy = (IToolbarBuddy)((IToolbarControl)m_hookHelper.Hook).Buddy;
                //toolbarbuddy.CurrentTool = oldtool;
                //((IToolbarControl)m_hookHelper.Hook).SetBuddyControl(toolbarbuddy);
                this.Deactivate();
            }
            else
            {
                m_engineEditor.AbortOperation();
                MessageBox.Show("切割点不在线上,未能成功切割选择的线段");
                //重新开始选点
                this.OnClick();
            }
            base.OnMouseDown(Button, Shift, X, Y);
        }