slic3r.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. #include "slic3r.hpp"
  2. #include "Geometry.hpp"
  3. #include "IO.hpp"
  4. #include "SLAPrint.hpp"
  5. #include "Print.hpp"
  6. #include "SimplePrint.hpp"
  7. #include "TriangleMesh.hpp"
  8. #include "libslic3r.h"
  9. #include <cmath>
  10. #include <chrono>
  11. #include <cstdio>
  12. #include <string>
  13. #include <cstring>
  14. #include <iomanip>
  15. #include <iostream>
  16. #include <math.h>
  17. #include <boost/filesystem.hpp>
  18. #include <boost/nowide/args.hpp>
  19. #include <boost/nowide/iostream.hpp>
  20. #include <stdexcept>
  21. #ifdef USE_WX
  22. #include "GUI/GUI.hpp"
  23. #endif
  24. using namespace Slic3r;
  25. #ifndef BUILD_TEST
  26. int
  27. main(int argc, char **argv) {
  28. return CLI().run(argc, argv);
  29. }
  30. #endif // BUILD_TEST
  31. int CLI::run(int argc, char **argv) {
  32. // Convert arguments to UTF-8 (needed on Windows).
  33. // argv then points to memory owned by a.
  34. boost::nowide::args a(argc, argv);
  35. // parse all command line options into a DynamicConfig
  36. t_config_option_keys opt_order;
  37. this->config_def.merge(cli_actions_config_def);
  38. this->config_def.merge(cli_transform_config_def);
  39. this->config_def.merge(cli_misc_config_def);
  40. this->config_def.merge(print_config_def);
  41. this->config.def = &this->config_def;
  42. // if any option is unsupported, print usage and abort immediately
  43. if (!this->config.read_cli(argc, argv, &this->input_files, &opt_order)) {
  44. this->print_help();
  45. return 1;
  46. }
  47. // parse actions and transform options
  48. for (auto const &opt_key : opt_order) {
  49. if (cli_actions_config_def.has(opt_key)) this->actions.push_back(opt_key);
  50. if (cli_transform_config_def.has(opt_key)) this->transforms.push_back(opt_key);
  51. }
  52. // load config files supplied via --load
  53. for (auto const &file : config.getStrings("load")) {
  54. if (!boost::filesystem::exists(file)) {
  55. if (config.getBool("ignore_nonexistent_file", false)) {
  56. continue;
  57. } else {
  58. boost::nowide::cerr << "No such file: " << file << std::endl;
  59. exit(1);
  60. }
  61. }
  62. DynamicPrintConfig c;
  63. try {
  64. c.load(file);
  65. } catch (std::exception &e) {
  66. boost::nowide::cerr << "Error while reading config file: " << e.what() << std::endl;
  67. exit(1);
  68. }
  69. c.normalize();
  70. this->print_config.apply(c);
  71. }
  72. // apply command line options to a more specific DynamicPrintConfig which provides normalize()
  73. // (command line options override --load files)
  74. this->print_config.apply(config, true);
  75. this->print_config.normalize();
  76. // create a static (full) print config to be used in our logic
  77. this->full_print_config.apply(this->print_config);
  78. // validate config
  79. try {
  80. this->full_print_config.validate();
  81. } catch (InvalidOptionException &e) {
  82. boost::nowide::cerr << e.what() << std::endl;
  83. return 1;
  84. }
  85. // read input file(s) if any
  86. for (auto const &file : input_files) {
  87. Model model;
  88. try {
  89. model = Model::read_from_file(file);
  90. } catch (std::exception &e) {
  91. boost::nowide::cerr << file << ": " << e.what() << std::endl;
  92. exit(1);
  93. }
  94. if (model.objects.empty()) {
  95. boost::nowide::cerr << "Error: file is empty: " << file << std::endl;
  96. continue;
  97. }
  98. this->models.push_back(model);
  99. }
  100. // loop through transform options
  101. for (auto const &opt_key : this->transforms) {
  102. if (opt_key == "merge") {
  103. Model m;
  104. for (auto &model : this->models)
  105. m.merge(model);
  106. // Rearrange instances unless --dont-arrange is supplied
  107. if (!this->config.getBool("dont_arrange")) {
  108. m.add_default_instances();
  109. const BoundingBoxf bb{ this->full_print_config.bed_shape.values };
  110. m.arrange_objects(
  111. this->full_print_config.min_object_distance(),
  112. // if we are going to use the merged model for printing, honor
  113. // the configured print bed for arranging, otherwise do it freely
  114. this->has_print_action() ? &bb : nullptr
  115. );
  116. }
  117. this->models = {m};
  118. } else if (opt_key == "duplicate") {
  119. const BoundingBoxf bb{ this->full_print_config.bed_shape.values };
  120. for (auto &model : this->models) {
  121. const bool all_objects_have_instances = std::none_of(
  122. model.objects.begin(), model.objects.end(),
  123. [](ModelObject* o){ return o->instances.empty(); }
  124. );
  125. if (all_objects_have_instances) {
  126. // if all input objects have defined position(s) apply duplication to the whole model
  127. model.duplicate(this->config.getInt("duplicate"), this->full_print_config.min_object_distance(), &bb);
  128. } else {
  129. model.add_default_instances();
  130. model.duplicate_objects(this->config.getInt("duplicate"), this->full_print_config.min_object_distance(), &bb);
  131. }
  132. }
  133. } else if (opt_key == "duplicate_grid") {
  134. auto &ints = this->config.opt<ConfigOptionInts>("duplicate_grid")->values;
  135. const int x = ints.size() > 0 ? ints.at(0) : 1;
  136. const int y = ints.size() > 1 ? ints.at(1) : 1;
  137. const double distance = this->full_print_config.duplicate_distance.value;
  138. for (auto &model : this->models)
  139. model.duplicate_objects_grid(x, y, (distance > 0) ? distance : 6); // TODO: this is not the right place for setting a default
  140. } else if (opt_key == "center") {
  141. for (auto &model : this->models) {
  142. model.add_default_instances();
  143. // this affects instances:
  144. model.center_instances_around_point(config.opt<ConfigOptionPoint>("center")->value);
  145. // this affects volumes:
  146. model.align_to_ground();
  147. }
  148. } else if (opt_key == "align_xy") {
  149. const Pointf p{ this->config.opt<ConfigOptionPoint>("align_xy")->value };
  150. for (auto &model : this->models) {
  151. BoundingBoxf3 bb{ model.bounding_box() };
  152. // this affects volumes:
  153. model.translate(-(bb.min.x - p.x), -(bb.min.y - p.y), -bb.min.z);
  154. }
  155. } else if (opt_key == "dont_arrange") {
  156. // do nothing - this option alters other transform options
  157. } else if (opt_key == "rotate") {
  158. for (auto &model : this->models)
  159. for (auto &o : model.objects)
  160. // this affects volumes:
  161. o->rotate(Geometry::deg2rad(config.getFloat(opt_key)), Z);
  162. } else if (opt_key == "rotate_x") {
  163. for (auto &model : this->models)
  164. for (auto &o : model.objects)
  165. // this affects volumes:
  166. o->rotate(Geometry::deg2rad(config.getFloat(opt_key)), X);
  167. } else if (opt_key == "rotate_y") {
  168. for (auto &model : this->models)
  169. for (auto &o : model.objects)
  170. // this affects volumes:
  171. o->rotate(Geometry::deg2rad(config.getFloat(opt_key)), Y);
  172. } else if (opt_key == "scale") {
  173. for (auto &model : this->models)
  174. for (auto &o : model.objects)
  175. // this affects volumes:
  176. o->scale(config.get_abs_value(opt_key, 1));
  177. } else if (opt_key == "scale_to_fit") {
  178. const auto opt = config.opt<ConfigOptionPoint3>(opt_key);
  179. if (!opt->is_positive_volume()) {
  180. boost::nowide::cerr << "--scale-to-fit requires a positive volume" << std::endl;
  181. return 1;
  182. }
  183. for (auto &model : this->models)
  184. for (auto &o : model.objects)
  185. // this affects volumes:
  186. o->scale_to_fit(opt->value);
  187. } else if (opt_key == "cut" || opt_key == "cut_x" || opt_key == "cut_y") {
  188. std::vector<Model> new_models;
  189. for (auto &model : this->models) {
  190. model.repair();
  191. model.translate(0, 0, -model.bounding_box().min.z); // align to z = 0
  192. Model out;
  193. for (auto &o : model.objects) {
  194. if (opt_key == "cut_x") {
  195. o->cut(X, config.getFloat("cut_x"), &out);
  196. } else if (opt_key == "cut_y") {
  197. o->cut(Y, config.getFloat("cut_y"), &out);
  198. } else if (opt_key == "cut") {
  199. o->cut(Z, config.getFloat("cut"), &out);
  200. }
  201. }
  202. // add each resulting object as a distinct model
  203. Model upper, lower;
  204. auto upper_obj = upper.add_object(*out.objects[0]);
  205. auto lower_obj = lower.add_object(*out.objects[1]);
  206. if (upper_obj->facets_count() > 0) new_models.push_back(upper);
  207. if (lower_obj->facets_count() > 0) new_models.push_back(lower);
  208. }
  209. // TODO: copy less stuff around using pointers
  210. this->models = new_models;
  211. if (this->actions.empty())
  212. this->actions.push_back("export_stl");
  213. } else if (opt_key == "cut_grid") {
  214. std::vector<Model> new_models;
  215. for (auto &model : this->models) {
  216. TriangleMesh mesh = model.mesh();
  217. mesh.repair();
  218. TriangleMeshPtrs meshes = mesh.cut_by_grid(config.opt<ConfigOptionPoint>("cut_grid")->value);
  219. size_t i = 0;
  220. for (TriangleMesh* m : meshes) {
  221. Model out;
  222. auto o = out.add_object();
  223. o->add_volume(*m);
  224. o->input_file += "_" + std::to_string(i++);
  225. delete m;
  226. }
  227. }
  228. // TODO: copy less stuff around using pointers
  229. this->models = new_models;
  230. if (this->actions.empty())
  231. this->actions.push_back("export_stl");
  232. } else if (opt_key == "split") {
  233. for (auto &model : this->models)
  234. model.split();
  235. } else if (opt_key == "repair") {
  236. for (auto &model : this->models)
  237. model.repair();
  238. } else {
  239. boost::nowide::cerr << "error: option not implemented yet: " << opt_key << std::endl;
  240. return 1;
  241. }
  242. }
  243. // loop through action options
  244. for (auto const &opt_key : this->actions) {
  245. if (opt_key == "help") {
  246. this->print_help();
  247. } else if (opt_key == "help_options") {
  248. this->print_help(true);
  249. } else if (opt_key == "save") {
  250. this->print_config.save(config.getString("save"));
  251. } else if (opt_key == "info") {
  252. // --info works on unrepaired model
  253. for (Model &model : this->models) {
  254. model.add_default_instances();
  255. model.print_info();
  256. }
  257. } else if (opt_key == "export_stl") {
  258. for (auto &model : this->models)
  259. model.add_default_instances();
  260. this->export_models(IO::STL);
  261. } else if (opt_key == "export_obj") {
  262. for (auto &model : this->models)
  263. model.add_default_instances();
  264. this->export_models(IO::OBJ);
  265. } else if (opt_key == "export_pov") {
  266. for (auto &model : this->models)
  267. model.add_default_instances();
  268. this->export_models(IO::POV);
  269. } else if (opt_key == "export_amf") {
  270. this->export_models(IO::AMF);
  271. } else if (opt_key == "export_3mf") {
  272. this->export_models(IO::TMF);
  273. } else if (opt_key == "export_sla") {
  274. boost::nowide::cerr << "--export-sla is not implemented yet" << std::endl;
  275. } else if (opt_key == "export_sla_svg") {
  276. for (const Model &model : this->models) {
  277. SLAPrint print(&model); // initialize print with model
  278. print.config.apply(this->print_config, true); // apply configuration
  279. print.slice(); // slice file
  280. const std::string outfile = this->output_filepath(model, IO::SVG);
  281. print.write_svg(outfile); // write SVG
  282. boost::nowide::cout << "SVG file exported to " << outfile << std::endl;
  283. }
  284. } else if (opt_key == "export_gcode") {
  285. for (const Model &model : this->models) {
  286. // If all objects have defined instances, their relative positions will be
  287. // honored when printing (they will be only centered, unless --dont-arrange
  288. // is supplied); if any object has no instances, it will get a default one
  289. // and all instances will be rearranged (unless --dont-arrange is supplied).
  290. SimplePrint print;
  291. print.status_cb = [](int ln, const std::string& msg) {
  292. boost::nowide::cout << msg << std::endl;
  293. };
  294. print.apply_config(this->print_config);
  295. print.arrange = !this->config.getBool("dont_arrange");
  296. print.center = !this->config.has("center")
  297. && !this->config.has("align_xy")
  298. && !this->config.getBool("dont_arrange");
  299. print.set_model(model);
  300. // start chronometer
  301. typedef std::chrono::high_resolution_clock clock_;
  302. typedef std::chrono::duration<double, std::ratio<1> > second_;
  303. std::chrono::time_point<clock_> t0{ clock_::now() };
  304. const std::string outfile = this->output_filepath(model, IO::Gcode);
  305. try {
  306. print.export_gcode(outfile);
  307. } catch (std::runtime_error &e) {
  308. boost::nowide::cerr << e.what() << std::endl;
  309. return 1;
  310. }
  311. boost::nowide::cout << "G-code exported to " << outfile << std::endl;
  312. // output some statistics
  313. double duration { std::chrono::duration_cast<second_>(clock_::now() - t0).count() };
  314. boost::nowide::cout << std::fixed << std::setprecision(0)
  315. << "Done. Process took " << (duration/60) << " minutes and "
  316. << std::setprecision(3)
  317. << std::fmod(duration, 60.0) << " seconds." << std::endl
  318. << std::setprecision(2)
  319. << "Filament required: " << print.total_used_filament() << "mm"
  320. << " (" << print.total_extruded_volume()/1000 << "cm3)" << std::endl;
  321. }
  322. } else {
  323. boost::nowide::cerr << "error: option not supported yet: " << opt_key << std::endl;
  324. return 1;
  325. }
  326. }
  327. if (actions.empty()) {
  328. #ifdef USE_WX
  329. GUI::App *gui = new GUI::App();
  330. gui->autosave = this->config.getString("autosave");
  331. gui->datadir = this->config.getString("datadir");
  332. GUI::App::SetInstance(gui);
  333. wxEntry(argc, argv);
  334. #else
  335. std::cout << "GUI support has not been built." << "\n";
  336. #endif
  337. }
  338. return 0;
  339. }
  340. void
  341. CLI::print_help(bool include_print_options) const {
  342. boost::nowide::cout
  343. << "Slic3r " << SLIC3R_VERSION << " (build commit: " << BUILD_COMMIT << ")" << std::endl
  344. << "https://slic3r.org/ - https://github.com/slic3r/Slic3r" << std::endl << std::endl
  345. << "Usage: slic3r [ ACTIONS ] [ TRANSFORM ] [ OPTIONS ] [ file.stl ... ]" << std::endl
  346. << std::endl
  347. << "Actions:" << std::endl;
  348. cli_actions_config_def.print_cli_help(boost::nowide::cout, false);
  349. boost::nowide::cout
  350. << std::endl
  351. << "Transform options:" << std::endl;
  352. cli_transform_config_def.print_cli_help(boost::nowide::cout, false);
  353. boost::nowide::cout
  354. << std::endl
  355. << "Other options:" << std::endl;
  356. cli_misc_config_def.print_cli_help(boost::nowide::cout, false);
  357. if (include_print_options) {
  358. boost::nowide::cout << std::endl;
  359. print_config_def.print_cli_help(boost::nowide::cout, true);
  360. } else {
  361. boost::nowide::cout
  362. << std::endl
  363. << "Run --help-options to see the full listing of print/G-code options." << std::endl;
  364. }
  365. }
  366. void
  367. CLI::export_models(IO::ExportFormat format) {
  368. for (const Model& model : this->models) {
  369. const std::string outfile = this->output_filepath(model, format);
  370. IO::write_model.at(format)(model, outfile);
  371. std::cout << "File exported to " << outfile << std::endl;
  372. }
  373. }
  374. std::string
  375. CLI::output_filepath(const Model &model, IO::ExportFormat format) const {
  376. // get the --output-filename-format option
  377. std::string filename_format = this->print_config.getString("output_filename_format", "[input_filename_base]");
  378. // strip the file extension and add the correct one
  379. filename_format = filename_format.substr(0, filename_format.find_last_of("."));
  380. filename_format += "." + IO::extensions.at(format);
  381. // this is the same logic used in Print::output_filepath()
  382. // TODO: factor it out to a single place?
  383. // find the first input_file of the model
  384. boost::filesystem::path input_file;
  385. for (auto o : model.objects) {
  386. if (!o->input_file.empty()) {
  387. input_file = o->input_file;
  388. break;
  389. }
  390. }
  391. // compute the automatic filename
  392. PlaceholderParser pp;
  393. pp.set("input_filename", input_file.filename().string());
  394. pp.set("input_filename_base", input_file.stem().string());
  395. pp.apply_config(this->config);
  396. const std::string filename = pp.process(filename_format);
  397. // use --output when available
  398. std::string outfile{ this->config.getString("output") };
  399. if (!outfile.empty()) {
  400. // if we were supplied a directory, use it and append our automatically generated filename
  401. const boost::filesystem::path out(outfile);
  402. if (boost::filesystem::is_directory(out))
  403. outfile = (out / filename).string();
  404. } else {
  405. outfile = (input_file.parent_path() / filename).string();
  406. }
  407. return outfile;
  408. }