Compare commits
18 Commits
f664bd79a4
...
da4d3228b0
| Author | SHA1 | Date | |
|---|---|---|---|
| da4d3228b0 | |||
| 3c1700c480 | |||
| f2f50f9914 | |||
| bf36a56387 | |||
| 6e131d402e | |||
| f04c75235c | |||
| 2e8f0e60c5 | |||
| b0e48442ca | |||
| 89d987f6c6 | |||
| 78ae737adb | |||
| 2391eb7050 | |||
| dd7443ddb8 | |||
| e5daf748c6 | |||
| 774012021c | |||
| 214cc94816 | |||
| 04031a7677 | |||
| 3e4ab60366 | |||
| 2b30498147 |
@@ -1,3 +0,0 @@
|
||||
[submodule "netDxf"]
|
||||
path = netDxf
|
||||
url = https://github.com/haplokuon/netDxf.git
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
<?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"/>
|
||||
<add key="EtchLength" value="1.0"/>
|
||||
</appSettings>
|
||||
</configuration>
|
||||
</configuration>
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
+2
-71
@@ -1,6 +1,4 @@
|
||||
using netDxf;
|
||||
using netDxf.Entities;
|
||||
using netDxf.Tables;
|
||||
using ACadSharp.Entities;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
@@ -53,71 +51,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 +68,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,21 +1,25 @@
|
||||
using netDxf;
|
||||
using netDxf.Entities;
|
||||
using ACadSharp;
|
||||
using ACadSharp.Entities;
|
||||
using ACadSharp.IO;
|
||||
using CSMath;
|
||||
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)
|
||||
{
|
||||
DxfDocument = DxfDocument.Load(dxfFile);
|
||||
using var reader = new DxfReader(dxfFile);
|
||||
Document = reader.Read();
|
||||
}
|
||||
|
||||
public BendLineExtractor(DxfDocument dxfDocument)
|
||||
public BendLineExtractor(CadDocument document)
|
||||
{
|
||||
DxfDocument = dxfDocument;
|
||||
Document = document;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -31,9 +35,12 @@ 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+)?)[^R\d]*R\s*(?<radius>\d+(\.\d+)?)\b",
|
||||
RegexOptions.Compiled | RegexOptions.IgnoreCase
|
||||
);
|
||||
|
||||
public DxfDocument DxfDocument { get; private set; }
|
||||
public CadDocument Document { get; private set; }
|
||||
|
||||
public List<Bend> GetBendLines()
|
||||
{
|
||||
@@ -43,9 +50,9 @@ namespace EtchBendLines
|
||||
if (ReplaceSharpRadius)
|
||||
FixSharpBends();
|
||||
|
||||
foreach (var line in DxfDocument.Lines)
|
||||
foreach (var line in Document.Entities.OfType<Line>())
|
||||
{
|
||||
if (line.Linetype.Name != "CENTERX2" && line.Layer.Name != "BEND")
|
||||
if (!IsBendLine(line))
|
||||
continue;
|
||||
|
||||
var bend = new Bend
|
||||
@@ -59,12 +66,29 @@ namespace EtchBendLines
|
||||
|
||||
AssignBendDirections(bends, bendNotes);
|
||||
|
||||
return bends.Where(b => b.Radius <= MaxBendRadius).ToList();
|
||||
return bends.Where(b => b.Radius == null || b.Radius <= MaxBendRadius).ToList();
|
||||
}
|
||||
|
||||
private bool IsBendLine(Line line)
|
||||
{
|
||||
if (line.LineType.Name != "CENTERX2")
|
||||
return false;
|
||||
|
||||
switch (line.Layer.Name.ToUpperInvariant())
|
||||
{
|
||||
case "0":
|
||||
case "BEND":
|
||||
case "BEND LINES":
|
||||
case "BENDLINES":
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private List<MText> GetBendNotes()
|
||||
{
|
||||
return DxfDocument.MTexts
|
||||
return Document.Entities.OfType<MText>()
|
||||
.Where(t => GetBendDirection(t) != BendDirection.Unknown)
|
||||
.ToList();
|
||||
}
|
||||
@@ -124,8 +148,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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -137,7 +163,7 @@ namespace EtchBendLines
|
||||
for (int i = bendNotesList.Count - 1; i >= 0; i--)
|
||||
{
|
||||
var note = bendNotesList[i];
|
||||
var notePos = note.Position.ToVector2();
|
||||
var notePos = note.InsertPoint.ToXY();
|
||||
var perpendicularPoint = bendLine.PointPerpendicularTo(notePos);
|
||||
var dist = notePos.DistanceTo(perpendicularPoint);
|
||||
var maxAcceptableDist = note.Height * 2.0;
|
||||
@@ -150,14 +176,14 @@ namespace EtchBendLines
|
||||
return null;
|
||||
|
||||
var closestNote = bendNotesList.First();
|
||||
var p1 = closestNote.Position.ToVector2();
|
||||
var p1 = closestNote.InsertPoint.ToXY();
|
||||
var p2 = bendLine.ClosestPointOnLineTo(p1);
|
||||
var dist2 = p1.DistanceTo(p2);
|
||||
|
||||
for (int i = 1; i < bendNotesList.Count; i++)
|
||||
{
|
||||
var note = bendNotesList[i];
|
||||
var p3 = note.Position.ToVector2();
|
||||
var p3 = note.InsertPoint.ToXY();
|
||||
var p4 = bendLine.ClosestPointOnLineTo(p3);
|
||||
var dist = p3.DistanceTo(p4);
|
||||
|
||||
|
||||
@@ -1,67 +1,18 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<ProjectGuid>{229C2FB9-6AD6-4A5D-B83A-D1146573D6F9}</ProjectGuid>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0-windows</TargetFramework>
|
||||
<OutputType>Library</OutputType>
|
||||
<ImplicitUsings>disable</ImplicitUsings>
|
||||
<Nullable>disable</Nullable>
|
||||
<RootNamespace>EtchBendLines</RootNamespace>
|
||||
<AssemblyName>EtchBendLines</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>bin\Debug\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup>
|
||||
<NoWin32Manifest>true</NoWin32Manifest>
|
||||
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Configuration" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
<Reference Include="System.Data.DataSetExtensions" />
|
||||
<Reference Include="Microsoft.CSharp" />
|
||||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.Net.Http" />
|
||||
<Reference Include="System.Xml" />
|
||||
<PackageReference Include="ACadSharp" Version="3.4.9" />
|
||||
<PackageReference Include="System.Configuration.ConfigurationManager" Version="8.0.0" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<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" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="App.config" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\netDxf\netDxf\netDxf.csproj">
|
||||
<Project>{785380e0-ceb9-4c34-82e5-60d0e33e848e}</Project>
|
||||
<Name>netDxf</Name>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
</Project>
|
||||
|
||||
</Project>
|
||||
|
||||
+163
-57
@@ -1,90 +1,196 @@
|
||||
using netDxf;
|
||||
using netDxf.Tables;
|
||||
using ACadSharp;
|
||||
using ACadSharp.Entities;
|
||||
using ACadSharp.Tables;
|
||||
using ACadSharp.IO;
|
||||
using CSMath;
|
||||
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
|
||||
Color = Color.Yellow
|
||||
};
|
||||
|
||||
public double EtchLength { get; set; } = 1.0;
|
||||
static readonly Layer EtchLayer = new Layer("ETCH")
|
||||
{
|
||||
Color = Color.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 CadDocument LoadDocument(string path)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var reader = new DxfReader(path);
|
||||
reader.Configuration.CreateDefaults = true;
|
||||
return reader.Read()
|
||||
?? throw new InvalidOperationException("DXF load returned null");
|
||||
}
|
||||
catch (Exception ex) when (ex is not InvalidOperationException)
|
||||
{
|
||||
throw new ApplicationException($"Failed to load DXF '{path}'", ex);
|
||||
}
|
||||
}
|
||||
|
||||
private List<Bend> ExtractBends(CadDocument doc)
|
||||
{
|
||||
var extractor = new BendLineExtractor(doc);
|
||||
return extractor.GetBendLines();
|
||||
}
|
||||
|
||||
private HashSet<string> BuildExistingKeySet(CadDocument doc)
|
||||
=> new HashSet<string>(
|
||||
doc.Entities.OfType<Line>()
|
||||
.Where(l => IsEtchLayer(l.Layer))
|
||||
.Select(l => KeyFor(l.StartPoint, l.EndPoint))
|
||||
);
|
||||
|
||||
private void InsertEtchLines(CadDocument 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.Entities.OfType<Line>().First(l => KeyFor(l) == key);
|
||||
existing.Layer = EtchLayer;
|
||||
}
|
||||
else
|
||||
{
|
||||
etch.Layer = EtchLayer;
|
||||
doc.Entities.Add(etch);
|
||||
existingKeys.Add(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void SaveDocument(CadDocument doc, string path)
|
||||
{
|
||||
using (var writer = new DxfWriter(path, doc, false))
|
||||
{
|
||||
writer.Write();
|
||||
}
|
||||
Console.WriteLine($"→ Saved with etch lines: {path}");
|
||||
}
|
||||
|
||||
private static string KeyFor(Line l) => KeyFor(l.StartPoint, l.EndPoint);
|
||||
|
||||
private static string KeyFor(XYZ a, XYZ 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 bends = ExtractBends(doc);
|
||||
|
||||
var bendLines = bendLineExtractor.GetBendLines();
|
||||
|
||||
if (bendLines.Count == 0)
|
||||
// Ensure all bend lines are on the BEND layer with ByLayer color
|
||||
foreach (var bend in bends)
|
||||
{
|
||||
Console.WriteLine("No bend lines found.");
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"Found {bendLines.Count} bend lines.");
|
||||
bend.Line.Layer = BendLayer;
|
||||
bend.Line.Color = Color.ByLayer;
|
||||
}
|
||||
|
||||
foreach (var bendLine in bendLines)
|
||||
{
|
||||
bendLine.Line.Layer = BendLayer;
|
||||
bendLine.Line.Color = AciColor.ByLayer;
|
||||
bendLine.BendNote.Layer = BendLayer;
|
||||
}
|
||||
var upBends = bends.Where(b => b.Direction == BendDirection.Up);
|
||||
var existing = BuildExistingKeySet(doc);
|
||||
|
||||
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 List<Line> GetEtchLines(Line bendLine, double etchLength)
|
||||
{
|
||||
var lines = new List<Line>();
|
||||
|
||||
return false;
|
||||
var startPoint = new XY(bendLine.StartPoint.X, bendLine.StartPoint.Y);
|
||||
var endPoint = new XY(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 XYZ(x, bottomY1, 0);
|
||||
var p2 = new XYZ(x, bottomY2, 0);
|
||||
var p3 = new XYZ(x, topY1, 0);
|
||||
var p4 = new XYZ(x, topY2, 0);
|
||||
|
||||
lines.Add(new Line(p1, p2));
|
||||
lines.Add(new Line(p3, p4));
|
||||
}
|
||||
else
|
||||
{
|
||||
var start = bendLine.StartPoint.ToXY();
|
||||
var end = bendLine.EndPoint.ToXY();
|
||||
|
||||
var dx = Math.Cos(angle) * etchLength;
|
||||
var dy = Math.Sin(angle) * etchLength;
|
||||
|
||||
var p1 = new XYZ(start.X, start.Y, 0);
|
||||
var p2 = new XYZ(start.X + dx, start.Y + dy, 0);
|
||||
var p3 = new XYZ(end.X, end.Y, 0);
|
||||
var p4 = new XYZ(end.X - dx, end.Y - dy, 0);
|
||||
|
||||
lines.Add(new Line(p1, p2));
|
||||
lines.Add(new Line(p3, p4));
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var line in lines)
|
||||
{
|
||||
line.Layer = EtchLayer;
|
||||
}
|
||||
|
||||
return lines;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+17
-17
@@ -1,5 +1,5 @@
|
||||
using netDxf;
|
||||
using netDxf.Entities;
|
||||
using ACadSharp.Entities;
|
||||
using CSMath;
|
||||
using System;
|
||||
|
||||
namespace EtchBendLines
|
||||
@@ -8,12 +8,12 @@ namespace EtchBendLines
|
||||
{
|
||||
const double TwoPI = Math.PI * 2.0;
|
||||
|
||||
public static Vector2 ToVector2(this Vector3 pt)
|
||||
public static XY ToXY(this XYZ pt)
|
||||
{
|
||||
return new Vector2(pt.X, pt.Y);
|
||||
return new XY(pt.X, pt.Y);
|
||||
}
|
||||
|
||||
public static bool IsEqualTo(this Vector3 pt, Vector3 pt1)
|
||||
public static bool IsEqualTo(this XYZ pt, XYZ pt1)
|
||||
{
|
||||
return pt.X.IsEqualTo(pt1.X) && pt.Y.IsEqualTo(pt1.Y) && pt.Z.IsEqualTo(pt1.Z);
|
||||
}
|
||||
@@ -50,10 +50,10 @@ namespace EtchBendLines
|
||||
return Math.Round(p1.Y - slope * p1.X, 4);
|
||||
}
|
||||
|
||||
public static Vector2 PointPerpendicularTo(this Line line, Vector2 pt)
|
||||
public static XY PointPerpendicularTo(this Line line, XY pt)
|
||||
{
|
||||
var startPoint = line.StartPoint.ToVector2();
|
||||
var endPoint = line.EndPoint.ToVector2();
|
||||
var startPoint = line.StartPoint.ToXY();
|
||||
var endPoint = line.EndPoint.ToXY();
|
||||
|
||||
var d1 = pt - startPoint;
|
||||
var d2 = endPoint - startPoint;
|
||||
@@ -61,20 +61,20 @@ namespace EtchBendLines
|
||||
var lengthSquared = d2.X * d2.X + d2.Y * d2.Y;
|
||||
var param = dotProduct / lengthSquared;
|
||||
|
||||
return new Vector2(
|
||||
return new XY(
|
||||
startPoint.X + param * d2.X,
|
||||
startPoint.Y + param * d2.Y);
|
||||
}
|
||||
|
||||
public static Vector2 MidPoint(this Line line)
|
||||
public static XY MidPoint(this Line line)
|
||||
{
|
||||
var x = (line.StartPoint.X + line.EndPoint.X) * 0.5;
|
||||
var y = (line.StartPoint.Y + line.EndPoint.Y) * 0.5;
|
||||
|
||||
return new Vector2(x, y);
|
||||
return new XY(x, y);
|
||||
}
|
||||
|
||||
public static double DistanceTo(this Vector2 startPoint, Vector2 endPoint)
|
||||
public static double DistanceTo(this XY startPoint, XY endPoint)
|
||||
{
|
||||
var x = endPoint.X - startPoint.X;
|
||||
var y = endPoint.Y - startPoint.Y;
|
||||
@@ -82,7 +82,7 @@ namespace EtchBendLines
|
||||
return Math.Sqrt(x * x + y * y);
|
||||
}
|
||||
|
||||
public static double AngleTo(this Vector2 startPoint, Vector2 endPoint)
|
||||
public static double AngleTo(this XY startPoint, XY endPoint)
|
||||
{
|
||||
var x = endPoint.X - startPoint.X;
|
||||
var y = endPoint.Y - startPoint.Y;
|
||||
@@ -133,10 +133,10 @@ namespace EtchBendLines
|
||||
return Math.Abs(b - a) <= tolerance;
|
||||
}
|
||||
|
||||
public static Vector2 ClosestPointOnLineTo(this Line line, Vector2 pt)
|
||||
public static XY ClosestPointOnLineTo(this Line line, XY pt)
|
||||
{
|
||||
var startPoint = line.StartPoint.ToVector2();
|
||||
var endPoint = line.EndPoint.ToVector2();
|
||||
var startPoint = line.StartPoint.ToXY();
|
||||
var endPoint = line.EndPoint.ToXY();
|
||||
|
||||
var diff1 = pt - startPoint;
|
||||
var diff2 = endPoint - startPoint;
|
||||
@@ -150,7 +150,7 @@ namespace EtchBendLines
|
||||
return endPoint;
|
||||
else
|
||||
{
|
||||
return new Vector2(
|
||||
return new XY(
|
||||
startPoint.X + param * diff2.X,
|
||||
startPoint.Y + param * diff2.Y);
|
||||
}
|
||||
|
||||
+43
-14
@@ -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()
|
||||
|
||||
-1
Submodule netDxf deleted from 67789bfe70
Reference in New Issue
Block a user