diff --git a/xs/src/libslic3r/SVG.cpp b/xs/src/libslic3r/SVG.cpp index db5ec7293..5374ed40b 100644 --- a/xs/src/libslic3r/SVG.cpp +++ b/xs/src/libslic3r/SVG.cpp @@ -1,8 +1,12 @@ #include "SVG.hpp" +#include + +#define COORD(x) ((float)unscale(x)*10) namespace Slic3r { SVG::SVG(const char* filename) + : arrows(true), filename(filename), fill("grey"), stroke("black") { this->f = fopen(filename, "w"); fprintf(this->f, @@ -13,13 +17,6 @@ SVG::SVG(const char* filename) " \n" " \n" ); - this->arrows = true; -} - -float -SVG::coordinate(long c) -{ - return (float)unscale(c)*10; } void @@ -27,7 +24,7 @@ SVG::AddLine(const Line &line) { fprintf(this->f, " coordinate(line.a.x), this->coordinate(line.a.y), this->coordinate(line.b.x), this->coordinate(line.b.y) + COORD(line.a.x), COORD(line.a.y), COORD(line.b.x), COORD(line.b.y) ); if (this->arrows) fprintf(this->f, " marker-end=\"url(#endArrow)\""); @@ -40,12 +37,61 @@ SVG::AddLine(const IntersectionLine &line) this->AddLine(Line(line.a, line.b)); } +void +SVG::draw(const ExPolygon &expolygon) +{ + std::string d; + Polygons pp = expolygon; + for (Polygons::const_iterator p = pp.begin(); p != pp.end(); ++p) { + d += this->get_path_d(*p) + " "; + } + this->path(d, true); +} + +void +SVG::draw(const Polygon &polygon) +{ + this->path(this->get_path_d(polygon), true); +} + +void +SVG::draw(const Polyline &polyline) +{ + this->path(this->get_path_d(polyline), false); +} + +void +SVG::path(const std::string &d, bool fill) +{ + fprintf( + this->f, + " \n", + d.c_str(), + fill ? this->fill.c_str() : "none", + this->stroke.c_str(), + fill ? "0" : "2" + ); +} + +std::string +SVG::get_path_d(const MultiPoint &mp) const +{ + std::ostringstream d; + d << "M"; + for (Points::const_iterator p = mp.points.begin(); p != mp.points.end(); ++p) { + d << " " << COORD(p->x); + d << " " << COORD(p->y); + } + d << " z"; + return d.str(); +} + void SVG::Close() { fprintf(this->f, "\n"); fclose(this->f); - printf("SVG file written.\n"); + printf("SVG written to %s\n", this->filename.c_str()); } } diff --git a/xs/src/libslic3r/SVG.hpp b/xs/src/libslic3r/SVG.hpp index 5d4cfd56e..f312f4a3b 100644 --- a/xs/src/libslic3r/SVG.hpp +++ b/xs/src/libslic3r/SVG.hpp @@ -2,6 +2,7 @@ #define slic3r_SVG_hpp_ #include +#include "ExPolygon.hpp" #include "Line.hpp" #include "TriangleMesh.hpp" @@ -9,15 +10,24 @@ namespace Slic3r { class SVG { - private: - FILE* f; - float coordinate(long c); public: bool arrows; + 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 Close(); + + private: + std::string filename; + FILE* f; + + void path(const std::string &d, bool fill); + std::string get_path_d(const MultiPoint &polygon) const; }; }