refactor(posts): move post-processor projects under Posts/
Group the Cincinnati and GravographIS plugin projects in a Posts/ folder so new machine posts have one home. Project names, namespaces, and the runtime Posts/ deploy target are unchanged; only relative paths in the solution and project references move.
This commit is contained in:
@@ -0,0 +1,108 @@
|
||||
using System;
|
||||
using System.IO.Ports;
|
||||
using System.Threading;
|
||||
|
||||
namespace OpenNest.Posts.GravographIS
|
||||
{
|
||||
/// <summary>
|
||||
/// Serial streamer for the Gravograph IS8000. 9600 8-N-1; flow control is
|
||||
/// configurable and defaults to RTS/CTS (the controller is buffered and drops
|
||||
/// CTS to apply backpressure). The job is sent in modest chunks rather than as
|
||||
/// one giant write so the handshake can pause the write mid-stream.
|
||||
/// </summary>
|
||||
public sealed class GravographISPort : IDisposable
|
||||
{
|
||||
private SerialPort port;
|
||||
|
||||
public const int DefaultBaudRate = 9600;
|
||||
public const int DefaultChunkSize = 256;
|
||||
public const int DefaultWriteTimeoutMs = 30000;
|
||||
|
||||
public int ChunkSize { get; set; } = DefaultChunkSize;
|
||||
public int WriteTimeoutMs { get; set; } = DefaultWriteTimeoutMs;
|
||||
|
||||
public bool IsOpen => port != null && port.IsOpen;
|
||||
|
||||
/// <summary>
|
||||
/// Opens the port at the controller's required line settings (9600 8-N-1)
|
||||
/// with the given <paramref name="handshake"/>. Throws if the port is
|
||||
/// already open or if opening fails.
|
||||
/// </summary>
|
||||
public void Open(string portName, Handshake handshake = Handshake.RequestToSend)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(portName))
|
||||
throw new ArgumentException("Port name is required.", nameof(portName));
|
||||
if (port != null)
|
||||
throw new InvalidOperationException("Port is already open.");
|
||||
|
||||
port = new SerialPort(portName, DefaultBaudRate, Parity.None, 8, StopBits.One)
|
||||
{
|
||||
Handshake = handshake,
|
||||
WriteTimeout = WriteTimeoutMs,
|
||||
ReadTimeout = WriteTimeoutMs,
|
||||
// DTR/RTS are needed for some USB-serial bridges and for RTS/CTS flow:
|
||||
DtrEnable = true,
|
||||
RtsEnable =
|
||||
handshake != Handshake.RequestToSend
|
||||
&& handshake != Handshake.RequestToSendXOnXOff,
|
||||
};
|
||||
|
||||
port.Open();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Streams the encoded job to the port in chunks. Cancellable. The chunked
|
||||
/// write is intentional — Write() blocks until the OS accepts the bytes,
|
||||
/// which with RTS/CTS or XOn/XOff yields cleanly when the controller's
|
||||
/// buffer is full.
|
||||
/// </summary>
|
||||
public void StreamJob(byte[] data, CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (data == null)
|
||||
throw new ArgumentNullException(nameof(data));
|
||||
if (port == null || !port.IsOpen)
|
||||
throw new InvalidOperationException("Port is not open.");
|
||||
|
||||
var chunk = ChunkSize > 0 ? ChunkSize : DefaultChunkSize;
|
||||
var offset = 0;
|
||||
|
||||
while (offset < data.Length)
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
var count = System.Math.Min(chunk, data.Length - offset);
|
||||
port.Write(data, offset, count);
|
||||
offset += count;
|
||||
}
|
||||
|
||||
// Block until the OS has handed the last bytes to the line. SerialPort
|
||||
// doesn't expose flush-and-drain directly; BaseStream.Flush is a no-op
|
||||
// on Windows, so this is best-effort.
|
||||
try
|
||||
{
|
||||
port.BaseStream.Flush();
|
||||
}
|
||||
catch
|
||||
{ /* ignored — Flush is advisory on SerialPort */
|
||||
}
|
||||
}
|
||||
|
||||
public void Close()
|
||||
{
|
||||
if (port == null)
|
||||
return;
|
||||
try
|
||||
{
|
||||
if (port.IsOpen)
|
||||
port.Close();
|
||||
}
|
||||
finally
|
||||
{
|
||||
port.Dispose();
|
||||
port = null;
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose() => Close();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
using System.ComponentModel;
|
||||
using OpenNest.CNC;
|
||||
|
||||
namespace OpenNest.Posts.GravographIS
|
||||
{
|
||||
/// <summary>
|
||||
/// Cut parameters for one kind of pass (engrave or cut). Edited in the post
|
||||
/// configuration PropertyGrid and persisted to JSON.
|
||||
/// </summary>
|
||||
[TypeConverter(typeof(ExpandableObjectConverter))]
|
||||
public sealed class LayerCutConfig
|
||||
{
|
||||
[DisplayName("Feed (mm/sec)")]
|
||||
[Description("XY and Z feed for this pass. Patches the VS and VZ wire commands.")]
|
||||
public int FeedMmPerSec { get; set; } = 10;
|
||||
|
||||
[DisplayName("Depth (inches)")]
|
||||
[Description(
|
||||
"Programmed Z plunge (DZ). Note: the spring-floated spindle means this does not set actual cut depth — tool protrusion does."
|
||||
)]
|
||||
public double Depth { get; set; } = 0.25;
|
||||
|
||||
[DisplayName("Pause Before")]
|
||||
[Description(
|
||||
"Stop the spindle and prompt the operator before this pass begins, so the tool can be swapped/adjusted."
|
||||
)]
|
||||
public bool PauseBefore { get; set; }
|
||||
|
||||
[DisplayName("Pause Message")]
|
||||
[Description("Message shown on the controller during the pause.")]
|
||||
public string PauseMessage { get; set; } = "";
|
||||
|
||||
public override string ToString() =>
|
||||
$"{FeedMmPerSec} mm/s, {Depth:0.###}\"" + (PauseBefore ? ", pause" : "");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Configuration for the Gravograph IS post processor: one <see cref="LayerCutConfig"/>
|
||||
/// per cut kind. The engrave block applies to <see cref="LayerType.Scribe"/> paths,
|
||||
/// the cut block to <see cref="LayerType.Cut"/>/<see cref="LayerType.Leadin"/>/<see cref="LayerType.Leadout"/>.
|
||||
/// The cut block carries the tool-change pause by default.
|
||||
/// </summary>
|
||||
public sealed class GravographISPostConfig
|
||||
{
|
||||
[Category("Engrave (Scribe)")]
|
||||
[DisplayName("Engrave")]
|
||||
[Description("Parameters for engrave/scribe geometry (text).")]
|
||||
public LayerCutConfig Engrave { get; set; } =
|
||||
new LayerCutConfig
|
||||
{
|
||||
FeedMmPerSec = 10,
|
||||
Depth = 0.25,
|
||||
PauseBefore = false,
|
||||
PauseMessage = "",
|
||||
};
|
||||
|
||||
[Category("Cut")]
|
||||
[DisplayName("Cut")]
|
||||
[Description(
|
||||
"Parameters for cut geometry (outlines). Pauses for a tool change by default."
|
||||
)]
|
||||
public LayerCutConfig Cut { get; set; } =
|
||||
new LayerCutConfig
|
||||
{
|
||||
FeedMmPerSec = 3,
|
||||
Depth = 0.25,
|
||||
PauseBefore = true,
|
||||
PauseMessage = "Change tool",
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Returns the cut config a polyline of the given layer should use, or null
|
||||
/// if the layer is non-cutting (<see cref="LayerType.Display"/>) and should be skipped.
|
||||
/// </summary>
|
||||
public LayerCutConfig ConfigFor(LayerType layer)
|
||||
{
|
||||
switch (layer)
|
||||
{
|
||||
case LayerType.Scribe:
|
||||
return Engrave;
|
||||
case LayerType.Cut:
|
||||
case LayerType.Leadin:
|
||||
case LayerType.Leadout:
|
||||
return Cut;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,169 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.IO.Ports;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Threading;
|
||||
using OpenNest.Geometry;
|
||||
|
||||
namespace OpenNest.Posts.GravographIS
|
||||
{
|
||||
/// <summary>
|
||||
/// IPostProcessor implementation for the Gravograph IS8000. <see cref="Post(Nest, Stream)"/>
|
||||
/// writes the binary HPGL bytes. For serial streaming, use <see cref="Stream(Nest, string, Handshake, CancellationToken)"/>.
|
||||
///
|
||||
/// Geometry is split by <see cref="OpenNest.CNC.LayerType"/> into an engrave pass
|
||||
/// (Scribe) and a cut pass (Cut), each with its own feed/depth from <see cref="Config"/>.
|
||||
/// The cut pass pauses by default so the operator can swap/adjust the tool.
|
||||
/// </summary>
|
||||
public sealed class GravographISPostProcessor : IConfigurablePostProcessor
|
||||
{
|
||||
private static readonly JsonSerializerOptions JsonOptions = new()
|
||||
{
|
||||
WriteIndented = true,
|
||||
Converters = { new JsonStringEnumConverter() },
|
||||
};
|
||||
|
||||
public string Name => "Gravograph IS8000";
|
||||
public string Author => "OpenNest";
|
||||
public string Description =>
|
||||
"Gravograph IS8000 mechanical engraver (binary HPGL over serial)";
|
||||
|
||||
public GravographISWriterOptions WriterOptions { get; } = new GravographISWriterOptions();
|
||||
|
||||
public NestPolylineExtractor Extractor { get; } = new NestPolylineExtractor();
|
||||
|
||||
public double StitchTolerance { get; set; } = PolylinePrePass.DefaultStitchTolerance;
|
||||
|
||||
public bool AllowReverse { get; set; } = true;
|
||||
|
||||
public GravographISPostConfig Config { get; }
|
||||
|
||||
object IConfigurablePostProcessor.Config => Config;
|
||||
|
||||
public GravographISPostProcessor()
|
||||
{
|
||||
var configPath = GetConfigPath();
|
||||
if (File.Exists(configPath))
|
||||
{
|
||||
var json = File.ReadAllText(configPath);
|
||||
Config =
|
||||
JsonSerializer.Deserialize<GravographISPostConfig>(json, JsonOptions)
|
||||
?? new GravographISPostConfig();
|
||||
}
|
||||
else
|
||||
{
|
||||
Config = new GravographISPostConfig();
|
||||
SaveConfig();
|
||||
}
|
||||
}
|
||||
|
||||
public GravographISPostProcessor(GravographISPostConfig config)
|
||||
{
|
||||
Config = config ?? throw new ArgumentNullException(nameof(config));
|
||||
}
|
||||
|
||||
public void SaveConfig()
|
||||
{
|
||||
var configPath = GetConfigPath();
|
||||
var json = JsonSerializer.Serialize(Config, JsonOptions);
|
||||
File.WriteAllText(configPath, json);
|
||||
}
|
||||
|
||||
private static string GetConfigPath()
|
||||
{
|
||||
var assemblyPath = typeof(GravographISPostProcessor).Assembly.Location;
|
||||
var dir = Path.GetDirectoryName(assemblyPath);
|
||||
var name = Path.GetFileNameWithoutExtension(assemblyPath);
|
||||
return Path.Combine(dir, name + ".json");
|
||||
}
|
||||
|
||||
public void Post(Nest nest, Stream outputStream)
|
||||
{
|
||||
if (nest == null)
|
||||
throw new ArgumentNullException(nameof(nest));
|
||||
if (outputStream == null)
|
||||
throw new ArgumentNullException(nameof(outputStream));
|
||||
|
||||
var passes = BuildPasses(Extractor.ExtractLayered(nest));
|
||||
new GravographISWriter(WriterOptions).Write(passes, outputStream);
|
||||
}
|
||||
|
||||
public void Post(Nest nest, string outputFile)
|
||||
{
|
||||
using var fs = new FileStream(outputFile, FileMode.Create, FileAccess.Write);
|
||||
Post(nest, fs);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Groups layer-tagged polylines into ordered tool passes: engrave (Scribe)
|
||||
/// first, then cut. Each group is stitch/reverse-optimized independently.
|
||||
/// Geometry whose layer maps to no config (Display) is skipped. When only one
|
||||
/// group is present, a single pass is returned (and so the writer emits no pause).
|
||||
/// </summary>
|
||||
public IReadOnlyList<GravographPass> BuildPasses(IEnumerable<LayeredPolyline> polylines)
|
||||
{
|
||||
if (polylines == null)
|
||||
throw new ArgumentNullException(nameof(polylines));
|
||||
|
||||
var engrave = new List<IReadOnlyList<Vector>>();
|
||||
var cut = new List<IReadOnlyList<Vector>>();
|
||||
|
||||
foreach (var poly in polylines)
|
||||
{
|
||||
if (poly == null)
|
||||
continue;
|
||||
var block = Config.ConfigFor(poly.Layer);
|
||||
if (block == null)
|
||||
continue; // non-cutting (Display) geometry
|
||||
if (ReferenceEquals(block, Config.Engrave))
|
||||
engrave.Add(poly.Points);
|
||||
else
|
||||
cut.Add(poly.Points);
|
||||
}
|
||||
|
||||
var passes = new List<GravographPass>();
|
||||
if (engrave.Count > 0)
|
||||
passes.Add(MakePass(Config.Engrave, engrave));
|
||||
if (cut.Count > 0)
|
||||
passes.Add(MakePass(Config.Cut, cut));
|
||||
|
||||
return passes;
|
||||
}
|
||||
|
||||
private GravographPass MakePass(LayerCutConfig block, List<IReadOnlyList<Vector>> polylines)
|
||||
{
|
||||
return new GravographPass
|
||||
{
|
||||
Polylines = PolylinePrePass.Prepare(polylines, StitchTolerance, AllowReverse),
|
||||
FeedMmPerSec = block.FeedMmPerSec,
|
||||
DepthInches = block.Depth,
|
||||
PauseBefore = block.PauseBefore,
|
||||
PauseMessage = block.PauseMessage ?? "",
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Buffers the encoded job in memory, then streams it to the named COM port.
|
||||
/// </summary>
|
||||
public void Stream(
|
||||
Nest nest,
|
||||
string portName,
|
||||
Handshake handshake = Handshake.RequestToSend,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
byte[] bytes;
|
||||
using (var ms = new MemoryStream())
|
||||
{
|
||||
Post(nest, ms);
|
||||
bytes = ms.ToArray();
|
||||
}
|
||||
|
||||
using var port = new GravographISPort();
|
||||
port.Open(portName, handshake);
|
||||
port.StreamJob(bytes, cancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,681 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using OpenNest.Geometry;
|
||||
|
||||
namespace OpenNest.Posts.GravographIS
|
||||
{
|
||||
/// <summary>
|
||||
/// One tool pass: a run of polylines cut at a single feed/depth, optionally
|
||||
/// preceded by an operator pause (to swap or adjust the tool). The Gravograph
|
||||
/// post builds one pass for engrave and one for cut.
|
||||
/// </summary>
|
||||
public sealed class GravographPass
|
||||
{
|
||||
public IEnumerable<IReadOnlyList<Vector>> Polylines { get; set; }
|
||||
|
||||
public int FeedMmPerSec { get; set; }
|
||||
|
||||
public double DepthInches { get; set; }
|
||||
|
||||
/// <summary>When true, park to origin and prompt the operator before this pass.</summary>
|
||||
public bool PauseBefore { get; set; }
|
||||
|
||||
public string PauseMessage { get; set; } = "";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Encodes polylines (in inches) into the Gravograph IS8000 native "binary HPGL"
|
||||
/// wire format. The byte stream is byte-exact against captures from GravoStyle'98.
|
||||
///
|
||||
/// Scale: 80 steps/mm = 2032 steps/inch. Y (and Z) are negated on the wire.
|
||||
/// Deltas are signed big-endian int16 (max ±32767 steps ≈ ±16 inches per move).
|
||||
/// </summary>
|
||||
public sealed class GravographISWriter
|
||||
{
|
||||
// 93-byte preamble — captured from GravoStyle'98 with the trailing
|
||||
// job-specific travel block stripped. The VS, VZ and DZ operands are
|
||||
// patched by the writer to reflect feed and depth options.
|
||||
//
|
||||
// The original capture ended with a DR command (FF FD 44 52 00 00)
|
||||
// followed by three 8-byte int16 records — same format as PU/PD —
|
||||
// that carried a chunked travel from the head's parked position to
|
||||
// the original job's first vertex (cumulative ΔX ≈ 1", ΔY ≈ 47").
|
||||
// Those frozen deltas have nothing to do with our job geometry, so
|
||||
// replaying them sends the head to a fixed point regardless of where
|
||||
// the operator set zero. Stripped for the same reason as the captured
|
||||
// fixed return-to-home block.
|
||||
private static readonly byte[] PreambleTemplate = new byte[]
|
||||
{
|
||||
0x21,
|
||||
0x41,
|
||||
0x53,
|
||||
0x20,
|
||||
0x33,
|
||||
0x38,
|
||||
0x3b,
|
||||
0x01,
|
||||
0x90,
|
||||
0x01,
|
||||
0xf4,
|
||||
0x01,
|
||||
0x90,
|
||||
0x01,
|
||||
0xf4,
|
||||
0x01,
|
||||
0x90,
|
||||
0x01,
|
||||
0xf4,
|
||||
0x00,
|
||||
0x00,
|
||||
0x00,
|
||||
0x00,
|
||||
0x00,
|
||||
0x00,
|
||||
0x00,
|
||||
0x00,
|
||||
0x00,
|
||||
0x00,
|
||||
0x00,
|
||||
0x00,
|
||||
0x00,
|
||||
0x09,
|
||||
0x00,
|
||||
0x00,
|
||||
0x03,
|
||||
0xe8,
|
||||
0x05,
|
||||
0x06,
|
||||
0x00,
|
||||
0x00,
|
||||
0x00,
|
||||
0x00,
|
||||
0x00,
|
||||
0x00,
|
||||
0xff,
|
||||
0xfd,
|
||||
0x32,
|
||||
0x44,
|
||||
0x00,
|
||||
0x00,
|
||||
0xff,
|
||||
0xfd,
|
||||
0x4d,
|
||||
0x43,
|
||||
0x00,
|
||||
0x01,
|
||||
0xff,
|
||||
0xfd,
|
||||
0x4f,
|
||||
0x55,
|
||||
0xff,
|
||||
0xfb,
|
||||
0xff,
|
||||
0xfd,
|
||||
0x4f,
|
||||
0x55,
|
||||
0xff,
|
||||
0xfa,
|
||||
0xff,
|
||||
0xfd,
|
||||
0x50,
|
||||
0x5a,
|
||||
0x00,
|
||||
0x00,
|
||||
0xff,
|
||||
0xfd,
|
||||
0x56,
|
||||
0x53,
|
||||
0x00,
|
||||
0x23,
|
||||
0xff,
|
||||
0xfd,
|
||||
0x56,
|
||||
0x5a,
|
||||
0x00,
|
||||
0x23,
|
||||
0xff,
|
||||
0xfd,
|
||||
0x44,
|
||||
0x5a,
|
||||
0x01,
|
||||
0xfc,
|
||||
};
|
||||
|
||||
// Stripped 36-byte postamble: lift, aux off, motor off, operator beep,
|
||||
// job-finish. The 24-byte return-to-home block that appears in GravoStyle's
|
||||
// captured postamble between MC and OP is intentionally OMITTED — those
|
||||
// three 8-byte int16 records carry chunked job-specific return deltas
|
||||
// (each record is [word1:int16][param:int16][ΔX:int16][ΔY:int16], same
|
||||
// format as PU/PD records; the original capture chunked the long Y return
|
||||
// across three records because each delta has to fit in int16). Reusing
|
||||
// GravoStyle's frozen deltas on different geometry overshoots the X-axis
|
||||
// limit. We emit calculated return deltas for the current job instead.
|
||||
// The writer now replaces the captured fixed return block with a calculated
|
||||
// lift + PU travel to the operator-set origin before these final commands.
|
||||
private static readonly byte[] EndJobBytes = new byte[]
|
||||
{
|
||||
0xff,
|
||||
0xfd,
|
||||
0x4f,
|
||||
0x55,
|
||||
0xff,
|
||||
0xfa, // OU 0xFFFA aux off
|
||||
0xff,
|
||||
0xfd,
|
||||
0x4f,
|
||||
0x55,
|
||||
0xff,
|
||||
0xfb, // OU 0xFFFB aux off
|
||||
0xff,
|
||||
0xfd,
|
||||
0x4d,
|
||||
0x43,
|
||||
0x00,
|
||||
0x00, // MC 0x0000 motor off
|
||||
0xff,
|
||||
0xfd,
|
||||
0x4f,
|
||||
0x50,
|
||||
0x00,
|
||||
0x00, // OP 0x0000 operator beep
|
||||
0xff,
|
||||
0xfd,
|
||||
0x4a,
|
||||
0x46,
|
||||
0x00,
|
||||
0x00, // JF 0x0000 job finish
|
||||
};
|
||||
|
||||
// 80 steps/mm × 25.4 mm/in
|
||||
internal const int StepsPerInch = 2032;
|
||||
|
||||
public GravographISWriterOptions Options { get; }
|
||||
|
||||
public GravographISWriter()
|
||||
: this(new GravographISWriterOptions()) { }
|
||||
|
||||
public GravographISWriter(GravographISWriterOptions options)
|
||||
{
|
||||
Options = options ?? throw new ArgumentNullException(nameof(options));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes the full byte stream (preamble + geometry + postamble) for the given
|
||||
/// polylines. Polyline coordinates are in inches, relative to the operator-set
|
||||
/// work origin. The writer emits a leading DR travel to the first polyline
|
||||
/// start before lowering for the first cut.
|
||||
/// </summary>
|
||||
public void Write(IEnumerable<IReadOnlyList<Vector>> polylines, Stream output)
|
||||
{
|
||||
if (polylines == null)
|
||||
throw new ArgumentNullException(nameof(polylines));
|
||||
|
||||
// A single pass at the configured feed/depth — byte-identical to the
|
||||
// original single-group output (no transitions, no pause).
|
||||
Write(
|
||||
new[]
|
||||
{
|
||||
new GravographPass
|
||||
{
|
||||
Polylines = polylines,
|
||||
FeedMmPerSec = Options.FeedMmPerSec,
|
||||
DepthInches = Options.DepthInches,
|
||||
PauseBefore = false,
|
||||
PauseMessage = "",
|
||||
},
|
||||
},
|
||||
output
|
||||
);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes the full byte stream for an ordered list of tool passes. The preamble
|
||||
/// carries the first pass's feed/depth; each later pass emits an inline feed
|
||||
/// (and depth, if changed) and, when <see cref="GravographPass.PauseBefore"/> is
|
||||
/// set, parks to the operator origin and emits an operator pause before cutting.
|
||||
/// </summary>
|
||||
public void Write(IReadOnlyList<GravographPass> passes, Stream output)
|
||||
{
|
||||
if (passes == null)
|
||||
throw new ArgumentNullException(nameof(passes));
|
||||
if (output == null)
|
||||
throw new ArgumentNullException(nameof(output));
|
||||
|
||||
var firstFeed = passes.Count > 0 ? passes[0].FeedMmPerSec : Options.FeedMmPerSec;
|
||||
var firstDepth = passes.Count > 0 ? passes[0].DepthInches : Options.DepthInches;
|
||||
|
||||
var preamble = (byte[])PreambleTemplate.Clone();
|
||||
PatchOperand(preamble, (byte)'V', (byte)'S', (short)firstFeed);
|
||||
PatchOperand(preamble, (byte)'V', (byte)'Z', (short)firstFeed);
|
||||
PatchOperand(preamble, (byte)'D', (byte)'Z', DepthInStepsAsInt16(firstDepth));
|
||||
output.Write(preamble, 0, preamble.Length);
|
||||
|
||||
// Cumulative head position from the operator-set upper-left origin, in
|
||||
// wire steps. The first polyline gets a leading DR travel from this
|
||||
// origin before PD lowers for cutting. Used by the envelope guard to
|
||||
// catch bad records before they ship to the engraver.
|
||||
var headX = 0;
|
||||
var headY = 0;
|
||||
var envelopeXSteps = (int)
|
||||
System.Math.Round(
|
||||
Options.WorkEnvelopeXMm * StepsPerMm,
|
||||
MidpointRounding.AwayFromZero
|
||||
);
|
||||
var envelopeYSteps = (int)
|
||||
System.Math.Round(
|
||||
Options.WorkEnvelopeYMm * StepsPerMm,
|
||||
MidpointRounding.AwayFromZero
|
||||
);
|
||||
|
||||
var firstPolyline = true;
|
||||
var polyIndex = 0;
|
||||
var currentFeed = firstFeed;
|
||||
var currentDepth = firstDepth;
|
||||
|
||||
for (var p = 0; p < passes.Count; p++)
|
||||
{
|
||||
var pass = passes[p];
|
||||
|
||||
if (p > 0)
|
||||
{
|
||||
if (pass.PauseBefore)
|
||||
{
|
||||
// Park: lift Z, then rapid (pen-up) back to the operator origin so
|
||||
// the head is clear of the work while the tool is swapped.
|
||||
WriteLiftOnly(output);
|
||||
WriteTravel(
|
||||
output,
|
||||
(byte)'P',
|
||||
(byte)'U',
|
||||
checked(-headX),
|
||||
checked(-headY),
|
||||
ref headX,
|
||||
ref headY,
|
||||
envelopeXSteps,
|
||||
envelopeYSteps,
|
||||
polyIndex
|
||||
);
|
||||
WritePauseCore(output, pass.PauseMessage);
|
||||
}
|
||||
|
||||
if (pass.FeedMmPerSec != currentFeed)
|
||||
{
|
||||
WriteCommand(output, (byte)'V', (byte)'S', (short)pass.FeedMmPerSec);
|
||||
WriteCommand(output, (byte)'V', (byte)'Z', (short)pass.FeedMmPerSec);
|
||||
currentFeed = pass.FeedMmPerSec;
|
||||
}
|
||||
|
||||
if (pass.DepthInches != currentDepth)
|
||||
{
|
||||
WriteCommand(
|
||||
output,
|
||||
(byte)'D',
|
||||
(byte)'Z',
|
||||
DepthInStepsAsInt16(pass.DepthInches)
|
||||
);
|
||||
currentDepth = pass.DepthInches;
|
||||
}
|
||||
}
|
||||
|
||||
if (pass.Polylines == null)
|
||||
continue;
|
||||
|
||||
foreach (var poly in pass.Polylines)
|
||||
{
|
||||
polyIndex++;
|
||||
if (poly == null || poly.Count < 2)
|
||||
continue;
|
||||
|
||||
WritePolyline(
|
||||
output,
|
||||
poly,
|
||||
ref firstPolyline,
|
||||
ref headX,
|
||||
ref headY,
|
||||
envelopeXSteps,
|
||||
envelopeYSteps,
|
||||
polyIndex
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
WriteLiftOnly(output);
|
||||
if (Options.ReturnToOriginAtEnd && !firstPolyline)
|
||||
{
|
||||
WriteTravel(
|
||||
output,
|
||||
(byte)'P',
|
||||
(byte)'U',
|
||||
checked(-headX),
|
||||
checked(-headY),
|
||||
ref headX,
|
||||
ref headY,
|
||||
envelopeXSteps,
|
||||
envelopeYSteps,
|
||||
polyIndex
|
||||
);
|
||||
}
|
||||
output.Write(EndJobBytes, 0, EndJobBytes.Length);
|
||||
}
|
||||
|
||||
private void WritePolyline(
|
||||
Stream output,
|
||||
IReadOnlyList<Vector> poly,
|
||||
ref bool firstPolyline,
|
||||
ref int headX,
|
||||
ref int headY,
|
||||
int envelopeXSteps,
|
||||
int envelopeYSteps,
|
||||
int polyIndex
|
||||
)
|
||||
{
|
||||
var (startX, startY) = ToWire(poly[0]);
|
||||
WriteTravel(
|
||||
output,
|
||||
firstPolyline ? (byte)'D' : (byte)'P',
|
||||
firstPolyline ? (byte)'R' : (byte)'U',
|
||||
checked(startX - headX),
|
||||
checked(startY - headY),
|
||||
ref headX,
|
||||
ref headY,
|
||||
envelopeXSteps,
|
||||
envelopeYSteps,
|
||||
polyIndex
|
||||
);
|
||||
|
||||
// PD command + single records-follow flag, then one record per segment.
|
||||
output.WriteByte(0xFF);
|
||||
output.WriteByte(0xFD);
|
||||
output.WriteByte((byte)'P');
|
||||
output.WriteByte((byte)'D');
|
||||
output.WriteByte(0x00);
|
||||
output.WriteByte(0x00);
|
||||
|
||||
var prevX = startX;
|
||||
var prevY = startY;
|
||||
for (int i = 1; i < poly.Count; i++)
|
||||
{
|
||||
var (cx, cy) = ToWire(poly[i]);
|
||||
var dx = checked(cx - prevX);
|
||||
var dy = checked(cy - prevY);
|
||||
EnsureEnvelope(
|
||||
headX + dx,
|
||||
headY + dy,
|
||||
envelopeXSteps,
|
||||
envelopeYSteps,
|
||||
polyIndex,
|
||||
segment: i,
|
||||
isTravel: false
|
||||
);
|
||||
WriteRecord(output, dx, dy);
|
||||
prevX = cx;
|
||||
prevY = cy;
|
||||
headX += dx;
|
||||
headY += dy;
|
||||
}
|
||||
|
||||
firstPolyline = false;
|
||||
}
|
||||
|
||||
// The operator pause, minus the leading lift/park which the caller emits.
|
||||
// Stops the spindle (MC off), turns off aux, writes the console message, then
|
||||
// restarts the spindle (MC on) so the job resumes when the operator presses start.
|
||||
private static void WritePauseCore(Stream s, string message)
|
||||
{
|
||||
WriteCommandRaw(s, (byte)'M', (byte)'C', 0x00, 0x00); // motor off
|
||||
WriteCommandRaw(s, (byte)'O', (byte)'U', 0xFF, 0xFB); // aux off
|
||||
WriteCommandRaw(s, (byte)'O', (byte)'U', 0xFF, 0xFA); // aux off
|
||||
WriteCommandRaw(s, (byte)'L', (byte)'B', 0x00, 0x00); // begin message
|
||||
WriteMessagePackets(s, message);
|
||||
WriteCommandRaw(s, (byte)'N', (byte)'R', 0x00, 0x01); // line terminator
|
||||
WriteCommandRaw(s, (byte)'L', (byte)'B', 0x00, 0x01); // end message
|
||||
WriteCommandRaw(s, (byte)'M', (byte)'C', 0x00, 0x01); // motor on
|
||||
}
|
||||
|
||||
// Console label packets carry two ASCII chars each; an odd-length message is
|
||||
// space-padded to a whole number of packets so widths stay 2 bytes.
|
||||
private static void WriteMessagePackets(Stream s, string message)
|
||||
{
|
||||
if (string.IsNullOrEmpty(message))
|
||||
return;
|
||||
|
||||
var chars = Encoding.ASCII.GetBytes(message);
|
||||
for (var i = 0; i < chars.Length; i += 2)
|
||||
{
|
||||
var c0 = chars[i];
|
||||
var c1 = (i + 1 < chars.Length) ? chars[i + 1] : (byte)0x20;
|
||||
WriteCommandRaw(s, (byte)'L', (byte)'B', c0, c1);
|
||||
}
|
||||
}
|
||||
|
||||
private static void WriteCommand(Stream s, byte c0, byte c1, short value)
|
||||
{
|
||||
WriteCommandRaw(s, c0, c1, (byte)((value >> 8) & 0xFF), (byte)(value & 0xFF));
|
||||
}
|
||||
|
||||
private static void WriteCommandRaw(Stream s, byte c0, byte c1, byte hi, byte lo)
|
||||
{
|
||||
s.WriteByte(0xFF);
|
||||
s.WriteByte(0xFD);
|
||||
s.WriteByte(c0);
|
||||
s.WriteByte(c1);
|
||||
s.WriteByte(hi);
|
||||
s.WriteByte(lo);
|
||||
}
|
||||
|
||||
private const double StepsPerMm = 80.0;
|
||||
|
||||
private void EnsureEnvelope(
|
||||
int wireX,
|
||||
int wireY,
|
||||
int envXSteps,
|
||||
int envYSteps,
|
||||
int polyIndex,
|
||||
int segment,
|
||||
bool isTravel
|
||||
)
|
||||
{
|
||||
if (!Options.EnvelopeGuardEnabled)
|
||||
return;
|
||||
|
||||
// Wire frame: X is identity to input; Y is negated. With the operator
|
||||
// origin set at the upper-left of the work envelope and an OpenNest
|
||||
// quadrant-4 plate, valid part coordinates are +X/right and -Y/down:
|
||||
// wireX ∈ [0, +envXSteps]
|
||||
// wireY ∈ [0, +envYSteps]
|
||||
if (wireX >= 0 && wireX <= envXSteps && wireY >= 0 && wireY <= envYSteps)
|
||||
return;
|
||||
|
||||
var inputX = wireX / (double)StepsPerInch;
|
||||
var inputY = -wireY / (double)StepsPerInch;
|
||||
var kind = isTravel ? "pen-up travel" : "cut segment";
|
||||
throw new InvalidOperationException(
|
||||
$"Polyline {polyIndex} {kind} (segment {segment}) would place the head at "
|
||||
+ $"({inputX:F3}\", {inputY:F3}\"), outside the {Options.WorkEnvelopeXMm}×{Options.WorkEnvelopeYMm} mm "
|
||||
+ $"work envelope from upper-left origin. Refusing to emit the record."
|
||||
);
|
||||
}
|
||||
|
||||
private static short DepthInStepsAsInt16(double depthInches)
|
||||
{
|
||||
var steps = (long)
|
||||
System.Math.Round(depthInches * StepsPerInch, MidpointRounding.AwayFromZero);
|
||||
if (steps < short.MinValue || steps > short.MaxValue)
|
||||
throw new ArgumentOutOfRangeException(
|
||||
nameof(depthInches),
|
||||
$"Depth {depthInches} in. → {steps} steps overflows int16."
|
||||
);
|
||||
return (short)steps;
|
||||
}
|
||||
|
||||
private static (int x, int y) ToWire(Vector v)
|
||||
{
|
||||
// Inches -> steps. With upper-left origin in OpenNest quadrant 4,
|
||||
// negative input Y is down; Y is negated on the wire.
|
||||
var x = (int)System.Math.Round(v.X * StepsPerInch, MidpointRounding.AwayFromZero);
|
||||
var y = (int)System.Math.Round(-v.Y * StepsPerInch, MidpointRounding.AwayFromZero);
|
||||
return (x, y);
|
||||
}
|
||||
|
||||
private void WriteTravel(
|
||||
Stream s,
|
||||
byte c0,
|
||||
byte c1,
|
||||
int dx,
|
||||
int dy,
|
||||
ref int headX,
|
||||
ref int headY,
|
||||
int envelopeXSteps,
|
||||
int envelopeYSteps,
|
||||
int polyIndex
|
||||
)
|
||||
{
|
||||
if (dx == 0 && dy == 0)
|
||||
return;
|
||||
|
||||
s.WriteByte(0xFF);
|
||||
s.WriteByte(0xFD);
|
||||
s.WriteByte(c0);
|
||||
s.WriteByte(c1);
|
||||
s.WriteByte(0x00);
|
||||
s.WriteByte(0x00);
|
||||
|
||||
var chunks = System.Math.Max(
|
||||
(int)System.Math.Ceiling(System.Math.Abs(dx) / (double)short.MaxValue),
|
||||
(int)System.Math.Ceiling(System.Math.Abs(dy) / (double)short.MaxValue)
|
||||
);
|
||||
if (chunks < 1)
|
||||
chunks = 1;
|
||||
|
||||
var emittedX = 0;
|
||||
var emittedY = 0;
|
||||
for (var i = 1; i <= chunks; i++)
|
||||
{
|
||||
var targetX = (int)
|
||||
System.Math.Round(dx * (i / (double)chunks), MidpointRounding.AwayFromZero);
|
||||
var targetY = (int)
|
||||
System.Math.Round(dy * (i / (double)chunks), MidpointRounding.AwayFromZero);
|
||||
var chunkX = checked(targetX - emittedX);
|
||||
var chunkY = checked(targetY - emittedY);
|
||||
|
||||
EnsureEnvelope(
|
||||
headX + chunkX,
|
||||
headY + chunkY,
|
||||
envelopeXSteps,
|
||||
envelopeYSteps,
|
||||
polyIndex,
|
||||
segment: 0,
|
||||
isTravel: true
|
||||
);
|
||||
WriteRecord(s, chunkX, chunkY);
|
||||
|
||||
emittedX = targetX;
|
||||
emittedY = targetY;
|
||||
headX += chunkX;
|
||||
headY += chunkY;
|
||||
}
|
||||
}
|
||||
|
||||
private static void WriteLiftOnly(Stream s)
|
||||
{
|
||||
s.WriteByte(0xFF);
|
||||
s.WriteByte(0xFD);
|
||||
s.WriteByte((byte)'P');
|
||||
s.WriteByte((byte)'U');
|
||||
s.WriteByte(0x00);
|
||||
s.WriteByte(0x01);
|
||||
}
|
||||
|
||||
private static void WriteCommandWithRecord(Stream s, byte c0, byte c1, int dx, int dy)
|
||||
{
|
||||
s.WriteByte(0xFF);
|
||||
s.WriteByte(0xFD);
|
||||
s.WriteByte(c0);
|
||||
s.WriteByte(c1);
|
||||
// Records-follow flag (0x0000) emitted once per PU/PD packet.
|
||||
s.WriteByte(0x00);
|
||||
s.WriteByte(0x00);
|
||||
WriteRecord(s, dx, dy);
|
||||
}
|
||||
|
||||
private static void WriteRecord(Stream s, int dx, int dy)
|
||||
{
|
||||
if (
|
||||
dx < short.MinValue
|
||||
|| dx > short.MaxValue
|
||||
|| dy < short.MinValue
|
||||
|| dy > short.MaxValue
|
||||
)
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
$"Move delta ({dx}, {dy}) steps overflows signed int16 — split moves upstream."
|
||||
);
|
||||
}
|
||||
|
||||
int word1;
|
||||
int param;
|
||||
|
||||
var absDx = (double)System.Math.Abs(dx);
|
||||
var absDy = (double)System.Math.Abs(dy);
|
||||
var len = System.Math.Sqrt(absDx * absDx + absDy * absDy);
|
||||
|
||||
if (len < 1.0)
|
||||
{
|
||||
// Zero-length lift (PU 00 01) is the dedicated form; for a record-carrying
|
||||
// packet a true zero-length move shouldn't occur, but stay numerically safe.
|
||||
word1 = 16384;
|
||||
param = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
var maxAbs = System.Math.Max(absDx, absDy);
|
||||
word1 = (int)
|
||||
System.Math.Round(16384.0 * maxAbs / len, MidpointRounding.AwayFromZero);
|
||||
param = (int)System.Math.Round(len / 22.4, MidpointRounding.AwayFromZero);
|
||||
if (param < 1)
|
||||
param = 1;
|
||||
if (param > 180)
|
||||
param = 180;
|
||||
if (word1 > 16384)
|
||||
word1 = 16384;
|
||||
}
|
||||
|
||||
WriteBigEndianInt16(s, (short)word1);
|
||||
WriteBigEndianInt16(s, (short)param);
|
||||
WriteBigEndianInt16(s, (short)dx);
|
||||
WriteBigEndianInt16(s, (short)dy);
|
||||
}
|
||||
|
||||
private static void WriteBigEndianInt16(Stream s, short value)
|
||||
{
|
||||
s.WriteByte((byte)((value >> 8) & 0xFF));
|
||||
s.WriteByte((byte)(value & 0xFF));
|
||||
}
|
||||
|
||||
// Locates the operand of a command (FF FD <c0> <c1> <hi> <lo>) and overwrites it.
|
||||
// Throws if the command isn't present — that would mean the preamble was mis-edited.
|
||||
private static void PatchOperand(byte[] buffer, byte c0, byte c1, short value)
|
||||
{
|
||||
for (int i = 0; i <= buffer.Length - 6; i++)
|
||||
{
|
||||
if (
|
||||
buffer[i] == 0xFF
|
||||
&& buffer[i + 1] == 0xFD
|
||||
&& buffer[i + 2] == c0
|
||||
&& buffer[i + 3] == c1
|
||||
)
|
||||
{
|
||||
buffer[i + 4] = (byte)((value >> 8) & 0xFF);
|
||||
buffer[i + 5] = (byte)(value & 0xFF);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
throw new InvalidOperationException(
|
||||
$"Command '{(char)c0}{(char)c1}' not found in preamble template."
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
namespace OpenNest.Posts.GravographIS
|
||||
{
|
||||
public sealed class GravographISWriterOptions
|
||||
{
|
||||
public double DepthInches { get; set; } = 0.25;
|
||||
|
||||
public int FeedMmPerSec { get; set; } = 35;
|
||||
|
||||
// IS8000 work envelope in millimeters, from the operator-set upper-left
|
||||
// work origin. Defaults to the catalog 0.610 m x 1.220 m bed. With an
|
||||
// OpenNest quadrant-4 plate, motion is allowed right (+X) and down (-Y).
|
||||
public double WorkEnvelopeXMm { get; set; } = 610.0;
|
||||
public double WorkEnvelopeYMm { get; set; } = 1220.0;
|
||||
|
||||
// When true, the writer throws an InvalidOperationException naming the
|
||||
// offending polyline and segment before any out-of-envelope record is
|
||||
// emitted. Disable only for off-machine encoding tests.
|
||||
public bool EnvelopeGuardEnabled { get; set; } = true;
|
||||
|
||||
// When true, lift at the end of the last cut and return to the
|
||||
// operator-set origin before shutting the job down.
|
||||
public bool ReturnToOriginAtEnd { get; set; } = true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,259 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using OpenNest.CNC;
|
||||
using OpenNest.Geometry;
|
||||
|
||||
namespace OpenNest.Posts.GravographIS
|
||||
{
|
||||
/// <summary>
|
||||
/// A polyline together with the <see cref="LayerType"/> of the moves that
|
||||
/// produced it. The Gravograph post groups by layer to emit separate engrave
|
||||
/// and cut passes (with a tool-change pause between them).
|
||||
/// </summary>
|
||||
public sealed class LayeredPolyline
|
||||
{
|
||||
public LayeredPolyline(List<Vector> points, LayerType layer)
|
||||
{
|
||||
Points = points;
|
||||
Layer = layer;
|
||||
}
|
||||
|
||||
public List<Vector> Points { get; }
|
||||
|
||||
public LayerType Layer { get; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Lifts polylines out of an OpenNest <see cref="Nest"/> for the Gravograph
|
||||
/// backend. Walks each <see cref="Part"/>'s <see cref="Program"/>, breaks
|
||||
/// polylines at rapid moves and at <see cref="LayerType"/> changes, and
|
||||
/// tessellates arcs to a chord-deviation tolerance (the wire format takes
|
||||
/// line segments only).
|
||||
/// </summary>
|
||||
public sealed class NestPolylineExtractor
|
||||
{
|
||||
public double ArcChordToleranceInches { get; set; } = 0.001;
|
||||
|
||||
/// <summary>
|
||||
/// Extracts polylines from every non-cutoff part in every plate of the nest,
|
||||
/// returning them in plate coordinates (inches). Layer information is dropped;
|
||||
/// use <see cref="ExtractLayered(Nest)"/> to keep it.
|
||||
/// </summary>
|
||||
public List<List<Vector>> Extract(Nest nest)
|
||||
{
|
||||
return ExtractLayered(nest).Select(p => p.Points).ToList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Extracts polylines for a single part without layer information.
|
||||
/// </summary>
|
||||
public List<List<Vector>> ExtractPart(Part part)
|
||||
{
|
||||
return ExtractPartLayered(part).Select(p => p.Points).ToList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Extracts layer-tagged polylines from every non-cutoff part in every plate,
|
||||
/// in plate coordinates (inches). Each polyline is layer-uniform.
|
||||
/// </summary>
|
||||
public List<LayeredPolyline> ExtractLayered(Nest nest)
|
||||
{
|
||||
if (nest == null)
|
||||
throw new ArgumentNullException(nameof(nest));
|
||||
|
||||
var result = new List<LayeredPolyline>();
|
||||
|
||||
foreach (var plate in nest.Plates)
|
||||
{
|
||||
foreach (var part in plate.Parts)
|
||||
{
|
||||
if (part.BaseDrawing != null && part.BaseDrawing.IsCutOff)
|
||||
continue;
|
||||
|
||||
ExtractPart(part, result);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Extracts layer-tagged polylines for a single part. Public so callers
|
||||
/// driving the writer directly (e.g. from a console one-off) can use it.
|
||||
/// </summary>
|
||||
public List<LayeredPolyline> ExtractPartLayered(Part part)
|
||||
{
|
||||
var list = new List<LayeredPolyline>();
|
||||
ExtractPart(part, list);
|
||||
return list;
|
||||
}
|
||||
|
||||
private void ExtractPart(Part part, List<LayeredPolyline> sink)
|
||||
{
|
||||
var program = part.Program;
|
||||
if (program == null)
|
||||
return;
|
||||
|
||||
// The walk below treats Motion.EndPoint as absolute. Convert a working
|
||||
// copy to absolute mode so G91 programs (the form OpenNest's UI writes)
|
||||
// produce correct geometry. Cloning keeps part.Program untouched.
|
||||
if (program.Mode == Mode.Incremental)
|
||||
{
|
||||
program = (Program)program.Clone();
|
||||
program.Mode = Mode.Absolute;
|
||||
}
|
||||
|
||||
var offset = part.Location;
|
||||
var pos = new Vector(0, 0);
|
||||
List<Vector> current = null;
|
||||
var currentLayer = LayerType.Cut;
|
||||
|
||||
foreach (var code in program.Codes)
|
||||
{
|
||||
if (code is Motion m && m.Suppressed)
|
||||
continue;
|
||||
|
||||
switch (code)
|
||||
{
|
||||
case RapidMove rapid:
|
||||
{
|
||||
FlushCurrent(sink, ref current, currentLayer);
|
||||
pos = rapid.EndPoint;
|
||||
break;
|
||||
}
|
||||
|
||||
case LinearMove linear:
|
||||
{
|
||||
StartOrSplit(
|
||||
sink,
|
||||
ref current,
|
||||
ref currentLayer,
|
||||
linear.Layer,
|
||||
pos + offset
|
||||
);
|
||||
var end = linear.EndPoint;
|
||||
current.Add(end + offset);
|
||||
pos = end;
|
||||
break;
|
||||
}
|
||||
|
||||
case ArcMove arc:
|
||||
{
|
||||
StartOrSplit(sink, ref current, ref currentLayer, arc.Layer, pos + offset);
|
||||
TessellateArc(pos, arc, offset, ArcChordToleranceInches, current);
|
||||
pos = arc.EndPoint;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
FlushCurrent(sink, ref current, currentLayer);
|
||||
}
|
||||
|
||||
// Ensures `current` is an open polyline whose layer matches `moveLayer`,
|
||||
// seeded at `seed` (the current pen position). When the layer changes
|
||||
// mid-chain the previous polyline is flushed and a new one begins at the
|
||||
// shared seam vertex so engrave and cut passes stay geometrically continuous.
|
||||
private static void StartOrSplit(
|
||||
List<LayeredPolyline> sink,
|
||||
ref List<Vector> current,
|
||||
ref LayerType currentLayer,
|
||||
LayerType moveLayer,
|
||||
Vector seed
|
||||
)
|
||||
{
|
||||
if (current == null)
|
||||
{
|
||||
current = new List<Vector> { seed };
|
||||
currentLayer = moveLayer;
|
||||
}
|
||||
else if (moveLayer != currentLayer)
|
||||
{
|
||||
FlushCurrent(sink, ref current, currentLayer);
|
||||
current = new List<Vector> { seed };
|
||||
currentLayer = moveLayer;
|
||||
}
|
||||
}
|
||||
|
||||
private static void FlushCurrent(
|
||||
List<LayeredPolyline> sink,
|
||||
ref List<Vector> current,
|
||||
LayerType layer
|
||||
)
|
||||
{
|
||||
if (current != null && current.Count >= 2)
|
||||
sink.Add(new LayeredPolyline(current, layer));
|
||||
current = null;
|
||||
}
|
||||
|
||||
// Sample points along an arc to within chordTol of the true curve. start is
|
||||
// the arc's start point (current pen position), arc.CenterPoint is absolute
|
||||
// (G-code I/J in this codebase are stored as the absolute center), arc.EndPoint
|
||||
// is absolute end. The starting point is assumed to already be in the polyline;
|
||||
// intermediate samples and the endpoint are appended.
|
||||
private static void TessellateArc(
|
||||
Vector start,
|
||||
ArcMove arc,
|
||||
Vector offset,
|
||||
double chordTol,
|
||||
List<Vector> sink
|
||||
)
|
||||
{
|
||||
var c = arc.CenterPoint;
|
||||
var r = c.DistanceTo(start);
|
||||
if (r < 1e-9)
|
||||
{
|
||||
sink.Add(arc.EndPoint + offset);
|
||||
return;
|
||||
}
|
||||
|
||||
var a0 = System.Math.Atan2(start.Y - c.Y, start.X - c.X);
|
||||
var a1 = System.Math.Atan2(arc.EndPoint.Y - c.Y, arc.EndPoint.X - c.X);
|
||||
|
||||
double sweep;
|
||||
if (arc.Rotation == RotationType.CW)
|
||||
{
|
||||
sweep = a0 - a1;
|
||||
if (sweep <= 0)
|
||||
sweep += 2 * System.Math.PI;
|
||||
}
|
||||
else
|
||||
{
|
||||
sweep = a1 - a0;
|
||||
if (sweep <= 0)
|
||||
sweep += 2 * System.Math.PI;
|
||||
}
|
||||
|
||||
// Treat a near-zero sweep with coincident start/end as a full circle.
|
||||
if (
|
||||
sweep < 1e-9
|
||||
&& System.Math.Abs(start.X - arc.EndPoint.X) < 1e-9
|
||||
&& System.Math.Abs(start.Y - arc.EndPoint.Y) < 1e-9
|
||||
)
|
||||
{
|
||||
sweep = 2 * System.Math.PI;
|
||||
}
|
||||
|
||||
// Max angle step from chord-deviation tolerance: dev = r * (1 - cos(t/2)).
|
||||
var maxAngleStep = 2.0 * System.Math.Acos(System.Math.Max(0.0, 1.0 - chordTol / r));
|
||||
if (double.IsNaN(maxAngleStep) || maxAngleStep <= 0)
|
||||
maxAngleStep = System.Math.PI / 32;
|
||||
|
||||
var steps = (int)System.Math.Ceiling(sweep / maxAngleStep);
|
||||
if (steps < 1)
|
||||
steps = 1;
|
||||
|
||||
var direction = arc.Rotation == RotationType.CW ? -1.0 : 1.0;
|
||||
for (int i = 1; i < steps; i++)
|
||||
{
|
||||
var t = sweep * (i / (double)steps);
|
||||
var ang = a0 + direction * t;
|
||||
var pt = new Vector(c.X + r * System.Math.Cos(ang), c.Y + r * System.Math.Sin(ang));
|
||||
sink.Add(pt + offset);
|
||||
}
|
||||
|
||||
sink.Add(arc.EndPoint + offset);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<RootNamespace>OpenNest.Posts.GravographIS</RootNamespace>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\OpenNest.Core\OpenNest.Core.csproj" />
|
||||
<PackageReference Include="System.IO.Ports" Version="8.0.0" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<InternalsVisibleTo Include="OpenNest.Tests" />
|
||||
</ItemGroup>
|
||||
<Target Name="CopyToPostsDir" AfterTargets="Build">
|
||||
<PropertyGroup>
|
||||
<PostsDir>..\..\OpenNest\bin\$(Configuration)\net8.0-windows\Posts\</PostsDir>
|
||||
</PropertyGroup>
|
||||
<MakeDir Directories="$(PostsDir)" />
|
||||
<Copy SourceFiles="$(TargetPath)" DestinationFolder="$(PostsDir)" SkipUnchangedFiles="true" ContinueOnError="true" />
|
||||
</Target>
|
||||
</Project>
|
||||
@@ -0,0 +1,211 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using OpenNest.Geometry;
|
||||
|
||||
namespace OpenNest.Posts.GravographIS
|
||||
{
|
||||
/// <summary>
|
||||
/// Geometry pre-pass for the Gravograph IS8000 backend. The machine is a dumb
|
||||
/// executor — it never reorders geometry and always lifts between separate
|
||||
/// entities — so we stitch shared-endpoint polylines together and reorder by
|
||||
/// nearest-neighbor before encoding.
|
||||
/// </summary>
|
||||
public static class PolylinePrePass
|
||||
{
|
||||
public const double DefaultStitchTolerance = 1e-6;
|
||||
|
||||
/// <summary>
|
||||
/// Joins polylines whose endpoints coincide (within <paramref name="tolerance"/>)
|
||||
/// into single continuous polylines. Polylines with fewer than two points are
|
||||
/// dropped. Direction is reversed as needed to make a join. Each input polyline
|
||||
/// is copied — the inputs are not mutated.
|
||||
/// </summary>
|
||||
public static List<List<Vector>> Stitch(
|
||||
IEnumerable<IReadOnlyList<Vector>> polylines,
|
||||
double tolerance = DefaultStitchTolerance
|
||||
)
|
||||
{
|
||||
if (polylines == null)
|
||||
throw new ArgumentNullException(nameof(polylines));
|
||||
|
||||
var segs = new List<List<Vector>>();
|
||||
foreach (var p in polylines)
|
||||
{
|
||||
if (p == null || p.Count < 2)
|
||||
continue;
|
||||
segs.Add(new List<Vector>(p));
|
||||
}
|
||||
|
||||
bool changed;
|
||||
do
|
||||
{
|
||||
changed = false;
|
||||
for (int i = 0; i < segs.Count; i++)
|
||||
{
|
||||
var a = segs[i];
|
||||
|
||||
for (int j = 0; j < segs.Count; j++)
|
||||
{
|
||||
if (i == j)
|
||||
continue;
|
||||
var b = segs[j];
|
||||
|
||||
// a-end ↔ b-start: append b to a (skip duplicated joint)
|
||||
if (Near(a[a.Count - 1], b[0], tolerance))
|
||||
{
|
||||
for (int k = 1; k < b.Count; k++)
|
||||
a.Add(b[k]);
|
||||
segs.RemoveAt(j);
|
||||
if (j < i)
|
||||
i--;
|
||||
changed = true;
|
||||
break;
|
||||
}
|
||||
|
||||
// a-end ↔ b-end: append reversed b to a
|
||||
if (Near(a[a.Count - 1], b[b.Count - 1], tolerance))
|
||||
{
|
||||
for (int k = b.Count - 2; k >= 0; k--)
|
||||
a.Add(b[k]);
|
||||
segs.RemoveAt(j);
|
||||
if (j < i)
|
||||
i--;
|
||||
changed = true;
|
||||
break;
|
||||
}
|
||||
|
||||
// a-start ↔ b-end: prepend b to a
|
||||
if (Near(a[0], b[b.Count - 1], tolerance))
|
||||
{
|
||||
var combined = new List<Vector>(b.Count + a.Count - 1);
|
||||
combined.AddRange(b);
|
||||
for (int k = 1; k < a.Count; k++)
|
||||
combined.Add(a[k]);
|
||||
segs[i] = combined;
|
||||
segs.RemoveAt(j);
|
||||
if (j < i)
|
||||
i--;
|
||||
changed = true;
|
||||
break;
|
||||
}
|
||||
|
||||
// a-start ↔ b-start: prepend reversed b to a
|
||||
if (Near(a[0], b[0], tolerance))
|
||||
{
|
||||
var combined = new List<Vector>(b.Count + a.Count - 1);
|
||||
for (int k = b.Count - 1; k >= 0; k--)
|
||||
combined.Add(b[k]);
|
||||
for (int k = 1; k < a.Count; k++)
|
||||
combined.Add(a[k]);
|
||||
segs[i] = combined;
|
||||
segs.RemoveAt(j);
|
||||
if (j < i)
|
||||
i--;
|
||||
changed = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (changed)
|
||||
break;
|
||||
}
|
||||
} while (changed);
|
||||
|
||||
return segs;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Greedy nearest-neighbor ordering of polylines starting from
|
||||
/// <paramref name="origin"/> (defaults to 0,0 = the work origin = the first
|
||||
/// polyline's first point on the wire). When <paramref name="allowReverse"/>
|
||||
/// is true a polyline may be reversed if its tail is closer than its head.
|
||||
/// </summary>
|
||||
public static List<List<Vector>> Reorder(
|
||||
IEnumerable<IReadOnlyList<Vector>> polylines,
|
||||
bool allowReverse = true,
|
||||
Vector? origin = null
|
||||
)
|
||||
{
|
||||
if (polylines == null)
|
||||
throw new ArgumentNullException(nameof(polylines));
|
||||
|
||||
var pool = new List<List<Vector>>();
|
||||
foreach (var p in polylines)
|
||||
{
|
||||
if (p == null || p.Count < 2)
|
||||
continue;
|
||||
pool.Add(new List<Vector>(p));
|
||||
}
|
||||
|
||||
var ordered = new List<List<Vector>>(pool.Count);
|
||||
var current = origin ?? new Vector(0, 0);
|
||||
|
||||
while (pool.Count > 0)
|
||||
{
|
||||
var bestIdx = -1;
|
||||
var bestReverse = false;
|
||||
var bestDistSq = double.PositiveInfinity;
|
||||
|
||||
for (int i = 0; i < pool.Count; i++)
|
||||
{
|
||||
var p = pool[i];
|
||||
var dHead = SquaredDistance(current, p[0]);
|
||||
if (dHead < bestDistSq)
|
||||
{
|
||||
bestDistSq = dHead;
|
||||
bestIdx = i;
|
||||
bestReverse = false;
|
||||
}
|
||||
|
||||
if (allowReverse)
|
||||
{
|
||||
var dTail = SquaredDistance(current, p[p.Count - 1]);
|
||||
if (dTail < bestDistSq)
|
||||
{
|
||||
bestDistSq = dTail;
|
||||
bestIdx = i;
|
||||
bestReverse = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var pick = pool[bestIdx];
|
||||
pool.RemoveAt(bestIdx);
|
||||
if (bestReverse)
|
||||
pick.Reverse();
|
||||
ordered.Add(pick);
|
||||
current = pick[pick.Count - 1];
|
||||
}
|
||||
|
||||
return ordered;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convenience: stitch then reorder.
|
||||
/// </summary>
|
||||
public static List<List<Vector>> Prepare(
|
||||
IEnumerable<IReadOnlyList<Vector>> polylines,
|
||||
double stitchTolerance = DefaultStitchTolerance,
|
||||
bool allowReverse = true,
|
||||
Vector? origin = null
|
||||
)
|
||||
{
|
||||
var stitched = Stitch(polylines, stitchTolerance);
|
||||
return Reorder(stitched, allowReverse, origin);
|
||||
}
|
||||
|
||||
private static bool Near(Vector a, Vector b, double tol)
|
||||
{
|
||||
var dx = a.X - b.X;
|
||||
var dy = a.Y - b.Y;
|
||||
return (dx * dx + dy * dy) <= tol * tol;
|
||||
}
|
||||
|
||||
private static double SquaredDistance(Vector a, Vector b)
|
||||
{
|
||||
var dx = a.X - b.X;
|
||||
var dy = a.Y - b.Y;
|
||||
return dx * dx + dy * dy;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user