Rearrange and clean up

This commit is contained in:
thewildmage
2023-05-30 21:49:33 -06:00
parent 326e771c63
commit 35da0b67c7
2 changed files with 498 additions and 389 deletions

View File

@@ -6,7 +6,10 @@
#include <map> #include <map>
namespace Slic3r { namespace Slic3r {
std::string calib_pressure_advance::move_to(Vec2d pt, std::string comment = std::string()) { Calib_Params::Calib_Params() : mode(CalibMode::Calib_None) { }
std::string CalibPressureAdvance::move_to(Vec2d pt, std::string comment = std::string())
{
std::stringstream gcode; std::stringstream gcode;
gcode << mp_gcodegen->retract(); gcode << mp_gcodegen->retract();
@@ -20,7 +23,8 @@ namespace Slic3r {
return gcode.str(); return gcode.str();
} }
std::string convert_number_to_string(double num) { std::string CalibPressureAdvance::convert_number_to_string(double num)
{
auto sNumber = std::to_string(num); auto sNumber = std::to_string(num);
sNumber.erase(sNumber.find_last_not_of('0') + 1, std::string::npos); sNumber.erase(sNumber.find_last_not_of('0') + 1, std::string::npos);
sNumber.erase(sNumber.find_last_not_of('.') + 1, std::string::npos); sNumber.erase(sNumber.find_last_not_of('.') + 1, std::string::npos);
@@ -28,7 +32,8 @@ namespace Slic3r {
return sNumber; return sNumber;
} }
std::string calib_pressure_advance::draw_digit(double startx, double starty, char c, calib_pressure_advance::DrawDigitMode mode) { std::string CalibPressureAdvance::draw_digit(double startx, double starty, char c, CalibPressureAdvance::DrawDigitMode mode)
{
auto& writer = mp_gcodegen->writer(); auto& writer = mp_gcodegen->writer();
std::stringstream gcode; std::stringstream gcode;
const double lw = 0.48; const double lw = 0.48;
@@ -51,7 +56,7 @@ namespace Slic3r {
Vec2d gap_p0_toward_p3, gap_p2_toward_p3; Vec2d gap_p0_toward_p3, gap_p2_toward_p3;
Vec2d dot_direction; Vec2d dot_direction;
if (mode == calib_pressure_advance::DrawDigitMode::Vertical) { if (mode == CalibPressureAdvance::DrawDigitMode::Vertical) {
// 1-------2-------5 // 1-------2-------5
// | | | // | | |
// | | | // | | |
@@ -173,7 +178,8 @@ namespace Slic3r {
return gcode.str(); return gcode.str();
} }
std::string calib_pressure_advance::draw_number(double startx, double starty, double value, calib_pressure_advance::DrawDigitMode mode) { std::string CalibPressureAdvance::draw_number(double startx, double starty, double value, calib_pressure_advance::DrawDigitMode mode)
{
auto sNumber = convert_number_to_string(value); auto sNumber = convert_number_to_string(value);
std::stringstream gcode; std::stringstream gcode;
gcode << mp_gcodegen->writer().set_speed(3600); gcode << mp_gcodegen->writer().set_speed(3600);
@@ -187,23 +193,24 @@ namespace Slic3r {
return gcode.str(); return gcode.str();
} }
bool calib_pressure_advance::is_delta() { bool CalibPressureAdvance::is_delta()
{
return mp_gcodegen->config().printable_area.values.size() > 4; return mp_gcodegen->config().printable_area.values.size() > 4;
} }
void calib_pressure_advance::delta_scale_bed_ext(BoundingBoxf& bed_ext) { void CalibPressureAdvance::delta_scale_bed_ext(BoundingBoxf& bed_ext)
{
bed_ext.scale(1.0f / 1.41421f); bed_ext.scale(1.0f / 1.41421f);
} }
void calib_pressure_advance::delta_modify_start(double& start_x, double& start_y, int count) { void CalibPressureAdvance::delta_modify_start(double& start_x, double& start_y, int count)
{
startx = -startx; startx = -startx;
starty = -(count * m_space_y) / 2; starty = -(count * m_space_y) / 2;
} }
calib_pressure_advance_line::calib_pressure_advance_line(GCode* gcodegen) :mp_gcodegen(gcodegen), m_length_short(20.0), m_length_long(40.0), m_space_y(3.5), m_line_width(0.6), m_draw_numbers(true) {} std::string CalibPressureAdvanceLine::generate_test(double start_pa /*= 0*/, double step_pa /*= 0.002*/, int count /*= 10*/)
{
std::string calib_pressure_advance_line::generate_test(double start_pa /*= 0*/, double step_pa /*= 0.002*/,
int count /*= 10*/) {
BoundingBoxf bed_ext = get_extents(mp_gcodegen->config().printable_area.values); BoundingBoxf bed_ext = get_extents(mp_gcodegen->config().printable_area.values);
const bool is_delta = is_delta(); const bool is_delta = is_delta();
if (is_delta) { if (is_delta) {
@@ -226,14 +233,14 @@ namespace Slic3r {
return print_pa_lines(startx, starty, start_pa, step_pa, count); return print_pa_lines(startx, starty, start_pa, step_pa, count);
} }
std::string calib_pressure_advance_line::print_pa_lines(double start_x, double start_y, double start_pa, double step_pa, int num) { std::string CalibPressureAdvanceLine::print_pa_lines(double start_x, double start_y, double start_pa, double step_pa, int num)
{
auto& writer = mp_gcodegen->writer(); auto& writer = mp_gcodegen->writer();
Flow line_flow = Flow(m_line_width, 0.2, m_nozzle_diameter); Flow line_flow = Flow(m_line_width, 0.2, m_nozzle_diameter);
Flow thin_line_flow = Flow(0.44, 0.2, m_nozzle_diameter); Flow thin_line_flow = Flow(0.44, 0.2, m_nozzle_diameter);
const double e_calib = line_flow.mm3_per_mm() / 2.40528; // filament_mm/extrusion_mm const double e_calib = line_flow.mm3_per_mm() / 2.40528; // filament_mm/extrusion_mm
const double e = thin_line_flow.mm3_per_mm() / 2.40528; // filament_mm/extrusion_mm const double e = thin_line_flow.mm3_per_mm() / 2.40528; // filament_mm/extrusion_mm
const double fast = m_fast_speed * 60.0; const double fast = m_fast_speed * 60.0;
const double slow = m_slow_speed * 60.0; const double slow = m_slow_speed * 60.0;
std::stringstream gcode; std::stringstream gcode;
@@ -247,7 +254,6 @@ namespace Slic3r {
gcode << writer.extrude_to_xy(Vec2d(prime_x, y_pos + 3 * m_space_y), e_calib * m_space_y * num * 1.1); gcode << writer.extrude_to_xy(Vec2d(prime_x, y_pos + 3 * m_space_y), e_calib * m_space_y * num * 1.1);
for (int i = 0; i < num; ++i) { for (int i = 0; i < num; ++i) {
gcode << writer.set_pressure_advance(start_pa + i * step_pa); gcode << writer.set_pressure_advance(start_pa + i * step_pa);
gcode << move_to(Vec2d(start_x, y_pos + i * m_space_y)); gcode << move_to(Vec2d(start_x, y_pos + i * m_space_y));
gcode << writer.set_speed(slow); gcode << writer.set_speed(slow);
@@ -256,7 +262,6 @@ namespace Slic3r {
gcode << writer.extrude_to_xy(Vec2d(start_x + m_length_short + m_length_long, y_pos + i * m_space_y), e_calib * m_length_long); gcode << writer.extrude_to_xy(Vec2d(start_x + m_length_short + m_length_long, y_pos + i * m_space_y), e_calib * m_length_long);
gcode << writer.set_speed(slow); gcode << writer.set_speed(slow);
gcode << writer.extrude_to_xy(Vec2d(start_x + m_length_short + m_length_long + m_length_short, y_pos + i * m_space_y), e_calib * m_length_short); gcode << writer.extrude_to_xy(Vec2d(start_x + m_length_short + m_length_long + m_length_short, y_pos + i * m_space_y), e_calib * m_length_short);
} }
gcode << writer.set_pressure_advance(0.0); gcode << writer.set_pressure_advance(0.0);
@@ -275,9 +280,8 @@ namespace Slic3r {
return gcode.str(); return gcode.str();
} }
calib_pressure_advance_pattern::calib_pressure_advance_pattern(GCode* gcodegen) : mp_gcodegen(gcodegen) { } std::string CalibPressureAdvancePattern::generate_test(double start_pa, double end_pa, double step_pa)
{
std::string calib_pressure_advance_pattern::generate_test(double start_pa, double end_pa, double step_pa) {
BoundingBoxf bed_ext = get_extents(mp_gcodegen->config().printable_area.values); BoundingBoxf bed_ext = get_extents(mp_gcodegen->config().printable_area.values);
if (is_delta()) { if (is_delta()) {
@@ -291,6 +295,7 @@ namespace Slic3r {
int num_patterns = std::ceil((end_pa - start_pa) / step_pa + 1); int num_patterns = std::ceil((end_pa - start_pa) / step_pa + 1);
num_patterns = std::min(num_patterns, int((h - 10) / m_space_y)); num_patterns = std::min(num_patterns, int((h - 10) / m_space_y));
// TODO: fix m_space_y above
auto object_size_x = object_size_x(num_patterns); auto object_size_x = object_size_x(num_patterns);
auto object_size_y = object_size_y(start_pa, step_pa, num_patterns); auto object_size_y = object_size_y(start_pa, step_pa, num_patterns);
@@ -323,31 +328,13 @@ namespace Slic3r {
return print_pa_pattern(start_x, start_y, start_pa, step_pa, num_patterns); return print_pa_pattern(start_x, start_y, start_pa, step_pa, num_patterns);
} }
std::string calib_pressure_advance_pattern::print_pa_pattern(double start_x, double start_y, double start_pa, double step_pa, int num) { double CalibPressureAdvancePattern::get_distance(double cur_x, double cur_y, double to_x, double to_y)
auto& writer = mp_gcodegen->writer(); {
// TODO set test parameters return std::hypot((to_x - cur_x), (to_y - cur_y));
std::stringstream gcode;
gcode << move_to(Vec2d(start_x, start_y, mp_gcodegen->config().initial_layer_print_height.value));
} }
double object_size_x(int num_patterns) { double CalibPressureAdvancePattern::max_numbering_height(double start_pa, double step_pa, int num_patterns)
return num_patterns * ((m_wall_count - 1) * line_spacing_angle()) + {
(num_patterns - 1) * (m_pattern_spacing + line_width()) +
std::cos(to_radians(m_corner_angle) / 2) * m_wall_side_length +
line_spacing_anchor() * m_anchor_perimeters
;
}
double object_size_y(double start_pa, double step_pa, int num_patterns) {
return 2 * std::sin(to_radians(m_corner_angle) / 2) * m_wall_side_length +
max_numbering_height(start_pa, step_pa, num_patterns) +
m_glyph_padding_vertical * 2 +
line_width_anchor()
}
double max_numbering_height(double start_pa, double step_pa, int num_patterns) {
int most_characters = 0; int most_characters = 0;
// note: only every other number is printed // note: only every other number is printed
@@ -362,11 +349,25 @@ namespace Slic3r {
return (most_characters * m_digit_segment_len) + ((most_characters - 1) * m_number_spacing); return (most_characters * m_digit_segment_len) + ((most_characters - 1) * m_number_spacing);
} }
double get_distance(double cur_x, double cur_y, double to_x, double to_y) { double CalibPressureAdvancePattern::object_size_x(int num_patterns)
return std::hypot((to_x - cur_x), (to_y - cur_y)); {
return num_patterns * ((m_wall_count - 1) * line_spacing_angle()) +
(num_patterns - 1) * (m_pattern_spacing + line_width()) +
std::cos(to_radians(m_corner_angle) / 2) * m_wall_side_length +
line_spacing_anchor() * m_anchor_perimeters
;
} }
std::string draw_line(double to_x, double to_y, std::string comment = std::string()) { double CalibPressureAdvancePattern::object_size_y(double start_pa, double step_pa, int num_patterns)
{
return 2 * std::sin(to_radians(m_corner_angle) / 2) * m_wall_side_length +
max_numbering_height(start_pa, step_pa, num_patterns) +
m_glyph_padding_vertical * 2 +
line_width_anchor()
}
std::string CalibPressureAdvancePattern::draw_line(double to_x, double to_y, std::string comment = std::string())
{
std::stringstream gcode; std::stringstream gcode;
auto& config = mp_gcodegen.config(); auto& config = mp_gcodegen.config();
auto& writer = mp_gcodegen.writer(); auto& writer = mp_gcodegen.writer();
@@ -388,5 +389,65 @@ namespace Slic3r {
return gcode.str(); return gcode.str();
} }
Calib_Params::Calib_Params() : mode(CalibMode::Calib_None) {} std::string CalibPressureAdvancePattern::draw_box(double min_x, double min_y, double size_x, double size_y)
{
auto& config = mp_gcodegen->config();
auto& writer = mp_gcodegen->writer();
std::stringstream gcode;
double x = min_x;
double y = min_y;
double max_x = min_x + size_x;
double max_y = min_y + size_y;
double layer_height;
if (mp_gcodegen.m_layer_index <= 0) {
layer_height = config.initial_layer_print_height.value;
} else {
layer_height = config.layer_height.value;
}
double spacing = line_width_anchor() - layer_height * (1 - M_PI / 4);
// if number of perims exceeds size of box, reduce it to max
int max_perimeters =
std::min(
// this is the equivalent of number of perims for concentric fill
std::floor(size_x * std::sin(to_radians(45))) / (spacing / std::sin(to_radians(45))),
std::floor(size_y * std::sin(to_radians(45))) / (spacing / std::sin(to_radians(45)))
)
;
int num_perimeters = std::min(config.num_perimeters, max_perimeters);
gcode << move_to(Vec2d(min_x, min_y));
for (int i = 0; i < num_perimeters; ++i) {
if (i != 0) { // after first perimeter, step inwards to start next perimeter
x += spacing;
y += spacing;
gcode << move_to(Vec2d(x, y), "Step inwards to print next perimeter");
}
// draw line up
y += size_y - (i * spacing) * 2;
gcode << writer.extrude_to_xy(Vec2d(x, y), );
}
return gcode.str();
}
Point last_pos = mp_gcodegen.last_pos();
const double length = get_distance(last_pos.x(), last_pos.y(), to_x, to_y);
auto dE = e_per_mm * length;
if (comment.empty()) {
gcode << writer.extrude_to_xy(Vec2d(to_x, to_y), dE);
} else {
gcode << writer.extrude_to_xy(Vec2d(to_x, to_y), dE, comment);
}
return gcode.str();
}
} // namespace Slic3r } // namespace Slic3r

View File

@@ -26,50 +26,67 @@ struct Calib_Params
CalibMode mode; CalibMode mode;
}; };
class calib_pressure_advance class CalibPressureAdvance {
protected:
CalibPressureAdvance(GCode* gcodegen) :
mp_gcodegen(gcodegen),
m_digit_segment_len(2),
m_max_number_length(5),
m_number_spacing(3.0)
{ {
public: m_nozzle_diameter = gcodegen->config().nozzle_diameter.get_at(0);
}
;
~CalibPressureAdvance() { };
enum class DrawDigitMode { enum class DrawDigitMode {
Horizontal = 0, Horizontal = 0,
Vertical Vertical
}; };
void set_nozzle_diameter() {
m_nozzle_diameter = mp_gcodegen->config().nozzle_diameter.get_at(0);
}
private:
std::string move_to(Vec2d pt, std::string comment = std::string()); std::string move_to(Vec2d pt, std::string comment = std::string());
std::string convert_number_to_string(double num); std::string convert_number_to_string(double num);
std::string draw_digit(double startx, double starty, char c, calib_pressure_advance::DrawDigitMode mode); std::string draw_digit(double startx, double starty, char c, CalibPressureAdvance::DrawDigitMode mode);
std::string draw_number(double startx, double starty, double value, calib_pressure_advance::DrawDigitMode mode); std::string draw_number(double startx, double starty, double value, CalibPressureAdvance::DrawDigitMode mode);
bool is_delta(); bool is_delta();
void delta_scale_bed_ext(BoundingBoxf& bed_ext); void delta_scale_bed_ext(BoundingBoxf& bed_ext);
void delta_modify_start(double start_x, double start_y, int count); void delta_modify_start(double start_x, double start_y, int count);
private:
GCode* mp_gcodegen;
double m_digit_segment_len {2};
double m_nozzle_diameter;
int m_max_number_length {5};
double m_number_spacing {3.0};
}
class calib_pressure_advance_line: public calib_pressure_advance GCode* mp_gcodegen;
{ double m_nozzle_diameter;
double m_digit_segment_len;
int m_max_number_length;
double m_number_spacing;
};
class CalibPressureAdvanceLine : public CalibPressureAdvance {
public: public:
calib_pressure_advance_line(GCode* gcodegen); CalibPressureAdvanceLine(GCode* gcodegen) :
~calib_pressure_advance_line() {} CalibPressureAdvance(gcodegen),
m_length_short(20.0),
m_length_long(40.0),
m_space_y(3.5),
m_line_width(0.6),
m_draw_numbers(true)
{ }
;
~CalibPressureAdvanceLine() { };
std::string generate_test(double start_pa = 0, double step_pa = 0.002, int count = 50); std::string generate_test(double start_pa = 0, double step_pa = 0.002, int count = 50);
void set_speed(double fast = 100.0, double slow = 20.0) { void set_speed(double fast = 100.0, double slow = 20.0) {
m_slow_speed = slow; m_slow_speed = slow;
m_fast_speed = fast; m_fast_speed = fast;
} }
double& line_width() { return m_line_width; }; double& line_width() { return m_line_width; };
bool& draw_numbers() { return m_draw_numbers; } bool& draw_numbers() { return m_draw_numbers; }
private: private:
std::string print_pa_lines(double start_x, double start_y, double start_pa, double step_pa, int num); std::string print_pa_lines(double start_x, double start_y, double start_pa, double step_pa, int num);
private:
double m_length_short, m_length_long; double m_length_short, m_length_long;
double m_space_y; double m_space_y;
double m_slow_speed, m_fast_speed; double m_slow_speed, m_fast_speed;
@@ -77,15 +94,40 @@ private:
bool m_draw_numbers; bool m_draw_numbers;
}; };
class calib_pressure_advance_pattern: public calib_pressure_advance class CalibPressureAdvancePattern : public CalibPressureAdvance {
{
public: public:
calib_pressure_advance_pattern(GCode* gcodegen); CalibPressureAdvancePattern(GCode* gcodegen) :
~calib_pressure_advance_pattern() {} CalibPressureAdvance(gcodegen),
std::string generate_test(double start_pa = 0, double end_pa = 0.08, double step_pa = 0.005); m_line_ratio(112.5),
m_extrusion_multiplier(0.98),
m_height_layer(0.2),
m_height_first_layer(0.25),
m_anchor_perimeters(4),
m_anchor_layer_line_ratio(140),
m_wall_count(3),
m_wall_side_length(30.0),
m_corner_angle(90),
m_pattern_spacing(2),
m_glyph_padding_horizontal(1),
m_glyph_padding_vertical(1)
{ }
;
~CalibPressureAdvancePattern() { };
std::string generate_test(
double start_pa = 0,
double end_pa = 0.08,
double step_pa = 0.005
);
private:
double to_radians(double degrees) { return degrees * M_PI / 180; } double to_radians(double degrees) { return degrees * M_PI / 180; }
double get_distance(double cur_x, double cur_y, double to_x, double to_y);
double line_width() { return m_nozzle_diameter * m_line_ratio / 100; }; double line_width() { return m_nozzle_diameter * m_line_ratio / 100; };
double line_width_anchor() { return m_nozzle_diameter * m_anchor_layer_line_ratio / 100; }; double line_width_anchor() { return m_nozzle_diameter * m_anchor_layer_line_ratio / 100; };
@@ -95,25 +137,31 @@ class calib_pressure_advance_pattern: public calib_pressure_advance
double line_spacing_anchor() { return line_width_anchor() - m_height_first_layer * (1 - M_PI / 4); }; double line_spacing_anchor() { return line_width_anchor() - m_height_first_layer * (1 - M_PI / 4); };
double line_spacing_angle() { return line_spacing() / std::sin(to_radians(m_corner_angle) / 2); }; double line_spacing_angle() { return line_spacing() / std::sin(to_radians(m_corner_angle) / 2); };
double max_numbering_height(double start_pa, double step_pa, int num_patterns);
double object_size_x(int num_patterns); double object_size_x(int num_patterns);
double object_size_y(double start_pa, double step_pa, int num_patterns); double object_size_y(double start_pa, double step_pa, int num_patterns);
double max_numbering_height(double start_pa, double step_pa, int num_patterns);
double get_distance(double cur_x, double cur_y, double to_x, double to_y);
std::string draw_line(double to_x, double to_y, std::string comment = std::string()); std::string draw_line(double to_x, double to_y, std::string comment = std::string());
private: std::string draw_box(double min_x, double min_y, double size_x, double size_y);
int m_anchor_layer_line_ratio {140};
int m_anchor_perimeters {4}; std::string print_pa_pattern(double start_x, double start_y, double start_pa, double step_pa, int num_patterns);
int m_corner_angle {90};
double m_extrusion_multiplier {0.98}; double m_line_ratio;
double m_height_first_layer {0.25}; double m_extrusion_multiplier;
double m_height_layer {0.2}; double m_height_layer;
double m_glyph_padding_horizontal {1}; double m_height_first_layer;
double m_glyph_padding_vertical {1};
double m_line_ratio {112.5}; int m_anchor_perimeters;
int m_pattern_spacing {2}; int m_anchor_layer_line_ratio;
int m_wall_count {3};
double m_wall_side_length {30.0}; int m_wall_count;
double m_wall_side_length;
int m_corner_angle;
int m_pattern_spacing;
double m_glyph_padding_horizontal;
double m_glyph_padding_vertical;
}; };
} // namespace Slic3r } // namespace Slic3r