Index: src/Deltares.DSoilModel.Forms/DSoilModelMapEditor.cs =================================================================== diff -u -r86 -r87 --- src/Deltares.DSoilModel.Forms/DSoilModelMapEditor.cs (.../DSoilModelMapEditor.cs) (revision 86) +++ src/Deltares.DSoilModel.Forms/DSoilModelMapEditor.cs (.../DSoilModelMapEditor.cs) (revision 87) @@ -306,120 +306,10 @@ /// private void LinkToNearestSegment() { - // objects to link - var cpts = mapEditor.SelectedObjects.Where(o => o is ConePenetrationTestData).Cast().ToList(); - var borings = mapEditor.SelectedObjects.Where(o => o is Boring).Cast().ToList(); - var segments = mapEditor.SelectedObjects.Where(o => o is SoilSegment).Cast().ToList(); - if (segments.Count == 0) - { - segments = project.SoilSegments; - } - foreach (var cpt in cpts) - { - LinkItemToSegments(cpt, segments); - } - foreach (var boring in borings) - { - LinkItemToSegments(boring, segments); - } + var selectedObjects = mapEditor.SelectedObjects.Where(o => o is ConePenetrationTestData || o is Boring || o is SoilSegment).ToArray(); + project.LinkCptsAndBoringsToNearestSegment(selectedObjects); } - private void LinkItemToSegments(IGeographic item, List segments) - { - double nearestDistance = double.MaxValue; - IGeographic nearestItem = null; - SoilSegment nearestSegment = null; - foreach (var segment in segments) - { - double distance = DotSpatialGeographicAlgorithms.Distance(item, segment); - if (distance < nearestDistance) - { - nearestDistance = distance; - nearestItem = item; - nearestSegment = segment; - } - } - if (nearestItem != null) - { - // link segment to item - SetSegmentLinkAndLocalCoordinate(nearestSegment, nearestItem); - - // check for other mechanism segment(s) with the same geographic definition and link to these as well - foreach (var segment in segments) - { - double distance = DotSpatialGeographicAlgorithms.Distance(item, segment); - if (Math.Abs(distance - nearestDistance) < GeometryConstants.Accuracy) - { - if (segment.Mechanism != nearestSegment.Mechanism && segment.Points.Count == nearestSegment.Points.Count) - { - if (CompareGeographicStrings(segment, nearestSegment)) - { - SetSegmentLinkAndLocalCoordinate(segment, nearestItem); - } - } - } - } - } - } - - private void SetSegmentLinkAndLocalCoordinate(SoilSegment segment, object item) - { - var itemPoint = item as IGeographicPoint; - if (itemPoint != null) - { - var segmentPoint = DotSpatialGeographicAlgorithms.NearestPointOnGeographicString(segment, itemPoint); - var xSegment = GeographicHelper.Instance.GetOffsetOnGeographicLineString(segmentPoint, segment); - - // link to cpt ? - var cpt = item as ConePenetrationTestData; - if (cpt != null) - { - if (segment.Cpts.All(x => x.ConePenetrationTestData != cpt)) - { - segment.Cpts.Add(new ConePenetrationTestPerSegment - { - ConePenetrationTestData = cpt, - Xlocal = xSegment - }); - } - } - - // link to boring ? - var boring = item as Boring; - if (boring != null) - { - if (segment.Borings.All(x => x.Boring != boring)) - { - segment.Borings.Add(new BoringPerSegment - { - Boring = boring, - Xlocal = xSegment - }); - } - } - } - } - - private bool CompareGeographicStrings(IGeographicString geographicString1, IGeographicString geographicString2) - { - if (geographicString1.Points.Count != geographicString2.Points.Count) - { - return false; - } - for (int i = 0; i < geographicString1.Points.Count; i++) - { - if (Math.Abs(geographicString1.Points[i].X - geographicString2.Points[i].X) > GeometryConstants.Accuracy) - { - return false; - } - if (Math.Abs(geographicString1.Points[i].Y - geographicString2.Points[i].Y) > GeometryConstants.Accuracy) - { - return false; - } - } - return true; - } - /// /// Selection change handler for specific interactions ///