Added --export-obj to slic3r.cpp. Some refactoring included

This commit is contained in:
Alessandro Ranellucci 2016-06-22 17:45:10 +02:00
parent b0378dddc6
commit 64da78788b
6 changed files with 50 additions and 35 deletions

View File

@ -40,6 +40,7 @@ add_library(libslic3r STATIC ${LIBDIR}/libslic3r/BoundingBox.cpp
${LIBDIR}/libslic3r/BridgeDetector.cpp ${LIBDIR}/libslic3r/BridgeDetector.cpp
${LIBDIR}/libslic3r/Extruder.cpp ${LIBDIR}/libslic3r/Extruder.cpp
${LIBDIR}/libslic3r/GCodeSender.cpp ${LIBDIR}/libslic3r/GCodeSender.cpp
${LIBDIR}/libslic3r/IO.cpp
${LIBDIR}/libslic3r/Line.cpp ${LIBDIR}/libslic3r/Line.cpp
${LIBDIR}/libslic3r/PlaceholderParser.cpp ${LIBDIR}/libslic3r/PlaceholderParser.cpp
${LIBDIR}/libslic3r/PrintConfig.cpp ${LIBDIR}/libslic3r/PrintConfig.cpp

View File

@ -1,5 +1,5 @@
#include "Model.hpp"
#include "IO.hpp"
#include "TriangleMesh.hpp" #include "TriangleMesh.hpp"
#include "SVGExport.hpp" #include "SVGExport.hpp"
#include "libslic3r.h" #include "libslic3r.h"
@ -13,24 +13,15 @@ using namespace Slic3r;
void confess_at(const char *file, int line, const char *func, const char *pat, ...){} void confess_at(const char *file, int line, const char *func, const char *pat, ...){}
void exportSVG(const char* stlname, const char* svgname, float layerheight=0.2, float initialheight=0., float scale=1.0, float rotate=0.){ void exportSVG(const Model &model, const std::string &outfile, float layerheight=0.2, float initialheight=0., float scale=1.0, float rotate=0.){
TriangleMesh t; TriangleMesh mesh = model.mesh();
std::string outname; mesh.scale(scale);
if(strlen(svgname)==0){ mesh.rotate_z(rotate);
outname=outname+stlname+".svg"; mesh.mirror_x();
}else{ mesh.align_to_origin();
outname=outname+svgname; SVGExport e(mesh, layerheight, initialheight);
} e.writeSVG(outfile);
t.ReadSTLFile(const_cast<char *>(stlname)); printf("writing: %s\n", outfile.c_str());
t.repair();
t.scale(scale);
t.rotate_z(rotate);
t.mirror_x();
t.align_to_origin();
SVGExport e(t,layerheight,initialheight);
const char* svgfilename=outname.data();
e.writeSVG(svgfilename);
printf("writing: %s\n",svgfilename);
} }
int main (int argc, char **argv){ int main (int argc, char **argv){
@ -48,18 +39,41 @@ int main (int argc, char **argv){
TCLAP::ValueArg<float> lhArg("","layer-height","Layer height in mm (default: 0.2)",false,0.2,"float"); TCLAP::ValueArg<float> lhArg("","layer-height","Layer height in mm (default: 0.2)",false,0.2,"float");
cmd.add( lhArg ); cmd.add( lhArg );
TCLAP::SwitchArg expsvg("","export-svg","Export a SVG file containing slices", cmd, false); TCLAP::SwitchArg expsvg("","export-svg","Export a SVG file containing slices", cmd, false);
TCLAP::SwitchArg expobj("","export-obj","Export the input file as OBJ", cmd, false);
TCLAP::UnlabeledValueArg<std::string> input("inputfile","Input STL file name", true, "", "input file name"); TCLAP::UnlabeledValueArg<std::string> input("inputfile","Input STL file name", true, "", "input file name");
cmd.add(input); cmd.add(input);
cmd.parse(argc,argv); cmd.parse(argc,argv);
if(expsvg.getValue()){
exportSVG(input.getValue().data(),outputArg.getValue().data(),lhArg.getValue(),flhArg.getValue(),scaleArg.getValue(),rotArg.getValue()); // read input file if any (TODO: read multiple)
Model model;
if (!input.getValue().empty()) {
Slic3r::IO::STL::read(input.getValue(), &model);
model.add_default_instances();
}
if (expobj.getValue()) {
std::string outfile = outputArg.getValue();
if (outfile.empty()) outfile = input.getValue() + ".obj";
TriangleMesh mesh = model.mesh();
printf("mesh has %zu facets\n", mesh.facets_count());
Slic3r::IO::OBJ::write(mesh, outfile);
printf("File exported to %s\n", outfile.c_str());
} else if (expsvg.getValue()) {
std::string outfile = outputArg.getValue();
if (outfile.empty()) outfile = input.getValue() + ".svg";
exportSVG(model, outfile, lhArg.getValue(), flhArg.getValue(), scaleArg.getValue(), rotArg.getValue());
} else { } else {
std::cerr << "error: only svg export currently supported"<< std::endl; std::cerr << "error: only --export-svg and --export-obj are currently supported"<< std::endl;
return 1; return 1;
} }
}catch (TCLAP::ArgException &e) } catch (TCLAP::ArgException &e) {
{ std::cerr << "error: " << e.error() << " for arg " << e.argId() << std::endl; } std::cerr << "error: " << e.error() << " for arg " << e.argId() << std::endl;
return 1;
}
return 0;
} }

View File

@ -4,7 +4,7 @@
namespace Slic3r { namespace IO { namespace Slic3r { namespace IO {
bool bool
STL::read_file(std::string input_file, Model* model) STL::read(std::string input_file, Model* model)
{ {
// TODO: encode file name // TODO: encode file name
// TODO: check that file exists // TODO: check that file exists

View File

@ -11,14 +11,14 @@ namespace Slic3r { namespace IO {
class STL class STL
{ {
public: public:
bool read_file(std::string input_file, Model* model); static bool read(std::string input_file, Model* model);
bool write(TriangleMesh& mesh, std::string output_file, bool binary = true); static bool write(TriangleMesh& mesh, std::string output_file, bool binary = true);
}; };
class OBJ class OBJ
{ {
public: public:
bool write(TriangleMesh& mesh, std::string output_file); static bool write(TriangleMesh& mesh, std::string output_file);
}; };
} } } }

View File

@ -22,9 +22,9 @@ SVGExport::SVGExport(TriangleMesh &t, float layerheight, float firstlayerheight)
//<g id="layer0" slic3r:z="0.1"> <path...> <path...> </g> //<g id="layer0" slic3r:z="0.1"> <path...> <path...> </g>
void SVGExport::writeSVG(const char* outputfile){ void SVGExport::writeSVG(const std::string &outputfile){
if(sliced){ if(sliced){
FILE* f = fopen(outputfile, "w"); FILE* f = fopen(outputfile.c_str(), "w");
fprintf(f, fprintf(f,
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"
"<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.0//EN\" \"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd\">\n" "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.0//EN\" \"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd\">\n"
@ -32,7 +32,7 @@ void SVGExport::writeSVG(const char* outputfile){
"<!-- Generated using Slic3r %s http://slic3r.org/ -->\n" "<!-- Generated using Slic3r %s http://slic3r.org/ -->\n"
,t->stl.stats.max.x*10,t->stl.stats.max.y*10,SLIC3R_VERSION); ,t->stl.stats.max.x*10,t->stl.stats.max.y*10,SLIC3R_VERSION);
for (int i=0;i<heights.size();i++){ for (int i=0;i<heights.size();i++){
fprintf(f,"\t<g id=\"layer%d\" slic3r:z=\"%0.4f\">",i,heights[i]); fprintf(f,"\t<g id=\"layer%d\" slic3r:z=\"%0.4f\">\n",i,heights[i]);
for (ExPolygons::const_iterator it = layers[i].begin(); it != layers[i].end(); ++it){ for (ExPolygons::const_iterator it = layers[i].begin(); it != layers[i].end(); ++it){
std::string pd; std::string pd;
Polygons pp = *it; Polygons pp = *it;

View File

@ -12,7 +12,7 @@ class SVGExport
{ {
public: public:
SVGExport(TriangleMesh &t, float layerheight, float firstlayerheight=0.0); SVGExport(TriangleMesh &t, float layerheight, float firstlayerheight=0.0);
void writeSVG(const char* outputfile); void writeSVG(const std::string &outputfile);
private: private:
TriangleMesh *t; TriangleMesh *t;
std::vector<ExPolygons> layers; std::vector<ExPolygons> layers;