#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, "\n" "\n" "\n" " \n" " \n" " \n" ); } void SVG::draw(const Line &line, std::string stroke) { fprintf(this->f, " arrows) fprintf(this->f, " marker-end=\"url(#endArrow)\""); fprintf(this->f, "/>\n"); } void SVG::draw(const Lines &lines, std::string stroke) { for (Lines::const_iterator it = lines.begin(); it != lines.end(); ++it) this->draw(*it, stroke); } void SVG::draw(const IntersectionLines &lines, std::string stroke) { for (IntersectionLines::const_iterator it = lines.begin(); it != lines.end(); ++it) this->draw((Line)*it, stroke); } void 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, true) + " "; } this->path(d, true); } void SVG::draw(const ExPolygons &expolygons, std::string fill) { for (ExPolygons::const_iterator it = expolygons.begin(); it != expolygons.end(); ++it) this->draw(*it, fill); } void SVG::draw(const Polygon &polygon, std::string fill) { this->fill = fill; this->path(this->get_path_d(polygon, true), !fill.empty()); } void SVG::draw(const Polygons &polygons, std::string fill) { for (Polygons::const_iterator it = polygons.begin(); it != polygons.end(); ++it) this->draw(*it, fill); } void SVG::draw(const Polyline &polyline, std::string stroke) { this->stroke = stroke; this->path(this->get_path_d(polyline, false), false); } void SVG::draw(const Polylines &polylines, std::string stroke) { for (Polylines::const_iterator it = polylines.begin(); it != polylines.end(); ++it) this->draw(*it, fill); } 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 SVG::draw(const Points &points, std::string fill, unsigned int radius) { for (Points::const_iterator it = points.begin(); it != points.end(); ++it) this->draw(*it, fill, radius); } 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", (this->arrows && !fill) ? " marker-end=\"url(#endArrow)\"" : "" ); } std::string SVG::get_path_d(const MultiPoint &mp, bool closed) 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) << " "; } if (closed) d << "z"; return d.str(); } void SVG::Close() { fprintf(this->f, "\n"); fclose(this->f); printf("SVG written to %s\n", this->filename.c_str()); } }