Skip to content

Commit

Permalink
refactor global to plate translation
Browse files Browse the repository at this point in the history
  • Loading branch information
SoftFever committed Apr 16, 2023
1 parent e74d366 commit 4a288c6
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
24 changes: 13 additions & 11 deletions src/libslic3r/GCode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1730,7 +1730,7 @@ void GCode::_do_export(Print& print, GCodeOutputStream &file, ThumbnailsGenerato
auto pts = std::make_unique<ConfigOptionPoints>();
pts->values.reserve(print.first_layer_convex_hull().size());
for (const Point& pt : print.first_layer_convex_hull().points)
pts->values.emplace_back(unscale(pt) - Vec2d(print.get_plate_origin().x(), print.get_plate_origin().y()));
pts->values.emplace_back(print.translate_to_print_space(pt));
BoundingBoxf bbox(pts->values);
m_placeholder_parser.set("first_layer_print_convex_hull", pts.release());
m_placeholder_parser.set("first_layer_print_min", new ConfigOptionFloats({bbox.min.x(), bbox.min.y()}));
Expand Down Expand Up @@ -4616,32 +4616,34 @@ std::string GCode::set_extruder(unsigned int extruder_id, double print_z)
return gcode;
}

inline std::string polygon_to_string(const Polygon &polygon, const Vec3d& offset) {
inline std::string polygon_to_string(const Polygon &polygon, Print *print) {
std::ostringstream gcode;
gcode << "[";
for (const Point &p : polygon.points) {
gcode << "[" << unscaled(p.x()) - offset.x() << "," << unscaled(p.y()) - offset.y() << "],";
const auto v = print->translate_to_print_space(p);
gcode << "[" << v.x() << "," << v.y() << "],";
}
gcode << "[" << unscaled(polygon.points.front().x()) - offset.x() << "," << unscaled(polygon.points.front().y()) - offset.y() << "]";
const auto first_v = print->translate_to_print_space(polygon.points.front());
gcode << "[" << first_v.x() << "," << first_v.y() << "]";
gcode << "]";
return gcode.str();
}
// this function iterator PrintObject and assign a seqential id to each object.
// this function iterator PrintObject and assign a seqential id to each object.
// this id is used to generate unique object id for each object.
std::string GCode::set_object_info(Print* print) {
std::string GCode::set_object_info(Print *print) {
std::ostringstream gcode;
size_t object_id = 0;
for (PrintObject* object : print->objects()) {
for (PrintObject *object : print->objects()) {
object->set_id(object_id++);
size_t inst_id = 0;
for (PrintInstance &inst : object->instances()) {
inst.id = inst_id++;
if (this->config().exclude_object && print->config().gcode_flavor.value == gcfKlipper) {
auto bbox = inst.get_bounding_box();
auto center = bbox.center() - print->get_plate_origin();
gcode << "EXCLUDE_OBJECT_DEFINE NAME=" << get_instance_name(object, inst)
<< " CENTER=" << center.x() << "," << center.y()
<< " POLYGON=" << polygon_to_string(inst.get_convex_hull_2d(), print->get_plate_origin()) << "\n";
auto center = print->translate_to_print_space(Vec2d(bbox.center().x(), bbox.center().y()));
gcode << "EXCLUDE_OBJECT_DEFINE NAME=" << get_instance_name(object, inst) << " CENTER=" << center.x()
<< "," << center.y() << " POLYGON=" << polygon_to_string(inst.get_convex_hull_2d(), print)
<< "\n";
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions src/libslic3r/Print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1945,6 +1945,15 @@ std::vector<Point> Print::first_layer_wipe_tower_corners(bool check_wipe_tower_e
return corners;
}

//SoftFever
Vec2d Print::translate_to_print_space(const Vec2d &point) const {
//const BoundingBoxf bed_bbox(config().printable_area.values);
return Vec2d(point(0) - m_origin(0), point(1) - m_origin(1));
}

Vec2d Print::translate_to_print_space(const Point &point) const {
return Vec2d(unscaled(point.x()) - m_origin(0), unscaled(point.y()) - m_origin(1));
}
void Print::finalize_first_layer_convex_hull()
{
append(m_first_layer_convex_hull.points, m_skirt_convex_hull);
Expand Down
3 changes: 3 additions & 0 deletions src/libslic3r/Print.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,9 @@ class Print : public PrintBaseWithState<PrintStep, psCount>
const CalibMode calib_mode() const { return m_calib_params.mode; }
void set_calib_params(const Calib_Params& params);
const Calib_Params& calib_params() const { return m_calib_params; }
Vec2d translate_to_print_space(const Vec2d &point) const;
// scaled point
Vec2d translate_to_print_space(const Point &point) const;
protected:
// Invalidates the step, and its depending steps in Print.
bool invalidate_step(PrintStep step);
Expand Down

0 comments on commit 4a288c6

Please sign in to comment.