From b774e63c8ab26beb8ab1601d54aeae48479aab48 Mon Sep 17 00:00:00 2001 From: Sameer Puri Date: Thu, 25 Apr 2019 17:17:37 -0500 Subject: [PATCH] Add initial position handling for close path --- README.md | 1 + src/main.rs | 2 +- src/turtle.rs | 32 +++++++++++++++++++++++++++----- 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 4343099..59430a5 100644 --- a/README.md +++ b/README.md @@ -12,3 +12,4 @@ Convert any SVG 1.1 path to gcode for a pen plotter, laser engraver, etc. ## Known bugs & whether fixed - [ ] Smooth curves should not use the control point when the previous curve is not of the same type (quadratic -> smooth cubic, cubic -> smooth quadratic) - [x] Image coordinates mirrored in the y-axis because SVGs uses upper left corner as (0,0) while GCode uses lower left as (0,0) +- [x] Close path command connects back to (0.0, 0.0) instead of the last move diff --git a/src/main.rs b/src/main.rs index bcb2caf..a4dfb6b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -164,7 +164,7 @@ fn svg2program(doc: &svgdom::Document, opts: ProgramOptions, mach: Machine) -> P } PathSegment::ClosePath { abs } => { // Ignore abs, should have identical effect: https://www.w3.org/TR/SVG/paths.html#PathDataClosePathCommand - p.extend(t.line(true, 0.0, 0.0, None, opts.feedrate)) + p.extend(t.close(None, opts.feedrate)) } PathSegment::LineTo { abs, x, y } => { p.extend(t.line(*abs, *x, *y, None, opts.feedrate)); diff --git a/src/turtle.rs b/src/turtle.rs index ed3cbf6..e671329 100644 --- a/src/turtle.rs +++ b/src/turtle.rs @@ -6,6 +6,7 @@ use lyon_geom::{ArcFlags, CubicBezierSegment, QuadraticBezierSegment, SvgArc}; pub struct Turtle { curpos: F64Point, + initpos: F64Point, curtran: Transform2D, scaling: Option>, transtack: Vec>, @@ -17,6 +18,7 @@ impl Default for Turtle { fn default() -> Self { Self { curpos: point(0.0, 0.0), + initpos: point(0.0, 0.0), curtran: Transform2D::identity(), scaling: None, transtack: vec![], @@ -46,7 +48,8 @@ impl Turtle { let mut to = point(x, y); to = self.curtran.transform_point(&to); self.curpos = to; - self.prev_ctrl = self.curpos; + self.initpos = to; + self.prev_ctrl = to; vec![ self.mach.tool_off(), @@ -61,6 +64,27 @@ impl Turtle { .collect() } + pub fn close(&mut self, z: Z, f: F) -> Program + where + Z: Into>, + F: Into>, + { + self.curpos = self.initpos; + vec![ + self.mach.tool_on(), + self.mach.absolute(), + vec![GCode::LinearInterpolation { + x: self.initpos.x.into(), + y: self.initpos.y.into(), + z: z.into(), + f: f.into(), + }], + ] + .drain(..) + .flatten() + .collect() + } + pub fn line(&mut self, abs: bool, x: X, y: Y, z: Z, f: F) -> Program where X: Into>, @@ -334,9 +358,7 @@ impl Turtle { self.curtran = self.curtran.post_mul(&old_scaling.inverse().unwrap()); } self.scaling = Some(scaling); - self.curtran = self - .curtran - .post_mul(&scaling); + self.curtran = self.curtran.post_mul(&scaling); } pub fn push_transform(&mut self, trans: Transform2D) { @@ -346,7 +368,7 @@ impl Turtle { .curtran .post_mul(&scaling.inverse().unwrap()) .pre_mul(&trans) - .post_mul(&scaling); + .post_mul(&scaling); } else { self.curtran = self.curtran.post_mul(&trans); }