Use C++ range-based iterations in slic3r.cpp and remove unused variables

This commit is contained in:
Alessandro Ranellucci 2017-05-17 21:05:14 +02:00
parent 5f6214b645
commit 6b68b71c78
2 changed files with 40 additions and 42 deletions

View File

@ -10,7 +10,7 @@
#include <cstring> #include <cstring>
#include <iostream> #include <iostream>
#include <math.h> #include <math.h>
#include "boost/filesystem.hpp" #include <boost/filesystem.hpp>
using namespace Slic3r; using namespace Slic3r;
@ -34,16 +34,15 @@ main(const int argc, const char **argv)
DynamicPrintConfig print_config; DynamicPrintConfig print_config;
// load config files supplied via --load // load config files supplied via --load
for (std::vector<std::string>::const_iterator file = cli_config.load.values.begin(); for (const std::string &file : cli_config.load.values) {
file != cli_config.load.values.end(); ++file) { if (!boost::filesystem::exists(file)) {
if (!boost::filesystem::exists(*file)) { std::cout << "No such file: " << file << std::endl;
std::cout << "No such file: " << *file << std::endl;
exit(1); exit(1);
} }
DynamicPrintConfig c; DynamicPrintConfig c;
try { try {
c.load(*file); c.load(file);
} catch (std::exception &e) { } catch (std::exception &e) {
std::cout << "Error while reading config file: " << e.what() << std::endl; std::cout << "Error while reading config file: " << e.what() << std::endl;
exit(1); exit(1);
@ -62,85 +61,85 @@ main(const int argc, const char **argv)
// read input file(s) if any // read input file(s) if any
std::vector<Model> models; std::vector<Model> models;
for (t_config_option_keys::const_iterator it = input_files.begin(); it != input_files.end(); ++it) { for (const t_config_option_key &file : input_files) {
if (!boost::filesystem::exists(*it)) { if (!boost::filesystem::exists(file)) {
std::cout << "No such file: " << *it << std::endl; std::cerr << "No such file: " << file << std::endl;
exit(1); exit(1);
} }
Model model; Model model;
try { try {
model = Model::read_from_file(*it); model = Model::read_from_file(file);
} catch (std::exception &e) { } catch (std::exception &e) {
std::cout << *it << ": " << e.what() << std::endl; std::cerr << file << ": " << e.what() << std::endl;
exit(1); exit(1);
} }
if (model.objects.empty()) { if (model.objects.empty()) {
printf("Error: file is empty: %s\n", it->c_str()); std::cerr << "Error: file is empty: " << file << std::endl;
continue; continue;
} }
model.add_default_instances(); model.add_default_instances();
// apply command line transform options // apply command line transform options
for (ModelObjectPtrs::iterator o = model.objects.begin(); o != model.objects.end(); ++o) { for (ModelObject* o : model.objects) {
if (cli_config.scale_to_fit.is_positive_volume()) if (cli_config.scale_to_fit.is_positive_volume())
(*o)->scale_to_fit(cli_config.scale_to_fit.value); o->scale_to_fit(cli_config.scale_to_fit.value);
// TODO: honor option order? // TODO: honor option order?
(*o)->scale(cli_config.scale.value); o->scale(cli_config.scale.value);
(*o)->rotate(Geometry::deg2rad(cli_config.rotate_x.value), X); o->rotate(Geometry::deg2rad(cli_config.rotate_x.value), X);
(*o)->rotate(Geometry::deg2rad(cli_config.rotate_y.value), Y); o->rotate(Geometry::deg2rad(cli_config.rotate_y.value), Y);
(*o)->rotate(Geometry::deg2rad(cli_config.rotate.value), Z); o->rotate(Geometry::deg2rad(cli_config.rotate.value), Z);
} }
// TODO: handle --merge // TODO: handle --merge
models.push_back(model); models.push_back(model);
} }
for (std::vector<Model>::iterator model = models.begin(); model != models.end(); ++model) { for (Model &model : models) {
if (cli_config.info) { if (cli_config.info) {
// --info works on unrepaired model // --info works on unrepaired model
model->print_info(); model.print_info();
} else if (cli_config.export_obj) { } else if (cli_config.export_obj) {
std::string outfile = cli_config.output.value; std::string outfile = cli_config.output.value;
if (outfile.empty()) outfile = model->objects.front()->input_file + ".obj"; if (outfile.empty()) outfile = model.objects.front()->input_file + ".obj";
TriangleMesh mesh = model->mesh(); TriangleMesh mesh = model.mesh();
mesh.repair(); mesh.repair();
IO::OBJ::write(mesh, outfile); IO::OBJ::write(mesh, outfile);
printf("File exported to %s\n", outfile.c_str()); std::cout << "File exported to " << outfile << std::endl;
} else if (cli_config.export_pov) { } else if (cli_config.export_pov) {
std::string outfile = cli_config.output.value; std::string outfile = cli_config.output.value;
if (outfile.empty()) outfile = model->objects.front()->input_file + ".pov"; if (outfile.empty()) outfile = model.objects.front()->input_file + ".pov";
TriangleMesh mesh = model->mesh(); TriangleMesh mesh = model.mesh();
mesh.repair(); mesh.repair();
IO::POV::write(mesh, outfile); IO::POV::write(mesh, outfile);
printf("File exported to %s\n", outfile.c_str()); std::cout << "File exported to " << outfile << std::endl;
} else if (cli_config.export_svg) { } else if (cli_config.export_svg) {
std::string outfile = cli_config.output.value; std::string outfile = cli_config.output.value;
if (outfile.empty()) outfile = model->objects.front()->input_file + ".svg"; if (outfile.empty()) outfile = model.objects.front()->input_file + ".svg";
SLAPrint print(&*model); SLAPrint print(&model);
print.config.apply(print_config, true); print.config.apply(print_config, true);
print.slice(); print.slice();
print.write_svg(outfile); print.write_svg(outfile);
printf("SVG file exported to %s\n", outfile.c_str()); std::cout << "SVG file exported to " << outfile << std::endl;
} else if (cli_config.cut_x > 0 || cli_config.cut_y > 0 || cli_config.cut > 0) { } else if (cli_config.cut_x > 0 || cli_config.cut_y > 0 || cli_config.cut > 0) {
model->repair(); model.repair();
model->translate(0, 0, -model->bounding_box().min.z); model.translate(0, 0, -model.bounding_box().min.z);
if (!model->objects.empty()) { if (!model.objects.empty()) {
// FIXME: cut all objects // FIXME: cut all objects
Model out; Model out;
if (cli_config.cut_x > 0) { if (cli_config.cut_x > 0) {
model->objects.front()->cut(X, cli_config.cut_x, &out); model.objects.front()->cut(X, cli_config.cut_x, &out);
} else if (cli_config.cut_y > 0) { } else if (cli_config.cut_y > 0) {
model->objects.front()->cut(Y, cli_config.cut_y, &out); model.objects.front()->cut(Y, cli_config.cut_y, &out);
} else { } else {
model->objects.front()->cut(Z, cli_config.cut, &out); model.objects.front()->cut(Z, cli_config.cut, &out);
} }
ModelObject &upper = *out.objects[0]; ModelObject &upper = *out.objects[0];
@ -156,15 +155,16 @@ main(const int argc, const char **argv)
} }
} }
} else if (cli_config.cut_grid.value.x > 0 && cli_config.cut_grid.value.y > 0) { } else if (cli_config.cut_grid.value.x > 0 && cli_config.cut_grid.value.y > 0) {
TriangleMesh mesh = model->mesh(); TriangleMesh mesh = model.mesh();
mesh.repair(); mesh.repair();
TriangleMeshPtrs meshes = mesh.cut_by_grid(cli_config.cut_grid.value); TriangleMeshPtrs meshes = mesh.cut_by_grid(cli_config.cut_grid.value);
for (TriangleMeshPtrs::iterator m = meshes.begin(); m != meshes.end(); ++m) { size_t i = 0;
for (TriangleMesh* m : meshes) {
std::ostringstream ss; std::ostringstream ss;
ss << model->objects.front()->input_file << "_" << (m - meshes.begin()) << ".stl"; ss << model.objects.front()->input_file << "_" << i++ << ".stl";
IO::STL::write(**m, ss.str()); IO::STL::write(*m, ss.str());
delete *m; delete m;
} }
} else { } else {
std::cerr << "error: command not supported" << std::endl; std::cerr << "error: command not supported" << std::endl;

View File

@ -625,8 +625,6 @@ void PrintObject::_slice()
{ {
coordf_t raft_height = 0; coordf_t raft_height = 0;
coordf_t print_z = 0;
coordf_t height = 0;
coordf_t first_layer_height = this->config.first_layer_height.get_abs_value(this->config.layer_height.value); coordf_t first_layer_height = this->config.first_layer_height.get_abs_value(this->config.layer_height.value);