diff --git a/src/libslic3r/BeltBrim.cpp b/src/libslic3r/BeltBrim.cpp index d51f1248de..c60a03ed21 100644 --- a/src/libslic3r/BeltBrim.cpp +++ b/src/libslic3r/BeltBrim.cpp @@ -7,6 +7,8 @@ #include "Polygon.hpp" #include "Print.hpp" #include "ShortestPath.hpp" +#include "Surface.hpp" +#include "SurfaceCollection.hpp" #include "Support/BeltFloorContext.hpp" #include "BoundingBox.hpp" #include "ExPolygon.hpp" @@ -257,9 +259,12 @@ struct BeltBrimContext }; // Emit the cross-belt brim lines that belong to the band [print_z - height, print_z]. +// wall_inset is how far the centreline of the outer wall of the band's object layer lies +// inside where the layer's slice met the belt, mm. static void belt_brim_band_paths(const BeltBrimContext &bc, coordf_t print_z, coordf_t height, + double wall_inset, const Polygons &obstacles, ExtrusionEntityCollection &out, ExPolygons &areas_out) @@ -272,10 +277,15 @@ static void belt_brim_band_paths(const BeltBrimContext &bc, // How wide this band is measured ON the belt, versus one nominal bead. const double band_in_plane = (u_hi - u_lo) * bc.frame.u_stretch(); - // Fraction of the layer height at which a line sits above the belt. Toward the - // downhill edge, so the sheet is reasonably thick while the nozzle stays clear of - // the belt itself. - static constexpr double BAND_CLEARANCE_FRACTION = 0.75; + // Nozzle-to-belt clearance of the object's own outer wall where it rests on the belt. + // The layer is sliced half a layer below print_z, so the slice meets the belt there, and + // the wall's centreline is inset from that edge, where the belt lies lower by the inset + // times the shear. No brim line sits closer to the belt than this, so brim and object + // are laid at the same height and one calibration of the belt height (endstop, extruder + // offset) serves both. A contour compensation that pushes the wall down to the belt + // cannot be matched: the brim keeps a tenth of a layer, so its lines still have a + // cross-section. + const double wall_clearance = std::max(0.5 * height + wall_inset * std::abs(bc.frame.shear), 0.1 * height); std::vector us; double uniform_clearance = 0.; // 0 => derive per line from its own position @@ -285,19 +295,20 @@ static void belt_brim_band_paths(const BeltBrimContext &bc, // wider. Two lattice lines in such a band would land almost on top of each other. if (band_in_plane <= 1.5 * bc.in_plane_pitch + EPSILON) { // Steep belt, which is the normal case: the band is narrower than one bead, so - // exactly one line fits. Place it at a FIXED fraction of the band rather than - // on a nominal-spacing lattice. On a lattice each line lands at an arbitrary - // point in its band, the clearance sweeps [0, height] from band to band, and the - // bead width therefore varies by 2x - visible as ragged, uneven brim lines. - // Anchoring to the band makes the clearance identical everywhere, so every bead - // is the same width. + // exactly one line fits. Place it at the outer wall's clearance rather than on + // a nominal-spacing lattice. On a lattice each line lands at an arbitrary point + // in its band, the clearance sweeps [0, height] from band to band, and the bead + // width therefore varies by 2x - visible as ragged, uneven brim lines. A fixed + // clearance is identical everywhere, so every bead is the same width. It can + // exceed the layer height, which only moves every band's line downhill by the + // same distance. // // The spacing is then whatever the bands give (height / sin(tilt) on the belt) // rather than the nominal bead spacing, so the flow below is matched to THAT // pitch. Matched flow at the real pitch is what keeps the sheet uniform and // gap-free; using nominal flow at band spacing would over-feed it. - us.push_back(scale_(bc.ctx.cutoff_u(print_z - BAND_CLEARANCE_FRACTION * height))); - uniform_clearance = BAND_CLEARANCE_FRACTION * height; + us.push_back(scale_(bc.ctx.cutoff_u(print_z - wall_clearance))); + uniform_clearance = wall_clearance; line_pitch = band_in_plane; } else { // Shallow belt: the band is wider than a bead, so it takes several lines and they @@ -324,18 +335,17 @@ static void belt_brim_band_paths(const BeltBrimContext &bc, // // A lattice line can fall where the belt is only a hair below the band's print_z. // The bead there would be laid scraping the belt while its flow is sized for a - // taller cell, so it is moved uphill to the same fraction of the band the - // single-line case uses. (The clearance is along slice Z; the real gap under the - // nozzle is clearance x cos(tilt), 0.53 h at 45 degrees for the 0.75 fraction.) + // taller cell, so it is moved to the outer wall's clearance, where the single-line + // case puts every line. (The clearance is along slice Z; the real gap under the + // nozzle is clearance x cos(tilt).) double clearance = uniform_clearance; if (clearance <= 0.) { const Point probe = bc.frame.from_axis == 0 ? Point(u, 0) : Point(0, u); - clearance = print_z - bc.ctx.floor_print_z(probe); - if (clearance < BAND_CLEARANCE_FRACTION * height) { - clearance = BAND_CLEARANCE_FRACTION * height; + clearance = std::min(print_z - bc.ctx.floor_print_z(probe), height); + if (clearance < wall_clearance) { + clearance = wall_clearance; u = scale_(bc.ctx.cutoff_u(print_z - clearance)); } - clearance = std::min(clearance, height); } // A line moved uphill can land on, or almost on, its neighbour; two beads closer // than half a pitch would be laid into the same cell. @@ -357,10 +367,12 @@ static void belt_brim_band_paths(const BeltBrimContext &bc, if (pieces.empty()) continue; - // with_cross_section, not with_height: it reaches the prescribed volume while - // KEEPING the extrusion spacing, so the bead is sized to fill exactly one - // pitch x clearance cell of the sheet. - const Flow f = bc.brim_flow.with_cross_section(float(line_pitch * clearance)); + // A bead as tall as the clearance and line_pitch apart from the next fills exactly + // one pitch x clearance cell of the sheet, at any clearance. (with_cross_section() + // makes a round bead of a quarter of the area asked for when the cell is small.) + const float height_f = float(clearance); + const Flow f(Flow::rounded_rectangle_extrusion_width_from_spacing(float(line_pitch), height_f), height_f, + bc.brim_flow.nozzle_diameter()); // Footprint of these beads, for the first-layer convex hull and bbox. for (const Polygon &p : offset(pieces, 0.5f * float(f.scaled_width()))) @@ -445,6 +457,9 @@ void make_belt_brim(PrintObject &object) ExPolygons footprint_acc; // The first layer that touches the belt: where the leading-edge brim is cut. const Layer *first_contact = nullptr; + // Per layer, the width of the outer wall resting on the belt, the narrowest where several + // regions do, as it sits lowest; 0 where the layer does not rest on the belt. + std::vector contact_wall_width(nlayers, 0.); for (size_t i = 0; i < nlayers; ++ i) { const Layer &layer = *object.layers()[i]; if (layer.lslices.empty()) @@ -466,6 +481,11 @@ void make_belt_brim(PrintObject &object) if (first_contact == nullptr) first_contact = &layer; expolygons_append(footprint_acc, std::move(contact)); + for (const LayerRegion *layerm : layer.regions()) + if (! layerm->slices.empty() && ! intersection(to_polygons(layerm->slices.surfaces), Polygons{ band }).empty()) { + const double w = layerm->flow(frExternalPerimeter).width(); + contact_wall_width[i] = contact_wall_width[i] > 0. ? std::min(contact_wall_width[i], w) : w; + } } const ExPolygons footprint = union_ex(footprint_acc); if (footprint.empty()) @@ -519,17 +539,37 @@ void make_belt_brim(PrintObject &object) : (low_side ? fbb.min.y() : fbb.max.y()); } + // How far each layer's outer wall, which the layer's brim band is levelled with, lies + // inside where the layer's slice met the belt: half its width, less the XY contour + // compensation that grew the slice toward the belt (none where slicing skips it, see + // PrintObject::slice_volumes()). The first layer's walls take initial_layer_line_width. + // A layer that does not rest on the belt takes the wall of the last one before it that + // does, and the layers ahead of the part the first one's. + const double contour_compensation = print.config().filament_diameter.size() > 1 && object.is_mm_painted() ? + 0. : object.config().xy_contour_compensation.value; + std::vector wall_insets(nlayers); + { + const auto first_wall = std::find_if(contact_wall_width.begin(), contact_wall_width.end(), [](double w) { return w > 0.; }); + double width = first_wall != contact_wall_width.end() ? *first_wall : 0.; + for (size_t i = 0; i < nlayers; ++ i) { + if (contact_wall_width[i] > 0.) + width = contact_wall_width[i]; + wall_insets[i] = 0.5 * width - contour_compensation; + } + } + // 4. Bands coincident with an object layer. std::vector by_layer(nlayers); std::vector areas_by_layer(nlayers); for (size_t i = 0; i < nlayers; ++ i) { const Layer &layer = *object.layers()[i]; const Polygons obstacles = belt_brim_obstacles(print, object, bc.region_bbox, layer.print_z, 0.5 * layer.height); - belt_brim_band_paths(bc, layer.print_z, layer.height, obstacles, by_layer[i], areas_by_layer[i]); + belt_brim_band_paths(bc, layer.print_z, layer.height, wall_insets[i], obstacles, by_layer[i], areas_by_layer[i]); } // 5. Apron prologue: the part of the region downhill of the object's first - // layer, which has no object layer to ride on. + // layer, which has no object layer to ride on. It continues the first layer's + // band, so it takes that layer's height, and the first contact's wall. std::vector prologue; { const Layer &first = *object.layers().front(); @@ -548,7 +588,7 @@ void make_belt_brim(PrintObject &object) BeltBrimBand band; band.print_z = z; band.height = h; - belt_brim_band_paths(bc, z, h, obstacles, band.fills, band.areas); + belt_brim_band_paths(bc, z, h, wall_insets.front(), obstacles, band.fills, band.areas); if (! band.fills.empty()) prologue.emplace_back(std::move(band)); } diff --git a/src/libslic3r/GCode.cpp b/src/libslic3r/GCode.cpp index da95469a18..d876c93dc1 100644 --- a/src/libslic3r/GCode.cpp +++ b/src/libslic3r/GCode.cpp @@ -3489,9 +3489,13 @@ void GCode::_do_export(Print& print, GCodeOutputStream &file, ThumbnailsGenerato // Undo what point_to_gcode() added (m_origin, minus the extruder offset) and // what the writer then took off (its XY offset, the plate origin). const Vec2d plate_offset = m_writer.get_xy_offset().cast(); + // Judged like the path the travel leads to (see belt_path_height()). + if (m_belt_travel_target != nullptr && m_belt_travel_target->role() == erBrim) + return true; return this->on_first_layer(Vec3d(point_logical.x() - (m_origin.x() - plate_offset.x()) + extruder_offset.x(), point_logical.y() - (m_origin.y() - plate_offset.y()) + extruder_offset.y(), - point_logical.z())); + point_logical.z()), + m_belt_travel_target != nullptr ? m_belt_travel_target->width : 0.); }); } @@ -9229,6 +9233,89 @@ static bool overhang_fan_active(float overlap, int overhang_fan_threshold, Extru } std::string GCode::_extrude(const ExtrusionPath &path, const std::string &path_description, double speed) +{ + std::vector> uncut_line_lengths; + const ExtrusionPaths pieces = this->belt_cut_at_band_edges(path, uncut_line_lengths); + if (pieces.size() < 2) + return this->_extrude_piece(path, path_description, speed); + std::string gcode; + for (size_t i = 0; i < pieces.size(); ++ i) { + m_belt_uncut_line_lengths = &uncut_line_lengths[i]; + gcode += this->_extrude_piece(pieces[i], path_description, speed); + } + m_belt_uncut_line_lengths = nullptr; + return gcode; +} + +// Belt printers: whether a path is on the first layer, and its step of the slow_down_layers +// ramp, set its speed, flow and acceleration as a whole. A wall loop runs from the belt up +// the part, so a path is cut where it crosses the edge of the first-layer band above the belt +// or of a band of the ramp, and each piece is judged on its own. Otherwise the loop would +// take the settings of wherever its seam is all the way round. Returns no pieces when the +// path stays in one band, and else, for each piece, the length of the uncut line each of its +// lines comes from, so that small-area flow compensation sees the line as it was. +ExtrusionPaths GCode::belt_cut_at_band_edges(const ExtrusionPath &path, std::vector> &uncut_line_lengths) const +{ + ExtrusionPaths pieces; + // A sloped seam carries its slope along it, and a contoured path scales each line's + // extrusion by the Z offset at the line's end, which a cut would move; both are judged + // whole. + if (! m_config.belt_printer.value || path.z_contoured || dynamic_cast(&path) != nullptr) + return pieces; + // Band 0 is the first layer as on_first_layer() has it, band k > 0 the heights the ramp + // gives step k (effective_layer_index_for_point()), the last one everything above it. + const double first = m_config.initial_layer_print_height.value; + const double band = this->first_layer_band_mm(); + const int last = std::max(1, m_config.slow_down_layers.value); + auto band_of = [this, first, last](double h) { + return h <= first + EPSILON ? 0 : std::clamp(this->belt_band_index(h), 1, last); + }; + auto band_top = [first, band](int k) { return k == 0 ? first : (k + 1) * band; }; + const Points3 &points = path.polyline.points; + double h_prev; + if (points.size() < 2 || ! this->belt_path_height(path, points.front(), h_prev)) + return pieces; + int band_prev = band_of(h_prev); + Points3 current{ points.front() }; + std::vector current_lengths; + auto extend = [¤t, ¤t_lengths](const Point3 &p, double uncut_length) { + if (p != current.back()) { + current.push_back(p); + current_lengths.push_back(uncut_length); + } + }; + for (size_t i = 1; i < points.size(); ++ i) { + double h; + this->belt_path_height(path, points[i], h); + const int band_i = band_of(h); + const double length = unscale((points[i] - points[i - 1]).cast().norm()); + // Cut at each edge the segment crosses. The belt is a plane, so the height is + // linear along the segment. + for (int k = band_prev; k != band_i; k += band_i > band_prev ? 1 : -1) { + const double edge = band_top(band_i > band_prev ? k : k - 1); + const double t = std::clamp((edge - h_prev) / (h - h_prev), 0., 1.); + const Vec3d cut = points[i - 1].cast() + t * (points[i] - points[i - 1]).cast(); + const Point3 cut_point(cut.x(), cut.y(), cut.z()); + extend(cut_point, length); + if (current.size() > 1) { + pieces.emplace_back(Polyline3(current), path); + uncut_line_lengths.emplace_back(std::move(current_lengths)); + } + current = { cut_point }; + current_lengths.clear(); + } + extend(points[i], length); + h_prev = h; + band_prev = band_i; + } + if (! pieces.empty() && current.size() > 1) { + pieces.emplace_back(Polyline3(current), path); + uncut_line_lengths.emplace_back(std::move(current_lengths)); + } + return pieces; +} + +std::string GCode::_extrude_piece(const ExtrusionPath &path, const std::string &path_description, double speed) { std::string gcode; @@ -9240,15 +9327,29 @@ std::string GCode::_extrude(const ExtrusionPath &path, const std::string &path_d // evaluator is inactive (non-belt printers, or belt printers without // a Z-axis shear) `path_on_first_layer` falls back to the legacy // layer-id check, so behavior is bit-identical to the pre-feature path. - // A belt brim apron band has no Layer of its own, so it publishes its Z - // through m_belt_brim_z instead; without that the plane would be probed at - // Z=0 and the apron mis-classified for fan and speed. const Vec3d path_point_mm{ unscale(path.first_point().x()), unscale(path.first_point().y()), m_layer ? m_layer->print_z : (m_belt_brim_z ? *m_belt_brim_z : 0.0) }; - const bool path_on_first_layer = this->on_first_layer(path_point_mm); + bool path_on_first_layer = this->on_first_layer(path_point_mm); + // On a belt a path is judged by how high above the belt all of it is: it is on the first + // layer when all of it is, and takes the slow_down_layers step of its middle. A piece cut + // at the edge of a band (see belt_cut_at_band_edges()) starts or ends on the edge, so + // neither of its ends would do. + int belt_layer = -1; + if (m_config.belt_printer.value) { + double lo = std::numeric_limits::max(), hi = std::numeric_limits::lowest(), h; + for (const Point3 &p : path.polyline.points) + if (this->belt_path_height(path, p, h)) { + lo = std::min(lo, h); + hi = std::max(hi, h); + } + if (lo <= hi) { + path_on_first_layer = hi <= m_config.initial_layer_print_height.value + EPSILON; + belt_layer = path_on_first_layer ? 0 : std::max(1, this->belt_band_index(0.5 * (lo + hi))); + } + } const ExtrusionPathSloped* sloped = dynamic_cast(&path); @@ -9277,7 +9378,10 @@ std::string GCode::_extrude(const ExtrusionPath &path, const std::string &path_d z = unscale_(path.polyline.lines().begin()->a.z()) + m_nominal_z; } + // The writer's first-layer test judges the travel like the path it leads to. + m_belt_travel_target = &path; gcode += this->travel_to(first_point, path.role(), "move to first " + description + " point", z); + m_belt_travel_target = nullptr; // Orca: ensure Z matches planned layer height if (!slope_need_z_travel && (_last_pos_undefined || m_need_change_layer_lift_z)) { @@ -9483,16 +9587,18 @@ std::string GCode::_extrude(const ExtrusionPath &path, const std::string &path_d // Use the belt-aware effective layer index when on a belt printer so // the speed fade tracks perpendicular distance from the plane on // belt printers; otherwise this falls back to the slicing layer id. - const int _layer = this->effective_layer_index_for_point(path_point_mm); + const int _layer = belt_layer >= 0 ? belt_layer : this->effective_layer_index_for_point(path_point_mm); // Belt printers: a tilted layer runs from the belt to the top of the part, so the // "first layers" the fan stays off for are a band along the belt. Mark where the // extrusion enters and leaves it, per segment, for the cooling buffer. const bool belt_band_tags = m_enable_cooling_markers && m_config.belt_printer.value; const int belt_band_layers = belt_band_tags ? m_config.close_fan_the_first_x_layers.get_at(m_writer.filament()->id()) : 0; - auto tag_belt_band = [this, &gcode, belt_band_tags, belt_band_layers, z = path_point_mm.z()](coord_t x, coord_t y) { + auto tag_belt_band = [this, &gcode, &path, &path_point_mm, belt_band_tags, belt_band_layers](const Point3 &p) { if (! belt_band_tags) return; - const bool in_band = this->effective_layer_index_for_point(Vec3d(unscale(x), unscale(y), z)) < belt_band_layers; + double h; + const int layer = this->belt_path_height(path, p, h) ? this->belt_band_index(h) : this->effective_layer_index_for_point(path_point_mm); + const bool in_band = layer < belt_band_layers; if (in_band != m_belt_in_band) { gcode += in_band ? ";_BELT_BAND_START\n" : ";_BELT_BAND_END\n"; m_belt_in_band = in_band; @@ -9963,7 +10069,11 @@ std::string GCode::_extrude(const ExtrusionPath &path, const std::string &path_d double total_length = sloped == nullptr ? 0. : path.polyline.length() * SCALING_FACTOR; double saved_z = m_writer.get_position().z(); + size_t line_idx = 0; for (const Line3& line : path.polyline.lines()) { + // A line cut at a band edge above a belt is compensated as the line it was. + const double uncut_length = m_belt_uncut_line_lengths != nullptr ? (*m_belt_uncut_line_lengths)[line_idx] : 0.; + ++ line_idx; std::string flow_description; const double line_length = line.length() * SCALING_FACTOR; if (line_length < EPSILON) @@ -9972,13 +10082,13 @@ std::string GCode::_extrude(const ExtrusionPath &path, const std::string &path_d auto dE = e_per_mm * line_length; if (_needSAFC(path)) { auto oldE = dE; - dE = m_small_area_infill_flow_compensator->modify_flow(line_length, dE, path.role()); + dE = m_small_area_infill_flow_compensator->modify_flow(m_belt_uncut_line_lengths != nullptr ? uncut_length : line_length, dE, path.role()); if (m_config.gcode_comments && oldE > 0 && oldE != dE) { flow_description = description + Slic3r::format(" | Old Flow Value: %0.5f Length: %0.5f",oldE, line_length); } } - tag_belt_band((line.a.x() + line.b.x()) / 2, (line.a.y() + line.b.y()) / 2); + tag_belt_band(line.midpoint()); if (path.z_contoured) { // ZAA: Z anti-aliased extrusion with variable Z per point Vec2d dest2d = this->point_to_gcode(line.b.to_point()); @@ -10109,7 +10219,7 @@ std::string GCode::_extrude(const ExtrusionPath &path, const std::string &path_d const ProcessedPoint &processed_point = new_points[i]; const ProcessedPoint &pre_processed_point = new_points[i-1]; Vec3d p = this->point_to_gcode_quantized(processed_point.p); - tag_belt_band((pre_processed_point.p.x() + processed_point.p.x()) / 2, (pre_processed_point.p.y() + processed_point.p.y()) / 2); + tag_belt_band(Point3((pre_processed_point.p + processed_point.p) / 2)); if (m_enable_cooling_markers) { if (enable_overhang_bridge_fan) { cur_fan_enabled = overhang_fan_active(processed_point.overlap, overhang_fan_threshold, path.role()); @@ -11496,7 +11606,22 @@ int GCode::belt_layer_past_first_layer_band(const Layer *object_layer) const return min_height >= this->first_layer_band_mm() - EPSILON ? 1 : 0; } -bool GCode::belt_height_above_floor(const Vec3d &point_slicing_mm, double &height_mm) const +bool GCode::belt_path_height(const ExtrusionPath &path, const Point3 &p, double &height_mm) const +{ + // A belt brim apron band has no Layer of its own, so it publishes its Z through + // m_belt_brim_z; without that the belt would be probed at Z=0 and the apron + // misjudged for fan and speed. + const double z = m_layer ? m_layer->print_z : (m_belt_brim_z ? *m_belt_brim_z : 0.0); + const Vec3d point(unscale(p.x()), unscale(p.y()), path.z_contoured ? z + unscale(p.z()) : z); + if (! this->belt_height_above_floor(point, height_mm, path.width)) + return false; + // The brim's lines are as wide as the bands are far apart, not as the bead they lay. + if (path.role() == erBrim) + height_mm = 0.; + return true; +} + +bool GCode::belt_height_above_floor(const Vec3d &point_slicing_mm, double &height_mm, double width) const { // The owning object, which is what carries the belt description. During // object-brim and coincident-apron emission m_layer still points at whichever @@ -11520,7 +11645,7 @@ bool GCode::belt_height_above_floor(const Vec3d &point_slicing_mm, double &heigh // horizontal slabs in the sliced frame, so the slab holding the material that // rests on the belt at this point is the one within one layer height of it. // A perpendicular measure would shrink the band by 1/cos(tilt). - height_mm = point_slicing_mm.z() - floor_z; + height_mm = point_slicing_mm.z() - floor_z - 0.5 * width * std::abs(sp.belt_floor_shear_factor); return true; } diff --git a/src/libslic3r/GCode.hpp b/src/libslic3r/GCode.hpp index e5fb414cd3..03c5325953 100644 --- a/src/libslic3r/GCode.hpp +++ b/src/libslic3r/GCode.hpp @@ -962,6 +962,12 @@ protected: // The last extrusion segment was inside the belt's first-layer fan band (see _extrude()). bool m_belt_in_band{false}; + // The path that the travel being emitted leads to, for the writer's first-layer test on a + // belt; null for any other travel. + const ExtrusionPath *m_belt_travel_target{nullptr}; + // For a path cut at the edges of the bands above a belt (see _extrude()), the length, mm, + // of the uncut line each of its lines comes from; null for any other path. + const std::vector *m_belt_uncut_line_lengths{nullptr}; std::set m_initial_layer_extruders; std::vector> m_sorted_layer_filaments; @@ -973,6 +979,9 @@ protected: double calc_max_volumetric_speed(const double layer_height, const double line_width, const std::string co_str); std::string _extrude(const ExtrusionPath &path, const std::string &path_description = "", double speed = -1); + ExtrusionPaths belt_cut_at_band_edges(const ExtrusionPath &path, std::vector> &uncut_line_lengths) const; + // _extrude() of a path that stays in one band above the belt (see belt_cut_at_band_edges()). + std::string _extrude_piece(const ExtrusionPath &path, const std::string &path_description, double speed); bool _needSAFC(const ExtrusionPath &path); void print_machine_envelope(GCodeOutputStream& file, Print& print); void _print_first_layer_bed_temperature(GCodeOutputStream &file, Print &print, const std::string &gcode, unsigned int first_printing_extruder_id, bool wait); @@ -983,10 +992,12 @@ protected: // Per-point first-layer test. On a belt printer the result depends on the // supplied slicing-frame point (its height above the belt); otherwise we // delegate to the legacy per-layer test. This is the entry point used by - // per-path call sites in _extrude. - bool on_first_layer(const Vec3d &point_slicing_mm) const { + // per-path call sites in _extrude and by travels, which pass the width of the + // extrusion they lead to (see belt_height_above_floor()). _extrude_piece() + // judges a belt path by all of its points (see belt_path_height()). + bool on_first_layer(const Vec3d &point_slicing_mm, double width = 0.) const { double h; - if (this->belt_height_above_floor(point_slicing_mm, h)) + if (this->belt_height_above_floor(point_slicing_mm, h, width)) return h <= m_config.initial_layer_print_height.value + EPSILON; return on_first_layer(); } @@ -995,12 +1006,14 @@ protected: // first_layer_band_mm() units; otherwise it is the legacy slicing layer index. int effective_layer_index_for_point(const Vec3d &point_slicing_mm) const { double h; - if (this->belt_height_above_floor(point_slicing_mm, h)) { - const double lh = this->first_layer_band_mm(); - return h <= 0. ? 0 : int(std::floor(h / lh)); - } + if (this->belt_height_above_floor(point_slicing_mm, h)) + return this->belt_band_index(h); return on_first_layer() ? 0 : layer_id(); } + // The effective layer index of a height above the belt. + int belt_band_index(double height_mm) const { + return height_mm <= 0. ? 0 : int(std::floor(height_mm / this->first_layer_band_mm())); + } // Band thickness for the *effective layer index*: one first layer height, so // "the first N layers" means the same height above the belt as on a flat bed. @@ -1016,7 +1029,17 @@ protected: // parameters (belt_floor_shear_factor / _from_axis / _z_shift) -- the same // description the support generator uses, independent of every remap and // back-transform. - bool belt_height_above_floor(const Vec3d &point_slicing_mm, double &height_mm) const; + // + // With `width`, the point is the nozzle over an extrusion that wide, and the height + // is taken above the highest belt under the bead: the belt rises across it, so the + // wall of a tilted layer that rests on the belt has its nozzle half a layer plus + // half its width x tan(tilt) up, more than one first layer at 45 degrees. + bool belt_height_above_floor(const Vec3d &point_slicing_mm, double &height_mm, double width = 0.) const; + // Height above the belt of a point of `path` on the layer being emitted, with the nozzle + // over the path's bead and the Z offset of a contoured path; false when this is not a belt + // print. A brim line rests on the belt by construction, however narrow the spacing of the + // bands makes its bead, so it is at 0. + bool belt_path_height(const ExtrusionPath &path, const Point3 &p, double &height_mm) const; // 1 / 0 / -1: the object layer is entirely past the first-layer band above the // belt / reaches into it / the belt surface is not known for it. int belt_layer_past_first_layer_band(const Layer *object_layer) const; diff --git a/tests/fff_print/test_print.cpp b/tests/fff_print/test_print.cpp index b4b97f560b..28cd24fd1f 100644 --- a/tests/fff_print/test_print.cpp +++ b/tests/fff_print/test_print.cpp @@ -1372,9 +1372,14 @@ TEST_CASE("Belt printers refuse an object taller than the gantry clearance", "[P // On a belt every tilted layer starts on the belt, so "the first layers" the fan stays off // for are a band along the belt, not the first slicing layers. The generator marks where // each extrusion segment enters and leaves that band and the cooling buffer keeps the fan -// off inside it, on every layer. +// off inside it, on every layer. The lowest wall of each tilted layer rests on the belt with +// its nozzle half a layer plus half its width up (0.1 + 0.21 mm here), so a band of one +// first layer has to be judged from the bead, not the nozzle, to hold it. TEST_CASE("Belt printers keep the part fan off within the band above the belt", "[Print][belt][Cooling]") { + // Fan-off layers, and the gantry Y below which an extrusion is well inside that band. + const auto [fan_off_layers, band_y] = GENERATE(table({ { 1, 0.35 }, { 3, 0.45 } })); + CAPTURE(fan_off_layers); DynamicPrintConfig config = DynamicPrintConfig::full_print_config(); config.set_deserialize_strict({ { "belt_printer", 1 }, @@ -1387,9 +1392,8 @@ TEST_CASE("Belt printers keep the part fan off within the band above the belt", { "initial_layer_print_height", 0.2 }, { "skirt_loops", 0 }, { "z_hop", 0 }, - // Three layers, 0.6 mm: the lowest wall of each tilted layer is centred about 0.3 mm - // above the belt (half a line width in from the contact edge). - { "close_fan_the_first_x_layers", 3 }, + { "outer_wall_line_width", 0.42 }, + { "close_fan_the_first_x_layers", fan_off_layers }, { "full_fan_speed_layer", 0 }, { "fan_min_speed", 100 }, { "fan_max_speed", 100 }, @@ -1406,9 +1410,9 @@ TEST_CASE("Belt printers keep the part fan off within the band above the belt", CHECK(gcode.find(";_BELT_BAND") == std::string::npos); // With this axis mapping machine Y is the height above the belt along the gantry. Walk - // the moves with the fan state: extrusions that stay within 0.45 mm of the belt are well - // inside the band and must print with the fan off; extrusions that stay 5 mm clear of it - // must print with it on. The first three slicing layers have the fan off altogether. + // the moves with the fan state: extrusions that stay below band_y are well inside the band + // and must print with the fan off; extrusions that stay 5 mm clear of it must print with + // it on. The first fan_off_layers slicing layers have the fan off altogether. size_t in_band = 0, in_band_fan_on = 0, clear = 0, clear_fan_off = 0; int layer = -1; bool fan_on = false; @@ -1430,8 +1434,8 @@ TEST_CASE("Belt printers keep the part fan off within the band above the belt", continue; const double y_new = std::atof(cmd.c_str() + ypos + 2); const bool extruding = epos != std::string::npos && std::atof(cmd.c_str() + epos + 2) > 0.; - if (extruding && layer >= 3) { - if (std::max(y, y_new) < 0.45) { + if (extruding && layer >= fan_off_layers) { + if (std::max(y, y_new) < band_y) { ++ in_band; in_band_fan_on += fan_on; } else if (std::min(y, y_new) > 5.) { @@ -1448,6 +1452,144 @@ TEST_CASE("Belt printers keep the part fan off within the band above the belt", CHECK(clear_fan_off == 0); } +// On a belt one wall loop runs from the belt up the part, and whether a path is on the first +// layer, and its step of the slow_down_layers ramp, set its speed, flow and acceleration as a +// whole. Each part of the loop must take the settings of its own height above the belt, +// wherever the seam starts the loop. +TEST_CASE("Belt printers use first-layer settings only where a wall rests on the belt", "[Print][belt][Regression]") +{ + const std::string seam = GENERATE("back", "random"); + const int slow_down_layers = GENERATE(0, 4); + CAPTURE(seam, slow_down_layers); + DynamicPrintConfig config = DynamicPrintConfig::full_print_config(); + config.set_deserialize_strict({ + { "belt_printer", 1 }, + { "belt_slice_rotation", "x" }, + { "belt_slice_rotation_angle", 45 }, + { "gcode_remap_x", "rev_x" }, + { "gcode_remap_y", "pos_z" }, + { "gcode_remap_z", "pos_y" }, + { "layer_height", 0.2 }, + { "initial_layer_print_height", 0.2 }, + { "skirt_loops", 0 }, + { "brim_type", "no_brim" }, + { "z_hop", 0 }, + { "wall_generator", "classic" }, + { "outer_wall_line_width", 0.42 }, + { "seam_position", seam }, + // One path per wall loop, so nothing but the edges of the bands cut it. + { "detect_overhang_wall", 0 }, + { "enable_overhang_speed", 0 }, + { "initial_layer_speed", 10 }, + { "outer_wall_speed", 40 }, + { "slow_down_layers", slow_down_layers }, + { "filament_max_volumetric_speed", 20 }, + { "slow_down_for_layer_cooling", 0 }, + { "set_other_flow_ratios", 1 }, + { "first_layer_flow_ratio", 0.8 }, + { "machine_start_gcode", "T[initial_tool]\n" }, + { "layer_change_gcode", "G92 E0\n" }, + }); + const std::string gcode = slice({ cube(20) }, config); + + // As above, machine Y is the height above the belt. The outer wall's nozzle is half its + // width, 0.21 mm, higher than the belt under its edge, so the first layer, up to 0.2 mm, + // ends at Y = 0.41 and step k of the ramp spans Y = 0.21 + [0.2 k, 0.2 (k + 1)). Its wall + // resting on the belt is at Y = 0.31. + const double ramp_step_2 = 10. + (40. - 10.) * 2. / 4.; + std::vector in_band_e, clear_e; // E per mm + size_t in_band = 0, in_band_fast = 0, step_2 = 0, step_2_off = 0, clear = 0, clear_slow = 0; + std::string type; + GCodeReader reader; + reader.parse_buffer(gcode, [&](GCodeReader &self, const GCodeReader::GCodeLine &line) { + if (boost::starts_with(line.raw(), ";TYPE:")) { + type = line.raw().substr(6); + return; + } + if (type != "Outer wall" || ! line.extruding(self) || line.dist_XY(self) <= EPSILON) + return; + const double y_lo = std::min(self.y(), line.new_Y(self)); + const double y_hi = std::max(self.y(), line.new_Y(self)); + const double f = line.new_F(self); + // Long moves only for the flow, so that the rounding of E does not blur it. + const bool long_move = line.dist_XY(self) >= 0.5; + if (y_hi < 0.35) { + ++ in_band; + in_band_fast += std::abs(f - 10. * 60.) > 1.; + if (long_move) + in_band_e.push_back(line.dist_E(self) / line.dist_XY(self)); + } else if (slow_down_layers == 4 && y_lo > 0.61 - 1e-3 && y_hi < 0.81 + 1e-3) { + ++ step_2; + step_2_off += std::abs(f - ramp_step_2 * 60.) > 1.; + } else if (y_lo > 1.5) { + ++ clear; + clear_slow += std::abs(f - 40. * 60.) > 1.; + if (long_move) + clear_e.push_back(line.dist_E(self) / line.dist_XY(self)); + } + }); + REQUIRE(in_band > 20); + REQUIRE(clear > 20); + CHECK(in_band_fast == 0); + CHECK(clear_slow == 0); + if (slow_down_layers == 4) { + REQUIRE(step_2 > 10); + CHECK(step_2_off == 0); + } + // first_layer_flow_ratio on the belt and nowhere else. + auto median = [](std::vector v) { + std::nth_element(v.begin(), v.begin() + v.size() / 2, v.end()); + return v[v.size() / 2]; + }; + CHECK_THAT(median(in_band_e) / median(clear_e), Catch::Matchers::WithinRel(0.8, 0.02)); +} + +// Small-area flow compensation scales a line's flow by the line's length. The cuts a belt path +// gets at the edges of the bands above the belt must not make it see the pieces as short lines. +TEST_CASE("Belt band cuts leave small-area flow compensation unchanged", "[Print][belt][Regression]") +{ + // Plastic per role. The slow_down_layers ramp adds band edges every 0.2 mm above the belt, + // here up to well past the top of the part, and with them cuts through the solid infill + // under its top face, but changes nothing else about how much plastic is laid. + auto plastic = [](int slow_down_layers) { + DynamicPrintConfig config = DynamicPrintConfig::full_print_config(); + config.set_deserialize_strict({ + { "belt_printer", 1 }, + { "belt_slice_rotation", "x" }, + { "belt_slice_rotation_angle", 45 }, + { "gcode_remap_x", "rev_x" }, + { "gcode_remap_y", "pos_z" }, + { "gcode_remap_z", "pos_y" }, + { "layer_height", 0.2 }, + { "initial_layer_print_height", 0.2 }, + { "skirt_loops", 0 }, + { "brim_type", "no_brim" }, + { "z_hop", 0 }, + { "bottom_shell_layers", 4 }, + { "internal_solid_infill_pattern", "monotonic" }, + { "small_area_infill_flow_compensation", 1 }, + { "slow_down_layers", slow_down_layers }, + { "machine_start_gcode", "T[initial_tool]\n" }, + { "layer_change_gcode", "G92 E0\n" }, + }); + std::map e; + std::string type; + GCodeReader reader; + reader.parse_buffer(slice({ cube(20) }, config), [&](GCodeReader &self, const GCodeReader::GCodeLine &line) { + if (boost::starts_with(line.raw(), ";TYPE:")) + type = line.raw().substr(6); + else if (line.extruding(self) && line.dist_XY(self) > EPSILON) + e[type] += line.dist_E(self); + }); + return e; + }; + const std::map uncut = plastic(0); + const std::map cut = plastic(200); + REQUIRE(uncut.count("Internal solid infill") == 1); + REQUIRE(cut.count("Internal solid infill") == 1); + CHECK_THAT(cut.at("Internal solid infill"), Catch::Matchers::WithinRel(uncut.at("Internal solid infill"), 1e-4)); +} + // Organic supports under an overhang on a belt printer reach below the object's first layer, // where the virtual belt raft layers sit at negative Z. The lowest of them used to get a // negative height and abort slicing with a negative flow error. diff --git a/tests/fff_print/test_skirt_brim.cpp b/tests/fff_print/test_skirt_brim.cpp index c732c54793..29326e9a49 100644 --- a/tests/fff_print/test_skirt_brim.cpp +++ b/tests/fff_print/test_skirt_brim.cpp @@ -17,6 +17,7 @@ #include #include #include +#include #include "libslic3r/Polygon.hpp" #include "libslic3r/Point.hpp" #include "libslic3r/Polyline.hpp" @@ -1409,22 +1410,30 @@ TEST_CASE("Belt brim coexists with support material", "[SkirtBrim][belt]") CHECK(role_passes(gc, "brim") > 0); } -// With a 0.3 mm first layer at 45 degrees the brim band on the belt is wider than one bead, -// so its lines go on the nominal lattice instead of at a fixed fraction of the band. A -// lattice line can then land where the belt is almost at the band's print_z; it must be -// moved uphill to the same 0.75 fraction the single-line case uses, not laid scraping the -// belt with its flow clamped to half a layer. +// On a shallow belt (25 degrees, 0.3 mm layers) the brim band on the belt is wider than one +// bead, so its lines go on the nominal lattice instead of at one fixed clearance. A lattice +// line can then land where the belt is almost at the band's print_z; it must be moved to the +// clearance of the object's outer wall, where every line of a steep belt sits, not laid +// scraping the belt with its flow clamped to half a layer. TEST_CASE("Belt brim lattice lines keep their clearance above the belt", "[SkirtBrim][belt]") { + const double layer_height = 0.3; + const double wall_width = 0.42; + const double tilt_deg = 25.; DynamicPrintConfig config = belt_brim_config(); config.set_deserialize_strict({ - { "layer_height", 0.3 }, - { "initial_layer_print_height", 0.3 }, + { "belt_slice_rotation_angle", tilt_deg }, + { "layer_height", layer_height }, + { "initial_layer_print_height", layer_height }, + { "outer_wall_line_width", wall_width }, { "brim_type", "outer_only" }, { "brim_width", 4 }, { "brim_object_gap", 0 }, }); const std::string gcode = slice({ cube(20) }, config); + // Along slice Z: half a layer down to where the slice meets the belt, then half a wall + // width inwards, where the belt lies lower by that times tan(tilt). + const double wall_clearance = 0.5 * layer_height + 0.5 * wall_width * std::tan(Geometry::deg2rad(tilt_deg)); // Heights of the brim extrusions, from the ;HEIGHT: tags inside ;TYPE:Brim sections. std::vector brim_heights; @@ -1437,12 +1446,246 @@ TEST_CASE("Belt brim lattice lines keep their clearance above the belt", "[Skirt brim_heights.push_back(std::stod(line.substr(8))); } REQUIRE(! brim_heights.empty()); + // Lattice lines have clearances of their own; a single clearance would mean the band + // took the steep-belt branch and the lattice went untested. + std::set distinct_heights; + for (const double h : brim_heights) + distinct_heights.insert(std::lround(h * 1000.)); + REQUIRE(distinct_heights.size() > 1); for (const double h : brim_heights) { - CHECK(h >= 0.75 * 0.3 - 1e-3); - CHECK(h <= 0.3 + 1e-3); + CHECK(h >= wall_clearance - 1e-3); + CHECK(h <= layer_height + 1e-3); } } +// Lowest gantry Y of the `type` extrusions on each layer, keyed by machine Z, which is the +// belt position and therefore one value per tilted layer. Under the belt remap the gantry is +// the height above the belt, so for a role resting on the belt this is its nozzle clearance. +static std::map belt_layer_lowest_y(const std::string &gcode, const std::string &type) +{ + std::map lowest; + bool in_type = false; + GCodeReader reader; + reader.parse_buffer(gcode, [&](GCodeReader &self, const GCodeReader::GCodeLine &line) { + if (boost::starts_with(line.raw(), ";TYPE:")) { + in_type = line.raw().substr(6) == type; + return; + } + if (! in_type || ! line.extruding(self) || line.dist_XY(self) <= EPSILON) + return; + const double y = std::min(self.y(), line.new_Y(self)); + auto [it, inserted] = lowest.emplace(std::lround(line.new_Z(self) * 1000.), y); + if (! inserted) + it->second = std::min(it->second, y); + }); + return lowest; +} + +// The brim is the first thing laid on the belt, so it is what a user levels the belt height +// against (endstop, extruder offset). Its lines must then sit exactly as high above the belt +// as the object's outer wall does where it rests on the belt, or a belt levelled for the brim +// lays the part too high to stick. +TEST_CASE("Belt brim sits as high above the belt as the object's outer wall", "[SkirtBrim][belt][Regression]") +{ + const double layer_height = GENERATE(0.2, 0.32); + // A wider first-layer line widens only the walls of object layer 0, so it must not lift + // the brim off the walls of the layers that rest on the belt. 0 keeps the outer wall's. + const double first_width = GENERATE(0., 0.6); + // XY contour compensation moves the slice's edge, and with it the wall, toward or away + // from the belt, also by more than half the wall's width. Shrinking it by half a layer or + // more lifts it off the belt's contact band, and the object gets no brim. + const double compensation = GENERATE(0., 0.05, -0.05, 0.25); + DYNAMIC_SECTION("layer_height=" << layer_height << " initial_layer_line_width=" << first_width + << " xy_contour_compensation=" << compensation) { + const double wall_width = 0.4; + DynamicPrintConfig config = belt_brim_config(); + config.set_deserialize_strict({ + { "layer_height", layer_height }, + { "initial_layer_print_height", layer_height }, + { "outer_wall_line_width", wall_width }, + { "initial_layer_line_width", first_width }, + { "xy_contour_compensation", compensation }, + { "print_flow_ratio", 1 }, + { "brim_flow_ratio", 1 }, + { "filament_diameter", "1.75" }, + { "brim_type", "outer_only" }, + { "brim_width", 4 }, + { "brim_object_gap", 0 }, + }); + const std::string gcode = slice({ cube(20) }, config); + const std::map brim = belt_layer_lowest_y(gcode, "Brim"); + const std::map wall = belt_layer_lowest_y(gcode, "Outer wall"); + REQUIRE(! brim.empty()); + REQUIRE(! wall.empty()); + + // The wall's clearance on the layers it rests on the belt, i.e. the most common lowest + // Y. Not the lowest one: the sliver at the first contact can carry a narrower wall, + // which sits lower; and layers past the part's bottom face do not reach the belt. + std::map votes; + for (const auto &[z, y] : wall) + ++ votes[std::lround(y * 1000.)]; + const auto typical = std::max_element(votes.begin(), votes.end(), + [](const auto &a, const auto &b) { return a.second < b.second; }); + const double wall_y = 0.001 * typical->first; + for (const auto &[z, y] : brim) + CHECK_THAT(y, Catch::Matchers::WithinAbs(wall_y, 0.005)); + + // Each brim line fills the cell between it and the next, one band apart on the belt + // (the layer height / sin(45 deg)) and as tall as its clearance (its ;HEIGHT:), also + // where the clearance is small. + const double filament_area = 0.25 * PI * 1.75 * 1.75; + const double pitch = layer_height * std::sqrt(2.); + size_t long_moves = 0; + double clearance = 0.; + bool in_brim = false; + GCodeReader reader; + reader.parse_buffer(gcode, [&](GCodeReader &self, const GCodeReader::GCodeLine &line) { + if (boost::starts_with(line.raw(), ";TYPE:")) + in_brim = line.raw().substr(6) == "Brim"; + else if (boost::starts_with(line.raw(), ";HEIGHT:")) + clearance = std::stod(line.raw().substr(8)); + else if (in_brim && line.extruding(self) && line.dist_XY(self) >= 1.) { + ++ long_moves; + CHECK_THAT(line.dist_E(self) / line.dist_XY(self) * filament_area, + Catch::Matchers::WithinRel(pitch * clearance, 0.01)); + } + }); + CHECK(long_moves > 20); + } +} + +// A modifier that gives the part a narrower outer wall away from the belt shares the tilted +// layers with the wall that rests on the belt. The brim must be levelled with the wall that +// rests on the belt, not with the narrowest wall in the layer. +TEST_CASE("Belt brim follows the wall that rests on the belt rather than a modifier's", "[SkirtBrim][belt][Regression]") +{ + DynamicPrintConfig config = belt_brim_config(); + config.set_deserialize_strict({ + { "gcode_comments", 1 }, + { "outer_wall_line_width", 0.6 }, + { "brim_type", "outer_only" }, + { "brim_width", 4 }, + { "brim_object_gap", 0 }, + }); + // The top half of the cube, 10 mm and more above the belt, gets a 0.4 mm outer wall. + DynamicPrintConfig modifier_config; + modifier_config.set_deserialize_strict({ { "outer_wall_line_width", 0.4 } }); + TriangleMesh modifier = make_cube(22., 22., 11.); + modifier.translate(-1.f, -1.f, 10.f); + Print print; + Model model; + init_print({ cube(20) }, print, model, config, nullptr, false); + model.objects.front()->add_volume(std::move(modifier), ModelVolumeType::PARAMETER_MODIFIER, false)->config.apply(modifier_config); + print.apply(model, config); + const std::string out = gcode(print); + + // Both walls print. + std::set wall_widths; + bool in_wall = false; + std::istringstream lines(out); + for (std::string line; std::getline(lines, line); ) { + if (boost::starts_with(line, ";TYPE:")) + in_wall = line.substr(6) == "Outer wall"; + else if (in_wall && boost::starts_with(line, ";WIDTH:")) + wall_widths.insert(std::lround(std::stod(line.substr(7)) * 100.)); + } + REQUIRE(wall_widths.count(40) == 1); + REQUIRE(wall_widths.count(60) == 1); + + const std::map brim = belt_layer_lowest_y(out, "Brim"); + const std::map wall = belt_layer_lowest_y(out, "Outer wall"); + REQUIRE(! brim.empty()); + std::map votes; + for (const auto &[z, y] : wall) + ++ votes[std::lround(y * 1000.)]; + const auto typical = std::max_element(votes.begin(), votes.end(), + [](const auto &a, const auto &b) { return a.second < b.second; }); + const double wall_y = 0.001 * typical->first; + // Half a 0.2 mm layer plus half the 0.6 mm wall, x tan(45 deg). + CHECK_THAT(wall_y, Catch::Matchers::WithinAbs(0.1 + 0.3, 0.005)); + for (const auto &[z, y] : brim) + CHECK_THAT(y, Catch::Matchers::WithinAbs(wall_y, 0.005)); +} + +// The brim rests on the belt, so it prints, and is reached, with first-layer settings, even +// though raising it to the outer wall's clearance puts the nozzle further above the belt than +// one first layer. With thin layers its lines are also far narrower than a bead, as narrow as +// the bands are far apart, so their width must not decide it either. +TEST_CASE("Belt brim lines print with first-layer settings", "[SkirtBrim][belt][Regression]") +{ + const double layer_height = GENERATE(0.2, 0.08); + CAPTURE(layer_height); + DynamicPrintConfig config = belt_brim_config(); + config.set_deserialize_strict({ + { "layer_height", layer_height }, + { "initial_layer_print_height", 0.2 }, + { "outer_wall_line_width", 0.45 }, + { "brim_type", "outer_only" }, + { "brim_width", 4 }, + { "brim_object_gap", 0 }, + { "z_hop", 0 }, + { "travel_speed", 200 }, + { "initial_layer_travel_speed", 50 }, + { "initial_layer_speed", 15 }, + { "filament_max_volumetric_speed", 20 }, + { "slow_down_for_layer_cooling", 0 }, + // The fan is on everywhere but in the band above the belt. + { "close_fan_the_first_x_layers", 1 }, + { "full_fan_speed_layer", 0 }, + { "fan_min_speed", 100 }, + { "fan_max_speed", 100 }, + { "fan_cooling_layer_time", 1000 }, + { "reduce_fan_stop_start_freq", 0 }, + }); + const std::string gcode = slice({ cube(20) }, config); + + // The feedrate of the last travel before each brim line, and the feedrate and fan of + // each of its moves. + size_t brim_lines = 0, fast_travels = 0, brim_moves = 0, brim_fast = 0, brim_fan_on = 0, fan_on_moves = 0; + std::string type; + double travel_f = 0.; + bool travelled = false, fan_on = false; + GCodeReader reader; + reader.parse_buffer(gcode, [&](GCodeReader &self, const GCodeReader::GCodeLine &line) { + if (boost::starts_with(line.raw(), ";TYPE:")) { + type = line.raw().substr(6); + return; + } + if (line.cmd_is("M107")) { + fan_on = false; + return; + } + if (line.cmd_is("M106")) { + const size_t s = line.raw().find('S'); + fan_on = s != std::string::npos && std::atof(line.raw().c_str() + s + 1) > 0.; + return; + } + if (! line.cmd_is("G1") || line.dist_XY(self) <= EPSILON) + return; + if (! line.extruding(self)) { + travel_f = line.new_F(self); + travelled = true; + return; + } + fan_on_moves += fan_on; + if (type == "Brim") { + if (travelled) { + ++ brim_lines; + fast_travels += std::abs(travel_f - 50. * 60.) > 1.; + } + ++ brim_moves; + brim_fast += std::abs(line.new_F(self) - 15. * 60.) > 1.; + brim_fan_on += fan_on; + } + travelled = false; + }); + REQUIRE(brim_lines > 20); + REQUIRE(fan_on_moves > 0); + CHECK(fast_travels == 0); + CHECK(brim_fast == 0); + CHECK(brim_fan_on == 0); +} + // The brim prints in the object's outer wall filament even when every extrusion of the object // is offered to purging (flush_into_objects): the tool ordering registers the brim filament // itself, so the writer always knows it.