Compare commits
10 Commits
f664bd79a4
...
89d987f6c6
| Author | SHA1 | Date | |
|---|---|---|---|
| 89d987f6c6 | |||
| 78ae737adb | |||
| 2391eb7050 | |||
| dd7443ddb8 | |||
| e5daf748c6 | |||
| 774012021c | |||
| 214cc94816 | |||
| 04031a7677 | |||
| 3e4ab60366 | |||
| 2b30498147 |
@@ -1,7 +1,7 @@
|
||||
<?xml version="1.0" encoding="utf-8" ?>
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<configuration>
|
||||
<startup>
|
||||
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
|
||||
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8"/>
|
||||
</startup>
|
||||
<appSettings>
|
||||
<add key="MaxBendRadius" value="2.0"/>
|
||||
|
||||
16
EtchBendLines/AppConfig.cs
Normal file
16
EtchBendLines/AppConfig.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using System.Configuration;
|
||||
|
||||
namespace EtchBendLines
|
||||
{
|
||||
public static class AppConfig
|
||||
{
|
||||
public static double GetDouble(string key)
|
||||
{
|
||||
if (!double.TryParse(ConfigurationManager.AppSettings[key], out var value))
|
||||
throw new Exception($"Failed to convert AppSetting[\"{key}\"] to double.");
|
||||
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -53,71 +53,6 @@ namespace EtchBendLines
|
||||
return bend.Slope == this.Slope;
|
||||
}
|
||||
|
||||
public List<Line> GetEtchLines(double etchLength)
|
||||
{
|
||||
var lines = new List<Line>();
|
||||
|
||||
var startPoint = new Vector2(Line.StartPoint.X, Line.StartPoint.Y);
|
||||
var endPoint = new Vector2(Line.EndPoint.X, Line.EndPoint.Y);
|
||||
var bendLength = startPoint.DistanceTo(endPoint);
|
||||
|
||||
if (bendLength < (etchLength * 3.0))
|
||||
{
|
||||
lines.Add(new Line(Line.StartPoint, Line.EndPoint));
|
||||
}
|
||||
else
|
||||
{
|
||||
var angle = startPoint.AngleTo(endPoint);
|
||||
|
||||
if (Line.IsVertical())
|
||||
{
|
||||
var x = Line.StartPoint.X;
|
||||
|
||||
var bottomY1 = Math.Min(startPoint.Y, endPoint.Y);
|
||||
var bottomY2 = bottomY1 + etchLength;
|
||||
|
||||
var topY1 = Math.Max(startPoint.Y, endPoint.Y);
|
||||
var topY2 = topY1 - etchLength;
|
||||
|
||||
var p1 = new Vector2(x, bottomY1);
|
||||
var p2 = new Vector2(x, bottomY2);
|
||||
var p3 = new Vector2(x, topY1);
|
||||
var p4 = new Vector2(x, topY2);
|
||||
|
||||
lines.Add(new Line(p1, p2));
|
||||
lines.Add(new Line(p3, p4));
|
||||
}
|
||||
else
|
||||
{
|
||||
var start = Line.StartPoint.ToVector2();
|
||||
var end = Line.EndPoint.ToVector2();
|
||||
|
||||
var x1 = Math.Cos(angle);
|
||||
var y1 = Math.Sin(angle);
|
||||
|
||||
var p1 = new Vector2(start.X, start.Y);
|
||||
var p2 = new Vector2(start.X + x1, start.Y + y1);
|
||||
var p3 = new Vector2(end.X, end.Y);
|
||||
var p4 = new Vector2(end.X - x1, end.Y - y1);
|
||||
|
||||
lines.Add(new Line(p1, p2));
|
||||
lines.Add(new Line(p3, p4));
|
||||
}
|
||||
}
|
||||
|
||||
var etchLayer = new Layer("ETCH")
|
||||
{
|
||||
Color = AciColor.Green,
|
||||
};
|
||||
|
||||
foreach (var line in lines)
|
||||
{
|
||||
line.Layer = etchLayer;
|
||||
}
|
||||
|
||||
return lines;
|
||||
}
|
||||
|
||||
public BendDirection Direction { get; set; }
|
||||
|
||||
public double Length
|
||||
@@ -135,8 +70,6 @@ namespace EtchBendLines
|
||||
public double? Angle { get; set; }
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"{Direction.ToString()} {Angle}° R{Radius}";
|
||||
}
|
||||
=> $"{Direction} {(Angle?.ToString("0.##") ?? "?")}° R{(Radius?.ToString("0.##") ?? "?")}";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
using netDxf;
|
||||
using netDxf.Entities;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace EtchBendLines
|
||||
{
|
||||
class BendLineExtractor
|
||||
public class BendLineExtractor
|
||||
{
|
||||
public BendLineExtractor(string dxfFile)
|
||||
{
|
||||
@@ -31,7 +32,10 @@ namespace EtchBendLines
|
||||
/// <summary>
|
||||
/// The regular expression pattern the bend note must match
|
||||
/// </summary>
|
||||
static readonly Regex bendNoteRegex = new Regex(@"(?<direction>UP|DOWN|DN)\s*(?<angle>\d*(\.\d*)?)°\s*R\s*(?<radius>\d*(\.\d*)?)");
|
||||
static readonly Regex bendNoteRegex = new Regex(
|
||||
@"\b(?<direction>UP|DOWN|DN)\s+(?<angle>\d+(\.\d+)?)°?\s*R\s*(?<radius>\d+(\.\d+)?)\b",
|
||||
RegexOptions.Compiled | RegexOptions.IgnoreCase
|
||||
);
|
||||
|
||||
public DxfDocument DxfDocument { get; private set; }
|
||||
|
||||
@@ -45,7 +49,7 @@ namespace EtchBendLines
|
||||
|
||||
foreach (var line in DxfDocument.Lines)
|
||||
{
|
||||
if (line.Linetype.Name != "CENTERX2" && line.Layer.Name != "BEND")
|
||||
if (!IsBendLine(line))
|
||||
continue;
|
||||
|
||||
var bend = new Bend
|
||||
@@ -62,6 +66,11 @@ namespace EtchBendLines
|
||||
return bends.Where(b => b.Radius <= MaxBendRadius).ToList();
|
||||
}
|
||||
|
||||
private bool IsBendLine(Line line)
|
||||
{
|
||||
return line.Linetype.Name == "CENTERX2" && line.Layer.Name == "BEND";
|
||||
}
|
||||
|
||||
private List<MText> GetBendNotes()
|
||||
{
|
||||
return DxfDocument.MTexts
|
||||
@@ -124,8 +133,10 @@ namespace EtchBendLines
|
||||
|
||||
if (match.Success)
|
||||
{
|
||||
bendline.Radius = double.Parse(match.Groups["radius"].Value);
|
||||
bendline.Angle = double.Parse(match.Groups["angle"].Value);
|
||||
var radius = match.Groups["radius"].Value;
|
||||
var angle = match.Groups["angle"].Value;
|
||||
bendline.Radius = double.Parse(radius, CultureInfo.InvariantCulture);
|
||||
bendline.Angle = double.Parse(angle, CultureInfo.InvariantCulture);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,9 +8,10 @@
|
||||
<OutputType>Exe</OutputType>
|
||||
<RootNamespace>EtchBendLines</RootNamespace>
|
||||
<AssemblyName>EtchBendLines</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
|
||||
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
|
||||
<TargetFrameworkProfile />
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||
@@ -46,6 +47,7 @@
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="AppConfig.cs" />
|
||||
<Compile Include="Bend.cs" />
|
||||
<Compile Include="BendDirection.cs" />
|
||||
<Compile Include="BendLineExtractor.cs" />
|
||||
|
||||
@@ -1,90 +1,182 @@
|
||||
using netDxf;
|
||||
using netDxf.Entities;
|
||||
using netDxf.Tables;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
|
||||
namespace EtchBendLines
|
||||
{
|
||||
public class Etcher
|
||||
{
|
||||
public Layer BendLayer = new Layer("BEND")
|
||||
public readonly Layer BendLayer = new Layer("BEND")
|
||||
{
|
||||
Color = AciColor.Yellow
|
||||
};
|
||||
|
||||
public double EtchLength { get; set; } = 1.0;
|
||||
static readonly Layer EtchLayer = new Layer("ETCH")
|
||||
{
|
||||
Color = AciColor.Green,
|
||||
};
|
||||
|
||||
private const double DefaultEtchLength = 1.0;
|
||||
|
||||
/// <summary>
|
||||
/// Maximum bend radius to be considered. Anything beyond this number will be rolled.
|
||||
/// </summary>
|
||||
public double MaxBendRadius { get; set; } = 4.0;
|
||||
|
||||
public void AddEtchLines(string filePath)
|
||||
private DxfDocument LoadDocument(string path)
|
||||
{
|
||||
try
|
||||
{
|
||||
return DxfDocument.Load(path)
|
||||
?? throw new InvalidOperationException("DXF load returned null");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new ApplicationException($"Failed to load DXF '{path}'", ex);
|
||||
}
|
||||
}
|
||||
|
||||
private IEnumerable<Bend> ExtractUpBends(DxfDocument doc)
|
||||
{
|
||||
// your existing BendLineExtractor logic
|
||||
var extractor = new BendLineExtractor(doc);
|
||||
return extractor.GetBendLines()
|
||||
.Where(b => b.Direction == BendDirection.Up);
|
||||
}
|
||||
|
||||
private HashSet<string> BuildExistingKeySet(DxfDocument doc)
|
||||
=> new HashSet<string>(
|
||||
doc.Lines
|
||||
.Where(l => IsEtchLayer(l.Layer))
|
||||
.Select(l => KeyFor(l.StartPoint, l.EndPoint))
|
||||
);
|
||||
|
||||
private void InsertEtchLines(DxfDocument doc, IEnumerable<Bend> bends, HashSet<string> existingKeys, double etchLength)
|
||||
{
|
||||
foreach (var bend in bends)
|
||||
{
|
||||
foreach (var etch in GetEtchLines(bend.Line, etchLength))
|
||||
{
|
||||
var key = KeyFor(etch.StartPoint, etch.EndPoint);
|
||||
if (existingKeys.Contains(key))
|
||||
{
|
||||
// ensure correct layer
|
||||
var existing = doc.Lines.First(l => KeyFor(l) == key);
|
||||
existing.Layer = EtchLayer;
|
||||
}
|
||||
else
|
||||
{
|
||||
etch.Layer = EtchLayer;
|
||||
doc.AddEntity(etch);
|
||||
existingKeys.Add(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void SaveDocument(DxfDocument doc, string path)
|
||||
{
|
||||
doc.Save(path);
|
||||
Console.WriteLine($"→ Saved with etch lines: {path}");
|
||||
}
|
||||
|
||||
private static string KeyFor(Line l) => KeyFor(l.StartPoint, l.EndPoint);
|
||||
|
||||
private static string KeyFor(Vector3 a, Vector3 b) => $"{a.X:F3},{a.Y:F3}|{b.X:F3},{b.Y:F3}";
|
||||
|
||||
public void AddEtchLines(string filePath, double etchLength = DefaultEtchLength)
|
||||
{
|
||||
Console.WriteLine(filePath);
|
||||
|
||||
var bendLineExtractor = new BendLineExtractor(filePath);
|
||||
bendLineExtractor.MaxBendRadius = MaxBendRadius;
|
||||
var doc = LoadDocument(filePath);
|
||||
var upBends = ExtractUpBends(doc);
|
||||
var existing = BuildExistingKeySet(doc);
|
||||
|
||||
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);
|
||||
InsertEtchLines(doc, upBends, existing, etchLength);
|
||||
SaveDocument(doc, filePath);
|
||||
}
|
||||
|
||||
private bool IsEtchLayer(Layer layer)
|
||||
{
|
||||
if (layer.Name == "ETCH")
|
||||
if (layer == null)
|
||||
return false;
|
||||
|
||||
if (layer.Name.Equals(EtchLayer.Name, StringComparison.OrdinalIgnoreCase))
|
||||
return true;
|
||||
|
||||
if (layer.Name == "SCRIBE")
|
||||
return true;
|
||||
switch (layer.Name)
|
||||
{
|
||||
case "ETCH":
|
||||
case "SCRIBE":
|
||||
case "SCRIBE-TEXT":
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (layer.Name == "SCRIBE-TEXT")
|
||||
return true;
|
||||
private IEnumerable<Line> GetEtchLines(Line bendLine, double etchLength)
|
||||
{
|
||||
var lines = new List<Line>();
|
||||
|
||||
return false;
|
||||
var startPoint = new Vector2(bendLine.StartPoint.X, bendLine.StartPoint.Y);
|
||||
var endPoint = new Vector2(bendLine.EndPoint.X, bendLine.EndPoint.Y);
|
||||
var bendLength = startPoint.DistanceTo(endPoint);
|
||||
|
||||
if (bendLength < (etchLength * 3.0))
|
||||
{
|
||||
lines.Add(new Line(bendLine.StartPoint, bendLine.EndPoint));
|
||||
}
|
||||
else
|
||||
{
|
||||
var angle = startPoint.AngleTo(endPoint);
|
||||
|
||||
if (bendLine.IsVertical())
|
||||
{
|
||||
var x = bendLine.StartPoint.X;
|
||||
|
||||
var bottomY1 = Math.Min(startPoint.Y, endPoint.Y);
|
||||
var bottomY2 = bottomY1 + etchLength;
|
||||
|
||||
var topY1 = Math.Max(startPoint.Y, endPoint.Y);
|
||||
var topY2 = topY1 - etchLength;
|
||||
|
||||
var p1 = new Vector2(x, bottomY1);
|
||||
var p2 = new Vector2(x, bottomY2);
|
||||
var p3 = new Vector2(x, topY1);
|
||||
var p4 = new Vector2(x, topY2);
|
||||
|
||||
lines.Add(new Line(p1, p2));
|
||||
lines.Add(new Line(p3, p4));
|
||||
}
|
||||
else
|
||||
{
|
||||
var start = bendLine.StartPoint.ToVector2();
|
||||
var end = bendLine.EndPoint.ToVector2();
|
||||
|
||||
var dx = Math.Cos(angle) * etchLength;
|
||||
var dy = Math.Sin(angle) * etchLength;
|
||||
|
||||
var p1 = new Vector2(start.X, start.Y);
|
||||
var p2 = new Vector2(start.X + dx, start.Y + dy);
|
||||
var p3 = new Vector2(end.X, end.Y);
|
||||
var p4 = new Vector2(end.X - dx, end.Y - dy);
|
||||
|
||||
lines.Add(new Line(p1, p2));
|
||||
lines.Add(new Line(p3, p4));
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var line in lines)
|
||||
{
|
||||
line.Layer = EtchLayer;
|
||||
}
|
||||
|
||||
yield break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,36 +3,65 @@ using System.Collections.Generic;
|
||||
using System.Configuration;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace EtchBendLines
|
||||
{
|
||||
public class Program
|
||||
{
|
||||
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)
|
||||
try
|
||||
{
|
||||
Console.WriteLine($"No DXF files founds. Place DXF files in \"{AppDomain.CurrentDomain.BaseDirectory}\" and run this program again.");
|
||||
PressAnyKeyToExit();
|
||||
Run(args);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.ForegroundColor = ConsoleColor.Red;
|
||||
Console.WriteLine($"An error occurred: {ex.Message}");
|
||||
Console.ResetColor();
|
||||
}
|
||||
|
||||
PressAnyKeyToExit();
|
||||
}
|
||||
|
||||
static void Run(string[] args)
|
||||
{
|
||||
var etchLength = AppConfig.GetDouble("EtchLength");
|
||||
var maxRadius = AppConfig.GetDouble("MaxBendRadius");
|
||||
|
||||
var etcher = new Etcher
|
||||
{
|
||||
MaxBendRadius = maxRadius
|
||||
};
|
||||
|
||||
var files = GetDxfFiles(args);
|
||||
if (files.Count == 0)
|
||||
{
|
||||
Console.WriteLine($"No DXF files found. Place DXF files in \"{AppDomain.CurrentDomain.BaseDirectory}\" and run this program again.");
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var file in files)
|
||||
{
|
||||
etcher.AddEtchLines(file);
|
||||
etcher.AddEtchLines(file, etchLength);
|
||||
Console.WriteLine();
|
||||
}
|
||||
}
|
||||
|
||||
PressAnyKeyToExit();
|
||||
static List<string> GetDxfFiles(string[] args)
|
||||
{
|
||||
var paths = args.Length > 0 ? args.ToList() : new List<string> { AppDomain.CurrentDomain.BaseDirectory };
|
||||
var files = new List<string>();
|
||||
|
||||
foreach (var path in paths)
|
||||
{
|
||||
if (File.Exists(path))
|
||||
files.Add(path);
|
||||
else if (Directory.Exists(path))
|
||||
files.AddRange(Directory.GetFiles(path, "*.dxf", SearchOption.AllDirectories));
|
||||
}
|
||||
|
||||
return files;
|
||||
}
|
||||
|
||||
static void PressAnyKeyToExit()
|
||||
|
||||
Reference in New Issue
Block a user