slic3r.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  1. #ifdef WIN32
  2. // Why?
  3. #define _WIN32_WINNT 0x0502
  4. // The standard Windows includes.
  5. #define WIN32_LEAN_AND_MEAN
  6. #define NOMINMAX
  7. #include <Windows.h>
  8. #include <wchar.h>
  9. // Let the NVIDIA and AMD know we want to use their graphics card
  10. // on a dual graphics card system.
  11. __declspec(dllexport) DWORD NvOptimusEnablement = 0x00000001;
  12. __declspec(dllexport) int AmdPowerXpressRequestHighPerformance = 1;
  13. #endif /* WIN32 */
  14. #include <cstdio>
  15. #include <string>
  16. #include <cstring>
  17. #include <iostream>
  18. #include <math.h>
  19. #include <boost/filesystem.hpp>
  20. #include <boost/nowide/args.hpp>
  21. #include <boost/nowide/cenv.hpp>
  22. #include <boost/nowide/iostream.hpp>
  23. #include "unix/fhs.hpp" // Generated by CMake from ../platform/unix/fhs.hpp.in
  24. #include "libslic3r/libslic3r.h"
  25. #include "libslic3r/Config.hpp"
  26. #include "libslic3r/Geometry.hpp"
  27. #include "libslic3r/Model.hpp"
  28. #include "libslic3r/Print.hpp"
  29. #include "libslic3r/SLAPrint.hpp"
  30. #include "libslic3r/TriangleMesh.hpp"
  31. #include "libslic3r/Format/AMF.hpp"
  32. #include "libslic3r/Format/3mf.hpp"
  33. #include "libslic3r/Format/STL.hpp"
  34. #include "libslic3r/Format/OBJ.hpp"
  35. #include "libslic3r/Utils.hpp"
  36. #include "slic3r.hpp"
  37. #include "slic3r/GUI/GUI.hpp"
  38. #include "slic3r/GUI/GUI_App.hpp"
  39. using namespace Slic3r;
  40. PrinterTechnology get_printer_technology(const DynamicConfig &config)
  41. {
  42. const ConfigOptionEnum<PrinterTechnology> *opt = config.option<ConfigOptionEnum<PrinterTechnology>>("printer_technology");
  43. return (opt == nullptr) ? ptUnknown : opt->value;
  44. }
  45. int CLI::run(int argc, char **argv)
  46. {
  47. if (! this->setup(argc, argv))
  48. return 1;
  49. m_extra_config.apply(m_config, true);
  50. m_extra_config.normalize();
  51. bool start_gui = m_actions.empty() &&
  52. // cutting transformations are setting an "export" action.
  53. std::find(m_transforms.begin(), m_transforms.end(), "cut") == m_transforms.end() &&
  54. std::find(m_transforms.begin(), m_transforms.end(), "cut_x") == m_transforms.end() &&
  55. std::find(m_transforms.begin(), m_transforms.end(), "cut_y") == m_transforms.end();
  56. PrinterTechnology printer_technology = get_printer_technology(m_extra_config);
  57. const std::vector<std::string> &load_configs = m_config.option<ConfigOptionStrings>("load", true)->values;
  58. // load config files supplied via --load
  59. for (auto const &file : load_configs) {
  60. if (! boost::filesystem::exists(file)) {
  61. if (m_config.opt_bool("ignore_nonexistent_file")) {
  62. continue;
  63. } else {
  64. boost::nowide::cerr << "No such file: " << file << std::endl;
  65. return 1;
  66. }
  67. }
  68. DynamicPrintConfig config;
  69. try {
  70. config.load(file);
  71. } catch (std::exception &ex) {
  72. boost::nowide::cerr << "Error while reading config file: " << ex.what() << std::endl;
  73. return 1;
  74. }
  75. config.normalize();
  76. PrinterTechnology other_printer_technology = get_printer_technology(config);
  77. if (printer_technology == ptUnknown) {
  78. printer_technology = other_printer_technology;
  79. } else if (printer_technology != other_printer_technology) {
  80. boost::nowide::cerr << "Mixing configurations for FFF and SLA technologies" << std::endl;
  81. return 1;
  82. }
  83. m_print_config.apply(config);
  84. }
  85. // Read input file(s) if any.
  86. for (const std::string &file : m_input_files) {
  87. if (! boost::filesystem::exists(file)) {
  88. boost::nowide::cerr << "No such file: " << file << std::endl;
  89. exit(1);
  90. }
  91. Model model;
  92. try {
  93. // When loading an AMF or 3MF, config is imported as well, including the printer technology.
  94. model = Model::read_from_file(file, &m_print_config, true);
  95. PrinterTechnology other_printer_technology = get_printer_technology(m_print_config);
  96. if (printer_technology == ptUnknown) {
  97. printer_technology = other_printer_technology;
  98. } else if (printer_technology != other_printer_technology) {
  99. boost::nowide::cerr << "Mixing configurations for FFF and SLA technologies" << std::endl;
  100. return 1;
  101. }
  102. } catch (std::exception &e) {
  103. boost::nowide::cerr << file << ": " << e.what() << std::endl;
  104. return 1;
  105. }
  106. if (model.objects.empty()) {
  107. boost::nowide::cerr << "Error: file is empty: " << file << std::endl;
  108. continue;
  109. }
  110. m_models.push_back(model);
  111. }
  112. // Apply command line options to a more specific DynamicPrintConfig which provides normalize()
  113. // (command line options override --load files)
  114. m_print_config.apply(m_extra_config, true);
  115. // Normalizing after importing the 3MFs / AMFs
  116. m_print_config.normalize();
  117. if (printer_technology == ptUnknown)
  118. printer_technology = std::find(m_actions.begin(), m_actions.end(), "export_sla") == m_actions.end() ? ptFFF : ptSLA;
  119. // Initialize full print configs for both the FFF and SLA technologies.
  120. FullPrintConfig fff_print_config;
  121. SLAFullPrintConfig sla_print_config;
  122. fff_print_config.apply(m_print_config, true);
  123. sla_print_config.apply(m_print_config, true);
  124. // Loop through transform options.
  125. for (auto const &opt_key : m_transforms) {
  126. if (opt_key == "merge") {
  127. Model m;
  128. for (auto &model : m_models)
  129. for (ModelObject *o : model.objects)
  130. m.add_object(*o);
  131. // Rearrange instances unless --dont-arrange is supplied
  132. if (! m_config.opt_bool("dont_arrange")) {
  133. m.add_default_instances();
  134. const BoundingBoxf &bb = fff_print_config.bed_shape.values;
  135. m.arrange_objects(
  136. fff_print_config.min_object_distance(),
  137. // If we are going to use the merged model for printing, honor
  138. // the configured print bed for arranging, otherwise do it freely.
  139. this->has_print_action() ? &bb : nullptr
  140. );
  141. }
  142. m_models.clear();
  143. m_models.emplace_back(std::move(m));
  144. } else if (opt_key == "duplicate") {
  145. const BoundingBoxf &bb = fff_print_config.bed_shape.values;
  146. for (auto &model : m_models) {
  147. const bool all_objects_have_instances = std::none_of(
  148. model.objects.begin(), model.objects.end(),
  149. [](ModelObject* o){ return o->instances.empty(); }
  150. );
  151. if (all_objects_have_instances) {
  152. // if all input objects have defined position(s) apply duplication to the whole model
  153. model.duplicate(m_config.opt_int("duplicate"), fff_print_config.min_object_distance(), &bb);
  154. } else {
  155. model.add_default_instances();
  156. model.duplicate_objects(m_config.opt_int("duplicate"), fff_print_config.min_object_distance(), &bb);
  157. }
  158. }
  159. } else if (opt_key == "duplicate_grid") {
  160. std::vector<int> &ints = m_config.option<ConfigOptionInts>("duplicate_grid")->values;
  161. const int x = ints.size() > 0 ? ints.at(0) : 1;
  162. const int y = ints.size() > 1 ? ints.at(1) : 1;
  163. const double distance = fff_print_config.duplicate_distance.value;
  164. for (auto &model : m_models)
  165. model.duplicate_objects_grid(x, y, (distance > 0) ? distance : 6); // TODO: this is not the right place for setting a default
  166. } else if (opt_key == "center") {
  167. for (auto &model : m_models) {
  168. model.add_default_instances();
  169. // this affects instances:
  170. model.center_instances_around_point(m_config.option<ConfigOptionPoint>("center")->value);
  171. // this affects volumes:
  172. //FIXME Vojtech: Who knows why the complete model should be aligned with Z as a single rigid body?
  173. //model.align_to_ground();
  174. BoundingBoxf3 bbox;
  175. for (ModelObject *model_object : model.objects)
  176. // We are interested into the Z span only, therefore it is sufficient to measure the bounding box of the 1st instance only.
  177. bbox.merge(model_object->instance_bounding_box(0, false));
  178. for (ModelObject *model_object : model.objects)
  179. for (ModelInstance *model_instance : model_object->instances)
  180. model_instance->set_offset(Z, model_instance->get_offset(Z) - bbox.min.z());
  181. }
  182. } else if (opt_key == "align_xy") {
  183. const Vec2d &p = m_config.option<ConfigOptionPoint>("align_xy")->value;
  184. for (auto &model : m_models) {
  185. BoundingBoxf3 bb = model.bounding_box();
  186. // this affects volumes:
  187. model.translate(-(bb.min.x() - p.x()), -(bb.min.y() - p.y()), -bb.min.z());
  188. }
  189. } else if (opt_key == "dont_arrange") {
  190. // do nothing - this option alters other transform options
  191. } else if (opt_key == "rotate") {
  192. for (auto &model : m_models)
  193. for (auto &o : model.objects)
  194. // this affects volumes:
  195. o->rotate(Geometry::deg2rad(m_config.opt_float(opt_key)), Z);
  196. } else if (opt_key == "rotate_x") {
  197. for (auto &model : m_models)
  198. for (auto &o : model.objects)
  199. // this affects volumes:
  200. o->rotate(Geometry::deg2rad(m_config.opt_float(opt_key)), X);
  201. } else if (opt_key == "rotate_y") {
  202. for (auto &model : m_models)
  203. for (auto &o : model.objects)
  204. // this affects volumes:
  205. o->rotate(Geometry::deg2rad(m_config.opt_float(opt_key)), Y);
  206. } else if (opt_key == "scale") {
  207. for (auto &model : m_models)
  208. for (auto &o : model.objects)
  209. // this affects volumes:
  210. o->scale(m_config.get_abs_value(opt_key, 1));
  211. } else if (opt_key == "scale_to_fit") {
  212. const Vec3d &opt = m_config.opt<ConfigOptionPoint3>(opt_key)->value;
  213. if (opt.x() <= 0 || opt.y() <= 0 || opt.z() <= 0) {
  214. boost::nowide::cerr << "--scale-to-fit requires a positive volume" << std::endl;
  215. return 1;
  216. }
  217. for (auto &model : m_models)
  218. for (auto &o : model.objects)
  219. // this affects volumes:
  220. o->scale_to_fit(opt);
  221. } else if (opt_key == "cut" || opt_key == "cut_x" || opt_key == "cut_y") {
  222. std::vector<Model> new_models;
  223. for (auto &model : m_models) {
  224. model.repair();
  225. model.translate(0, 0, -model.bounding_box().min.z()); // align to z = 0
  226. size_t num_objects = model.objects.size();
  227. for (size_t i = 0; i < num_objects; ++ i) {
  228. #if 0
  229. if (opt_key == "cut_x") {
  230. o->cut(X, m_config.opt_float("cut_x"), &out);
  231. } else if (opt_key == "cut_y") {
  232. o->cut(Y, m_config.opt_float("cut_y"), &out);
  233. } else if (opt_key == "cut") {
  234. o->cut(Z, m_config.opt_float("cut"), &out);
  235. }
  236. #else
  237. model.objects.front()->cut(0, m_config.opt_float("cut"), true, true, true);
  238. #endif
  239. model.delete_object(size_t(0));
  240. }
  241. }
  242. // TODO: copy less stuff around using pointers
  243. m_models = new_models;
  244. if (m_actions.empty())
  245. m_actions.push_back("export_stl");
  246. }
  247. #if 0
  248. else if (opt_key == "cut_grid") {
  249. std::vector<Model> new_models;
  250. for (auto &model : m_models) {
  251. TriangleMesh mesh = model.mesh();
  252. mesh.repair();
  253. TriangleMeshPtrs meshes = mesh.cut_by_grid(m_config.option<ConfigOptionPoint>("cut_grid")->value);
  254. size_t i = 0;
  255. for (TriangleMesh* m : meshes) {
  256. Model out;
  257. auto o = out.add_object();
  258. o->add_volume(*m);
  259. o->input_file += "_" + std::to_string(i++);
  260. delete m;
  261. }
  262. }
  263. // TODO: copy less stuff around using pointers
  264. m_models = new_models;
  265. if (m_actions.empty())
  266. m_actions.push_back("export_stl");
  267. }
  268. #endif
  269. else if (opt_key == "split") {
  270. for (Model &model : m_models) {
  271. size_t num_objects = model.objects.size();
  272. for (size_t i = 0; i < num_objects; ++ i) {
  273. model.objects.front()->split(nullptr);
  274. model.delete_object(size_t(0));
  275. }
  276. }
  277. } else if (opt_key == "repair") {
  278. for (auto &model : m_models)
  279. model.repair();
  280. } else {
  281. boost::nowide::cerr << "error: option not implemented yet: " << opt_key << std::endl;
  282. return 1;
  283. }
  284. }
  285. // loop through action options
  286. for (auto const &opt_key : m_actions) {
  287. if (opt_key == "help") {
  288. this->print_help();
  289. } else if (opt_key == "help_fff") {
  290. this->print_help(true, ptFFF);
  291. } else if (opt_key == "help_sla") {
  292. this->print_help(true, ptSLA);
  293. } else if (opt_key == "save") {
  294. //FIXME check for mixing the FFF / SLA parameters.
  295. // or better save fff_print_config vs. sla_print_config
  296. m_print_config.save(m_config.opt_string("save"));
  297. } else if (opt_key == "info") {
  298. // --info works on unrepaired model
  299. for (Model &model : m_models) {
  300. model.add_default_instances();
  301. model.print_info();
  302. }
  303. } else if (opt_key == "export_stl") {
  304. for (auto &model : m_models)
  305. model.add_default_instances();
  306. if (! this->export_models(IO::STL))
  307. return 1;
  308. } else if (opt_key == "export_obj") {
  309. for (auto &model : m_models)
  310. model.add_default_instances();
  311. if (! this->export_models(IO::OBJ))
  312. return 1;
  313. } else if (opt_key == "export_amf") {
  314. if (! this->export_models(IO::AMF))
  315. return 1;
  316. } else if (opt_key == "export_3mf") {
  317. if (! this->export_models(IO::TMF))
  318. return 1;
  319. } else if (opt_key == "export_gcode" || opt_key == "export_sla" || opt_key == "slice") {
  320. if (opt_key == "export_gcode" && printer_technology == ptSLA) {
  321. boost::nowide::cerr << "error: cannot export G-code for an FFF configuration" << std::endl;
  322. return 1;
  323. } else if (opt_key == "export_sla" && printer_technology == ptFFF) {
  324. boost::nowide::cerr << "error: cannot export SLA slices for a SLA configuration" << std::endl;
  325. return 1;
  326. }
  327. // Make a copy of the model if the current action is not the last action, as the model may be
  328. // modified by the centering and such.
  329. Model model_copy;
  330. bool make_copy = &opt_key != &m_actions.back();
  331. for (Model &model_in : m_models) {
  332. if (make_copy)
  333. model_copy = model_in;
  334. Model &model = make_copy ? model_copy : model_in;
  335. // If all objects have defined instances, their relative positions will be
  336. // honored when printing (they will be only centered, unless --dont-arrange
  337. // is supplied); if any object has no instances, it will get a default one
  338. // and all instances will be rearranged (unless --dont-arrange is supplied).
  339. std::string outfile = m_config.opt_string("output");
  340. Print fff_print;
  341. SLAPrint sla_print;
  342. PrintBase *print = (printer_technology == ptFFF) ? static_cast<PrintBase*>(&fff_print) : static_cast<PrintBase*>(&sla_print);
  343. if (! m_config.opt_bool("dont_arrange")) {
  344. //FIXME make the min_object_distance configurable.
  345. model.arrange_objects(fff_print.config().min_object_distance());
  346. model.center_instances_around_point(m_config.option<ConfigOptionPoint>("center")->value);
  347. }
  348. if (printer_technology == ptFFF) {
  349. for (auto* mo : model.objects)
  350. fff_print.auto_assign_extruders(mo);
  351. }
  352. print->apply(model, m_print_config);
  353. std::string err = print->validate();
  354. if (! err.empty()) {
  355. boost::nowide::cerr << err << std::endl;
  356. return 1;
  357. }
  358. if (print->empty())
  359. boost::nowide::cout << "Nothing to print for " << outfile << " . Either the print is empty or no object is fully inside the print volume." << std::endl;
  360. else
  361. try {
  362. std::string outfile_final;
  363. print->process();
  364. if (printer_technology == ptFFF) {
  365. // The outfile is processed by a PlaceholderParser.
  366. outfile = fff_print.export_gcode(outfile, nullptr);
  367. outfile_final = fff_print.print_statistics().finalize_output_path(outfile);
  368. } else {
  369. outfile = sla_print.output_filepath(outfile);
  370. //FIXME Tamas, please port it to miniz
  371. // sla_print.export_raster<SLAZipFmt>(outfile);
  372. outfile_final = sla_print.print_statistics().finalize_output_path(outfile);
  373. }
  374. if (outfile != outfile_final && Slic3r::rename_file(outfile, outfile_final) != 0) {
  375. boost::nowide::cerr << "Renaming file " << outfile << " to " << outfile_final << " failed" << std::endl;
  376. return 1;
  377. }
  378. boost::nowide::cout << "Slicing result exported to " << outfile << std::endl;
  379. } catch (const std::exception &ex) {
  380. boost::nowide::cerr << ex.what() << std::endl;
  381. return 1;
  382. }
  383. /*
  384. print.center = ! m_config.has("center")
  385. && ! m_config.has("align_xy")
  386. && ! m_config.opt_bool("dont_arrange");
  387. print.set_model(model);
  388. // start chronometer
  389. typedef std::chrono::high_resolution_clock clock_;
  390. typedef std::chrono::duration<double, std::ratio<1> > second_;
  391. std::chrono::time_point<clock_> t0{ clock_::now() };
  392. const std::string outfile = this->output_filepath(model, IO::Gcode);
  393. try {
  394. print.export_gcode(outfile);
  395. } catch (std::runtime_error &e) {
  396. boost::nowide::cerr << e.what() << std::endl;
  397. return 1;
  398. }
  399. boost::nowide::cout << "G-code exported to " << outfile << std::endl;
  400. // output some statistics
  401. double duration { std::chrono::duration_cast<second_>(clock_::now() - t0).count() };
  402. boost::nowide::cout << std::fixed << std::setprecision(0)
  403. << "Done. Process took " << (duration/60) << " minutes and "
  404. << std::setprecision(3)
  405. << std::fmod(duration, 60.0) << " seconds." << std::endl
  406. << std::setprecision(2)
  407. << "Filament required: " << print.total_used_filament() << "mm"
  408. << " (" << print.total_extruded_volume()/1000 << "cm3)" << std::endl;
  409. */
  410. }
  411. } else {
  412. boost::nowide::cerr << "error: option not supported yet: " << opt_key << std::endl;
  413. return 1;
  414. }
  415. }
  416. if (start_gui) {
  417. #if 1
  418. // #ifdef USE_WX
  419. GUI::GUI_App *gui = new GUI::GUI_App();
  420. // gui->autosave = m_config.opt_string("autosave");
  421. GUI::GUI_App::SetInstance(gui);
  422. gui->CallAfter([gui, this, &load_configs] {
  423. if (!gui->initialized()) {
  424. return;
  425. }
  426. #if 0
  427. // Load the cummulative config over the currently active profiles.
  428. //FIXME if multiple configs are loaded, only the last one will have an effect.
  429. // We need to decide what to do about loading of separate presets (just print preset, just filament preset etc).
  430. // As of now only the full configs are supported here.
  431. if (!m_print_config.empty())
  432. gui->mainframe->load_config(m_print_config);
  433. #endif
  434. if (! load_configs.empty())
  435. // Load the last config to give it a name at the UI. The name of the preset may be later
  436. // changed by loading an AMF or 3MF.
  437. //FIXME this is not strictly correct, as one may pass a print/filament/printer profile here instead of a full config.
  438. gui->mainframe->load_config_file(load_configs.back());
  439. // If loading a 3MF file, the config is loaded from the last one.
  440. if (! m_input_files.empty())
  441. gui->plater()->load_files(m_input_files, true, true);
  442. if (! m_extra_config.empty())
  443. gui->mainframe->load_config(m_extra_config);
  444. });
  445. return wxEntry(argc, argv);
  446. #else
  447. // No GUI support. Just print out a help.
  448. this->print_help(false);
  449. // If started without a parameter, consider it to be OK, otherwise report an error code (no action etc).
  450. return (argc == 0) ? 0 : 1;
  451. #endif
  452. }
  453. return 0;
  454. }
  455. bool CLI::setup(int argc, char **argv)
  456. {
  457. {
  458. const char *loglevel = boost::nowide::getenv("SLIC3R_LOGLEVEL");
  459. if (loglevel != nullptr) {
  460. if (loglevel[0] >= '0' && loglevel[0] <= '9' && loglevel[1] == 0)
  461. set_logging_level(loglevel[0] - '0');
  462. else
  463. boost::nowide::cerr << "Invalid SLIC3R_LOGLEVEL environment variable: " << loglevel << std::endl;
  464. }
  465. }
  466. boost::filesystem::path path_to_binary = boost::filesystem::system_complete(argv[0]);
  467. // Path from the Slic3r binary to its resources.
  468. #ifdef __APPLE__
  469. // The application is packed in the .dmg archive as 'Slic3r.app/Contents/MacOS/Slic3r'
  470. // The resources are packed to 'Slic3r.app/Contents/Resources'
  471. boost::filesystem::path path_resources = path_to_binary.parent_path() / "../Resources";
  472. #elif defined _WIN32
  473. // The application is packed in the .zip archive in the root,
  474. // The resources are packed to 'resources'
  475. // Path from Slic3r binary to resources:
  476. boost::filesystem::path path_resources = path_to_binary.parent_path() / "resources";
  477. #elif defined SLIC3R_FHS
  478. // The application is packaged according to the Linux Filesystem Hierarchy Standard
  479. // Resources are set to the 'Architecture-independent (shared) data', typically /usr/share or /usr/local/share
  480. boost::filesystem::path path_resources = SLIC3R_FHS_RESOURCES;
  481. #else
  482. // The application is packed in the .tar.bz archive (or in AppImage) as 'bin/slic3r',
  483. // The resources are packed to 'resources'
  484. // Path from Slic3r binary to resources:
  485. boost::filesystem::path path_resources = path_to_binary.parent_path() / "../resources";
  486. #endif
  487. set_resources_dir(path_resources.string());
  488. set_var_dir((path_resources / "icons").string());
  489. set_local_dir((path_resources / "localization").string());
  490. // Parse all command line options into a DynamicConfig.
  491. // If any option is unsupported, print usage and abort immediately.
  492. t_config_option_keys opt_order;
  493. if (! m_config.read_cli(argc, argv, &m_input_files, &opt_order)) {
  494. this->print_help();
  495. return false;
  496. }
  497. // Parse actions and transform options.
  498. for (auto const &opt_key : opt_order) {
  499. if (cli_actions_config_def.has(opt_key))
  500. m_actions.emplace_back(opt_key);
  501. if (cli_transform_config_def.has(opt_key))
  502. m_transforms.emplace_back(opt_key);
  503. }
  504. {
  505. const ConfigOptionInt *opt_loglevel = m_config.opt<ConfigOptionInt>("loglevel");
  506. if (opt_loglevel != 0)
  507. set_logging_level(opt_loglevel->value);
  508. }
  509. // Initialize with defaults.
  510. for (const t_optiondef_map *options : { &cli_actions_config_def.options, &cli_transform_config_def.options, &cli_misc_config_def.options })
  511. for (const std::pair<t_config_option_key, ConfigOptionDef> &optdef : *options)
  512. m_config.optptr(optdef.first, true);
  513. set_data_dir(m_config.opt_string("datadir"));
  514. return true;
  515. }
  516. void CLI::print_help(bool include_print_options, PrinterTechnology printer_technology) const
  517. {
  518. boost::nowide::cout
  519. << "Slic3r Prusa Edition " << SLIC3R_BUILD << std::endl
  520. << "https://github.com/prusa3d/Slic3r" << std::endl << std::endl
  521. << "Usage: slic3r [ ACTIONS ] [ TRANSFORM ] [ OPTIONS ] [ file.stl ... ]" << std::endl
  522. << std::endl
  523. << "Actions:" << std::endl;
  524. cli_actions_config_def.print_cli_help(boost::nowide::cout, false);
  525. boost::nowide::cout
  526. << std::endl
  527. << "Transform options:" << std::endl;
  528. cli_transform_config_def.print_cli_help(boost::nowide::cout, false);
  529. boost::nowide::cout
  530. << std::endl
  531. << "Other options:" << std::endl;
  532. cli_misc_config_def.print_cli_help(boost::nowide::cout, false);
  533. if (include_print_options) {
  534. boost::nowide::cout << std::endl;
  535. print_config_def.print_cli_help(boost::nowide::cout, true, [printer_technology](const ConfigOptionDef &def)
  536. { return printer_technology == ptAny || def.printer_technology == ptAny || printer_technology == def.printer_technology; });
  537. } else {
  538. boost::nowide::cout
  539. << std::endl
  540. << "Run --help-fff / --help-sla to see the full listing of print options." << std::endl;
  541. }
  542. }
  543. bool CLI::export_models(IO::ExportFormat format)
  544. {
  545. for (Model &model : m_models) {
  546. const std::string path = this->output_filepath(model, format);
  547. bool success = false;
  548. switch (format) {
  549. case IO::AMF: success = Slic3r::store_amf(path.c_str(), &model, nullptr); break;
  550. case IO::OBJ: success = Slic3r::store_obj(path.c_str(), &model); break;
  551. case IO::STL: success = Slic3r::store_stl(path.c_str(), &model, true); break;
  552. case IO::TMF: success = Slic3r::store_3mf(path.c_str(), &model, nullptr); break;
  553. default: assert(false); break;
  554. }
  555. if (success)
  556. std::cout << "File exported to " << path << std::endl;
  557. else {
  558. std::cerr << "File export to " << path << " failed" << std::endl;
  559. return false;
  560. }
  561. }
  562. return true;
  563. }
  564. std::string CLI::output_filepath(const Model &model, IO::ExportFormat format) const
  565. {
  566. std::string ext;
  567. switch (format) {
  568. case IO::AMF: ext = ".amf"; break;
  569. case IO::OBJ: ext = ".obj"; break;
  570. case IO::STL: ext = ".stl"; break;
  571. case IO::TMF: ext = ".3mf"; break;
  572. default: assert(false); break;
  573. };
  574. auto proposed_path = boost::filesystem::path(model.propose_export_file_name_and_path(ext));
  575. // use --output when available
  576. std::string cmdline_param = m_config.opt_string("output", false);
  577. if (! cmdline_param.empty()) {
  578. // if we were supplied a directory, use it and append our automatically generated filename
  579. boost::filesystem::path cmdline_path(cmdline_param);
  580. if (boost::filesystem::is_directory(cmdline_path))
  581. proposed_path = cmdline_path / proposed_path.filename();
  582. else
  583. proposed_path = cmdline_path;
  584. }
  585. return proposed_path.string();
  586. }
  587. #ifdef _MSC_VER
  588. extern "C" {
  589. __declspec(dllexport) int __stdcall slic3r_main(int argc, wchar_t **argv)
  590. {
  591. // Convert wchar_t arguments to UTF8.
  592. std::vector<std::string> argv_narrow;
  593. std::vector<char*> argv_ptrs(argc + 1, nullptr);
  594. for (size_t i = 0; i < argc; ++ i)
  595. argv_narrow.emplace_back(boost::nowide::narrow(argv[i]));
  596. for (size_t i = 0; i < argc; ++ i)
  597. argv_ptrs[i] = const_cast<char*>(argv_narrow[i].data());
  598. // Call the UTF8 main.
  599. return CLI().run(argc, argv_ptrs.data());
  600. }
  601. }
  602. #else /* _MSC_VER */
  603. int main(int argc, char **argv)
  604. {
  605. return CLI().run(argc, argv);
  606. }
  607. #endif /* _MSC_VER */