Skip to content

Commit

Permalink
fix legend & kickstart, add discrete colors for legend
Browse files Browse the repository at this point in the history
  • Loading branch information
supermerill committed Feb 13, 2024
2 parents 75855cb + 80ba439 commit 6128659
Show file tree
Hide file tree
Showing 9 changed files with 786 additions and 372 deletions.
23 changes: 2 additions & 21 deletions src/libslic3r/Fill/Fill.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,7 @@ void Layer::make_fills(FillAdaptive::Octree* adaptive_fill_octree, FillAdaptive:
// Unpacks the collection, creates multiple collections per path.
// The path type could be ExtrusionPath, ExtrusionLoop or ExtrusionEntityCollection.
// Why the paths are unpacked?
for (LayerRegion *layerm : m_regions)
for (LayerRegion *layerm : m_regions) {
for (const ExtrusionEntity *thin_fill : layerm->thin_fills.entities()) {
ExtrusionEntityCollection *collection = new ExtrusionEntityCollection();
if (!layerm->fills.can_sort() && layerm->fills.entities().size() > 0 && layerm->fills.entities()[0]->is_collection()) {
Expand All @@ -729,27 +729,8 @@ void Layer::make_fills(FillAdaptive::Octree* adaptive_fill_octree, FillAdaptive:
layerm->fills.append(ExtrusionEntitiesPtr{ collection });
collection->append(*thin_fill);
}
}

#ifndef NDEBUG
for (LayerRegion *layerm : m_regions)
for (size_t i1 = 0; i1 < layerm->fills.entities().size(); ++i1) {
assert(dynamic_cast<ExtrusionEntityCollection*>(layerm->fills.entities()[i1]) != nullptr);
if (!layerm->fills.can_sort() && layerm->fills.entities().size() > 0 && i1 == 0){
ExtrusionEntityCollection* no_sort_fill = static_cast<ExtrusionEntityCollection*>(layerm->fills.entities()[0]);
assert(no_sort_fill != nullptr);
assert(!no_sort_fill->empty());
for (size_t i2 = 0; i2 < no_sort_fill->entities().size(); ++i2) {
ExtrusionEntityCollection* priority_fill = dynamic_cast<ExtrusionEntityCollection*>(no_sort_fill->entities()[i2]);
assert(priority_fill != nullptr);
assert(!priority_fill->empty());
if (!no_sort_fill->can_sort()) {
for (size_t i3 = 0; i3 < priority_fill->entities().size(); ++i3)
assert(dynamic_cast<ExtrusionEntityCollection*>(priority_fill->entities()[i3]) != nullptr);
}
}
}
}
#endif
}

// Create ironing extrusions over top surfaces.
Expand Down
38 changes: 24 additions & 14 deletions src/libslic3r/GCode/FanMover.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,18 @@ int16_t get_fan_speed(const std::string &line, GCodeFlavor flavor) {

}

void FanMover::_put_in_middle_G1(std::list<BufferData>::iterator item_to_split, float nb_sec_since_itemtosplit_start, BufferData &&line_to_write) {
void FanMover::_put_in_middle_G1(std::list<BufferData>::iterator item_to_split, float nb_sec_since_itemtosplit_start, BufferData &&line_to_write, float max_time) {
assert(item_to_split != m_buffer.end());
if (nb_sec_since_itemtosplit_start > item_to_split->time * 0.9) {
// if the fan is at the end of the g1 and the diff is less than 10% of the delay, then don't bother
if (nb_sec_since_itemtosplit_start > item_to_split->time * 0.9 && (item_to_split->time - nb_sec_since_itemtosplit_start) < max_time * 0.1) {
// doesn't really need to be split, print it after
m_buffer.insert(next(item_to_split), line_to_write);
} else if (nb_sec_since_itemtosplit_start < item_to_split->time * 0.1) {
} else
// does it need to be split?
// if it's almost at the start of the g1, and the time "lost" is less than 10%
if (nb_sec_since_itemtosplit_start < item_to_split->time * 0.1 && nb_sec_since_itemtosplit_start < max_time * 0.1 &&
// and the previous isn't a fan value
(item_to_split == m_buffer.begin() || std::prev(item_to_split)->fan_speed < 0)) {
// doesn't really need to be split, print it before
//will also print before if line_to_split.time == 0
m_buffer.insert(item_to_split, line_to_write);
Expand Down Expand Up @@ -340,11 +346,18 @@ void FanMover::_process_gcode_line(GCodeReader& reader, const GCodeReader::GCode
if (fan_speed >= 0) {
const auto fan_baseline = (m_writer.config.fan_percentage.value ? 100.0 : 255.0);
fan_speed = 100 * fan_speed / fan_baseline;
//speed change: stop kickstart reverting if any
m_current_kickstart.time = -1;
if (!m_is_custom_gcode) {
// if slow down => put in the queue. if not =>
if (m_back_buffer_fan_speed < fan_speed) {
if (m_current_kickstart.time > 0) {
assert(m_back_buffer_fan_speed == m_current_kickstart.fan_speed);
}
if (m_back_buffer_fan_speed >= fan_speed) {
if (m_current_kickstart.time > 0) {
// stop kiskstart, and slow down
m_current_kickstart.time = -1;
//this fan speed will be printed, to make and end to the kickstart
}
} else {
if (nb_seconds_delay > 0 && (!only_overhangs || current_role == ExtrusionRole::erOverhangPerimeter)) {
//don't put this command in the queue
time = -1;
Expand Down Expand Up @@ -376,7 +389,7 @@ void FanMover::_process_gcode_line(GCodeReader& reader, const GCodeReader::GCode
time_count -= it->time;
if (time_count< 0) {
//found something that is lower than us
_put_in_middle_G1(it, it->time + time_count, BufferData(std::string(line.raw()), 0, fan_speed, true));
_put_in_middle_G1(it, it->time + time_count, BufferData(std::string(line.raw()), 0, fan_speed, true), nb_seconds_delay);
//found, stop
break;
}
Expand Down Expand Up @@ -502,7 +515,7 @@ void FanMover::_process_gcode_line(GCodeReader& reader, const GCodeReader::GCode
m_current_kickstart.time -= time;
if (m_current_kickstart.time < 0) {
//prev is possible because we just do a emplace_back.
_put_in_middle_G1(prev(m_buffer.end()), time + m_current_kickstart.time, BufferData{ m_current_kickstart.raw, 0, m_current_kickstart.fan_speed, true });
_put_in_middle_G1(prev(m_buffer.end()), time + m_current_kickstart.time, BufferData{ m_current_kickstart.raw, 0, m_current_kickstart.fan_speed, true }, kickstart);
}
}
}/* else {
Expand Down Expand Up @@ -547,17 +560,14 @@ void FanMover::write_buffer_data()
if (frontdata.fan_speed < 0 || frontdata.fan_speed != m_front_buffer_fan_speed || frontdata.is_kickstart) {
if (frontdata.is_kickstart && frontdata.fan_speed < m_front_buffer_fan_speed) {
// you have to slow down! not kickstart! rewrite the fan speed.
m_process_output += _set_fan(
frontdata.fan_speed); // m_writer.set_fan(frontdata.fan_speed,true); //FIXME extruder id (or use the
// gcode writer, but then you have to disable the multi-thread thing

m_process_output += _set_fan(frontdata.fan_speed) + "\n";
m_front_buffer_fan_speed = frontdata.fan_speed;
} else {
m_process_output += frontdata.raw + "\n";
if (frontdata.fan_speed >= 0) {
if (frontdata.fan_speed >= 0 || frontdata.is_kickstart) {
// note that this is the only place where the fan_speed is set and we print from the buffer, as if the
// fan_speed >= 0 => time == 0 and as this flush all time == 0 lines from the back of the queue...
m_front_buffer_fan_speed = frontdata.fan_speed;
m_front_buffer_fan_speed = frontdata.is_kickstart ? 100 : frontdata.fan_speed;
}
}
}
Expand Down
15 changes: 10 additions & 5 deletions src/libslic3r/GCode/FanMover.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ class FanMover
{
private:
const std::regex regex_fan_speed;
const float nb_seconds_delay;
const float nb_seconds_delay; // in s
const bool with_D_option;
const bool relative_e;
const bool only_overhangs;
const float kickstart;
const float kickstart; // in s

GCodeReader m_parser{};
const GCodeWriter& m_writer;
Expand Down Expand Up @@ -76,8 +76,13 @@ class FanMover

private:
BufferData& put_in_buffer(BufferData&& data) {
m_buffer_time_size += data.time;
m_buffer.emplace_back(data);
m_buffer_time_size += data.time;
if (data.fan_speed >= 0 && !m_buffer.empty() && m_buffer.back().fan_speed >= 0) {
// erase last item
m_buffer.back() = data;
} else {
m_buffer.emplace_back(data);
}
return m_buffer.back();
}
std::list<BufferData>::iterator remove_from_buffer(std::list<BufferData>::iterator data) {
Expand All @@ -88,7 +93,7 @@ class FanMover
void _process_gcode_line(GCodeReader& reader, const GCodeReader::GCodeLine& line);
void _process_ACTIVATE_EXTRUDER(const std::string_view command);
void _process_T(const std::string_view command);
void _put_in_middle_G1(std::list<BufferData>::iterator item_to_split, float nb_sec, BufferData&& line_to_write);
void _put_in_middle_G1(std::list<BufferData>::iterator item_to_split, float nb_sec, BufferData&& line_to_write, float max_time);
void _print_in_middle_G1(BufferData& line_to_split, float nb_sec, const std::string& line_to_write);
void _remove_slow_fan(int16_t min_speed, float past_sec);
void write_buffer_data();
Expand Down
20 changes: 18 additions & 2 deletions src/libslic3r/GCodeWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,15 @@ std::string GCodeWriter::_travel_to_z(double z, const std::string &comment)
gcode << " F" << F_NUM(speed * 60.0);
COMMENT(comment);
gcode << "\n";
return gcode.str();
// replace 'Z-0' by ' Z0'
std::string str = gcode.str();
if (auto it = str.find("Z-0 "); it != std::string::npos) {
str.replace(it, 2, "Z");
}
if (auto it = str.find("Z-0\n"); it != std::string::npos) {
str.replace(it, 2, "Z");
}
return str;
}

bool GCodeWriter::will_move_z(double z) const
Expand Down Expand Up @@ -641,7 +649,15 @@ std::string GCodeWriter::extrude_to_xyz(const Vec3d &point, double dE, const std
gcode << " " << m_extrusion_axis << E_NUM(m_tool->E());
COMMENT(comment);
gcode << "\n";
return gcode.str();
// replace 'Z-0' by ' Z0'
std::string str = gcode.str();
if (auto it = str.find("Z-0 "); it != std::string::npos) {
str.replace(it, 2, "Z");
}
if (auto it = str.find("Z-0\n"); it != std::string::npos) {
str.replace(it, 2, "Z");
}
return str;
}

std::string GCodeWriter::retract(bool before_wipe)
Expand Down
Loading

0 comments on commit 6128659

Please sign in to comment.