New methods in Slic3r::SVG C++ class

This commit is contained in:
Alessandro Ranellucci 2015-01-06 16:26:15 +01:00
parent f0de57cbe4
commit 713fcb5e8e
2 changed files with 68 additions and 12 deletions

View File

@ -1,8 +1,12 @@
#include "SVG.hpp" #include "SVG.hpp"
#include <iostream>
#define COORD(x) ((float)unscale(x)*10)
namespace Slic3r { namespace Slic3r {
SVG::SVG(const char* filename) SVG::SVG(const char* filename)
: arrows(true), filename(filename), fill("grey"), stroke("black")
{ {
this->f = fopen(filename, "w"); this->f = fopen(filename, "w");
fprintf(this->f, 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" " <polyline fill=\"darkblue\" points=\"0,0 10,5 0,10 1,5\" />\n"
" </marker>\n" " </marker>\n"
); );
this->arrows = true;
}
float
SVG::coordinate(long c)
{
return (float)unscale(c)*10;
} }
void void
@ -27,7 +24,7 @@ SVG::AddLine(const Line &line)
{ {
fprintf(this->f, fprintf(this->f,
" <line x1=\"%f\" y1=\"%f\" x2=\"%f\" y2=\"%f\" style=\"stroke: black; stroke-width: 2\"", " <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) if (this->arrows)
fprintf(this->f, " marker-end=\"url(#endArrow)\""); fprintf(this->f, " marker-end=\"url(#endArrow)\"");
@ -40,12 +37,61 @@ SVG::AddLine(const IntersectionLine &line)
this->AddLine(Line(line.a, line.b)); 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,
" <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 void
SVG::Close() SVG::Close()
{ {
fprintf(this->f, "</svg>\n"); fprintf(this->f, "</svg>\n");
fclose(this->f); fclose(this->f);
printf("SVG file written.\n"); printf("SVG written to %s\n", this->filename.c_str());
} }
} }

View File

@ -2,6 +2,7 @@
#define slic3r_SVG_hpp_ #define slic3r_SVG_hpp_
#include <myinit.h> #include <myinit.h>
#include "ExPolygon.hpp"
#include "Line.hpp" #include "Line.hpp"
#include "TriangleMesh.hpp" #include "TriangleMesh.hpp"
@ -9,15 +10,24 @@ namespace Slic3r {
class SVG class SVG
{ {
private:
FILE* f;
float coordinate(long c);
public: public:
bool arrows; bool arrows;
std::string fill, stroke;
SVG(const char* filename); SVG(const char* filename);
void AddLine(const Line &line); void AddLine(const Line &line);
void AddLine(const IntersectionLine &line); void AddLine(const IntersectionLine &line);
void draw(const ExPolygon &expolygon);
void draw(const Polygon &polygon);
void draw(const Polyline &polyline);
void Close(); 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;
}; };
} }