Initial commit
This commit is contained in:
226
PepLib.Dxf/DxfConverter.cs
Normal file
226
PepLib.Dxf/DxfConverter.cs
Normal file
@@ -0,0 +1,226 @@
|
||||
using PepLib.Codes;
|
||||
using netDxf;
|
||||
using netDxf.Entities;
|
||||
using netDxf.Tables;
|
||||
using System;
|
||||
using PepProgram = PepLib.Program;
|
||||
|
||||
namespace PepLib.Dxf
|
||||
{
|
||||
public class DxfConverter
|
||||
{
|
||||
private const double RadToDeg = 180.0 / Math.PI;
|
||||
|
||||
private DxfDocument doc;
|
||||
private Vector2 curpos;
|
||||
private ProgrammingMode mode;
|
||||
private readonly Layer cutLayer;
|
||||
private readonly Layer rapidLayer;
|
||||
private readonly Layer plateLayer;
|
||||
|
||||
public DxfConverter()
|
||||
{
|
||||
doc = new DxfDocument();
|
||||
|
||||
cutLayer = new Layer("Cut");
|
||||
cutLayer.Color = AciColor.Red;
|
||||
|
||||
rapidLayer = new Layer("Rapid");
|
||||
rapidLayer.Color = AciColor.Blue;
|
||||
rapidLayer.LineType = LineType.Dashed;
|
||||
|
||||
plateLayer = new Layer("Plate");
|
||||
plateLayer.Color = AciColor.Cyan;
|
||||
}
|
||||
|
||||
public DxfDocument ConvertLoop(Loop loop)
|
||||
{
|
||||
doc = new DxfDocument();
|
||||
AddProgram(loop);
|
||||
return doc;
|
||||
}
|
||||
|
||||
public DxfDocument ConvertPlate(Plate plate)
|
||||
{
|
||||
doc = new DxfDocument();
|
||||
AddPlateOutline(plate);
|
||||
|
||||
foreach (var part in plate.Parts)
|
||||
{
|
||||
curpos = part.Location.ToDxfVector();
|
||||
AddProgram(part.Program);
|
||||
}
|
||||
|
||||
return doc;
|
||||
}
|
||||
|
||||
public DxfDocument ConvertProgram(PepProgram program)
|
||||
{
|
||||
doc = new DxfDocument();
|
||||
AddProgram(program);
|
||||
return doc;
|
||||
}
|
||||
|
||||
private void AddPlateOutline(Plate plate)
|
||||
{
|
||||
Vector2 pt1;
|
||||
Vector2 pt2;
|
||||
Vector2 pt3;
|
||||
Vector2 pt4;
|
||||
|
||||
switch (plate.Quadrant)
|
||||
{
|
||||
case 1:
|
||||
pt1 = new Vector2(0, 0);
|
||||
pt2 = new Vector2(0, plate.Size.Height);
|
||||
pt3 = new Vector2(plate.Size.Width, plate.Size.Height);
|
||||
pt4 = new Vector2(plate.Size.Width, 0);
|
||||
break;
|
||||
|
||||
case 2:
|
||||
pt1 = new Vector2(0, 0);
|
||||
pt2 = new Vector2(0, plate.Size.Height);
|
||||
pt3 = new Vector2(-plate.Size.Width, plate.Size.Height);
|
||||
pt4 = new Vector2(-plate.Size.Width, 0);
|
||||
break;
|
||||
|
||||
case 3:
|
||||
pt1 = new Vector2(0, 0);
|
||||
pt2 = new Vector2(0, -plate.Size.Height);
|
||||
pt3 = new Vector2(-plate.Size.Width, -plate.Size.Height);
|
||||
pt4 = new Vector2(-plate.Size.Width, 0);
|
||||
break;
|
||||
|
||||
case 4:
|
||||
pt1 = new Vector2(0, 0);
|
||||
pt2 = new Vector2(0, -plate.Size.Height);
|
||||
pt3 = new Vector2(plate.Size.Width, -plate.Size.Height);
|
||||
pt4 = new Vector2(plate.Size.Width, 0);
|
||||
break;
|
||||
|
||||
default:
|
||||
return;
|
||||
}
|
||||
|
||||
doc.AddEntity(new Line(pt1, pt2) { Layer = plateLayer });
|
||||
doc.AddEntity(new Line(pt2, pt3) { Layer = plateLayer });
|
||||
doc.AddEntity(new Line(pt3, pt4) { Layer = plateLayer });
|
||||
doc.AddEntity(new Line(pt4, pt1) { Layer = plateLayer });
|
||||
}
|
||||
|
||||
private void AddProgram(PepProgram program)
|
||||
{
|
||||
mode = program.Mode;
|
||||
|
||||
foreach (var code in program)
|
||||
{
|
||||
switch (code.CodeType())
|
||||
{
|
||||
case CodeType.CircularMove:
|
||||
var arc = (CircularMove)code;
|
||||
AddCircularMove(arc);
|
||||
break;
|
||||
|
||||
case CodeType.LinearMove:
|
||||
var line = (LinearMove)code;
|
||||
AddLinearMove(line);
|
||||
break;
|
||||
|
||||
case CodeType.RapidMove:
|
||||
var rapid = (RapidMove)code;
|
||||
AddRapidMove(rapid);
|
||||
break;
|
||||
|
||||
case CodeType.SubProgramCall:
|
||||
var tmpmode = mode;
|
||||
var subpgm = (PepLib.Codes.SubProgramCall)code;
|
||||
AddProgram(subpgm.Loop);
|
||||
mode = tmpmode;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void AddLinearMove(LinearMove line)
|
||||
{
|
||||
var pt = line.EndPoint.ToDxfVector();
|
||||
|
||||
if (mode == ProgrammingMode.Incremental)
|
||||
pt += curpos;
|
||||
|
||||
var ln = new Line(curpos, pt);
|
||||
ln.Layer = cutLayer;
|
||||
doc.AddEntity(ln);
|
||||
curpos = pt;
|
||||
}
|
||||
|
||||
private void AddRapidMove(RapidMove rapid)
|
||||
{
|
||||
var pt = rapid.EndPoint.ToDxfVector();
|
||||
|
||||
if (mode == ProgrammingMode.Incremental)
|
||||
pt += curpos;
|
||||
|
||||
var ln = new Line(curpos, pt);
|
||||
ln.Layer = rapidLayer;
|
||||
doc.AddEntity(ln);
|
||||
curpos = pt;
|
||||
}
|
||||
|
||||
private void AddCircularMove(CircularMove arc)
|
||||
{
|
||||
var center = arc.CenterPoint.ToDxfVector();
|
||||
var endpt = arc.EndPoint.ToDxfVector();
|
||||
|
||||
if (mode == ProgrammingMode.Incremental)
|
||||
{
|
||||
endpt += curpos;
|
||||
center += curpos;
|
||||
}
|
||||
|
||||
// start angle in radians
|
||||
var startAngle = Math.Atan2(
|
||||
curpos.Y - center.Y,
|
||||
curpos.X - center.X);
|
||||
|
||||
// end angle in radians
|
||||
var endAngle = Math.Atan2(
|
||||
endpt.Y - center.Y,
|
||||
endpt.X - center.X);
|
||||
|
||||
// convert the angles to degrees
|
||||
startAngle *= RadToDeg;
|
||||
endAngle *= RadToDeg;
|
||||
|
||||
if (arc.Rotation == PepLib.RotationType.CW)
|
||||
Swap(ref startAngle, ref endAngle);
|
||||
|
||||
var dx = endpt.X - center.X;
|
||||
var dy = endpt.Y - center.Y;
|
||||
|
||||
var radius = Math.Sqrt(dx * dx + dy * dy);
|
||||
|
||||
if (startAngle.IsEqualTo(endAngle))
|
||||
{
|
||||
var circle = new Circle(center, radius);
|
||||
circle.Layer = cutLayer;
|
||||
doc.AddEntity(circle);
|
||||
}
|
||||
else
|
||||
{
|
||||
var arc2 = new Arc(center, radius, startAngle, endAngle);
|
||||
arc2.Layer = cutLayer;
|
||||
doc.AddEntity(arc2);
|
||||
}
|
||||
|
||||
curpos = endpt;
|
||||
}
|
||||
|
||||
private static void Swap<T>(ref T a, ref T b)
|
||||
{
|
||||
T c = a;
|
||||
a = b;
|
||||
b = c;
|
||||
}
|
||||
}
|
||||
}
|
||||
13
PepLib.Dxf/DxfExtensions.cs
Normal file
13
PepLib.Dxf/DxfExtensions.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using DxfVector = netDxf.Vector2;
|
||||
using PepVector = PepLib.Vector;
|
||||
|
||||
namespace PepLib.Dxf
|
||||
{
|
||||
internal static class DxfExtensions
|
||||
{
|
||||
public static PepVector ToPepVector(this DxfVector v)
|
||||
{
|
||||
return new PepVector(v.X, v.Y);
|
||||
}
|
||||
}
|
||||
}
|
||||
13
PepLib.Dxf/PepExtensions.cs
Normal file
13
PepLib.Dxf/PepExtensions.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using DxfVector = netDxf.Vector2;
|
||||
using PepVector = PepLib.Vector;
|
||||
|
||||
namespace PepLib.Dxf
|
||||
{
|
||||
internal static class PepExtensions
|
||||
{
|
||||
public static DxfVector ToDxfVector(this PepVector v)
|
||||
{
|
||||
return new DxfVector(v.X, v.Y);
|
||||
}
|
||||
}
|
||||
}
|
||||
59
PepLib.Dxf/PepLib.Dxf.csproj
Normal file
59
PepLib.Dxf/PepLib.Dxf.csproj
Normal file
@@ -0,0 +1,59 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<ProjectGuid>{E15A7403-B16D-4D11-A061-2F5E98DF36B0}</ProjectGuid>
|
||||
<OutputType>Library</OutputType>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>PepLib.Dxf</RootNamespace>
|
||||
<AssemblyName>PepLib.Dxf</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<TargetFrameworkProfile />
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<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' ">
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="netDxf">
|
||||
<HintPath>..\..\lib\netDxf.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="DxfExtensions.cs" />
|
||||
<Compile Include="DxfConverter.cs" />
|
||||
<Compile Include="PepExtensions.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\PepLib\PepLib.csproj">
|
||||
<Project>{22360453-B878-49FA-A5DC-0D9C577DE902}</Project>
|
||||
<Name>PepLib</Name>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||
Other similar extension points exist, see Microsoft.Common.targets.
|
||||
<Target Name="BeforeBuild">
|
||||
</Target>
|
||||
<Target Name="AfterBuild">
|
||||
</Target>
|
||||
-->
|
||||
</Project>
|
||||
36
PepLib.Dxf/Properties/AssemblyInfo.cs
Normal file
36
PepLib.Dxf/Properties/AssemblyInfo.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
// General Information about an assembly is controlled through the following
|
||||
// set of attributes. Change these attribute values to modify the information
|
||||
// associated with an assembly.
|
||||
[assembly: AssemblyTitle("PepLib.Dxf")]
|
||||
[assembly: AssemblyDescription("")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("")]
|
||||
[assembly: AssemblyProduct("PepLib.Dxf")]
|
||||
[assembly: AssemblyCopyright("Copyright © AJ Isaacs 2014")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
|
||||
// Setting ComVisible to false makes the types in this assembly not visible
|
||||
// to COM components. If you need to access a type in this assembly from
|
||||
// COM, set the ComVisible attribute to true on that type.
|
||||
[assembly: ComVisible(false)]
|
||||
|
||||
// The following GUID is for the ID of the typelib if this project is exposed to COM
|
||||
[assembly: Guid("5f159892-11c1-46f4-94be-ae9a7e81c1bc")]
|
||||
|
||||
// Version information for an assembly consists of the following four values:
|
||||
//
|
||||
// Major Version
|
||||
// Minor Version
|
||||
// Build Number
|
||||
// Revision
|
||||
//
|
||||
// You can specify all the values or you can default the Build and Revision Numbers
|
||||
// by using the '*' as shown below:
|
||||
// [assembly: AssemblyVersion("1.0.*")]
|
||||
[assembly: AssemblyVersion("1.0.0.0")]
|
||||
[assembly: AssemblyFileVersion("1.0.0.0")]
|
||||
BIN
PepLib.Dxf/lib/netDxf.dll
Normal file
BIN
PepLib.Dxf/lib/netDxf.dll
Normal file
Binary file not shown.
Reference in New Issue
Block a user