FIX: auto-arrange fails for multi-color objects

The cost for switching extruders was too big.

Change-Id: Ic9de6b6aabb7e69db79a0201f0dbb9d039097df9
This commit is contained in:
Arthur 2022-08-12 20:15:06 +08:00 committed by Lane.Wei
parent 06f878f13b
commit d95e85851c
4 changed files with 46 additions and 19 deletions

View file

@ -5,6 +5,7 @@
#include <fstream>
#include <string>
#include <boost/filesystem.hpp>
#include <libnest2d/nester.hpp>
namespace libnest2d { namespace svg {
@ -27,9 +28,10 @@ public:
OrigoLocation origo_location;
Coord mm_in_coord_units;
double width, height;
double x0, y0;
Config():
origo_location(BOTTOMLEFT), mm_in_coord_units(1000000),
width(500), height(500) {}
width(500), height(500),x0(100) {}
};
@ -42,10 +44,12 @@ public:
SVGWriter(const Config& conf = Config()):
conf_(conf) {}
void setSize(const Box& box) {
conf_.height = static_cast<double>(box.height()) /
void setSize(const Box &box) {
conf_.x0 = box.width() / 5;
conf_.x0 = box.height() / 5;
conf_.height = static_cast<double>(box.height() + conf_.y0*2) /
conf_.mm_in_coord_units;
conf_.width = static_cast<double>(box.width()) /
conf_.width = static_cast<double>(box.width() + conf_.x0*2) /
conf_.mm_in_coord_units;
}
@ -56,11 +60,17 @@ public:
std::round(conf_.height*conf_.mm_in_coord_units) );
auto& contour = shapelike::contour(tsh);
for(auto& v : contour) setY(v, -getY(v) + d);
for (auto &v : contour) {
setX(v, getX(v) + conf_.x0); // right shift so we can draw outside the bounding box
setY(v, -getY(v) + d + conf_.y0);
}
auto& holes = shapelike::holes(tsh);
for(auto& h : holes) for(auto& v : h) setY(v, -getY(v) + d);
for (auto &h : holes)
for (auto &v : h) {
setX(v, getX(v) + conf_.x0);
setY(v, -getY(v) + d + conf_.y0);
}
}
currentLayer() +=
shapelike::serialize<Formats::SVG>(tsh,
@ -126,6 +136,22 @@ public:
};
}
// save svg in utf-8 file name
void save(const boost::filesystem::path &filepath)
{
size_t lyrc = svg_layers_.size() > 1 ? 1 : 0;
size_t last = svg_layers_.size() > 1 ? svg_layers_.size() : 0;
for (auto &lyr : svg_layers_) {
boost::filesystem::ofstream out(filepath, std::fstream::out);
if (out.is_open()) out << lyr;
if (lyrc == last && !finished_) out << "\n</svg>\n";
out.flush();
out.close();
lyrc++;
};
}
private:
std::string& currentLayer() { return svg_layers_.back(); }