Exclude bend lines where radius is too large.

This commit is contained in:
AJ
2019-09-10 14:41:58 -04:00
parent 80d4f78858
commit 5da216dec2
4 changed files with 150 additions and 124 deletions

View File

@@ -3,4 +3,7 @@
<startup> <startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" /> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup> </startup>
<appSettings>
<add key="MaxBendRadius" value="2.0"/>
</appSettings>
</configuration> </configuration>

View File

@@ -33,6 +33,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Configuration" />
<Reference Include="System.Core" /> <Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" /> <Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" /> <Reference Include="System.Data.DataSetExtensions" />

View File

@@ -139,5 +139,28 @@ namespace EtchBendLines
{ {
return Math.Abs(b - a) <= tolerance; return Math.Abs(b - a) <= tolerance;
} }
public static Vector2 ClosestPointOnLineTo(this Line line, Vector2 pt)
{
var startPoint = line.StartPoint.ToVector2();
var endPoint = line.EndPoint.ToVector2();
var diff1 = pt - startPoint;
var diff2 = endPoint - startPoint;
var dotProduct = diff1.X * diff2.X + diff1.Y * diff2.Y;
var lengthSquared = diff2.X * diff2.X + diff2.Y * diff2.Y;
var param = dotProduct / lengthSquared;
if (param < 0)
return startPoint;
else if (param > 1)
return endPoint;
else
{
return new Vector2(
startPoint.X + param * diff2.X,
startPoint.Y + param * diff2.Y);
}
}
} }
} }

View File

@@ -3,10 +3,10 @@ using netDxf.Entities;
using netDxf.Tables; using netDxf.Tables;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Configuration;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Text; using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace EtchBendLines namespace EtchBendLines
{ {
@@ -19,6 +19,8 @@ namespace EtchBendLines
Color = AciColor.Yellow Color = AciColor.Yellow
}; };
static Regex bendNoteRegex = new Regex(@"(?<direction>UP|DOWN|DN)\s*(?<angle>\d*(\.\d*)?)°\s*R\s*(?<radius>\d*(\.\d*)?)");
static void Main(string[] args) static void Main(string[] args)
{ {
var path = AppDomain.CurrentDomain.BaseDirectory; var path = AppDomain.CurrentDomain.BaseDirectory;
@@ -85,8 +87,6 @@ namespace EtchBendLines
note.Layer = BendLayer; note.Layer = BendLayer;
} }
AssignBendDirections(bendLines, bendNotes);
var upBends = bendLines.Where(b => b.Direction == BendDirection.Up); var upBends = bendLines.Where(b => b.Direction == BendDirection.Up);
var upBendCount = upBends.Count(); var upBendCount = upBends.Count();
var downBendCount = bendLines.Count - upBendCount; var downBendCount = bendLines.Count - upBendCount;
@@ -142,8 +142,6 @@ namespace EtchBendLines
if (bendNote == null) if (bendNote == null)
continue; continue;
bendNote.Layer = BendLayer;
var note = bendNote.Value.ToUpper(); var note = bendNote.Value.ToUpper();
if (note.Contains("UP")) if (note.Contains("UP"))
@@ -151,8 +149,21 @@ namespace EtchBendLines
else if (note.Contains("DOWN") || note.Contains("DN")) else if (note.Contains("DOWN") || note.Contains("DN"))
bendline.Direction = BendDirection.Down; bendline.Direction = BendDirection.Down;
var match = bendNoteRegex.Match(note);
if (match.Success)
{
bendline.Radius = double.Parse(match.Groups["radius"].Value);
bendline.Angle = double.Parse(match.Groups["angle"].Value);
} }
} }
}
static double MaxBendRadius
{
get { return double.Parse(ConfigurationManager.AppSettings["MaxBendRadius"]); }
}
//static MText FindBendNote(Line bendLine, IEnumerable<MText> bendNotes) //static MText FindBendNote(Line bendLine, IEnumerable<MText> bendNotes)
// { // {
@@ -195,59 +206,45 @@ namespace EtchBendLines
// return closestNote; // return closestNote;
// } // }
static MText FindBendNote(Line bendLIne, IEnumerable<MText> bendNotes) static MText FindBendNote(Line bendLine, IEnumerable<MText> bendNotes)
{ {
var list = bendNotes.ToList(); var bendNotesList = bendNotes.ToList();
var shortestDist = double.MaxValue;
var shortestDistIndex = -1;
for (int i = 0; i < list.Count; i++) for (int i = bendNotesList.Count - 1; i >= 0; i--)
{ {
var note = list[i]; var note = bendNotesList[i];
var notePos = note.Position.ToVector2(); var notePos = note.Position.ToVector2();
var perpendicularPoint = bendLIne.PointPerpendicularTo(notePos); var perpendicularPoint = bendLine.PointPerpendicularTo(notePos);
var dist = notePos.DistanceTo(perpendicularPoint); var dist = notePos.DistanceTo(perpendicularPoint);
var maxAcceptableDist = note.Height * 2.0;
if (dist < shortestDist) if (dist > maxAcceptableDist)
{ bendNotesList.RemoveAt(i);
shortestDistIndex = i;
shortestDist = dist;
}
} }
if (shortestDistIndex == -1) if (bendNotesList.Count == 0)
return null; return null;
var bendNote = list[shortestDistIndex]; var closestNote = bendNotesList.First();
var maxAcceptableDist = bendNote.Height * 2.0; var p1 = closestNote.Position.ToVector2();
var p2 = bendLine.ClosestPointOnLineTo(p1);
var dist2 = p1.DistanceTo(p2);
if (shortestDist > maxAcceptableDist) for (int i = 1; i < bendNotesList.Count; i++)
return null;
return bendNote;
}
static Vector2? FindClosestPoint(Vector2 originPoint, List<Vector2> pts)
{ {
if (pts == null || pts.Any() == false) var note = bendNotesList[i];
return null; var p3 = note.Position.ToVector2();
var p4 = bendLine.ClosestPointOnLineTo(p3);
var dist = p3.DistanceTo(p4);
var closest = pts[0]; if (dist < dist2)
var distance = originPoint.DistanceTo(closest);
for (int i = 1; i < pts.Count; i++)
{ {
var pt = pts[i]; dist2 = dist;
var dist = originPoint.DistanceTo(pt); closestNote = note;
if (dist < distance)
{
distance = dist;
closest = pts[i];
} }
} }
return closest; return closestNote;
} }
static DxfDocument LoadDoc(string file) static DxfDocument LoadDoc(string file)
@@ -258,6 +255,7 @@ namespace EtchBendLines
static List<Bend> GetBendLines(DxfDocument dxf) static List<Bend> GetBendLines(DxfDocument dxf)
{ {
var bends = new List<Bend>(); var bends = new List<Bend>();
var bendNotes = GetBendNotes(dxf);
foreach (var line in dxf.Lines) foreach (var line in dxf.Lines)
{ {
@@ -270,11 +268,12 @@ namespace EtchBendLines
Direction = BendDirection.Unknown Direction = BendDirection.Unknown
}; };
bends.Add(bend); bends.Add(bend);
} }
return bends; AssignBendDirections(bends, bendNotes);
return bends.Where(b => b.Radius <= MaxBendRadius).ToList();
} }
static List<MText> GetBendNotes(DxfDocument dxf) static List<MText> GetBendNotes(DxfDocument dxf)