Added Etcher class

This commit is contained in:
AJ
2021-03-08 22:38:29 -05:00
parent 1e66f9d960
commit f664bd79a4
4 changed files with 98 additions and 87 deletions

View File

@@ -5,5 +5,6 @@
</startup>
<appSettings>
<add key="MaxBendRadius" value="2.0"/>
<add key="EtchLength" value="1.0"/>
</appSettings>
</configuration>

View File

@@ -49,6 +49,7 @@
<Compile Include="Bend.cs" />
<Compile Include="BendDirection.cs" />
<Compile Include="BendLineExtractor.cs" />
<Compile Include="Etcher.cs" />
<Compile Include="Extensions.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />

90
EtchBendLines/Etcher.cs Normal file
View File

@@ -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;
}
}
}

View File

@@ -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<Bend> bends)
{
if (bends.Count == 0)