Skip to content

Commit

Permalink
Use C++ range-based iterations in slic3r.cpp and remove unused variables
Browse files Browse the repository at this point in the history
  • Loading branch information
alranel committed May 18, 2017
1 parent 5f6214b commit 6b68b71
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 42 deletions.
80 changes: 40 additions & 40 deletions src/slic3r.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <cstring>
#include <iostream>
#include <math.h>
#include "boost/filesystem.hpp"
#include <boost/filesystem.hpp>

using namespace Slic3r;

Expand All @@ -34,16 +34,15 @@ main(const int argc, const char **argv)
DynamicPrintConfig print_config;

// load config files supplied via --load
for (std::vector<std::string>::const_iterator file = cli_config.load.values.begin();
file != cli_config.load.values.end(); ++file) {
if (!boost::filesystem::exists(*file)) {
std::cout << "No such file: " << *file << std::endl;
for (const std::string &file : cli_config.load.values) {
if (!boost::filesystem::exists(file)) {
std::cout << "No such file: " << file << std::endl;
exit(1);
}

DynamicPrintConfig c;
try {
c.load(*file);
c.load(file);
} catch (std::exception &e) {
std::cout << "Error while reading config file: " << e.what() << std::endl;
exit(1);
Expand All @@ -62,85 +61,85 @@ main(const int argc, const char **argv)

// read input file(s) if any
std::vector<Model> models;
for (t_config_option_keys::const_iterator it = input_files.begin(); it != input_files.end(); ++it) {
if (!boost::filesystem::exists(*it)) {
std::cout << "No such file: " << *it << std::endl;
for (const t_config_option_key &file : input_files) {
if (!boost::filesystem::exists(file)) {
std::cerr << "No such file: " << file << std::endl;
exit(1);
}

Model model;
try {
model = Model::read_from_file(*it);
model = Model::read_from_file(file);
} catch (std::exception &e) {
std::cout << *it << ": " << e.what() << std::endl;
std::cerr << file << ": " << e.what() << std::endl;
exit(1);
}

if (model.objects.empty()) {
printf("Error: file is empty: %s\n", it->c_str());
std::cerr << "Error: file is empty: " << file << std::endl;
continue;
}

model.add_default_instances();

// 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())
(*o)->scale_to_fit(cli_config.scale_to_fit.value);
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);
(*o)->rotate(Geometry::deg2rad(cli_config.rotate_y.value), Y);
(*o)->rotate(Geometry::deg2rad(cli_config.rotate.value), Z);
o->scale(cli_config.scale.value);
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.value), Z);
}

// TODO: handle --merge
models.push_back(model);
}

for (std::vector<Model>::iterator model = models.begin(); model != models.end(); ++model) {
for (Model &model : models) {
if (cli_config.info) {
// --info works on unrepaired model
model->print_info();
model.print_info();
} else if (cli_config.export_obj) {
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();
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) {
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();
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) {
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.slice();
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) {
model->repair();
model->translate(0, 0, -model->bounding_box().min.z);
model.repair();
model.translate(0, 0, -model.bounding_box().min.z);

if (!model->objects.empty()) {
if (!model.objects.empty()) {
// FIXME: cut all objects
Model out;
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) {
model->objects.front()->cut(Y, cli_config.cut_y, &out);
model.objects.front()->cut(Y, cli_config.cut_y, &out);
} else {
model->objects.front()->cut(Z, cli_config.cut, &out);
model.objects.front()->cut(Z, cli_config.cut, &out);
}

ModelObject &upper = *out.objects[0];
Expand All @@ -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) {
TriangleMesh mesh = model->mesh();
TriangleMesh mesh = model.mesh();
mesh.repair();

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;
ss << model->objects.front()->input_file << "_" << (m - meshes.begin()) << ".stl";
IO::STL::write(**m, ss.str());
delete *m;
ss << model.objects.front()->input_file << "_" << i++ << ".stl";
IO::STL::write(*m, ss.str());
delete m;
}
} else {
std::cerr << "error: command not supported" << std::endl;
Expand Down
2 changes: 0 additions & 2 deletions xs/src/libslic3r/PrintObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -625,8 +625,6 @@ void PrintObject::_slice()
{

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);


Expand Down

1 comment on commit 6b68b71

@lordofhyphens
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking about doing something like this across most areas of the code myself.

Please sign in to comment.