diff --git a/xs/src/libslic3r/GCode/Analyzer.cpp b/xs/src/libslic3r/GCode/Analyzer.cpp index 8d27cf63c..2ddb319fc 100644 --- a/xs/src/libslic3r/GCode/Analyzer.cpp +++ b/xs/src/libslic3r/GCode/Analyzer.cpp @@ -122,9 +122,10 @@ GCodeAnalyzer::PreviewData::Extrusion::Layer::Layer(float z, const ExtrusionPath { } -GCodeAnalyzer::PreviewData::Travel::Polyline::Polyline(EType type, EDirection direction, const Polyline3& polyline) +GCodeAnalyzer::PreviewData::Travel::Polyline::Polyline(EType type, EDirection direction, float feedrate, const Polyline3& polyline) : type(type) , direction(direction) + , feedrate(feedrate) , polyline(polyline) { } @@ -1002,11 +1003,11 @@ void GCodeAnalyzer::_calc_gcode_preview_travel(Print& print) { struct Helper { - static void store_polyline(const Polyline3& polyline, PreviewData::Travel::EType type, PreviewData::Travel::Polyline::EDirection direction, Print& print) + static void store_polyline(const Polyline3& polyline, PreviewData::Travel::EType type, PreviewData::Travel::Polyline::EDirection direction, float feedrate, Print& print) { // if the polyline is valid, store it if (polyline.is_valid()) - print.gcode_preview.travel.polylines.emplace_back(type, direction, polyline); + print.gcode_preview.travel.polylines.emplace_back(type, direction, feedrate, polyline); } }; @@ -1018,6 +1019,7 @@ void GCodeAnalyzer::_calc_gcode_preview_travel(Print& print) Pointf3 position(FLT_MAX, FLT_MAX, FLT_MAX); PreviewData::Travel::EType type = PreviewData::Travel::Num_Types; PreviewData::Travel::Polyline::EDirection direction = PreviewData::Travel::Polyline::Num_Directions; + float feedrate = FLT_MAX; // constructs the polylines while traversing the moves for (const GCodeMove& move : travel_moves->second) @@ -1025,11 +1027,11 @@ void GCodeAnalyzer::_calc_gcode_preview_travel(Print& print) PreviewData::Travel::EType move_type = (move.delta_extruder < 0.0f) ? PreviewData::Travel::Retract : ((move.delta_extruder > 0.0f) ? PreviewData::Travel::Extrude : PreviewData::Travel::Move); PreviewData::Travel::Polyline::EDirection move_direction = ((move.start_position.x != move.end_position.x) || (move.start_position.y != move.end_position.y)) ? PreviewData::Travel::Polyline::Generic : PreviewData::Travel::Polyline::Vertical; - if ((type != move_type) || (direction != move_direction) || (position != move.start_position)) + if ((type != move_type) || (direction != move_direction) || (feedrate != move.data.feedrate) || (position != move.start_position)) { // store current polyline polyline.remove_duplicate_points(); - Helper::store_polyline(polyline, type, direction, print); + Helper::store_polyline(polyline, type, direction, feedrate, print); // reset current polyline polyline = Polyline3(); @@ -1045,11 +1047,12 @@ void GCodeAnalyzer::_calc_gcode_preview_travel(Print& print) // update current values position = move.end_position; type = move_type; + feedrate = move.data.feedrate; } // store last polyline polyline.remove_duplicate_points(); - Helper::store_polyline(polyline, type, direction, print); + Helper::store_polyline(polyline, type, direction, feedrate, print); } void GCodeAnalyzer::_calc_gcode_preview_retractions(Print& print) diff --git a/xs/src/libslic3r/GCode/Analyzer.hpp b/xs/src/libslic3r/GCode/Analyzer.hpp index c4e71799b..a72874a35 100644 --- a/xs/src/libslic3r/GCode/Analyzer.hpp +++ b/xs/src/libslic3r/GCode/Analyzer.hpp @@ -215,9 +215,10 @@ public: EType type; EDirection direction; + float feedrate; Polyline3 polyline; - Polyline(EType type, EDirection direction, const Polyline3& polyline); + Polyline(EType type, EDirection direction, float feedrate, const Polyline3& polyline); }; typedef std::vector PolylinesList; diff --git a/xs/src/slic3r/GUI/3DScene.cpp b/xs/src/slic3r/GUI/3DScene.cpp index a79e38035..589a23e86 100644 --- a/xs/src/slic3r/GUI/3DScene.cpp +++ b/xs/src/slic3r/GUI/3DScene.cpp @@ -1863,59 +1863,141 @@ void _3DScene::_load_gcode_travel_paths(const Print& print, GLVolumeCollection& typedef std::vector TypesList; + // Helper structure for feedrate + struct Feedrate + { + float value; + GLVolume* volume; + + explicit Feedrate(float value) + : value(value) + , volume(nullptr) + { + } + + bool operator == (const Feedrate& other) const + { + return value == other.value; + } + }; + + typedef std::vector FeedratesList; + size_t initial_volumes_count = volumes.volumes.size(); s_gcode_preview_data.first_volumes.emplace_back(GCodePreviewData::Travel, 0, (unsigned int)initial_volumes_count); - // detects types - TypesList types; - for (const GCodeAnalyzer::PreviewData::Travel::Polyline& polyline : print.gcode_preview.travel.polylines) + if (print.gcode_preview.extrusion.view_type == GCodeAnalyzer::PreviewData::Extrusion::Feedrate) { - if (std::find(types.begin(), types.end(), Type(polyline.type)) == types.end()) - types.emplace_back(polyline.type); - } + // colors travels by feedrate - // nothing to render, return - if (types.empty()) - return; - - // creates a new volume for each type - for (Type& type : types) - { - GLVolume* volume = new GLVolume(print.gcode_preview.travel.type_colors[type.value].rgba); - if (volume != nullptr) + // detects feedrates + FeedratesList feedrates; + for (const GCodeAnalyzer::PreviewData::Travel::Polyline& polyline : print.gcode_preview.travel.polylines) { - type.volume = volume; - volumes.volumes.emplace_back(volume); + if (std::find(feedrates.begin(), feedrates.end(), Feedrate(polyline.feedrate)) == feedrates.end()) + feedrates.emplace_back(polyline.feedrate); } - else + + // nothing to render, return + if (feedrates.empty()) + return; + + // creates a new volume for each feedrate + for (Feedrate& feedrate : feedrates) { - // an error occourred - restore to previous state and return - if (initial_volumes_count != volumes.volumes.size()) + GLVolume* volume = new GLVolume(print.gcode_preview.get_extrusion_feedrate_color(feedrate.value).rgba); + if (volume != nullptr) { - std::vector::iterator begin = volumes.volumes.begin() + initial_volumes_count; - std::vector::iterator end = volumes.volumes.end(); - for (std::vector::iterator it = begin; it < end; ++it) + feedrate.volume = volume; + volumes.volumes.emplace_back(volume); + } + else + { + // an error occourred - restore to previous state and return + if (initial_volumes_count != volumes.volumes.size()) { - GLVolume* volume = *it; - delete volume; + std::vector::iterator begin = volumes.volumes.begin() + initial_volumes_count; + std::vector::iterator end = volumes.volumes.end(); + for (std::vector::iterator it = begin; it < end; ++it) + { + GLVolume* volume = *it; + delete volume; + } + volumes.volumes.erase(begin, end); + return; } - volumes.volumes.erase(begin, end); - return; + } + } + + // populates volumes + for (const GCodeAnalyzer::PreviewData::Travel::Polyline& polyline : print.gcode_preview.travel.polylines) + { + FeedratesList::iterator feedrate = std::find(feedrates.begin(), feedrates.end(), Feedrate(polyline.feedrate)); + if (feedrate != feedrates.end()) + { + feedrate->volume->print_zs.push_back(unscale(polyline.polyline.bounding_box().max.z)); + feedrate->volume->offsets.push_back(feedrate->volume->indexed_vertex_array.quad_indices.size()); + feedrate->volume->offsets.push_back(feedrate->volume->indexed_vertex_array.triangle_indices.size()); + + polyline3_to_verts(polyline.polyline, print.gcode_preview.travel.width, print.gcode_preview.travel.height, *feedrate->volume); } } } - - // populates volumes - for (const GCodeAnalyzer::PreviewData::Travel::Polyline& polyline : print.gcode_preview.travel.polylines) + else { - TypesList::iterator type = std::find(types.begin(), types.end(), Type(polyline.type)); - if (type != types.end()) - { - type->volume->print_zs.push_back(unscale(polyline.polyline.bounding_box().max.z)); - type->volume->offsets.push_back(type->volume->indexed_vertex_array.quad_indices.size()); - type->volume->offsets.push_back(type->volume->indexed_vertex_array.triangle_indices.size()); + // colors travels by travel type - polyline3_to_verts(polyline.polyline, print.gcode_preview.travel.width, print.gcode_preview.travel.height, *type->volume); + // detects types + TypesList types; + for (const GCodeAnalyzer::PreviewData::Travel::Polyline& polyline : print.gcode_preview.travel.polylines) + { + if (std::find(types.begin(), types.end(), Type(polyline.type)) == types.end()) + types.emplace_back(polyline.type); + } + + // nothing to render, return + if (types.empty()) + return; + + // creates a new volume for each type + for (Type& type : types) + { + GLVolume* volume = new GLVolume(print.gcode_preview.travel.type_colors[type.value].rgba); + if (volume != nullptr) + { + type.volume = volume; + volumes.volumes.emplace_back(volume); + } + else + { + // an error occourred - restore to previous state and return + if (initial_volumes_count != volumes.volumes.size()) + { + std::vector::iterator begin = volumes.volumes.begin() + initial_volumes_count; + std::vector::iterator end = volumes.volumes.end(); + for (std::vector::iterator it = begin; it < end; ++it) + { + GLVolume* volume = *it; + delete volume; + } + volumes.volumes.erase(begin, end); + return; + } + } + } + + // populates volumes + for (const GCodeAnalyzer::PreviewData::Travel::Polyline& polyline : print.gcode_preview.travel.polylines) + { + TypesList::iterator type = std::find(types.begin(), types.end(), Type(polyline.type)); + if (type != types.end()) + { + type->volume->print_zs.push_back(unscale(polyline.polyline.bounding_box().max.z)); + type->volume->offsets.push_back(type->volume->indexed_vertex_array.quad_indices.size()); + type->volume->offsets.push_back(type->volume->indexed_vertex_array.triangle_indices.size()); + + polyline3_to_verts(polyline.polyline, print.gcode_preview.travel.width, print.gcode_preview.travel.height, *type->volume); + } } }