Skip to content

Commit

Permalink
fixes, min/max for preview legends
Browse files Browse the repository at this point in the history
  • Loading branch information
supermerill committed Feb 12, 2024
2 parents 3c0076e + f9a41ac commit fd5e6f8
Show file tree
Hide file tree
Showing 17 changed files with 716 additions and 365 deletions.
4 changes: 2 additions & 2 deletions src/imgui/imgui.h
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ namespace ImGui
IMGUI_API bool InputText(const char* label, char* buf, size_t buf_size, ImGuiInputTextFlags flags = 0, ImGuiInputTextCallback callback = NULL, void* user_data = NULL);
IMGUI_API bool InputTextMultiline(const char* label, char* buf, size_t buf_size, const ImVec2& size = ImVec2(0, 0), ImGuiInputTextFlags flags = 0, ImGuiInputTextCallback callback = NULL, void* user_data = NULL);
IMGUI_API bool InputTextWithHint(const char* label, const char* hint, char* buf, size_t buf_size, ImGuiInputTextFlags flags = 0, ImGuiInputTextCallback callback = NULL, void* user_data = NULL);
IMGUI_API bool InputFloat(const char* label, float* v, float step = 0.0f, float step_fast = 0.0f, const char* format = "%.3f", ImGuiInputTextFlags flags = 0);
IMGUI_API bool InputFloat(const char* label, float* v, float step = 0.0f, float step_fast = 0.0f, const char* format = "%.3f", ImGuiInputTextFlags flags = 0, float p_data_void = FLT_MAX);
IMGUI_API bool InputFloat2(const char* label, float v[2], const char* format = "%.3f", ImGuiInputTextFlags flags = 0);
IMGUI_API bool InputFloat3(const char* label, float v[3], const char* format = "%.3f", ImGuiInputTextFlags flags = 0);
IMGUI_API bool InputFloat4(const char* label, float v[4], const char* format = "%.3f", ImGuiInputTextFlags flags = 0);
Expand All @@ -561,7 +561,7 @@ namespace ImGui
IMGUI_API bool InputInt3(const char* label, int v[3], ImGuiInputTextFlags flags = 0);
IMGUI_API bool InputInt4(const char* label, int v[4], ImGuiInputTextFlags flags = 0);
IMGUI_API bool InputDouble(const char* label, double* v, double step = 0.0, double step_fast = 0.0, const char* format = "%.6f", ImGuiInputTextFlags flags = 0);
IMGUI_API bool InputScalar(const char* label, ImGuiDataType data_type, void* p_data, const void* p_step = NULL, const void* p_step_fast = NULL, const char* format = NULL, ImGuiInputTextFlags flags = 0);
IMGUI_API bool InputScalar(const char* label, ImGuiDataType data_type, void* p_data, const void* p_step = NULL, const void* p_step_fast = NULL, const char* format = NULL, ImGuiInputTextFlags flags = 0, void* p_data_void = nullptr);
IMGUI_API bool InputScalarN(const char* label, ImGuiDataType data_type, void* p_data, int components, const void* p_step = NULL, const void* p_step_fast = NULL, const char* format = NULL, ImGuiInputTextFlags flags = 0);

// Widgets: Color Editor/Picker (tip: the ColorEdit* functions have a little color square that can be left-clicked to open a picker, and right-clicked to open an option menu.)
Expand Down
2 changes: 1 addition & 1 deletion src/imgui/imgui_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -2580,7 +2580,7 @@ namespace ImGui
IMGUI_API const ImGuiDataTypeInfo* DataTypeGetInfo(ImGuiDataType data_type);
IMGUI_API int DataTypeFormatString(char* buf, int buf_size, ImGuiDataType data_type, const void* p_data, const char* format);
IMGUI_API void DataTypeApplyOp(ImGuiDataType data_type, int op, void* output, const void* arg_1, const void* arg_2);
IMGUI_API bool DataTypeApplyOpFromText(const char* buf, const char* initial_value_buf, ImGuiDataType data_type, void* p_data, const char* format);
IMGUI_API bool DataTypeApplyOpFromText(const char* buf, const char* initial_value_buf, ImGuiDataType data_type, void* p_data, const char* format, void* p_default_data = nullptr);
IMGUI_API int DataTypeCompare(ImGuiDataType data_type, const void* arg_1, const void* arg_2);
IMGUI_API bool DataTypeClamp(ImGuiDataType data_type, void* p_data, const void* p_min, const void* p_max);

Expand Down
27 changes: 18 additions & 9 deletions src/imgui/imgui_widgets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1909,7 +1909,7 @@ void ImGui::DataTypeApplyOp(ImGuiDataType data_type, int op, void* output, const

// User can input math operators (e.g. +100) to edit a numerical values.
// NB: This is _not_ a full expression evaluator. We should probably add one and replace this dumb mess..
bool ImGui::DataTypeApplyOpFromText(const char* buf, const char* initial_value_buf, ImGuiDataType data_type, void* p_data, const char* format)
bool ImGui::DataTypeApplyOpFromText(const char* buf, const char* initial_value_buf, ImGuiDataType data_type, void* p_data, const char* format, void* p_default_data)
{
while (ImCharIsBlankA(*buf))
buf++;
Expand All @@ -1927,8 +1927,6 @@ bool ImGui::DataTypeApplyOpFromText(const char* buf, const char* initial_value_b
{
op = 0;
}
if (!buf[0])
return false;

// Copy the value in an opaque buffer so we can compare at the end of the function if it changed at all.
const ImGuiDataTypeInfo* type_info = DataTypeGetInfo(data_type);
Expand All @@ -1937,6 +1935,15 @@ bool ImGui::DataTypeApplyOpFromText(const char* buf, const char* initial_value_b

if (format == NULL)
format = type_info->ScanFmt;

if (!buf[0])
if (p_default_data && data_type == ImGuiDataType_Float) {
float* v = (float*)p_data;
*v = (*(const float*)p_default_data);
return memcmp(&data_backup, p_data, type_info->Size) != 0;
} else {
return false;
}

// FIXME-LEGACY: The aim is to remove those operators and write a proper expression evaluator at some point..
int arg1i = 0;
Expand Down Expand Up @@ -3339,7 +3346,7 @@ bool ImGui::TempInputScalar(const ImRect& bb, ImGuiID id, const char* label, ImG

// Note: p_data, p_step, p_step_fast are _pointers_ to a memory address holding the data. For an Input widget, p_step and p_step_fast are optional.
// Read code of e.g. InputFloat(), InputInt() etc. or examples in 'Demo->Widgets->Data Types' to understand how to use this function directly.
bool ImGui::InputScalar(const char* label, ImGuiDataType data_type, void* p_data, const void* p_step, const void* p_step_fast, const char* format, ImGuiInputTextFlags flags)
bool ImGui::InputScalar(const char* label, ImGuiDataType data_type, void* p_data, const void* p_step, const void* p_step_fast, const char* format, ImGuiInputTextFlags flags, void* p_data_void)
{
ImGuiWindow* window = GetCurrentWindow();
if (window->SkipItems)
Expand All @@ -3353,6 +3360,8 @@ bool ImGui::InputScalar(const char* label, ImGuiDataType data_type, void* p_data

char buf[64];
DataTypeFormatString(buf, IM_ARRAYSIZE(buf), data_type, p_data, format);
if (p_data_void && data_type == ImGuiDataType_Float && (*(const float*)p_data) == (*(const float*)p_data_void))
buf[0] = 0;

bool value_changed = false;
if ((flags & (ImGuiInputTextFlags_CharsHexadecimal | ImGuiInputTextFlags_CharsScientific)) == 0)
Expand All @@ -3368,7 +3377,7 @@ bool ImGui::InputScalar(const char* label, ImGuiDataType data_type, void* p_data
PushID(label);
SetNextItemWidth(ImMax(1.0f, CalcItemWidth() - (button_size + style.ItemInnerSpacing.x) * 2));
if (InputText("", buf, IM_ARRAYSIZE(buf), flags)) // PushId(label) + "" gives us the expected ID from outside point of view
value_changed = DataTypeApplyOpFromText(buf, g.InputTextState.InitialTextA.Data, data_type, p_data, format);
value_changed = DataTypeApplyOpFromText(buf, g.InputTextState.InitialTextA.Data, data_type, p_data, format, p_data_void);

// Step buttons
const ImVec2 backup_frame_padding = style.FramePadding;
Expand Down Expand Up @@ -3402,8 +3411,8 @@ bool ImGui::InputScalar(const char* label, ImGuiDataType data_type, void* p_data
}
else
{
if (InputText(label, buf, IM_ARRAYSIZE(buf), flags))
value_changed = DataTypeApplyOpFromText(buf, g.InputTextState.InitialTextA.Data, data_type, p_data, format);
if (InputText(label, buf, flags))
value_changed = DataTypeApplyOpFromText(buf, g.InputTextState.InitialTextA.Data, data_type, p_data, format, p_data_void);
}
if (value_changed)
MarkItemEdited(window->DC.LastItemId);
Expand Down Expand Up @@ -3446,10 +3455,10 @@ bool ImGui::InputScalarN(const char* label, ImGuiDataType data_type, void* p_dat
return value_changed;
}

bool ImGui::InputFloat(const char* label, float* v, float step, float step_fast, const char* format, ImGuiInputTextFlags flags)
bool ImGui::InputFloat(const char* label, float* v, float step, float step_fast, const char* format, ImGuiInputTextFlags flags, float p_data_void)
{
flags |= ImGuiInputTextFlags_CharsScientific;
return InputScalar(label, ImGuiDataType_Float, (void*)v, (void*)(step > 0.0f ? &step : NULL), (void*)(step_fast > 0.0f ? &step_fast : NULL), format, flags);
return InputScalar(label, ImGuiDataType_Float, (void*)v, (void*)(step > 0.0f ? &step : NULL), (void*)(step_fast > 0.0f ? &step_fast : NULL), format, flags, p_data_void == FLT_MAX ? (void*)nullptr : (void*)&p_data_void);
}

bool ImGui::InputFloat2(const char* label, float v[2], const char* format, ImGuiInputTextFlags flags)
Expand Down
62 changes: 53 additions & 9 deletions src/libslic3r/GCode/FanMover.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ void FanMover::_remove_slow_fan(int16_t min_speed, float past_sec) {
}

std::string FanMover::_set_fan(int16_t speed) {
const Tool* tool = m_writer.get_tool(m_currrent_extruder < 20 ? m_currrent_extruder : 0);
const Tool* tool = m_writer.get_tool(m_current_extruder < 20 ? m_current_extruder : 0);
std::string str = GCodeWriter::set_fan(m_writer.config.gcode_flavor.value, m_writer.config.gcode_comments.value, speed, tool ? tool->fan_offset() : 0, m_writer.config.fan_percentage.value);
if(!str.empty() && str.back() == '\n')
return str.substr(0,str.size()-1);
Expand Down Expand Up @@ -248,13 +248,51 @@ void FanMover::_process_T(const std::string_view command)

// T-1 is a valid gcode line for RepRap Firmwares (used to deselects all tools) see https://github.com/prusa3d/PrusaSlicer/issues/5677
if ((flavor != gcfRepRap && flavor != gcfSprinter) || eid != -1)
m_currrent_extruder = static_cast<uint16_t>(0);
m_current_extruder = static_cast<uint16_t>(0);
} else {
m_currrent_extruder = static_cast<uint16_t>(eid);
m_current_extruder = static_cast<uint16_t>(eid);
}
}
}


void FanMover::_process_ACTIVATE_EXTRUDER(const std::string_view cmd)
{
if (size_t cmd_end = cmd.find("ACTIVATE_EXTRUDER"); cmd_end != std::string::npos) {
bool error = false;
size_t extruder_pos_start = cmd.find("EXTRUDER", cmd_end + std::string_view("ACTIVATE_EXTRUDER").size()) + std::string_view("EXTRUDER").size();
assert(cmd[extruder_pos_start - 1] == 'R');
if (extruder_pos_start != std::string::npos) {
//remove next char until '-' or [0-9]
while (extruder_pos_start < cmd.size() && (cmd[extruder_pos_start] == ' ' || cmd[extruder_pos_start] == '=' || cmd[extruder_pos_start] == '\t'))
++extruder_pos_start;
size_t extruder_pos_end = extruder_pos_start + 1;
while (extruder_pos_end < cmd.size() && cmd[extruder_pos_end] != ' ' && cmd[extruder_pos_end] != '\t' && cmd[extruder_pos_end] != '\r' && cmd[extruder_pos_end] != '\n')
++extruder_pos_end;
std::string_view extruder_name = cmd.substr(extruder_pos_start, extruder_pos_end-extruder_pos_start);
// we have a "name". It may be whatever or "extruder" + X
for (const Extruder &extruder : m_writer.extruders()) {
if (m_writer.config.tool_name.values[extruder.id()] == extruder_name) {
m_current_extruder = static_cast<uint16_t>(extruder.id());
return;
}
}
std::string extruder_str("extruder");
if (extruder_str == extruder_name) {
m_current_extruder = static_cast<uint16_t>(0);
return;
}
for (const Extruder &extruder : m_writer.extruders()) {
if (extruder_str + std::to_string(extruder.id()) == extruder_name) {
m_current_extruder = static_cast<uint16_t>(extruder.id());
return;
}
}
}
BOOST_LOG_TRIVIAL(error) << "invalid ACTIVATE_EXTRUDER gcode command: '" << cmd << "', ignored by the fam mover post-process.";
}
}

void FanMover::_process_gcode_line(GCodeReader& reader, const GCodeReader::GCodeLine& line)
{
// processes 'normal' gcode lines
Expand All @@ -266,6 +304,9 @@ void FanMover::_process_gcode_line(GCodeReader& reader, const GCodeReader::GCode
if (line.has_f())
m_current_speed = line.f() / 60.0f;
switch (::toupper(cmd[0])) {
case 'A':
_process_ACTIVATE_EXTRUDER(line.raw());
break;
case 'T':
case 't':
_process_T(cmd);
Expand Down Expand Up @@ -442,17 +483,20 @@ void FanMover::_process_gcode_line(GCodeReader& reader, const GCodeReader::GCode
if (line.has(Axis::E)) {
new_data.e = reader.e();
if (relative_e) {
assert(new_data.e == 0);
new_data.de = line.e();
// GCode reader doesn't know it's relative extrusion, we have to do it ourself.
//assert(new_data.e == 0);
new_data.e = 0;
} else
new_data.de = line.dist_E(reader);
}
assert(new_data.dx == 0 || reader.x() == new_data.x);
assert(new_data.dx == 0 || reader.x() + new_data.dx == line.x());
assert(new_data.dy == 0 ||reader.y() == new_data.y);
assert(new_data.dy == 0 || reader.y() + new_data.dy == line.y());
assert(new_data.de == 0 || reader.e() == new_data.e);
assert(new_data.de == 0 || reader.e() + new_data.de == line.e());
assert(new_data.dx == 0 || std::abs(reader.x() + new_data.dx - line.x()) < 0.00001f);
assert(new_data.dy == 0 || reader.y() == new_data.y);
assert(new_data.dy == 0 || std::abs(reader.y() + new_data.dy - line.y()) < 0.00001f);
assert(new_data.de == 0 || (relative_e?0:reader.e()) == new_data.e);
assert(new_data.de == 0 || std::abs((relative_e?0.f:reader.e()) + new_data.de - line.e()) < 0.00001f);
//assert(new_data.de == 0 ||(relative_e?0.f:reader.e()) + new_data.de == line.e());

if (m_current_kickstart.time > 0 && time > 0) {
m_current_kickstart.time -= time;
Expand Down
3 changes: 2 additions & 1 deletion src/libslic3r/GCode/FanMover.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class FanMover
// in unit/second
double m_current_speed = 1000 / 60.0;
bool m_is_custom_gcode = false;
uint16_t m_currrent_extruder = 0;
uint16_t m_current_extruder = 0;

// variable for when you add a line (front of the buffer)
int m_front_buffer_fan_speed = 1;
Expand Down Expand Up @@ -86,6 +86,7 @@ class FanMover
}
// Processes the given gcode line
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 _print_in_middle_G1(BufferData& line_to_split, float nb_sec, const std::string& line_to_write);
Expand Down
72 changes: 64 additions & 8 deletions src/libslic3r/GCode/PressureEqualizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ PressureEqualizer::PressureEqualizer(const Slic3r::GCodeConfig &config) : m_use_
m_filament_crossections.push_back(float(a));
}

m_extruder_names.clear();
for (const std::string & str: config.tool_name.values) {
m_extruder_names.push_back(str);
}

// Volumetric rate of a 0.45mm x 0.2mm extrusion at 60mm/s XY movement: 0.45*0.2*60*60=5.4*60 = 324 mm^3/min
// Volumetric rate of a 0.45mm x 0.2mm extrusion at 20mm/s XY movement: 0.45*0.2*20*60=1.8*60 = 108 mm^3/min
// Slope of the volumetric rate, changing from 20mm/s to 60mm/s over 2 seconds: (5.4-1.8)*60*60/2=60*60*1.8 = 6480 mm^3/min^2 = 1.8 mm^3/s^2
Expand Down Expand Up @@ -235,7 +240,7 @@ bool PressureEqualizer::process_line(const char *line, const char *line_end, GCo
gcode = parse_int(line);
} catch (Slic3r::InvalidArgument &) {
// Ignore invalid GCodes.
eatws(line);
eatws(line);
break;
}

Expand Down Expand Up @@ -368,16 +373,26 @@ bool PressureEqualizer::process_line(const char *line, const char *line_end, GCo
// Ignore the rest of the M-codes.
break;
}
case 'A': {
parse_activate_extruder(str_line);
break;
}
case 'T':
{
// Activate an extruder head.
int new_extruder = parse_int(line);
if (new_extruder != int(m_current_extruder)) {
m_current_extruder = new_extruder;
m_retracted = true;
buf.type = GCODELINETYPE_TOOL_CHANGE;
} else {
buf.type = GCODELINETYPE_NOOP;
try {
int new_extruder = parse_int(line);
if (new_extruder != int(m_current_extruder)) {
m_current_extruder = new_extruder;
//m_retracted = true; // why?
buf.type = GCODELINETYPE_TOOL_CHANGE;
} else {
buf.type = GCODELINETYPE_NOOP;
}
} catch (Slic3r::InvalidArgument &) {
// Ignore invalid GCodes.
eatws(line);
break;
}
break;
}
Expand All @@ -393,6 +408,47 @@ bool PressureEqualizer::process_line(const char *line, const char *line_end, GCo
return true;
}

void PressureEqualizer::parse_activate_extruder(const std::string &line_str)
{
if (size_t cmd_end = line_str.find("CTIVATE_EXTRUDER"); cmd_end != std::string::npos) {
bool error = false;
size_t extruder_pos_start = line_str.find("EXTRUDER", cmd_end + std::string_view("CTIVATE_EXTRUDER").size()) +
std::string_view("EXTRUDER").size();
assert(line_str[extruder_pos_start - 1] == 'R');
if (extruder_pos_start != std::string::npos) {
// remove next char until '-' or [0-9]
while (extruder_pos_start < line_str.size() &&
(line_str[extruder_pos_start] == ' ' || line_str[extruder_pos_start] == '=' ||
line_str[extruder_pos_start] == '\t'))
++extruder_pos_start;
size_t extruder_pos_end = extruder_pos_start + 1;
while (extruder_pos_end < line_str.size() && line_str[extruder_pos_end] != ' ' &&
line_str[extruder_pos_end] != '\t' && line_str[extruder_pos_end] != '\r' &&
line_str[extruder_pos_end] != '\n')
++extruder_pos_end;
std::string extruder_name = line_str.substr(extruder_pos_start, extruder_pos_end - extruder_pos_start);
// we have a "name". It may be whatever or "extruder" + X
for (size_t extruder_idx = 0; extruder_idx < m_extruder_names.size(); ++extruder_idx) {
if (m_extruder_names[extruder_idx] == extruder_name) {
m_current_extruder = extruder_idx;
return;
}
}
std::string extruder_str("extruder");
if (extruder_str == extruder_name) {
m_current_extruder = 0;
return;
}
for (size_t extruder_idx = 0; extruder_idx < m_extruder_names.size(); ++extruder_idx) {
if (extruder_str + std::to_string(extruder_idx) == extruder_name) {
m_current_extruder = extruder_idx;
return;
}
}
}
}
}

void PressureEqualizer::output_gcode_line(const size_t line_idx)
{
GCodeLine &line = m_gcode_lines[line_idx];
Expand Down
Loading

0 comments on commit fd5e6f8

Please sign in to comment.