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

@ -922,27 +922,28 @@ private:
#ifdef SVGTOOLS_HPP #ifdef SVGTOOLS_HPP
svg::SVGWriter<RawShape> svgwriter; svg::SVGWriter<RawShape> svgwriter;
svgwriter.setSize(binbb);
svgwriter.writeShape(box2RawShape(binbb), "none", "black"); svgwriter.writeShape(box2RawShape(binbb), "none", "black");
for (int i = 0; i < nfps.size(); i++) for (int i = 0; i < nfps.size(); i++)
svgwriter.writeShape(nfps[i], "none", "blue"); svgwriter.writeShape(nfps[i], "none", "blue");
for (int i = 0; i < items_.size(); i++) for (int i = 0; i < items_.size(); i++)
svgwriter.writeItem(items_[i], "none", "black"); svgwriter.writeItem(items_[i], "none", "black");
//for (int i = 0; i < merged_pile_.size(); i++) for (int i = 0; i < merged_pile_.size(); i++)
// svgwriter.writeShape(merged_pile_[i], "none", "yellow"); svgwriter.writeShape(merged_pile_[i], "none", "yellow");
svgwriter.writeItem(item, "none", "red", 2);
svgwriter.writeItem(item, "none", "red");
std::stringstream ss; std::stringstream ss;
ss.setf(std::ios::fixed | std::ios::showpoint); ss.setf(std::ios::fixed | std::ios::showpoint);
ss.precision(1); ss.precision(1);
ss << "trans=" << round(item.translation().x() / 1e6) << "," << round(item.translation().y() / 1e6) ss << "t=" << round(item.translation().x() / 1e6) << "," << round(item.translation().y() / 1e6)
<< "-rot=" << round(item.rotation().toDegrees()) //<< "-rot=" << round(item.rotation().toDegrees())
<< "-score=" << round(global_score); << "-sco=" << round(global_score);
svgwriter.draw_text(20, 20, ss.str(), "blue", 20); svgwriter.draw_text(20, 20, ss.str(), "blue", 20);
ss.str(""); ss.str("");
ss << "items.size=" << items_.size() ss << "items.size=" << items_.size()
<< "-merged_pile.size=" << merged_pile_.size(); << "-merged_pile.size=" << merged_pile_.size();
svgwriter.draw_text(20, 40, ss.str(), "blue", 20); svgwriter.draw_text(20, 40, ss.str(), "blue", 20);
svgwriter.save("SVG/nfpplacer_" + std::to_string(plate_id) + "_" + item.name + "_" + ss.str()); svgwriter.save(boost::filesystem::path("SVG")/ ("nfpplacer_" + std::to_string(plate_id) + "_" + ss.str() + "_" + item.name + ".svg"));
#endif #endif
if(can_pack) { if(can_pack) {

View file

@ -5,6 +5,7 @@
#include <fstream> #include <fstream>
#include <string> #include <string>
#include <boost/filesystem.hpp>
#include <libnest2d/nester.hpp> #include <libnest2d/nester.hpp>
namespace libnest2d { namespace svg { namespace libnest2d { namespace svg {
@ -27,9 +28,10 @@ public:
OrigoLocation origo_location; OrigoLocation origo_location;
Coord mm_in_coord_units; Coord mm_in_coord_units;
double width, height; double width, height;
double x0, y0;
Config(): Config():
origo_location(BOTTOMLEFT), mm_in_coord_units(1000000), 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()): SVGWriter(const Config& conf = Config()):
conf_(conf) {} conf_(conf) {}
void setSize(const Box& box) { void setSize(const Box &box) {
conf_.height = static_cast<double>(box.height()) / 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_.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; conf_.mm_in_coord_units;
} }
@ -56,11 +60,17 @@ public:
std::round(conf_.height*conf_.mm_in_coord_units) ); std::round(conf_.height*conf_.mm_in_coord_units) );
auto& contour = shapelike::contour(tsh); 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); 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() += currentLayer() +=
shapelike::serialize<Formats::SVG>(tsh, 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: private:
std::string& currentLayer() { return svg_layers_.back(); } std::string& currentLayer() { return svg_layers_.back(); }

View file

@ -436,10 +436,12 @@ protected:
if (!params.allow_multi_materials_on_same_plate) if (!params.allow_multi_materials_on_same_plate)
score += LARGE_COST_TO_REJECT * (item.extrude_id != p.extrude_id); score += LARGE_COST_TO_REJECT * (item.extrude_id != p.extrude_id);
} }
// for layered printing, we want extruder change as few as possible // for layered printing, we want extruder change as few as possible
// this has very weak effect, CAN NOT use a large weight
if (!params.is_seq_print) { if (!params.is_seq_print) {
extruder_ids.insert(item.extrude_id); extruder_ids.insert(item.extrude_id);
score += 0.2 * LARGE_COST_TO_REJECT * std::max(0, ((int)extruder_ids.size() - 1)); score += 1 * std::max(0, ((int)extruder_ids.size() - 1));
} }
return std::make_tuple(score, fullbb); return std::make_tuple(score, fullbb);

View file

@ -138,11 +138,9 @@ ArrangePolygon get_instance_arrange_poly(ModelInstance* instance, const Slic3r::
ap.print_temp = config.opt_int("nozzle_temperature", ap.extrude_ids.back() - 1); ap.print_temp = config.opt_int("nozzle_temperature", ap.extrude_ids.back() - 1);
if (config.has("nozzle_temperature_initial_layer")) //get the nozzle_temperature_initial_layer if (config.has("nozzle_temperature_initial_layer")) //get the nozzle_temperature_initial_layer
ap.first_print_temp = config.opt_int("nozzle_temperature_initial_layer", ap.extrude_ids.back() - 1); ap.first_print_temp = config.opt_int("nozzle_temperature_initial_layer", ap.extrude_ids.back() - 1);
// BBS: since first_bed_temp packs all 3 temperatures, vitrify_temp should follow same routine
if (config.has("temperature_vitrification")) { if (config.has("temperature_vitrification")) {
int tmp = config.opt_int("temperature_vitrification", ap.extrude_ids.back() - 1); ap.vitrify_temp = config.opt_int("temperature_vitrification", ap.extrude_ids.back() - 1);
for (int i = 0; i < BedType::btCount; i++)
ap.vitrify_temp += tmp * pow(100, BedType::btCount - i - 1);
} }
// get brim width // get brim width