Added C++ command line processing, thanks @alexrj and @loh

This commit is contained in:
bubnikv 2018-09-20 16:48:13 +02:00
parent 6ca5a18d05
commit add45a8f6e
11 changed files with 467 additions and 105 deletions

View file

@ -1,7 +1,12 @@
//#include "ConfigBase.hpp"
#include "Config.hpp"
#include "Geometry.hpp"
//#include "IO.hpp"
#include "Model.hpp"
//#include "SLAPrint.hpp"
#include "Print.hpp"
#include "TriangleMesh.hpp"
#include "Format/3mf.hpp"
#include "libslic3r.h"
#include <cstdio>
#include <string>
@ -12,6 +17,9 @@
#include <boost/nowide/args.hpp>
#include <boost/nowide/iostream.hpp>
#ifdef USE_WX
// #include "GUI/GUI.hpp"
#endif
#include "slic3r/GUI/GUI.hpp"
using namespace Slic3r;
@ -87,37 +95,55 @@ void MyFrame::OnHello(wxCommandEvent& event)
wxLogMessage("Hello world from wxWidgets!");
}
/// utility function for displaying CLI usage
void printUsage();
#if 1
int
main(int argc, char **argv)
using namespace Slic3r;
int main(int argc, char **argv)
{
// Convert arguments to UTF-8 (needed on Windows).
// argv then points to memory owned by a.
// Convert arguments to UTF-8 (needed on Windows). argv then points to memory owned by a.
boost::nowide::args a(argc, argv);
#if 0
// parse all command line options into a DynamicConfig
ConfigDef config_def;
config_def.merge(cli_config_def);
config_def.merge(print_config_def);
DynamicConfig config(&config_def);
DynamicPrintAndCLIConfig config;
t_config_option_keys input_files;
config.read_cli(argc, argv, &input_files);
// if any option is unsupported, print usage and abort immediately
if (! config.read_cli(argc, argv, &input_files)) {
printUsage();
return 0;
}
// apply command line options to a more handy CLIConfig
CLIConfig cli_config;
cli_config.apply(config, true);
DynamicPrintConfig print_config;
#if 1
MyApp *gui = new MyApp();
MyApp::SetInstance(gui);
wxEntry(argc, argv);
#endif
#ifdef USE_WX
if (cli_config.gui) {
GUI::App *gui = new GUI::App();
GUI::App::SetInstance(gui);
wxEntry(argc, argv);
}
#else
if (cli_config.gui) {
std::cout << "GUI support has not been built." << "\n";
}
#endif
// load config files supplied via --load
for (const std::string &file : cli_config.load.values) {
if (!boost::filesystem::exists(file)) {
boost::nowide::cout << "No such file: " << file << std::endl;
exit(1);
}
DynamicPrintConfig c;
try {
c.load(file);
@ -135,12 +161,13 @@ main(int argc, char **argv)
print_config.normalize();
// write config if requested
if (!cli_config.save.value.empty()) print_config.save(cli_config.save.value);
if (! cli_config.save.value.empty())
print_config.save(cli_config.save.value);
// read input file(s) if any
std::vector<Model> models;
for (const t_config_option_key &file : input_files) {
if (!boost::filesystem::exists(file)) {
if (! boost::filesystem::exists(file)) {
boost::nowide::cerr << "No such file: " << file << std::endl;
exit(1);
}
@ -162,9 +189,10 @@ main(int argc, char **argv)
// apply command line transform options
for (ModelObject* o : model.objects) {
/*
if (cli_config.scale_to_fit.is_positive_volume())
o->scale_to_fit(cli_config.scale_to_fit.value);
*/
// TODO: honor option order?
o->scale(cli_config.scale.value);
o->rotate(Geometry::deg2rad(cli_config.rotate_x.value), X);
@ -175,87 +203,79 @@ main(int argc, char **argv)
// TODO: handle --merge
models.push_back(model);
}
if (cli_config.help) {
printUsage();
return 0;
}
for (Model &model : models) {
if (cli_config.info) {
// --info works on unrepaired model
model.print_info();
} else if (cli_config.export_obj) {
} else if (cli_config.export_3mf) {
std::string outfile = cli_config.output.value;
if (outfile.empty()) outfile = model.objects.front()->input_file + ".obj";
TriangleMesh mesh = model.mesh();
mesh.repair();
IO::OBJ::write(mesh, outfile);
boost::nowide::cout << "File exported to " << outfile << std::endl;
} else if (cli_config.export_pov) {
std::string outfile = cli_config.output.value;
if (outfile.empty()) outfile = model.objects.front()->input_file + ".pov";
TriangleMesh mesh = model.mesh();
mesh.repair();
IO::POV::write(mesh, outfile);
boost::nowide::cout << "File exported to " << outfile << std::endl;
} else if (cli_config.export_svg) {
std::string outfile = cli_config.output.value;
if (outfile.empty()) outfile = model.objects.front()->input_file + ".svg";
SLAPrint print(&model);
print.config.apply(print_config, true);
print.slice();
print.write_svg(outfile);
boost::nowide::cout << "SVG file exported to " << outfile << std::endl;
} else if (cli_config.cut_x > 0 || cli_config.cut_y > 0 || cli_config.cut > 0) {
if (outfile.empty()) outfile = model.objects.front()->input_file;
// Check if the file is already a 3mf.
if(outfile.substr(outfile.find_last_of('.'), outfile.length()) == ".3mf")
outfile = outfile.substr(0, outfile.find_last_of('.')) + "_2" + ".3mf";
else
// Remove the previous extension and add .3mf extention.
outfile = outfile.substr(0, outfile.find_last_of('.')) + ".3mf";
store_3mf(outfile.c_str(), &model, nullptr, false);
boost::nowide::cout << "File file exported to " << outfile << std::endl;
} else if (cli_config.cut > 0) {
model.repair();
model.translate(0, 0, -model.bounding_box().min.z);
if (!model.objects.empty()) {
// FIXME: cut all objects
model.translate(0, 0, - model.bounding_box().min(2));
if (! model.objects.empty()) {
Model out;
if (cli_config.cut_x > 0) {
model.objects.front()->cut(X, cli_config.cut_x, &out);
} else if (cli_config.cut_y > 0) {
model.objects.front()->cut(Y, cli_config.cut_y, &out);
} else {
model.objects.front()->cut(Z, cli_config.cut, &out);
}
model.objects.front()->cut(cli_config.cut, &out);
ModelObject &upper = *out.objects[0];
ModelObject &lower = *out.objects[1];
if (upper.facets_count() > 0) {
TriangleMesh m = upper.mesh();
IO::STL::write(m, upper.input_file + "_upper.stl");
}
if (lower.facets_count() > 0) {
TriangleMesh m = lower.mesh();
IO::STL::write(m, lower.input_file + "_lower.stl");
}
// Use the input name and trim off the extension.
std::string outfile = cli_config.output.value;
if (outfile.empty())
outfile = model.objects.front()->input_file;
outfile = outfile.substr(0, outfile.find_last_of('.'));
std::cerr << outfile << "\n";
if (upper.facets_count() > 0)
upper.mesh().write_binary((outfile + "_upper.stl").c_str());
if (lower.facets_count() > 0)
lower.mesh().write_binary((outfile + "_lower.stl").c_str());
}
} else if (cli_config.cut_grid.value.x > 0 && cli_config.cut_grid.value.y > 0) {
TriangleMesh mesh = model.mesh();
mesh.repair();
TriangleMeshPtrs meshes = mesh.cut_by_grid(cli_config.cut_grid.value);
size_t i = 0;
for (TriangleMesh* m : meshes) {
std::ostringstream ss;
ss << model.objects.front()->input_file << "_" << i++ << ".stl";
IO::STL::write(*m, ss.str());
delete m;
} else if (cli_config.slice) {
std::string outfile = cli_config.output.value;
Print print;
model.arrange_objects(print.config().min_object_distance());
model.center_instances_around_point(cli_config.center);
if (outfile.empty()) outfile = model.objects.front()->input_file + ".gcode";
print.apply_config(print_config);
for (auto* mo : model.objects) {
print.auto_assign_extruders(mo);
print.add_model_object(mo);
}
print.validate();
print.export_gcode(outfile, nullptr);
} else {
boost::nowide::cerr << "error: command not supported" << std::endl;
return 1;
}
}
#endif
MyApp *gui = new MyApp();
MyApp::SetInstance(gui);
wxEntry(argc, argv);
return 0;
}
#endif
void printUsage()
{
std::cout << "Slic3r " << SLIC3R_VERSION << " is a STL-to-GCODE translator for RepRap 3D printers" << "\n"
<< "written by Alessandro Ranellucci <aar@cpan.org> - http://slic3r.org/ - https://github.com/slic3r/Slic3r" << "\n"
// << "Git Version " << BUILD_COMMIT << "\n\n"
<< "Usage: ./slic3r [ OPTIONS ] [ file.stl ] [ file2.stl ] ..." << "\n";
// CLI Options
std::cout << "** CLI OPTIONS **\n";
print_cli_options(boost::nowide::cout);
std::cout << "****\n";
// Print options
std::cout << "** PRINT OPTIONS **\n";
print_print_options(boost::nowide::cout);
std::cout << "****\n";
}