mirror of
https://git.mirrors.martin98.com/https://github.com/prusa3d/PrusaSlicer.git
synced 2025-08-14 00:26:01 +08:00
Added --export-obj to slic3r.cpp. Some refactoring included
This commit is contained in:
parent
b0378dddc6
commit
64da78788b
@ -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
|
||||
|
@ -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<char *>(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<std::string> 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<float> 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<std::string> 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;
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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);
|
||||
};
|
||||
|
||||
} }
|
||||
|
@ -22,9 +22,9 @@ SVGExport::SVGExport(TriangleMesh &t, float layerheight, float firstlayerheight)
|
||||
|
||||
//<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){
|
||||
FILE* f = fopen(outputfile, "w");
|
||||
FILE* f = fopen(outputfile.c_str(), "w");
|
||||
fprintf(f,
|
||||
"<?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"
|
||||
@ -32,7 +32,7 @@ void SVGExport::writeSVG(const char* outputfile){
|
||||
"<!-- Generated using Slic3r %s http://slic3r.org/ -->\n"
|
||||
,t->stl.stats.max.x*10,t->stl.stats.max.y*10,SLIC3R_VERSION);
|
||||
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){
|
||||
std::string pd;
|
||||
Polygons pp = *it;
|
||||
|
@ -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<ExPolygons> layers;
|
||||
|
Loading…
x
Reference in New Issue
Block a user