public override void OnMouseMove(ICoordinate worldPosition, MouseEventArgs e) { StartDrawing(); if (!isBusy) { // If the newNodeTool is active but not actual dragging a new 'Node' show the snap position. IPoint point = GeometryFactory.CreatePoint(worldPosition); snapResult = MapControl.SnapTool.ExecuteLayerSnapRules(Layer, null, point, worldPosition, -1); } else { IPoint point = GeometryFactory.CreatePoint(worldPosition); snapResult = MapControl.SnapTool.ExecuteLayerSnapRules(Layer, null, point, worldPosition, -1); if (snapResult != null) { newNode.Coordinates[0].X = snapResult.Location.X; newNode.Coordinates[0].Y = snapResult.Location.Y; newNodeLayer.Style = nodeStyle; } else { newNode.Coordinates[0].X = worldPosition.X; newNode.Coordinates[0].Y = worldPosition.Y; newNodeLayer.Style = errorNodeStyle; } } DoDrawing(true); StopDrawing(); }
/// <summary> /// Update snapping /// </summary> /// <param name="sourceLayer"></param> /// The layer of feature. /// <param name="feature"></param> /// Feature that is snapped. Feature is not always available. /// <param name="geometry"></param> /// actual geometry of the feature that is snapped. /// <param name="worldPosition"></param> /// <param name="trackerIndex"></param> public ISnapResult ExecuteLayerSnapRules(ILayer sourceLayer, IFeature feature, IGeometry geometry, ICoordinate worldPosition, int trackerIndex) { IList <ISnapRule> snapRules = MapControl.GetSnapRules(sourceLayer, feature, geometry, trackerIndex); ISnapResult snapResult = null; for (int i = 0; i < snapRules.Count; i++) { ISnapRule rule = snapRules[i]; snapResult = ExecuteSnapRule(rule, feature, geometry, null, worldPosition, trackerIndex); if (null != snapResult) { break; } // If snapping failed for the last rule and snapping is obligatory // any position is valid // todo add rule with SnapRole.Free? if ((!rule.Obligatory) && (i == snapRules.Count - 1)) { snapResult = new SnapResult(worldPosition, null, null, -1, -1); } } if (0 == snapRules.Count) { snapResult = new SnapResult(worldPosition, null, null, -1, -1); } return(snapResult); }
///// <summary> ///// snapping specific for a tool. Called before layer specific snappping is applied. ///// </summary> ///// <param name="sourceLayer"></param> ///// <param name="snapSource"></param> ///// <param name="worldPos"></param> ///// <param name="Envelope"></param> ///// <returns></returns> public void Snap(ILayer sourceLayer, IGeometry snapSource, ICoordinate worldPos, IEnvelope Envelope) { SnapResult = null; IFeature sourceFeature = MapControl.SelectTool.FeatureEditors[0].SourceFeature; if (sourceFeature.Geometry != snapSource) { return; } SnapRole snapRole = SnapRole.FreeAtObject; if ((Control.ModifierKeys & Keys.Control) == Keys.Control) { snapRole = SnapRole.Free; } ISnapRule snapRule = new SnapRule { SourceLayer = sourceLayer, TargetLayer = sourceLayer, Obligatory = true, SnapRole = snapRole, PixelGravity = 4 }; SnapResult = MapControl.SnapTool.ExecuteSnapRule( snapRule, sourceFeature, sourceFeature.Geometry, new List <IFeature> { sourceFeature }, worldPos, -1); }
///// <summary> ///// snapping specific for a tool. Called before layer specific snappping is applied. ///// </summary> ///// <param name="sourceLayer"></param> ///// <param name="snapSource"></param> ///// <param name="worldPos"></param> ///// <param name="Envelope"></param> ///// <returns></returns> public void Snap(ILayer sourceLayer, IGeometry snapSource, ICoordinate worldPos, IEnvelope Envelope) { SnapResult = null; IFeature sourceFeature = MapControl.SelectTool.FeatureEditors[0].SourceFeature; if (sourceFeature.Geometry != snapSource) return; SnapRole snapRole = SnapRole.FreeAtObject; if ((Control.ModifierKeys & Keys.Control) == Keys.Control) snapRole = SnapRole.Free; ISnapRule snapRule = new SnapRule { SourceLayer = sourceLayer, TargetLayer = sourceLayer, Obligatory = true, SnapRole = snapRole, PixelGravity = 4 }; SnapResult = MapControl.SnapTool.ExecuteSnapRule( snapRule, sourceFeature, sourceFeature.Geometry, new List<IFeature> { sourceFeature }, worldPos, -1); }
public static void LineStringSnapStartEnd(ref double minDistance, ref ISnapResult snapResult, ILineString lineString, ICoordinate worldPos) { ICoordinate c1 = lineString.Coordinates[0]; ICoordinate location; int snapIndexPrevious; int snapIndexNext; double distance = GeometryHelper.Distance(c1.X, c1.Y, worldPos.X, worldPos.Y); if (distance < minDistance) { location = c1; snapIndexPrevious = 0; snapIndexNext = 0; minDistance = distance; snapResult = new SnapResult(location, null, lineString, snapIndexPrevious, snapIndexNext); } ICoordinate c2 = lineString.Coordinates[lineString.Coordinates.Length - 1]; distance = GeometryHelper.Distance(c2.X, c2.Y, worldPos.X, worldPos.Y); if (distance >= minDistance) { return; } location = c2; snapIndexPrevious = lineString.Coordinates.Length - 1; snapIndexNext = lineString.Coordinates.Length - 1; snapResult = new SnapResult(location, null, lineString, snapIndexPrevious, snapIndexNext); }
public static void LineStringSnapAllTrackers(ref double minDistance, ref ISnapResult snapResult, ILineString lineString, ICoordinate worldPos) { for (int i = 0; i < lineString.Coordinates.Length; i++) { ICoordinate c1 = lineString.Coordinates[i]; double distance = GeometryHelper.Distance(c1.X, c1.Y, worldPos.X, worldPos.Y); if (distance >= minDistance) continue; minDistance = distance; snapResult = new SnapResult(lineString.Coordinates[i], null, lineString, i, i); } }
public static void LineStringSnapFreeAtObject(ref double minDistance, ref ISnapResult snapResult, IFeature feature, ILineString lineString, ICoordinate worldPos) { int vertexIndex; var nearestPoint = GeometryHelper.GetNearestPointAtLine(lineString, worldPos, minDistance, out vertexIndex); if (nearestPoint == null) { return; } minDistance = GeometryHelper.Distance(nearestPoint.X, nearestPoint.Y, worldPos.X, worldPos.Y); snapResult = new SnapResult(nearestPoint, feature, lineString, vertexIndex - 1, vertexIndex); }
public static void LineStringSnapFree(ref double minDistance, ref ISnapResult snapResult, ILineString lineString, ICoordinate worldPos) { for (int i = 1; i < lineString.Coordinates.Length; i++) { ICoordinate c1 = lineString.Coordinates[i - 1]; ICoordinate c2 = lineString.Coordinates[i]; double distance = GeometryHelper.LinePointDistance(c1.X, c1.Y, c2.X, c2.Y, worldPos.X, worldPos.Y); if (distance >= minDistance) continue; minDistance = distance; snapResult = new SnapResult(GeometryFactory.CreateCoordinate(worldPos.X, worldPos.Y), null, lineString, i - 1, i); } }
public static void LineStringSnapAllTrackers(ref double minDistance, ref ISnapResult snapResult, ILineString lineString, ICoordinate worldPos) { for (int i = 0; i < lineString.Coordinates.Length; i++) { ICoordinate c1 = lineString.Coordinates[i]; double distance = GeometryHelper.Distance(c1.X, c1.Y, worldPos.X, worldPos.Y); if (distance >= minDistance) { continue; } minDistance = distance; snapResult = new SnapResult(lineString.Coordinates[i], null, lineString, i, i); } }
public static void LineStringSnapFree(ref double minDistance, ref ISnapResult snapResult, ILineString lineString, ICoordinate worldPos) { for (int i = 1; i < lineString.Coordinates.Length; i++) { ICoordinate c1 = lineString.Coordinates[i - 1]; ICoordinate c2 = lineString.Coordinates[i]; double distance = GeometryHelper.LinePointDistance(c1.X, c1.Y, c2.X, c2.Y, worldPos.X, worldPos.Y); if (distance >= minDistance) { continue; } minDistance = distance; snapResult = new SnapResult(GeometryFactory.CreateCoordinate(worldPos.X, worldPos.Y), null, lineString, i - 1, i); } }
public virtual bool MoveTracker(ITrackerFeature trackerFeature, double deltaX, double deltaY, ISnapResult snapResult) { int index = -1; IList<int> handles = new List<int>(); IList<IGeometry> geometries = new List<IGeometry>(); for (int i = 0; i < trackers.Count; i++) { geometries.Add(trackers[i].Geometry); if (trackers[i].Selected) { handles.Add(i); } if (trackers[i] == trackerFeature) { index = i; } } if (-1 == index) throw new ArgumentException("Can not find tracker; can not move."); //return false; if (0 == handles.Count) throw new ArgumentException("No trackers selected, can not move."); //return false; if (null != FallOffPolicy) { FallOffPolicy.Move(TargetFeature.Geometry, geometries, handles, index, deltaX, deltaY); } else { GeometryHelper.MoveCoordinate(TargetFeature.Geometry, index, deltaX, deltaY); TargetFeature.Geometry.GeometryChangedAction(); GeometryHelper.MoveCoordinate(trackerFeature.Geometry, 0, deltaX, deltaY); trackerFeature.Geometry.GeometryChangedAction(); } foreach (IFeatureRelationEditor topologyRule in TopologyRules) { topologyRule.UpdateRelatedFeatures(SourceFeature, TargetFeature.Geometry, handles); } return true; }
private IGeometry GetDefaultGeometry(ISnapResult snapResult) { IGeometry defaultGeometry = null; IFeatureEditor featureEditor = MapControl.SelectTool.GetFeatureEditor(Layer, null); if (null != featureEditor) { defaultGeometry = featureEditor.CreateDefaultGeometry(Layer, newLineGeometry[0], (null != snapResult) ? snapResult.NearestTarget : null, (null != snapResult) ? snapResult.Location : null); if (null != defaultGeometry) { newLineGeometry.Clear(); newLineGeometry.Add(defaultGeometry); } } return(defaultGeometry); }
private void ShowSnapResult(ISnapResult snapResult) { ((DataTableFeatureProvider)snapLayer.DataSource).Clear(); if (null == snapResult) { return; } IList <IGeometry> visibleSnaps = snapResult.VisibleSnaps; //if (null == visisbleSnaps) if (0 == visibleSnaps.Count) { List <ICoordinate> vertices = new List <ICoordinate>(); if (-1 != snapResult.SnapIndexPrevious) { vertices.Add(GeometryFactory.CreateCoordinate(snapResult.NearestTarget.Coordinates[snapResult.SnapIndexPrevious].X, snapResult.NearestTarget.Coordinates[snapResult.SnapIndexPrevious].Y)); } vertices.Add(GeometryFactory.CreateCoordinate(snapResult.Location.X, snapResult.Location.Y)); IGeometry active = GeometryFactory.CreatePoint(snapResult.Location.X, snapResult.Location.Y); if (-1 != snapResult.SnapIndexNext) { vertices.Add(GeometryFactory.CreateCoordinate(snapResult.NearestTarget.Coordinates[snapResult.SnapIndexNext].X, snapResult.NearestTarget.Coordinates[snapResult.SnapIndexNext].Y)); } if (vertices.Count > 1) { ILineString snapLineString = GeometryFactory.CreateLineString(vertices.ToArray()); ((DataTableFeatureProvider)snapLayer.DataSource).Add(snapLineString); } ((DataTableFeatureProvider)snapLayer.DataSource).Add(active); } else { foreach (var snap in visibleSnaps) { ((DataTableFeatureProvider)snapLayer.DataSource).Add(snap); } } }
public override void OnMouseDown(ICoordinate worldPosition, MouseEventArgs e) { if (e.Button != MouseButtons.Left) { return; } isBusy = true; StartDrawing(); newNode = GeometryFactory.CreatePoint(worldPosition); ((DataTableFeatureProvider)newNodeLayer.DataSource).Clear(); newNodeLayer.DataSource.Add(newNode); snapResult = MapControl.SnapTool.ExecuteLayerSnapRules(Layer, null, newNode, worldPosition, -1); if (snapResult != null) { newNode.Coordinates[0].X = snapResult.Location.X; newNode.Coordinates[0].Y = snapResult.Location.Y; } newNodeLayer.Style = MapControl.SnapTool.Failed ? errorNodeStyle : nodeStyle; }
public override void OnMouseDown(ICoordinate worldPosition, MouseEventArgs e) { if (e.Button != MouseButtons.Left) { return; } isBusy = true; StartDrawing(); newNode = GeometryFactory.CreatePoint(worldPosition); ((DataTableFeatureProvider)newNodeLayer.DataSource).Clear(); newNodeLayer.DataSource.Add(newNode); snapResult = MapControl.SnapTool.ExecuteLayerSnapRules(Layer, null, newNode, worldPosition, - 1); if (snapResult != null) { newNode.Coordinates[0].X = snapResult.Location.X; newNode.Coordinates[0].Y = snapResult.Location.Y; } newNodeLayer.Style = MapControl.SnapTool.Failed ? errorNodeStyle : nodeStyle; }
public static void PolygonSnapFreeAtObject(ref double minDistance, ref ISnapResult snapResult, IPolygon polygon, ICoordinate worldPos) { for (int i = 1; i < polygon.Coordinates.Length; i++) { ICoordinate c1 = polygon.Coordinates[i - 1]; ICoordinate c2 = polygon.Coordinates[i]; double distance = GeometryHelper.LinePointDistance(c1.X, c1.Y, c2.X, c2.Y, worldPos.X, worldPos.Y); if (distance >= minDistance) { continue; } minDistance = distance; ICoordinate min_c1 = polygon.Coordinates[i - 1]; ICoordinate min_c2 = polygon.Coordinates[i]; snapResult = new SnapResult(GeometryHelper.NearestPointAtSegment(min_c1.X, min_c1.Y, min_c2.X, min_c2.Y, worldPos.X, worldPos.Y), null, polygon, i - 1, i); } }
public void SnapToLineStringUsingFreeAtObject() { MockRepository mockRepository = new MockRepository(); IFeature feature = mockRepository.CreateMock <IFeature>(); using (mockRepository.Record()) { Expect.Call(feature.Geometry).Return(lineString).Repeat.Any(); } using (mockRepository.Playback()) { VectorLayer lineStringLayer = new VectorLayer { DataSource = new FeatureCollection { Features = new List <IFeature> { feature } } }; IPoint snapSource = new Point(5, 5); SnapRule snapRule = new SnapRule { TargetLayer = lineStringLayer, SnapRole = SnapRole.FreeAtObject, Obligatory = true, PixelGravity = 40 }; ISnapResult snapResults = snapRule.Execute(null, snapSource, null, snapSource.Coordinates[0], CreateEnvelope(snapSource, 10), 0); Assert.IsNotNull(snapResults); Assert.IsNotNull(snapResults.Location); Assert.AreEqual(0, snapResults.Location.Y); Assert.AreEqual(5, snapResults.Location.X); } }
public static void LineStringSnapStartEnd(ref double minDistance, ref ISnapResult snapResult, ILineString lineString, ICoordinate worldPos) { ICoordinate c1 = lineString.Coordinates[0]; ICoordinate location; int snapIndexPrevious; int snapIndexNext; double distance = GeometryHelper.Distance(c1.X, c1.Y, worldPos.X, worldPos.Y); if (distance < minDistance) { location = c1; snapIndexPrevious = 0; snapIndexNext = 0; minDistance = distance; snapResult = new SnapResult(location, null, lineString, snapIndexPrevious, snapIndexNext); } ICoordinate c2 = lineString.Coordinates[lineString.Coordinates.Length - 1]; distance = GeometryHelper.Distance(c2.X, c2.Y, worldPos.X, worldPos.Y); if (distance >= minDistance) return; location = c2; snapIndexPrevious = lineString.Coordinates.Length - 1; snapIndexNext = lineString.Coordinates.Length - 1; snapResult = new SnapResult(location, null, lineString, snapIndexPrevious, snapIndexNext); }
public override bool MoveTracker(ITrackerFeature trackerFeature, double deltaX, double deltaY, ISnapResult snapResult) { if (trackerFeature == AllTracker) { int index = -1; IList<int> handles = new List<int>(); IList<IGeometry> geometries = new List<IGeometry>(); for (int i = 0; i < trackers.Count; i++) { geometries.Add(trackers[i].Geometry); //if (trackers[i].Selected) { handles.Add(i); } if (trackers[i] == trackerFeature) { index = i; } } if (0 == handles.Count) return false; if (null == FallOffPolicy) { FallOffPolicy = new NoFallOffPolicy(); } FallOffPolicy.Move(TargetFeature.Geometry, geometries, handles, index, deltaX, deltaY); foreach (IFeatureRelationEditor topologyRule in TopologyRules) { topologyRule.UpdateRelatedFeatures(SourceFeature, TargetFeature.Geometry, new List<int> { 0 }); } return true; } return base.MoveTracker(trackerFeature, deltaX, deltaY, snapResult); }
public static void LineStringSnapStart(ref ISnapResult snapResult, ILineString lineString) { snapResult = new SnapResult(lineString.Coordinates[0], null, lineString, 0, 0); }
public static void LineStringSnapEnd(ref ISnapResult snapResult, ILineString lineString) { snapResult = new SnapResult(lineString.Coordinates[lineString.Coordinates.Length - 1], null, lineString, lineString.Coordinates.Length - 1, lineString.Coordinates.Length - 1); }
public virtual void Stop(ISnapResult snapResult) { Stop(); }
public static void PointSnap(ref ISnapResult snapResult, IGeometry geometry) { snapResult = new SnapResult(geometry.Coordinates[0], null, geometry, 0, 0); }
public ISnapResult Snap(ILayer layer, SnapRole snapRole, IGeometry snapSource, ICoordinate worldPos, IEnvelope envelope, int pixelGravity, IList <IFeature> snapTargets) { var targetVectorLayer = (VectorLayer)layer; var snapCandidates = targetVectorLayer.DataSource.GetFeatures(envelope); if (!layer.Enabled) { return(null); } // hack preserve snapTargets functionality IList <IGeometry> snapTargetGeometries = new List <IGeometry>(); if (null != snapTargets) { for (int i = 0; i < snapTargets.Count; i++) { snapTargetGeometries.Add(snapTargets[i].Geometry); } } double minDistance = double.MaxValue; // TODO: incapsulate minDistance in ISnapResult ISnapResult snapResult = null; foreach (IFeature feature in snapCandidates) { IGeometry geometry = feature.Geometry; if ((null != snapTargets) && (snapTargetGeometries.IndexOf(geometry) == -1)) { continue; } if (snapRole == SnapRole.None) { continue; } if (geometry is IPolygon) { IPolygon polygon = (IPolygon)geometry; switch (snapRole) { case SnapRole.Free: SnappingHelper.PolygonSnapFree(ref minDistance, ref snapResult, polygon, worldPos); break; default: //case SnapRole.FreeAtObject: SnappingHelper.PolygonSnapFreeAtObject(ref minDistance, ref snapResult, polygon, worldPos); break; } } if (geometry is ILineString) { ILineString lineString = (ILineString)geometry; switch (snapRole) { case SnapRole.Free: SnappingHelper.LineStringSnapFree(ref minDistance, ref snapResult, lineString, worldPos); break; case SnapRole.FreeAtObject: SnappingHelper.LineStringSnapFreeAtObject(ref minDistance, ref snapResult, feature, lineString, worldPos); break; case SnapRole.TrackersNoStartNoEnd: break; case SnapRole.AllTrackers: SnappingHelper.LineStringSnapAllTrackers(ref minDistance, ref snapResult, lineString, worldPos); break; case SnapRole.Start: SnappingHelper.LineStringSnapStart(ref snapResult, lineString); break; case SnapRole.End: SnappingHelper.LineStringSnapEnd(ref snapResult, lineString); break; case SnapRole.StartEnd: SnappingHelper.LineStringSnapStartEnd(ref minDistance, ref snapResult, lineString, worldPos); break; } } else if (geometry is IPoint) { SnappingHelper.PointSnap(ref snapResult, geometry); } } // foreach (IGeometry geometry in snapCandidates) return(snapResult); }
public virtual bool MoveTracker(ITrackerFeature trackerFeature, double deltaX, double deltaY, ISnapResult snapResult) { int index = -1; IList <int> handles = new List <int>(); IList <IGeometry> geometries = new List <IGeometry>(); for (int i = 0; i < trackers.Count; i++) { geometries.Add(trackers[i].Geometry); if (trackers[i].Selected) { handles.Add(i); } if (trackers[i] == trackerFeature) { index = i; } } if (-1 == index) { throw new ArgumentException("Can not find tracker; can not move."); } //return false; if (0 == handles.Count) { throw new ArgumentException("No trackers selected, can not move."); } //return false; if (null != FallOffPolicy) { FallOffPolicy.Move(TargetFeature.Geometry, geometries, handles, index, deltaX, deltaY); } else { GeometryHelper.MoveCoordinate(TargetFeature.Geometry, index, deltaX, deltaY); TargetFeature.Geometry.GeometryChangedAction(); GeometryHelper.MoveCoordinate(trackerFeature.Geometry, 0, deltaX, deltaY); trackerFeature.Geometry.GeometryChangedAction(); } foreach (IFeatureRelationEditor topologyRule in TopologyRules) { topologyRule.UpdateRelatedFeatures(SourceFeature, TargetFeature.Geometry, handles); } return(true); }
public override bool MoveTracker(ITrackerFeature trackerFeature, double deltaX, double deltaY, ISnapResult snapResult) { if (trackerFeature == AllTracker) { int index = -1; IList <int> handles = new List <int>(); IList <IGeometry> geometries = new List <IGeometry>(); for (int i = 0; i < trackers.Count; i++) { geometries.Add(trackers[i].Geometry); //if (trackers[i].Selected) { handles.Add(i); } if (trackers[i] == trackerFeature) { index = i; } } if (0 == handles.Count) { return(false); } if (null == FallOffPolicy) { FallOffPolicy = new NoFallOffPolicy(); } FallOffPolicy.Move(TargetFeature.Geometry, geometries, handles, index, deltaX, deltaY); foreach (IFeatureRelationEditor topologyRule in TopologyRules) { topologyRule.UpdateRelatedFeatures(SourceFeature, TargetFeature.Geometry, new List <int> { 0 }); } return(true); } return(base.MoveTracker(trackerFeature, deltaX, deltaY, snapResult)); }
public virtual bool MoveTracker(ITrackerFeature trackerFeature, double deltaX, double deltaY, ISnapResult snapResult) { int index = -1; IList <int> handles = new List <int>(); IList <IGeometry> geometries = new List <IGeometry>(); for (int i = 0; i < trackers.Count; i++) { geometries.Add(trackers[i].Geometry); if (trackers[i].Selected) { handles.Add(i); } if (trackers[i] == trackerFeature) { index = i; } } //NS, 2013-10-29, Untuk polygon hasil buffer control point seharusnya tidak bisa dipindah-pindah, // kecuali titik tengahnya yang di drag.... if (trackerFeature.FeatureEditor.SourceFeature.Attributes.Keys.Contains("Buffer")) { if ((bool)trackerFeature.FeatureEditor.SourceFeature.Attributes["Buffer"]) { return(false); //throw new ArgumentException("Jangan dipindah-pindah :P."); } } //NS, 2013-09-09, Karena bila "throw ne ArgumentException", user awam akan menganggap error fatal, //jadi d return false saja deh... if (-1 == index) { //throw new ArgumentException("Can not find tracker; can not move."); //NS, 2013-09-09 return(false); } if (0 == handles.Count) { //throw new ArgumentException("No trackers selected, can not move."); //NS, 2013-09-09 return(false); } if (null != FallOffPolicy) { FallOffPolicy.Move(TargetFeature.Geometry, geometries, handles, index, deltaX, deltaY); } else { GeometryHelper.MoveCoordinate(TargetFeature.Geometry, index, deltaX, deltaY); TargetFeature.Geometry.GeometryChangedAction(); GeometryHelper.MoveCoordinate(trackerFeature.Geometry, 0, deltaX, deltaY); trackerFeature.Geometry.GeometryChangedAction(); } foreach (IFeatureRelationEditor topologyRule in TopologyRules) { topologyRule.UpdateRelatedFeatures(SourceFeature, TargetFeature.Geometry, handles); } return(true); }
private void ShowSnapResult(ISnapResult snapResult) { ((DataTableFeatureProvider)snapLayer.DataSource).Clear(); if (null == snapResult) return; IList<IGeometry> visibleSnaps = snapResult.VisibleSnaps; //if (null == visisbleSnaps) if (0 == visibleSnaps.Count) { List<ICoordinate> vertices = new List<ICoordinate>(); if (-1 != snapResult.SnapIndexPrevious) { vertices.Add(GeometryFactory.CreateCoordinate(snapResult.NearestTarget.Coordinates[snapResult.SnapIndexPrevious].X, snapResult.NearestTarget.Coordinates[snapResult.SnapIndexPrevious].Y)); } vertices.Add(GeometryFactory.CreateCoordinate(snapResult.Location.X, snapResult.Location.Y)); IGeometry active = GeometryFactory.CreatePoint(snapResult.Location.X, snapResult.Location.Y); if (-1 != snapResult.SnapIndexNext) { vertices.Add(GeometryFactory.CreateCoordinate(snapResult.NearestTarget.Coordinates[snapResult.SnapIndexNext].X, snapResult.NearestTarget.Coordinates[snapResult.SnapIndexNext].Y)); } if (vertices.Count > 1) { ILineString snapLineString = GeometryFactory.CreateLineString(vertices.ToArray()); ((DataTableFeatureProvider)snapLayer.DataSource).Add(snapLineString); } ((DataTableFeatureProvider)snapLayer.DataSource).Add(active); } else { foreach (var snap in visibleSnaps) { ((DataTableFeatureProvider)snapLayer.DataSource).Add(snap); } } }
public override void OnMouseUp(ICoordinate worldPosition, MouseEventArgs e) { if (e.Button != MouseButtons.Left) { return; } if (newLineGeometry.Count == 0) { return; } ISnapResult snapResult = MapControl.SnapTool.ExecuteLayerSnapRules(Layer, null, adding ? newLineGeometry[0] : null, worldPosition, adding ? newLineGeometry[0].Coordinates.Length - 1 : -1); IGeometry defaultGeometry = GetDefaultGeometry(snapResult); ILineString lineString = (ILineString)newLineGeometry[0]; if (null == lineString) { isBusy = false; return; } if (ActualAutoCurve) { if (null == defaultGeometry) { snapResult = Snap(worldPosition); if (null == snapResult) { // hack if obligatory snapping failed mimic result. This is not valid for NewNodeTool // Think of solution within snaprule snapResult = new SnapResult(worldPosition, null, null, -1, -1); } if (TemporalEnd) { GeometryHelper.SetCoordinate(lineString, lineString.Coordinates.Length - 1, snapResult.Location); } else { lineString = AppendCurvePoint(lineString, snapResult.Location); } if (!KeepDuplicates) { lineString = RemoveDuplicatePoints(lineString); } } adding = false; newLineGeometry[0] = lineString; //Flush(); SelectTool selectTool = MapControl.SelectTool; if (null != lineString && snapResult != null) { // TODO: call editor here instead of feature provider IFeature newFeature; try { if (CloseLine) { lineString = CloseLineString(lineString); } newFeature = FeatureProvider.Add(lineString); // will add Cross Section and call ConnectCrossSectionToBranch } catch (Exception exception) { // an exception during add operation can fail; for example when adding a branch feature log.Warn(exception.Message); adding = false; StopDrawing(); newLineGeometry.Clear(); return; } // was adding succesfull? if (null != newFeature) { //Layer.RenderRequired = true; MapControl.SelectTool.Select(Layer, newFeature, 0); } } else { // do not add a linestring with zero length selectTool.Clear(); } adding = false; StopDrawing(); newLineGeometry.Clear(); } Layer.RenderRequired = true; MapControl.Refresh(); isBusy = false; }
public static void PolygonSnapFreeAtObject(ref double minDistance, ref ISnapResult snapResult, IPolygon polygon, ICoordinate worldPos) { for (int i = 1; i < polygon.Coordinates.Length; i++) { ICoordinate c1 = polygon.Coordinates[i - 1]; ICoordinate c2 = polygon.Coordinates[i]; double distance = GeometryHelper.LinePointDistance(c1.X, c1.Y, c2.X, c2.Y, worldPos.X, worldPos.Y); if (distance >= minDistance) continue; minDistance = distance; ICoordinate min_c1 = polygon.Coordinates[i - 1]; ICoordinate min_c2 = polygon.Coordinates[i]; snapResult = new SnapResult(GeometryHelper.NearestPointAtSegment(min_c1.X, min_c1.Y, min_c2.X, min_c2.Y, worldPos.X, worldPos.Y), null, polygon, i - 1, i); } }