private void ScaleRotate(Point srcPoint) { if (!PDE_Tools.IsExpanded) { return; } ParcelData parcelData = ParcelGridContainer.DataContext as ParcelData; _moveScale = _oldScale = parcelData.ScaleValue; _moveRotation = _oldRotation = parcelData.RotationValue; _srPoint = ParcelMap.ScreenToMap(srcPoint); _srSnapPoint = null; double spX = _srPoint.X; double spY = _srPoint.Y; // Find _startPoint in list of points. If "close" snap to that point. // Otherwise user can free form scale or rotate parcel lines. double shortestDistance = double.MaxValue; Int32 shortestId = -1; ESRI.ArcGIS.Client.Geometry.MapPoint foundPoint = null; foreach (KeyValuePair <Int32, ESRI.ArcGIS.Client.Geometry.MapPoint> kvp in _calculatedPoints) { double x = kvp.Value.X; double y = kvp.Value.Y; double distance = GeometryUtil.LineLength(spX, spY, x, y); if ((distance < shortestDistance) && (distance < _xmlConfiguation.SnapTolerance)) { shortestDistance = distance; shortestId = kvp.Key; foundPoint = new ESRI.ArcGIS.Client.Geometry.MapPoint(x, y); } } if (BearingDistanceToPoint(shortestId, out _srBearingToPoint, out _srDistanceToPoint, out _srSnapPoint)) { // BearingDistanceToPoint will fail if shortestId == -1 _srSnapPointId = shortestId; if (RotationButton.IsChecked == true) { double radialSearch = _srDistanceToPoint * parcelData.ScaleValue; // We seem to be getting some numerical precision error when rotating... this does not // really matter here; we only need to re-buffer if the changes are > 0.1. // if the user re-rotate with the same rotate point, try to avoid re-caching. if ((_originPoint == null) || (_lastGeometryCP == null) || (Math.Abs(_lastSearchDistance - radialSearch) > 0.1) || !_lastBufferBasedOnCurve || (_lastGeometryCP.X != _originPoint.X) || (_lastGeometryCP.Y != _originPoint.Y)) { ESRI.ArcGIS.Client.Geometry.MapPoint offsetOriginPoint = new ESRI.ArcGIS.Client.Geometry.MapPoint(_originPoint.X - radialSearch, _originPoint.Y); // Create a geometry circle from the anchor/rotating point to the snap point. // We will create create a cache of all these points within the buffer distance // of this circle. ESRI.ArcGIS.Client.Geometry.MapPoint endPoint; ESRI.ArcGIS.Client.Geometry.Polyline circle = GeometryUtil.ConstructArcSegment(offsetOriginPoint, 0.0, 0.001, radialSearch, false, SweepDirection.Counterclockwise, out endPoint); _lastGeometryCP = _originPoint; _lastSearchDistance = radialSearch; _lastBufferBasedOnCurve = true; CacheSnapObjects(circle, radialSearch); } } else if (ScaleButton.IsChecked == true) { double mapDistanceBuffer = _srDistanceToPoint * 1.5 * parcelData.ScaleValue; // if the user re-scales with the same scale point, try to avoid re-caching. if ((_originPoint == null) || (_lastGeometryCP == null) || (_lastSearchDistance < mapDistanceBuffer) || _lastBufferBasedOnCurve || (_lastGeometryCP.X != _originPoint.X) || (_lastGeometryCP.Y != _originPoint.Y)) { // Create a line from the anchor/rotating point to the snap point * 1.5 of the distance. // We will create create a cache of all these points within the buffer distance // of this line. ESRI.ArcGIS.Client.Geometry.MapPoint endPoint; ESRI.ArcGIS.Client.Geometry.Polyline snapLine = GeometryUtil.Line(_originPoint, _srBearingToPoint - parcelData.RotationValue, mapDistanceBuffer, out endPoint); if (snapLine != null) { _lastGeometryCP = _originPoint; _lastSearchDistance = mapDistanceBuffer; _lastBufferBasedOnCurve = false; CacheSnapObjects(snapLine, mapDistanceBuffer); } } } // else no snapping. CalculateAndAddLineGraphics(); // Redraw so we have snap graphic shown } else // BearingDistanceToPoint returns false if id = -1 { _srSnapPointId = -1; } }
private void ParcelMap_MouseMove(object sender, MouseEventArgs e) { if (ParcelLineInfoWindow.IsOpen == true && ParcelLineInfoWindow.Visibility == System.Windows.Visibility.Visible) { const double hideDistance = 25; double width = ParcelLineInfoWindow.ActualWidth; double height = ParcelLineInfoWindow.ActualHeight; var anchorScreenPoint = ParcelMap.MapToScreen(ParcelLineInfoWindow.Anchor); double x1 = anchorScreenPoint.X - width / 2 - hideDistance; double y1 = anchorScreenPoint.Y - height - hideDistance - 10; // -ve for info indicator double x2 = anchorScreenPoint.X + width / 2 + hideDistance; double y2 = anchorScreenPoint.Y + hideDistance; var envelope = new ESRI.ArcGIS.Client.Geometry.Envelope(x1, y1, x2, y2); Point pointLoc = e.GetPosition(this); if (!envelope.Intersects(new ESRI.ArcGIS.Client.Geometry.Envelope(pointLoc.X, pointLoc.Y, pointLoc.X, pointLoc.Y))) { ParcelLineInfoWindow.IsOpen = false; ParcelMap.Focus(); // Cause any non-committed cell in the popup window to lose its focus. This will commit the cell. } } if ((_srPoint == null) || !PDE_Tools.IsExpanded) { return; } ParcelData parcelData = ParcelGridContainer.DataContext as ParcelData; ESRI.ArcGIS.Client.Geometry.MapPoint currentPoint = ParcelMap.ScreenToMap(e.GetPosition(this)); if (RotationButton.IsChecked == true) { double rotation = GeometryUtil.Angle(_srPoint, currentPoint, _originPoint) + _oldRotation; while (rotation < -Math.PI) { rotation += Math.PI * 2; } while (rotation > Math.PI) { rotation -= Math.PI * 2; } parcelData.RotationValue = rotation; } else if (ScaleButton.IsChecked == true) { parcelData.ScaleValue = GeometryUtil.Scale(_srPoint, currentPoint, _originPoint) * _oldScale; } // If we have a snap point, adjust scale/rotation if we can snap point. if (_srSnapPointId != -1) { bool isRotating = RotationButton.IsChecked.GetValueOrDefault(false); bool isScaling = ScaleButton.IsChecked.GetValueOrDefault(false); double distanceToPoint = _srDistanceToPoint * parcelData.ScaleValue; double bearingToPoint = _srBearingToPoint - parcelData.RotationValue; if (bearingToPoint >= 2 * Math.PI) { bearingToPoint -= 2 * Math.PI; } ESRI.ArcGIS.Client.Geometry.MapPoint snapPointSR = GeometryUtil.ConstructPoint(_originPoint, bearingToPoint, distanceToPoint); if (snapPointSR != null) { ESRI.ArcGIS.Client.Geometry.Polyline snapLine; SnapPointToCacheObjects(snapPointSR, isScaling, out snapLine); // if scaling, skip zero distance so if (snapLine != null) // we don't snap to origin point. { bool ok = false; ESRI.ArcGIS.Client.Geometry.MapPoint intersectPoint = null; if (isRotating) { ok = GeometryUtil.ConstructPointLineCurveIntersection(snapLine, _originPoint, bearingToPoint, distanceToPoint, out intersectPoint); // distanceToPoint is radius here if (ok) // Only snap if the mouse location is within snap solution { ok = GeometryUtil.LineLength(intersectPoint, currentPoint) <= _xmlConfiguation.SnapTolerance; } if (ok) { parcelData.RotationValue = GeometryUtil.Angle(_srSnapPoint, intersectPoint, _originPoint); } } else if (isScaling) { ESRI.ArcGIS.Client.Geometry.MapPoint endPoint = GeometryUtil.ConstructPoint(_originPoint, bearingToPoint, distanceToPoint + _xmlConfiguation.SnapTolerance); ESRI.ArcGIS.Client.Geometry.Polyline sourceLine = GeometryUtil.Line(_originPoint, endPoint); ok = GeometryUtil.ConstructPointLineLineIntersection(snapLine, sourceLine, out intersectPoint); if (ok) // Only snap if the mouse location is within snap solution { ok = GeometryUtil.LineLength(intersectPoint, currentPoint) <= _xmlConfiguation.SnapTolerance; } if (ok) { double scale = GeometryUtil.Scale(_srSnapPoint, intersectPoint, _originPoint); if (scale > 0.0) { parcelData.ScaleValue = scale; } } } // Test code for debugging. // //GraphicsLayer testGraphicsLayer = ParcelMap.Layers["TestGraphicLayer"] as GraphicsLayer; //testGraphicsLayer.ClearGraphics(); //if (intersectPoint != null) //{ // ESRI.ArcGIS.Client.Graphic graphic = new ESRI.ArcGIS.Client.Graphic() // { // Geometry = intersectPoint, // Symbol = LayoutRoot.Resources["TestMarkerSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol // }; // testGraphicsLayer.Graphics.Add(graphic); //} } } } // Only redraw if there have been an update; // Otherwise runtime does not process mouse up and over flashes. if ((parcelData.ScaleValue != _moveScale) || (parcelData.RotationValue != _moveRotation)) { CalculateAndAddLineGraphics(); _moveScale = parcelData.ScaleValue; _moveRotation = parcelData.RotationValue; } }