GCodeProcessor -> Extract toolpaths height from gcode moves

This commit is contained in:
enricoturri1966 2020-08-13 12:51:50 +02:00
parent 7be5860908
commit b80bde11f3
5 changed files with 64 additions and 46 deletions

View file

@ -23,7 +23,7 @@ static const float DEFAULT_ACCELERATION = 1500.0f; // Prusa Firmware 1_75mm_MK2
namespace Slic3r {
const std::string GCodeProcessor::Extrusion_Role_Tag = "ExtrType:";
const std::string GCodeProcessor::Width_Tag = "PrusaSlicer__WIDTH:";
const std::string GCodeProcessor::Width_Tag = "Width:";
const std::string GCodeProcessor::Height_Tag = "Height:";
const std::string GCodeProcessor::Color_Change_Tag = "Color change";
const std::string GCodeProcessor::Pause_Print_Tag = "Pause print";
@ -81,6 +81,19 @@ static float acceleration_time_from_distance(float initial_feedrate, float dista
return (acceleration != 0.0f) ? (speed_from_distance(initial_feedrate, distance, acceleration) - initial_feedrate) / acceleration : 0.0f;
}
float round_to_nearest(float value, unsigned int decimals)
{
float res = 0.0f;
if (decimals == 0)
res = std::round(value);
else {
char buf[64];
sprintf(buf, "%.*g", decimals, value);
res = std::stof(buf);
}
return res;
}
void GCodeProcessor::CachedPosition::reset()
{
std::fill(position.begin(), position.end(), FLT_MAX);
@ -666,6 +679,7 @@ void GCodeProcessor::reset()
m_extruder_id = 0;
m_extruder_colors = ExtruderColors();
m_filament_diameters = std::vector<float>();
m_extruded_last_z = 0.0f;
m_cp_color.reset();
m_producer = EProducer::Unknown;
@ -1285,14 +1299,6 @@ void GCodeProcessor::process_G1(const GCodeReader::GCodeLine& line)
else if (delta_pos[X] != 0.0f || delta_pos[Y] != 0.0f || delta_pos[Z] != 0.0f)
type = EMoveType::Travel;
if (type == EMoveType::Extrude && (m_width == 0.0f || m_height == 0.0f)) {
if (m_extrusion_role != erCustom) {
m_width = 0.5f;
m_height = 0.5f;
}
type = EMoveType::Travel;
}
return type;
};
@ -1325,8 +1331,26 @@ void GCodeProcessor::process_G1(const GCodeReader::GCodeLine& line)
if (type == EMoveType::Extrude) {
if (delta_pos[E] > 0.0f) {
float ds = std::sqrt(sqr(delta_pos[X]) + sqr(delta_pos[Y]) + sqr(delta_pos[Z]));
if (ds > 0.0f && static_cast<size_t>(m_extruder_id) < m_filament_diameters.size())
m_mm3_per_mm = round_nearest(delta_pos[E] * static_cast<float>(M_PI) * sqr(static_cast<float>(m_filament_diameters[m_extruder_id])) / (4.0f * ds), 3);
if (ds > 0.0f && static_cast<size_t>(m_extruder_id) < m_filament_diameters.size()) {
// extruded filament volume / tool displacement
m_mm3_per_mm = round_to_nearest(static_cast<float>(M_PI * sqr(m_filament_diameters[m_extruder_id]) * 0.25) * delta_pos[E] / ds, 4);
}
if (m_end_position[Z] > m_extruded_last_z + EPSILON) {
m_height = round_to_nearest(m_end_position[Z] - m_extruded_last_z, 4);
m_extruded_last_z = m_end_position[Z];
}
}
}
if (type == EMoveType::Extrude && (m_width == 0.0f || m_height == 0.0f)) {
if ((m_width == 0.0f && m_height == 0.0f) || m_extrusion_role == erCustom)
type = EMoveType::Travel;
else {
if (m_width == 0.0f)
m_width = 0.5f;
if (m_height == 0.0f)
m_height = 0.5f;
}
}

View file

@ -304,6 +304,7 @@ namespace Slic3r {
unsigned char m_extruder_id;
ExtruderColors m_extruder_colors;
std::vector<float> m_filament_diameters;
float m_extruded_last_z;
CpColor m_cp_color;
enum class EProducer

View file

@ -110,19 +110,20 @@ std::string header_slic3r_generated();
// getpid platform wrapper
extern unsigned get_current_pid();
#if !ENABLE_GCODE_VIEWER
template <typename Real>
Real round_nearest(Real value, unsigned int decimals)
{
Real res = (Real)0;
if (decimals == 0)
res = ::round(value);
else
{
else {
Real power = ::pow((Real)10, (int)decimals);
res = ::round(value * power + (Real)0.5) / power;
}
return res;
}
#endif // !ENABLE_GCODE_VIEWER
// Compute the next highest power of 2 of 32-bit v
// http://graphics.stanford.edu/~seander/bithacks.html

View file

@ -235,38 +235,19 @@ const std::vector<GCodeViewer::Color> GCodeViewer::Extrusion_Role_Colors {{
{ 0.00f, 0.00f, 0.00f } // erMixed
}};
//const std::vector<GCodeViewer::Color> GCodeViewer::Extrusion_Role_Colors {{
// { 0.75f, 0.75f, 0.75f }, // erNone
// { 1.00f, 1.00f, 0.40f }, // erPerimeter
// { 1.00f, 0.65f, 0.00f }, // erExternalPerimeter
// { 0.00f, 0.00f, 1.00f }, // erOverhangPerimeter
// { 0.69f, 0.19f, 0.16f }, // erInternalInfill
// { 0.84f, 0.20f, 0.84f }, // erSolidInfill
// { 1.00f, 0.10f, 0.10f }, // erTopSolidInfill
// { 1.00f, 0.55f, 0.41f }, // erIroning
// { 0.60f, 0.60f, 1.00f }, // erBridgeInfill
// { 1.00f, 1.00f, 1.00f }, // erGapFill
// { 0.52f, 0.48f, 0.13f }, // erSkirt
// { 0.00f, 1.00f, 0.00f }, // erSupportMaterial
// { 0.00f, 0.50f, 0.00f }, // erSupportMaterialInterface
// { 0.70f, 0.89f, 0.67f }, // erWipeTower
// { 0.16f, 0.80f, 0.58f }, // erCustom
// { 0.00f, 0.00f, 0.00f } // erMixed
//}};
const std::vector<GCodeViewer::Color> GCodeViewer::Options_Colors {{
{ 1.00f, 0.00f, 1.00f }, // Retractions
{ 0.00f, 1.00f, 1.00f }, // Unretractions
{ 1.00f, 1.00f, 1.00f }, // ToolChanges
{ 1.00f, 0.00f, 0.00f }, // ColorChanges
{ 0.00f, 1.00f, 0.00f }, // PausePrints
{ 0.00f, 0.00f, 1.00f } // CustomGCodes
{ 0.803f, 0.135f, 0.839f }, // Retractions
{ 0.287f, 0.679f, 0.810f }, // Unretractions
{ 0.758f, 0.744f, 0.389f }, // ToolChanges
{ 0.856f, 0.582f, 0.546f }, // ColorChanges
{ 0.322f, 0.942f, 0.512f }, // PausePrints
{ 0.886f, 0.825f, 0.262f } // CustomGCodes
}};
const std::vector<GCodeViewer::Color> GCodeViewer::Travel_Colors {{
{ 0.0f, 0.0f, 0.5f }, // Move
{ 0.0f, 0.5f, 0.0f }, // Extrude
{ 0.5f, 0.0f, 0.0f } // Retract
{ 0.219f, 0.282f, 0.609f }, // Move
{ 0.112f, 0.422f, 0.103f }, // Extrude
{ 0.505f, 0.064f, 0.028f } // Retract
}};
const std::vector<GCodeViewer::Color> GCodeViewer::Range_Colors {{
@ -279,7 +260,8 @@ const std::vector<GCodeViewer::Color> GCodeViewer::Range_Colors {{
{ 0.961f, 0.808f, 0.039f },
{ 0.890f, 0.533f, 0.125f },
{ 0.820f, 0.408f, 0.188f },
{ 0.761f, 0.322f, 0.235f } // reddish
{ 0.761f, 0.322f, 0.235f },
{ 0.581f, 0.149f, 0.087f } // reddish
}};
bool GCodeViewer::init()
@ -1525,11 +1507,15 @@ void GCodeViewer::render_legend() const
append_item(EItemType::Rect, Range_Colors[i], buf);
};
float step_size = range.step_size();
if (step_size == 0.0f)
if (range.count == 1)
// single item use case
append_range_item(0, range.min, decimals);
else if (range.count == 2) {
append_range_item(static_cast<int>(Range_Colors.size()) - 1, range.max, decimals);
append_range_item(0, range.min, decimals);
}
else {
float step_size = range.step_size();
for (int i = static_cast<int>(Range_Colors.size()) - 1; i >= 0; --i) {
append_range_item(i, range.min + static_cast<float>(i) * step_size, decimals);
}

View file

@ -142,11 +142,17 @@ class GCodeViewer
{
float min;
float max;
unsigned int count;
Range() { reset(); }
void update_from(const float value) { min = std::min(min, value); max = std::max(max, value); }
void reset() { min = FLT_MAX; max = -FLT_MAX; }
void update_from(const float value) {
if (value != max && value != min)
++count;
min = std::min(min, value);
max = std::max(max, value);
}
void reset() { min = FLT_MAX; max = -FLT_MAX; count = 0; }
float step_size() const { return (max - min) / (static_cast<float>(Range_Colors.size()) - 1.0f); }
Color get_color_at(float value) const;