public void AddZToSketch(IEditSketch3 sketch)
        {
            var    editor  = ArcUtils.Editor;
            var    editorZ = editor as IEditorZ;
            IPoint point   = sketch.LastPoint;

            if ((editorZ != null) && (point != null) && sketch.ZAware)
            {
                double z = GetHeight(point.X, point.Y);
                editorZ.UseZOffset = true;
                editorZ.ZOffset    = z;

                IGeometry         geometry = sketch.Geometry;
                var               ptColl   = geometry as IPointCollection4;
                ISketchOperation2 sketchOp = new SketchOperationClass();
                sketchOp.Start(editor);
                IPoint pointc = null;

                if ((!geometry.IsEmpty) && (geometry is IPoint) && (ptColl == null))
                {
                    pointc = geometry as IPoint;
                    ptColl = new MultipointClass();
                    ptColl.AddPoint(pointc);
                }

                if (ptColl != null)
                {
                    int nrPoints = ptColl.PointCount;

                    for (int i = 0; i < nrPoints; i++)
                    {
                        IPoint pointC = ptColl.Point[i];
                        // ReSharper disable CompareOfFloatsByEqualityOperator

                        if (pointC.Z == 0)
                        {
                            IPoint newPoint = new PointClass {
                                X = pointC.X, Y = pointC.Y, Z = z
                            };
                            ptColl.UpdatePoint(i, newPoint);
                        }

                        // ReSharper restore CompareOfFloatsByEqualityOperator
                    }
                }

                sketch.Geometry = pointc ?? (ptColl as IGeometry);
                geometry        = sketch.Geometry;

                if (geometry != null)
                {
                    sketchOp.Finish(geometry.Envelope, esriSketchOperationType.esriSketchOperationGeneral, geometry);
                }
            }
        }
Ejemplo n.º 2
0
        public void UpdatePoint(PointMeasurementData measurementData, int index)
        {
            _index = index;
            bool notCreated = NotCreated;
            MeasurementPointS measurementPoint = measurementData.measurementPoint;
            double            x = measurementPoint.x;
            double            y = measurementPoint.y;
            double            z = measurementPoint.z;

            _point = ArcUtils.GsToMapPoint(x, y, z);

            IActiveView            activeView = ArcUtils.ActiveView;
            var                    display    = activeView.ScreenDisplay;
            IDisplayTransformation dispTrans  = display.DisplayTransformation;
            double                 size       = dispTrans.FromPoints(PointSize);
            double                 xmin       = x - size;
            double                 xmax       = x + size;
            double                 ymin       = y - size;
            double                 ymax       = y + size;

            _oldEnvelope = _envelope;

            foreach (var observation in _observations)
            {
                double[] obs = observation.Value;

                if (obs.Length >= 2)
                {
                    double xdir = (_point.X - obs[0]) / 2;
                    double ydir = (_point.Y - obs[1]) / 2;
                    xmin = Math.Min(xmin, _point.X + xdir);
                    ymin = Math.Min(ymin, _point.Y + ydir);
                    xmax = Math.Max(xmax, obs[0]);
                    ymax = Math.Max(ymax, obs[1]);
                }
            }

            _envelope = new EnvelopeClass {
                XMin = xmin, XMax = xmax, YMin = ymin, YMax = ymax
            };
            var avEvents = ArcUtils.ActiveViewEvents;

            if (avEvents != null)
            {
                if (!notCreated)
                {
                    avEvents.AfterDraw -= AvEventsAfterDraw;
                }

                avEvents.AfterDraw += AvEventsAfterDraw;
            }

            IEditor3 editor = ArcUtils.Editor;
            var      sketch = editor as IEditSketch3;

            if ((sketch != null) && (_measurement != null))
            {
                IGeometry geometry = sketch.Geometry;
                int       nrPoints;
                var       ptColl = _measurement.ToPointCollection(geometry, out nrPoints);

                if ((ptColl != null) && _measurement.IsSketch)
                {
                    if (_intId <= nrPoints)
                    {
                        IPoint pointC = ptColl.Point[_intId - 1];

                        if (!IsSame(pointC))
                        {
                            ISketchOperation2 sketchOp = new SketchOperationClass();
                            sketchOp.Start(editor);
                            IPoint point = new PointClass {
                                X = _point.X, Y = _point.Y, Z = _point.Z, M = _index, ZAware = sketch.ZAware
                            };

                            if (_measurement.IsPointMeasurement)
                            {
                                sketch.Geometry = point;
                            }
                            else
                            {
                                ptColl.UpdatePoint((_intId - 1), point);

                                if ((_intId == 1) && ((nrPoints + 1) == ptColl.PointCount))
                                {
                                    ptColl.UpdatePoint((ptColl.PointCount - 1), point);
                                }

                                sketch.Geometry = ptColl as IGeometry;
                            }

                            geometry = sketch.Geometry;

                            if (geometry != null)
                            {
                                sketchOp.Finish(geometry.Envelope, esriSketchOperationType.esriSketchOperationGeneral, geometry);
                            }
                        }
                    }
                    else
                    {
                        ISketchOperation2 sketchOp = new SketchOperationClass();
                        sketchOp.Start(editor);
                        IPoint point = new PointClass {
                            X = _point.X, Y = _point.Y, Z = _point.Z, M = _index, ZAware = sketch.ZAware
                        };
                        int nrPoints2 = ptColl.PointCount;

                        switch (nrPoints2)
                        {
                        case 0:
                            ptColl.AddPoint(point);

                            if (geometry is IPolygon4)
                            {
                                ptColl.AddPoint(point);
                            }
                            break;

                        case 1:
                            ptColl.AddPoint(point);
                            break;

                        default:
                            if (_intId <= (nrPoints + 1))
                            {
                                object point1 = ((_intId - 1) == nrPoints2) ? Type.Missing : (_intId - 1);
                                object point2 = Type.Missing;
                                ptColl.AddPoint(point, ref point1, ref point2);
                            }

                            break;
                        }

                        sketch.Geometry = ptColl as IGeometry;
                        geometry        = sketch.Geometry;

                        if (geometry != null)
                        {
                            sketchOp.Finish(geometry.Envelope, esriSketchOperationType.esriSketchOperationGeneral, geometry);
                        }
                    }
                }
                else
                {
                    if (geometry is IPoint)
                    {
                        if (geometry.IsEmpty)
                        {
                            if ((!double.IsNaN(_point.X)) && (!double.IsNaN(_point.Y)))
                            {
                                if (!_added)
                                {
                                    IApplication application = ArcMap.Application;
                                    ICommandItem tool        = application.CurrentTool;
                                    ICommand     command     = tool.Command;

                                    if (!(command is IEditTool))
                                    {
                                        _added = true;
                                        var    editorZ = editor as IEditorZ;
                                        double zOffset = 0.0;

                                        if (editorZ != null)
                                        {
                                            zOffset         = editorZ.ZOffset;
                                            editorZ.ZOffset = _point.Z;
                                        }

                                        ISketchOperation2 sketchOp = new SketchOperationClass();
                                        sketchOp.Start(editor);
                                        IPoint point = new PointClass {
                                            X = _point.X, Y = _point.Y, Z = _point.Z, M = _index, ZAware = sketch.ZAware
                                        };
                                        sketch.Geometry = point;
                                        geometry        = sketch.Geometry;
                                        sketchOp.Finish(geometry.Envelope, esriSketchOperationType.esriSketchOperationGeneral, geometry);
                                        sketch.FinishSketch();

                                        if (editorZ != null)
                                        {
                                            editorZ.ZOffset = zOffset;
                                        }

                                        _added = false;
                                    }
                                }
                            }
                        }
                        else
                        {
                            var pointC = geometry as IPoint;

                            if (!IsSame(pointC))
                            {
                                if ((!double.IsNaN(_point.X)) && (!double.IsNaN(_point.Y)))
                                {
                                    ISketchOperation2 sketchOp = new SketchOperationClass();
                                    sketchOp.Start(editor);
                                    IPoint point = new PointClass {
                                        X = _point.X, Y = _point.Y, Z = _point.Z, M = _index, ZAware = sketch.ZAware
                                    };
                                    sketch.Geometry = point;
                                    geometry        = sketch.Geometry;
                                    sketchOp.Finish(geometry.Envelope, esriSketchOperationType.esriSketchOperationGeneral, geometry);
                                }
                            }
                        }
                    }
                }
            }

            Update();
        }
        private static void OnCurrentTaskChanged()
        {
            try
            {
                IEditor3 editor = ArcUtils.Editor;
                LogClient.Info("On CurrentTask Changed");
                _doSelection = true;

                if (editor != null)
                {
                    var sketch     = editor as IEditSketch3;
                    var editLayers = editor as IEditLayers;

                    if ((sketch != null) && (editLayers != null))
                    {
                        IEditTask   task         = editor.CurrentTask;
                        ILayer      currentLayer = editLayers.CurrentLayer;
                        VectorLayer vectorLayer  = (EditFeatures.Count != 1)
              ? ((currentLayer == null) ? null : GetLayer(currentLayer))
              : GetLayer(EditFeatures[0]);

                        if ((task != null) && ((vectorLayer != null) && (vectorLayer.IsVisibleInGlobespotter)))
                        {
                            var taskName = task as IEditTaskName;

                            if (taskName != null)
                            {
                                IGeometry geometry = sketch.Geometry;
                                string    name     = taskName.UniqueName;

                                if (name == "GarciaUI_ModifyFeatureTask")
                                {
                                    Measurement measurement = Measurement.Get(geometry, false);

                                    if (measurement != null)
                                    {
                                        int nrPoints;
                                        var ptColl = measurement.ToPointCollection(geometry, out nrPoints);

                                        if (ptColl != null)
                                        {
                                            ISketchOperation2 sketchOp = new SketchOperationClass();
                                            sketchOp.Start(editor);

                                            for (int j = 0; j < nrPoints; j++)
                                            {
                                                IPoint           point  = ptColl.Point[j];
                                                MeasurementPoint mpoint = measurement.IsPointMeasurement
                          ? measurement.GetPoint(point, false)
                          : measurement.GetPoint(point);

                                                double m      = (mpoint == null) ? double.NaN : mpoint.M;
                                                double z      = (mpoint == null) ? double.NaN : mpoint.Z;
                                                IPoint point2 = new PointClass {
                                                    X = point.X, Y = point.Y, Z = z, M = m, ZAware = sketch.ZAware
                                                };
                                                ptColl.UpdatePoint(j, point2);

                                                if (measurement.IsPointMeasurement)
                                                {
                                                    sketch.Geometry = point2;
                                                }
                                            }

                                            if (!measurement.IsPointMeasurement)
                                            {
                                                sketch.Geometry = ptColl as IGeometry;
                                            }

                                            geometry = sketch.Geometry;

                                            if (geometry != null)
                                            {
                                                sketchOp.Finish(geometry.Envelope, esriSketchOperationType.esriSketchOperationGeneral, geometry);
                                            }
                                        }

                                        measurement.SetSketch();
                                        measurement.OpenMeasurement();
                                        measurement.DisableMeasurementSeries();
                                    }
                                }
                                else
                                {
                                    Measurement measurement = Measurement.Get(geometry, false);

                                    if (measurement != null)
                                    {
                                        measurement.EnableMeasurementSeries();
                                    }

                                    OnSelectionChanged();
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogClient.Error("VectorLayer.OnCurrentTaskChanged", ex.Message, ex);
                Trace.WriteLine(ex.Message, "VectorLayer.OnCurrentTaskChanged");
            }
        }
        protected override void OnClick()
        {
            IEditor  pEd  = (IEditor)ArcMap.Editor;
            IEditor2 pEd2 = (IEditor2)ArcMap.Editor;

            IEditProperties  pEdProps1 = pEd as IEditProperties;
            IEditProperties2 pEdProps2 = pEd as IEditProperties2;

            IEditSketch2       pSketch2 = ArcMap.Editor as IEditSketch2;
            ISegmentCollection pSegColl = pSketch2.Geometry as ISegmentCollection;

            double dLineDirection = 0;

            ISketchTool sketchTool = ArcMap.Application.CurrentTool.Command as ISketchTool;

            if (sketchTool.Constraint == esriSketchConstraint.esriConstraintAngle)
            {
                dLineDirection = sketchTool.AngleConstraint;
            }
            else
            {
                ILine pRubberBandLine = new LineClass();
                pRubberBandLine.PutCoords(pSketch2.LastPoint, pEd2.Location);
                dLineDirection = pRubberBandLine.Angle;
            }

            IAngularConverter pAngConv = new AngularConverterClass();

            pAngConv.SetAngle(dLineDirection, esriDirectionType.esriDTPolar, esriDirectionUnits.esriDURadians);

            int iSegCnt = pSegColl.SegmentCount;
            dlgSpiralParameters SpiralEntryDialog = new dlgSpiralParameters();

            string sBearing = pAngConv.GetString(pEdProps2.DirectionType, pEdProps2.DirectionUnits, pEdProps2.AngularUnitPrecision);

            SpiralEntryDialog.txtDirection.Text = sBearing;
            //Display the dialog
            DialogResult pDialogResult = SpiralEntryDialog.ShowDialog();

            esriCurveDensifyMethod DensifyMethod = esriCurveDensifyMethod.esriCurveDensifyByAngle; //default
            double dDensifyParameter             = 2 * Math.PI / 180;                              //2 degrees //default

            if (SpiralEntryDialog.optCustomDensification.Checked)
            {
                DensifyMethod = (esriCurveDensifyMethod)SpiralEntryDialog.cboDensificationType.SelectedIndex;
                if (DensifyMethod == esriCurveDensifyMethod.esriCurveDensifyByAngle)
                {
                    dDensifyParameter = Convert.ToDouble(SpiralEntryDialog.numAngleDensification.Value) * Math.PI / 180;
                }
                else
                {
                    if (!Double.TryParse(SpiralEntryDialog.txtDensifyValue.Text, out dDensifyParameter))
                    {
                        dDensifyParameter = 2;
                    }
                }
            }

            if (pDialogResult != DialogResult.OK)
            {
                return;
            }

            if (SpiralEntryDialog.txtStartRadius.Text.ToLower().Trim() == "infinity" && SpiralEntryDialog.txtEndRadius.Text.ToLower().Trim() == "infinity")
            {
                return;
            }


            double dSpiralRadius1 = Double.MaxValue; //default to infinity
            double dFromCurvature = 0;

            if (SpiralEntryDialog.txtStartRadius.Text.ToLower() != "infinity")
            {
                if (Double.TryParse(SpiralEntryDialog.txtStartRadius.Text, out dSpiralRadius1))
                {
                    dFromCurvature = 1 / dSpiralRadius1;
                }
                else
                {
                    return;
                }
            }

            double dSpiralRadius2 = Double.MaxValue; //default to infinity
            double dToCurvature   = 0;

            if (SpiralEntryDialog.txtEndRadius.Text.ToLower() != "infinity")
            {
                if (Double.TryParse(SpiralEntryDialog.txtEndRadius.Text, out dSpiralRadius2))
                {
                    dToCurvature = 1 / dSpiralRadius2;
                }
                else
                {
                    return;
                }
            }

            bool bIsCCW = (dSpiralRadius1 > dSpiralRadius2) ? SpiralEntryDialog.optRight.Checked : SpiralEntryDialog.optLeft.Checked;

            bool bSpecialCaseCircularArc = (dSpiralRadius1 == dSpiralRadius2);

            if (!pAngConv.SetString(SpiralEntryDialog.txtDirection.Text, pEdProps2.DirectionType, pEdProps2.DirectionUnits))
            {
                return;
            }

            double dNorthAzimuthRadians = pAngConv.GetAngle(esriDirectionType.esriDTNorthAzimuth, esriDirectionUnits.esriDURadians);

            IVector3D pVec = new Vector3DClass();

            pVec.PolarSet(dNorthAzimuthRadians, 0, 500);

            IPoint pTangentPoint = new PointClass();

            pTangentPoint.PutCoords(pSketch2.LastPoint.X + pVec.XComponent, pSketch2.LastPoint.Y + pVec.YComponent);

            //double dStreamingTol = pEdProps1.StreamTolerance;
            //if (dStreamingTol == 0)
            //  dStreamingTol = 0.001 * 5000; //metric

            //double dSpiralOffsetGeometryPrecision = 0.001 * 250; //metric 0.25 m

            IPolyline6 theSpiralPolyLine = null;
            double     dExitTangent      = 0;

            if (SpiralEntryDialog.cboPathLengthParameter.SelectedIndex == 0)
            {
                double dSpiralArcLength;
                if (!Double.TryParse(SpiralEntryDialog.txtPathLengthParameter.Text, out dSpiralArcLength))
                {
                    return;
                }

                if (bSpecialCaseCircularArc)
                {
                    ILine pInTangentLine = new LineClass();
                    pInTangentLine.PutCoords(pSketch2.LastPoint, pTangentPoint);

                    ISegment pTangentSegment = (ISegment)pInTangentLine;
                    IConstructCircularArc2 pCircArcConstr = new ESRI.ArcGIS.Geometry.CircularArcClass() as IConstructCircularArc2;
                    pCircArcConstr.ConstructTangentRadiusArc(pTangentSegment, false, bIsCCW, dSpiralRadius1, dSpiralArcLength);
                    ICircularArc pArcSegment = pCircArcConstr as ICircularArc;

                    //Get chord Line from tangent curve constructor
                    ILine pChordLine = new LineClass();
                    pChordLine.PutCoords(pArcSegment.FromPoint, pArcSegment.ToPoint);
                    double dPolarRadians = pChordLine.Angle; //to get the chord azimuth

                    pCircArcConstr.ConstructBearingRadiusArc(pSketch2.LastPoint, dPolarRadians, bIsCCW, dSpiralRadius1, dSpiralArcLength);

                    dExitTangent = pArcSegment.ToAngle + Math.PI / 2;

                    ISegmentCollection segCollection = new PolylineClass() as ISegmentCollection;
                    object             obj           = Type.Missing;
                    segCollection.AddSegment((ISegment)pArcSegment, ref obj, ref obj);
                    theSpiralPolyLine = segCollection as IPolyline6;
                }
                else
                {
                    theSpiralPolyLine = ConstructSpiralbyLength(pSketch2.LastPoint, pTangentPoint, dFromCurvature, dToCurvature, bIsCCW, dSpiralArcLength,
                                                                DensifyMethod, dDensifyParameter, out dExitTangent);
                }
            }

            if (SpiralEntryDialog.cboPathLengthParameter.SelectedIndex == 1)
            {
                if (!pAngConv.SetString(SpiralEntryDialog.txtPathLengthParameter.Text, esriDirectionType.esriDTPolar, pEdProps2.DirectionUnits))
                {
                    return;
                }
                double dSpiralDeltaAngle = pAngConv.GetAngle(esriDirectionType.esriDTPolar, esriDirectionUnits.esriDURadians);

                if (bSpecialCaseCircularArc)
                {
                    ILine pInTangentLine = new LineClass();
                    pInTangentLine.PutCoords(pSketch2.LastPoint, pTangentPoint);

                    ISegment pTangentSegment = (ISegment)pInTangentLine;
                    IConstructCircularArc2 pCircArcConstr = new ESRI.ArcGIS.Geometry.CircularArcClass() as IConstructCircularArc2;
                    pCircArcConstr.ConstructTangentRadiusAngle(pTangentSegment, false, bIsCCW, dSpiralRadius1, dSpiralDeltaAngle);
                    ICircularArc pArcSegment = pCircArcConstr as ICircularArc;

                    //Get chord Line from tangent curve constructor
                    ILine pChordLine = new LineClass();
                    pChordLine.PutCoords(pArcSegment.FromPoint, pArcSegment.ToPoint);
                    double dPolarRadians = pChordLine.Angle; //to get the chord azimuth

                    pCircArcConstr.ConstructBearingRadiusAngle(pSketch2.LastPoint, dPolarRadians, bIsCCW, dSpiralRadius1, dSpiralDeltaAngle);

                    dExitTangent = pArcSegment.ToAngle + Math.PI / 2;

                    ISegmentCollection segCollection = new PolylineClass() as ISegmentCollection;
                    object             obj           = Type.Missing;
                    segCollection.AddSegment((ISegment)pArcSegment, ref obj, ref obj);
                    theSpiralPolyLine = segCollection as IPolyline6;
                }
                else
                {
                    theSpiralPolyLine = ConstructSpiralbyDeltaAngle(pSketch2.LastPoint, pTangentPoint, dFromCurvature, dToCurvature, bIsCCW, dSpiralDeltaAngle,
                                                                    DensifyMethod, dDensifyParameter, out dExitTangent);
                }
            }

            if (theSpiralPolyLine == null)
            {
                MessageBox.Show("A spiral could not be created with the entered parameters.");
                return;
            }

            ISegmentCollection pSpiralSegCollection = theSpiralPolyLine as ISegmentCollection;
            //Start a sketch operation and insert the new envelope into the sketch
            ISketchOperation2 sketchOp = new SketchOperationClass();

            sketchOp.Start(ArcMap.Editor);
            sketchOp.MenuString = "Add Spiral";
            pSegColl.AddSegmentCollection(pSpiralSegCollection);
            IGeometry geom = pSegColl as IGeometry;

            pSketch2.Geometry = geom;

            //set the angle constraint to the exit tangent of the spiral
            sketchTool.Constraint      = esriSketchConstraint.esriConstraintAngle;
            sketchTool.AngleConstraint = dExitTangent;

            sketchOp.Finish(ArcMap.Document.ActiveView.Extent, esriSketchOperationType.esriSketchOperationGeneral, null);
        }