diff options
author | Alessandro Ranellucci <aar@cpan.org> | 2015-01-06 18:26:15 +0300 |
---|---|---|
committer | Alessandro Ranellucci <aar@cpan.org> | 2015-01-06 18:26:15 +0300 |
commit | 713fcb5e8eabe21646b25febe43ac09f422370a0 (patch) | |
tree | 82bfdbf95ea38cde133a2e7c611eea0d7bee6ed9 /xs/src/libslic3r/SVG.cpp | |
parent | f0de57cbe41489836e194a82ce5e1bea5482fc96 (diff) |
New methods in Slic3r::SVG C++ class
Diffstat (limited to 'xs/src/libslic3r/SVG.cpp')
-rw-r--r-- | xs/src/libslic3r/SVG.cpp | 64 |
1 files changed, 55 insertions, 9 deletions
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 <iostream> + +#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) " <polyline fill=\"darkblue\" points=\"0,0 10,5 0,10 1,5\" />\n" " </marker>\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, " <line x1=\"%f\" y1=\"%f\" x2=\"%f\" y2=\"%f\" style=\"stroke: black; stroke-width: 2\"", - this->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)\""); @@ -41,11 +38,60 @@ SVG::AddLine(const IntersectionLine &line) } 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, + " <path d=\"%s\" style=\"fill: %s; stroke: %s; stroke-width: %s; fill-type: evenodd\" />\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, "</svg>\n"); fclose(this->f); - printf("SVG file written.\n"); + printf("SVG written to %s\n", this->filename.c_str()); } } |