diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 89bd31396..2c117bdca 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -40,6 +40,7 @@ add_library(libslic3r STATIC ${LIBDIR}/libslic3r/BoundingBox.cpp ${LIBDIR}/libslic3r/BridgeDetector.cpp ${LIBDIR}/libslic3r/Extruder.cpp ${LIBDIR}/libslic3r/GCodeSender.cpp + ${LIBDIR}/libslic3r/IO.cpp ${LIBDIR}/libslic3r/Line.cpp ${LIBDIR}/libslic3r/PlaceholderParser.cpp ${LIBDIR}/libslic3r/PrintConfig.cpp diff --git a/src/slic3r.cpp b/src/slic3r.cpp index 7e39ae0e3..a7e640f2a 100644 --- a/src/slic3r.cpp +++ b/src/slic3r.cpp @@ -1,5 +1,5 @@ - - +#include "Model.hpp" +#include "IO.hpp" #include "TriangleMesh.hpp" #include "SVGExport.hpp" #include "libslic3r.h" @@ -13,29 +13,20 @@ using namespace Slic3r; 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.){ - TriangleMesh t; - std::string outname; - if(strlen(svgname)==0){ - outname=outname+stlname+".svg"; - }else{ - outname=outname+svgname; - } - t.ReadSTLFile(const_cast(stlname)); - 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); +void exportSVG(const Model &model, const std::string &outfile, float layerheight=0.2, float initialheight=0., float scale=1.0, float rotate=0.){ + TriangleMesh mesh = model.mesh(); + mesh.scale(scale); + mesh.rotate_z(rotate); + mesh.mirror_x(); + mesh.align_to_origin(); + SVGExport e(mesh, layerheight, initialheight); + e.writeSVG(outfile); + printf("writing: %s\n", outfile.c_str()); } int main (int argc, char **argv){ - try{ + try { TCLAP::CmdLine cmd("Rudimentary commandline slic3r, currently only supports STL to SVG slice export.", ' ', SLIC3R_VERSION); TCLAP::ValueArg outputArg("o","output","File to output results to",false,"","output file name"); cmd.add( outputArg ); @@ -48,18 +39,41 @@ int main (int argc, char **argv){ TCLAP::ValueArg lhArg("","layer-height","Layer height in mm (default: 0.2)",false,0.2,"float"); cmd.add( lhArg ); 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 input("inputfile","Input STL file name", true, "", "input file name"); cmd.add(input); cmd.parse(argc,argv); - if(expsvg.getValue()){ - exportSVG(input.getValue().data(),outputArg.getValue().data(),lhArg.getValue(),flhArg.getValue(),scaleArg.getValue(),rotArg.getValue()); - }else{ - std::cerr << "error: only svg export currently supported"<< std::endl; + + // 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 { + std::cerr << "error: only --export-svg and --export-obj are currently supported"<< std::endl; return 1; } - }catch (TCLAP::ArgException &e) - { std::cerr << "error: " << e.error() << " for arg " << e.argId() << std::endl; } + } catch (TCLAP::ArgException &e) { + std::cerr << "error: " << e.error() << " for arg " << e.argId() << std::endl; + return 1; + } + return 0; } diff --git a/xs/src/libslic3r/IO.cpp b/xs/src/libslic3r/IO.cpp index 81ac8a127..7bd42cea5 100644 --- a/xs/src/libslic3r/IO.cpp +++ b/xs/src/libslic3r/IO.cpp @@ -4,7 +4,7 @@ namespace Slic3r { namespace IO { bool -STL::read_file(std::string input_file, Model* model) +STL::read(std::string input_file, Model* model) { // TODO: encode file name // TODO: check that file exists diff --git a/xs/src/libslic3r/IO.hpp b/xs/src/libslic3r/IO.hpp index 248f3be54..0c3cd991f 100644 --- a/xs/src/libslic3r/IO.hpp +++ b/xs/src/libslic3r/IO.hpp @@ -11,14 +11,14 @@ namespace Slic3r { namespace IO { class STL { public: - bool read_file(std::string input_file, Model* model); - bool write(TriangleMesh& mesh, std::string output_file, bool binary = true); + static bool read(std::string input_file, Model* model); + static bool write(TriangleMesh& mesh, std::string output_file, bool binary = true); }; class OBJ { public: - bool write(TriangleMesh& mesh, std::string output_file); + static bool write(TriangleMesh& mesh, std::string output_file); }; } } diff --git a/xs/src/libslic3r/SVGExport.cpp b/xs/src/libslic3r/SVGExport.cpp index 2fb1b0cf5..ead30a04e 100644 --- a/xs/src/libslic3r/SVGExport.cpp +++ b/xs/src/libslic3r/SVGExport.cpp @@ -22,9 +22,9 @@ SVGExport::SVGExport(TriangleMesh &t, float layerheight, float firstlayerheight) // -void SVGExport::writeSVG(const char* outputfile){ +void SVGExport::writeSVG(const std::string &outputfile){ if(sliced){ - FILE* f = fopen(outputfile, "w"); + FILE* f = fopen(outputfile.c_str(), "w"); fprintf(f, "\n" "\n" @@ -32,7 +32,7 @@ void SVGExport::writeSVG(const char* outputfile){ "\n" ,t->stl.stats.max.x*10,t->stl.stats.max.y*10,SLIC3R_VERSION); for (int i=0;i",i,heights[i]); + fprintf(f,"\t\n",i,heights[i]); for (ExPolygons::const_iterator it = layers[i].begin(); it != layers[i].end(); ++it){ std::string pd; Polygons pp = *it; diff --git a/xs/src/libslic3r/SVGExport.hpp b/xs/src/libslic3r/SVGExport.hpp index 554e627b6..3d5f2b127 100644 --- a/xs/src/libslic3r/SVGExport.hpp +++ b/xs/src/libslic3r/SVGExport.hpp @@ -12,7 +12,7 @@ class SVGExport { public: SVGExport(TriangleMesh &t, float layerheight, float firstlayerheight=0.0); - void writeSVG(const char* outputfile); + void writeSVG(const std::string &outputfile); private: TriangleMesh *t; std::vector layers;