GetPartType
This commit is contained in:
@@ -8,8 +8,6 @@ namespace EtchBendLines
|
||||
{
|
||||
public class Bend
|
||||
{
|
||||
const double RadPerDeg = Math.PI / 180.0;
|
||||
|
||||
public Line Line { get; set; }
|
||||
|
||||
public double YIntercept
|
||||
@@ -32,7 +30,17 @@ namespace EtchBendLines
|
||||
get { return Line.IsHorizontal(); }
|
||||
}
|
||||
|
||||
public bool IsCollinearTo(Bend bend)
|
||||
public bool IsParallelTo(Bend bend)
|
||||
{
|
||||
return Line.IsParallelTo(bend.Line);
|
||||
}
|
||||
|
||||
public bool IsPerpendicularTo(Bend bend)
|
||||
{
|
||||
return Line.IsPerpendicularTo(bend.Line);
|
||||
}
|
||||
|
||||
public bool IsCollinearTo(Bend bend)
|
||||
{
|
||||
if (bend.IsVertical || this.IsVertical)
|
||||
return (bend.IsVertical && this.IsVertical && bend.YIntercept == this.YIntercept);
|
||||
@@ -146,5 +154,14 @@ namespace EtchBendLines
|
||||
return Math.Sqrt(x * x + y * y);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public double? Radius { get; set; }
|
||||
|
||||
public double? Angle { get; set; }
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"{Direction.ToString()} {Angle}° R{Radius}";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -92,11 +92,47 @@ namespace EtchBendLines
|
||||
return NormalizeRad(Math.Atan2(y, x));
|
||||
}
|
||||
|
||||
|
||||
static double NormalizeRad(double angle)
|
||||
{
|
||||
double r = angle % TwoPI;
|
||||
return r < 0 ? TwoPI + r : r;
|
||||
}
|
||||
|
||||
public static bool IsPerpendicularTo(this Line line1, Line line2)
|
||||
{
|
||||
bool line1Vertical = line1.IsVertical();
|
||||
bool line2Vertical = line2.IsVertical();
|
||||
|
||||
if (line1Vertical)
|
||||
return line2.IsHorizontal();
|
||||
else if (line2.IsVertical())
|
||||
return line1.IsHorizontal();
|
||||
|
||||
return line1.Slope().IsEqualTo(-1 / line2.Slope());
|
||||
}
|
||||
|
||||
public static bool IsParallelTo(this Line line1, Line line2)
|
||||
{
|
||||
if (line1.IsVertical())
|
||||
{
|
||||
if (line2.IsVertical())
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
else if (line2.IsVertical())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return line2.Slope().IsEqualTo(line1.Slope());
|
||||
}
|
||||
|
||||
public const double Epsilon = 0.00001;
|
||||
|
||||
public static bool IsEqualTo(this double a, double b, double tolerance = Epsilon)
|
||||
{
|
||||
return Math.Abs(b - a) <= tolerance;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,8 +34,7 @@ namespace EtchBendLines
|
||||
foreach (var file in files)
|
||||
{
|
||||
AddEtchLines(file);
|
||||
Console.WriteLine("----------------------------------------------------------------");
|
||||
|
||||
Console.WriteLine();
|
||||
}
|
||||
|
||||
PressAnyKeyToExit();
|
||||
@@ -49,8 +48,7 @@ namespace EtchBendLines
|
||||
|
||||
static void AddEtchLines(string filePath)
|
||||
{
|
||||
var name = Path.GetFileNameWithoutExtension(filePath);
|
||||
Console.WriteLine($"Adding etch lines to file \"{name}\"");
|
||||
Console.WriteLine(filePath);
|
||||
|
||||
var dxf = LoadDoc(filePath);
|
||||
var bendLines = GetBendLines(dxf);
|
||||
@@ -90,8 +88,12 @@ namespace EtchBendLines
|
||||
AssignBendDirections(bendLines, bendNotes);
|
||||
|
||||
var upBends = bendLines.Where(b => b.Direction == BendDirection.Up);
|
||||
var upBendCount = upBends.Count();
|
||||
var downBendCount = bendLines.Count - upBendCount;
|
||||
|
||||
Console.WriteLine($"{upBends.Count()} up bends, {bendLines.Count - upBends.Count()} down bends.");
|
||||
Console.WriteLine($"{upBendCount} Up {downBendCount} Down");
|
||||
|
||||
var partType = GetPartType(bendLines);
|
||||
|
||||
foreach (var bendline in upBends)
|
||||
{
|
||||
@@ -262,5 +264,62 @@ namespace EtchBendLines
|
||||
|
||||
return bendNotes;
|
||||
}
|
||||
|
||||
static PartType GetPartType(List<Bend> bends)
|
||||
{
|
||||
if (bends.Count == 0)
|
||||
return PartType.Flat;
|
||||
|
||||
var upBends = bends.Where(b => b.Direction == BendDirection.Up).ToList();
|
||||
var downBends = bends.Where(b => b.Direction == BendDirection.Down).ToList();
|
||||
|
||||
if (upBends.Count == 0 || downBends.Count == 0)
|
||||
{
|
||||
// bends are going the same direction
|
||||
|
||||
if (bends.Count == 2 && bends[0].IsParallelTo(bends[1]))
|
||||
return PartType.Channel;
|
||||
|
||||
if (bends.Count == 4)
|
||||
{
|
||||
var groups = bends.GroupBy(b => b.Line.Slope()).ToList();
|
||||
|
||||
if (groups.Count == 2)
|
||||
{
|
||||
var bend1 = groups[0].First();
|
||||
var bend2 = groups[1].First();
|
||||
|
||||
if (bend1.IsPerpendicularTo(bend2))
|
||||
{
|
||||
return PartType.Pan;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (bends.Count == 1)
|
||||
return PartType.Angle;
|
||||
|
||||
if (bends.Count == 2)
|
||||
{
|
||||
var bend1 = bends[0];
|
||||
var bend2 = bends[1];
|
||||
|
||||
if (bend1.IsParallelTo(bend2))
|
||||
return PartType.ZAngle;
|
||||
}
|
||||
|
||||
return PartType.Other;
|
||||
}
|
||||
}
|
||||
|
||||
public enum PartType
|
||||
{
|
||||
Flat,
|
||||
Angle,
|
||||
Channel,
|
||||
Pan,
|
||||
ZAngle,
|
||||
Other
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user