From 5e100abe2545ba5f61edce541c91b63aa3e14347 Mon Sep 17 00:00:00 2001 From: Alessandro Ranellucci Date: Tue, 6 Jan 2015 20:51:48 +0100 Subject: [PATCH] Added several drawing methods to Slic3r::SVG --- xs/src/libslic3r/SVG.cpp | 50 ++++++++++++++++++++++++++-------------- xs/src/libslic3r/SVG.hpp | 11 +++++---- 2 files changed, 39 insertions(+), 22 deletions(-) diff --git a/xs/src/libslic3r/SVG.cpp b/xs/src/libslic3r/SVG.cpp index 5374ed40b..328d5421a 100644 --- a/xs/src/libslic3r/SVG.cpp +++ b/xs/src/libslic3r/SVG.cpp @@ -20,11 +20,11 @@ SVG::SVG(const char* filename) } void -SVG::AddLine(const Line &line) +SVG::draw(const Line &line, std::string stroke) { fprintf(this->f, - " arrows) fprintf(this->f, " marker-end=\"url(#endArrow)\""); @@ -34,30 +34,45 @@ SVG::AddLine(const Line &line) void SVG::AddLine(const IntersectionLine &line) { - this->AddLine(Line(line.a, line.b)); + this->draw(Line(line.a, line.b)); } void -SVG::draw(const ExPolygon &expolygon) +SVG::draw(const ExPolygon &expolygon, std::string fill) { + this->fill = fill; + std::string d; Polygons pp = expolygon; for (Polygons::const_iterator p = pp.begin(); p != pp.end(); ++p) { - d += this->get_path_d(*p) + " "; + d += this->get_path_d(*p, true) + " "; } this->path(d, true); } void -SVG::draw(const Polygon &polygon) +SVG::draw(const Polygon &polygon, std::string fill) { - this->path(this->get_path_d(polygon), true); + this->fill = fill; + this->path(this->get_path_d(polygon, true), true); } void -SVG::draw(const Polyline &polyline) +SVG::draw(const Polyline &polyline, std::string stroke) { - this->path(this->get_path_d(polyline), false); + this->stroke = stroke; + this->path(this->get_path_d(polyline, false), false); +} + +void +SVG::draw(const Point &point, std::string fill, unsigned int radius) +{ + std::ostringstream svg; + svg << " "; + + fprintf(this->f, "%s\n", svg.str().c_str()); } void @@ -65,24 +80,25 @@ SVG::path(const std::string &d, bool fill) { fprintf( this->f, - " \n", + " \n", d.c_str(), fill ? this->fill.c_str() : "none", this->stroke.c_str(), - fill ? "0" : "2" + fill ? "0" : "2", + (this->arrows && !fill) ? " marker-end=\"url(#endArrow)\"" : "" ); } std::string -SVG::get_path_d(const MultiPoint &mp) const +SVG::get_path_d(const MultiPoint &mp, bool closed) const { std::ostringstream d; - d << "M"; + d << "M "; for (Points::const_iterator p = mp.points.begin(); p != mp.points.end(); ++p) { - d << " " << COORD(p->x); - d << " " << COORD(p->y); + d << COORD(p->x) << " "; + d << COORD(p->y) << " "; } - d << " z"; + if (closed) d << "z"; return d.str(); } diff --git a/xs/src/libslic3r/SVG.hpp b/xs/src/libslic3r/SVG.hpp index f312f4a3b..bb0d5adcf 100644 --- a/xs/src/libslic3r/SVG.hpp +++ b/xs/src/libslic3r/SVG.hpp @@ -15,11 +15,12 @@ class SVG std::string fill, stroke; SVG(const char* filename); - void AddLine(const Line &line); void AddLine(const IntersectionLine &line); - void draw(const ExPolygon &expolygon); - void draw(const Polygon &polygon); - void draw(const Polyline &polyline); + void draw(const Line &line, std::string stroke = "black"); + void draw(const ExPolygon &expolygon, std::string fill = "grey"); + void draw(const Polygon &polygon, std::string fill = "grey"); + void draw(const Polyline &polyline, std::string stroke = "black"); + void draw(const Point &point, std::string fill = "black", unsigned int radius = 3); void Close(); private: @@ -27,7 +28,7 @@ class SVG FILE* f; void path(const std::string &d, bool fill); - std::string get_path_d(const MultiPoint &polygon) const; + std::string get_path_d(const MultiPoint &mp, bool closed = false) const; }; }