From f664bd79a4c52cb8fa6ea70665fcd5d941e02c61 Mon Sep 17 00:00:00 2001 From: AJ Date: Mon, 8 Mar 2021 22:38:29 -0500 Subject: [PATCH] Added Etcher class --- EtchBendLines/App.config | 1 + EtchBendLines/EtchBendLines.csproj | 1 + EtchBendLines/Etcher.cs | 90 +++++++++++++++++++++++++++++ EtchBendLines/Program.cs | 93 ++---------------------------- 4 files changed, 98 insertions(+), 87 deletions(-) create mode 100644 EtchBendLines/Etcher.cs diff --git a/EtchBendLines/App.config b/EtchBendLines/App.config index 8a70eab..03eba7f 100644 --- a/EtchBendLines/App.config +++ b/EtchBendLines/App.config @@ -5,5 +5,6 @@ + \ No newline at end of file diff --git a/EtchBendLines/EtchBendLines.csproj b/EtchBendLines/EtchBendLines.csproj index b10db63..761f21d 100644 --- a/EtchBendLines/EtchBendLines.csproj +++ b/EtchBendLines/EtchBendLines.csproj @@ -49,6 +49,7 @@ + diff --git a/EtchBendLines/Etcher.cs b/EtchBendLines/Etcher.cs new file mode 100644 index 0000000..63a8aef --- /dev/null +++ b/EtchBendLines/Etcher.cs @@ -0,0 +1,90 @@ +using netDxf; +using netDxf.Tables; +using System; +using System.Collections.Generic; +using System.Linq; + +namespace EtchBendLines +{ + public class Etcher + { + public Layer BendLayer = new Layer("BEND") + { + Color = AciColor.Yellow + }; + + public double EtchLength { get; set; } = 1.0; + + public double MaxBendRadius { get; set; } = 4.0; + + public void AddEtchLines(string filePath) + { + Console.WriteLine(filePath); + + var bendLineExtractor = new BendLineExtractor(filePath); + bendLineExtractor.MaxBendRadius = MaxBendRadius; + + var bendLines = bendLineExtractor.GetBendLines(); + + if (bendLines.Count == 0) + { + Console.WriteLine("No bend lines found."); + return; + } + else + { + Console.WriteLine($"Found {bendLines.Count} bend lines."); + } + + foreach (var bendLine in bendLines) + { + bendLine.Line.Layer = BendLayer; + bendLine.Line.Color = AciColor.ByLayer; + bendLine.BendNote.Layer = BendLayer; + } + + var upBends = bendLines.Where(b => b.Direction == BendDirection.Up); + var upBendCount = upBends.Count(); + var downBendCount = bendLines.Count - upBendCount; + + Console.WriteLine($"{upBendCount} Up {downBendCount} Down"); + + foreach (var bendline in upBends) + { + var etchLines = bendline.GetEtchLines(EtchLength); + + foreach (var etchLine in etchLines) + { + var existing = bendLineExtractor.DxfDocument.Lines + .Where(l => IsEtchLayer(l.Layer)) + .FirstOrDefault(l => l.StartPoint.IsEqualTo(etchLine.StartPoint) && l.EndPoint.IsEqualTo(etchLine.EndPoint)); + + if (existing != null) + { + // ensure the layer is correct and skip adding the etch line since it already exists. + existing.Layer = etchLine.Layer; + continue; + } + + bendLineExtractor.DxfDocument.AddEntity(etchLine); + } + } + + bendLineExtractor.DxfDocument.Save(filePath); + } + + private bool IsEtchLayer(Layer layer) + { + if (layer.Name == "ETCH") + return true; + + if (layer.Name == "SCRIBE") + return true; + + if (layer.Name == "SCRIBE-TEXT") + return true; + + return false; + } + } +} diff --git a/EtchBendLines/Program.cs b/EtchBendLines/Program.cs index 1aee6f2..b8ca6b7 100644 --- a/EtchBendLines/Program.cs +++ b/EtchBendLines/Program.cs @@ -1,6 +1,4 @@ -using netDxf; -using netDxf.Tables; -using System; +using System; using System.Collections.Generic; using System.Configuration; using System.IO; @@ -11,18 +9,16 @@ namespace EtchBendLines { public class Program { - const double ETCH_LENGTH = 1.0; - - static Layer BendLayer = new Layer("BEND") - { - Color = AciColor.Yellow - }; + static Etcher etcher = new Etcher(); static void Main(string[] args) { var path = AppDomain.CurrentDomain.BaseDirectory; var files = Directory.GetFiles(path, "*.dxf", SearchOption.AllDirectories); + etcher.EtchLength = double.Parse(ConfigurationManager.AppSettings["EtchLength"]); + etcher.MaxBendRadius = double.Parse(ConfigurationManager.AppSettings["MaxBendRadius"]); + if (files == null || files.Length == 0) { Console.WriteLine($"No DXF files founds. Place DXF files in \"{AppDomain.CurrentDomain.BaseDirectory}\" and run this program again."); @@ -32,7 +28,7 @@ namespace EtchBendLines foreach (var file in files) { - AddEtchLines(file); + etcher.AddEtchLines(file); Console.WriteLine(); } @@ -45,83 +41,6 @@ namespace EtchBendLines Console.ReadKey(); } - public static void AddEtchLines(string filePath) - { - Console.WriteLine(filePath); - - var bendLineExtractor = new BendLineExtractor(filePath); - bendLineExtractor.MaxBendRadius = MaxBendRadius; - - var bendLines = bendLineExtractor.GetBendLines(); - - if (bendLines.Count == 0) - { - Console.WriteLine("No bend lines found."); - return; - } - else - { - Console.WriteLine($"Found {bendLines.Count} bend lines."); - } - - foreach (var bendLine in bendLines) - { - bendLine.Line.Layer = BendLayer; - bendLine.Line.Color = AciColor.ByLayer; - bendLine.BendNote.Layer = BendLayer; - } - - var upBends = bendLines.Where(b => b.Direction == BendDirection.Up); - var upBendCount = upBends.Count(); - var downBendCount = bendLines.Count - upBendCount; - - Console.WriteLine($"{upBendCount} Up {downBendCount} Down"); - - var partType = GetPartType(bendLines); - - foreach (var bendline in upBends) - { - var etchLines = bendline.GetEtchLines(ETCH_LENGTH); - - foreach (var etchLine in etchLines) - { - var existing = bendLineExtractor.DxfDocument.Lines - .Where(l => IsEtchLayer(l.Layer)) - .FirstOrDefault(l => l.StartPoint.IsEqualTo(etchLine.StartPoint) && l.EndPoint.IsEqualTo(etchLine.EndPoint)); - - if (existing != null) - { - // ensure the layer is correct and skip adding the etch line since it already exists. - existing.Layer = etchLine.Layer; - continue; - } - - bendLineExtractor.DxfDocument.AddEntity(etchLine); - } - } - - bendLineExtractor.DxfDocument.Save(filePath); - } - - static bool IsEtchLayer(Layer layer) - { - if (layer.Name == "ETCH") - return true; - - if (layer.Name == "SCRIBE") - return true; - - if (layer.Name == "SCRIBE-TEXT") - return true; - - return false; - } - - static double MaxBendRadius - { - get { return double.Parse(ConfigurationManager.AppSettings["MaxBendRadius"]); } - } - static PartType GetPartType(List bends) { if (bends.Count == 0)