New gcode visualization library - Separation of tool colors and color changes colors to simplify client code

This commit is contained in:
enricoturri1966 2024-01-23 10:11:50 +01:00 committed by Lukas Matena
parent 24f0c66886
commit 6529f28f26
12 changed files with 212 additions and 102 deletions

View File

@ -5,6 +5,7 @@
#ifndef VGCODE_GCODEINPUTDATA_HPP
#define VGCODE_GCODEINPUTDATA_HPP
#include "Types.hpp"
#include "PathVertex.hpp"
namespace libvgcode {
@ -21,6 +22,14 @@ struct GCodeInputData
// See: PathVertex
//
std::vector<PathVertex> vertices;
//
// Palette for extruders colors
//
Palette tools_colors;
//
// Palette for color print colors
//
Palette color_print_colors;
};
} // namespace libvgcode

View File

@ -146,6 +146,21 @@ public:
//
void set_tool_colors(const Palette& colors);
//
// Return the count of colors in the palette used to render
// the toolpaths when the view type is EViewType::ColorPrint.
//
size_t get_color_print_colors_count() const;
//
// Return the palette used to render the toolpaths when
// the view type is EViewType::ColorPrint.
//
const Palette& get_color_print_colors() const;
//
// Set the palette used to render the toolpaths when
// the view type is EViewType::ColorPrint with the given one.
//
void set_color_print_colors(const Palette& colors);
//
// Get the color range for the given view type.
// Valid view types are:
// EViewType::Height

View File

@ -137,6 +137,21 @@ void Viewer::set_tool_colors(const Palette& colors)
m_impl->set_tool_colors(colors);
}
size_t Viewer::get_color_print_colors_count() const
{
return m_impl->get_color_print_colors_count();
}
const Palette& Viewer::get_color_print_colors() const
{
return m_impl->get_color_print_colors();
}
void Viewer::set_color_print_colors(const Palette& colors)
{
m_impl->set_color_print_colors(colors);
}
const ColorRange& Viewer::get_color_range(EViewType type) const
{
return m_impl->get_color_range(type);

View File

@ -482,6 +482,9 @@ void ViewerImpl::load(GCodeInputData&& gcode_data)
m_loading = true;
m_vertices = std::move(gcode_data.vertices);
m_tool_colors = std::move(gcode_data.tools_colors);
m_color_print_colors = std::move(gcode_data.color_print_colors);
m_settings.spiral_vase_mode = gcode_data.spiral_vase_mode;
std::array<float, TIME_MODES_COUNT> times{ 0.0f, 0.0f };
@ -983,7 +986,7 @@ Color ViewerImpl::get_vertex_color(const PathVertex& v) const
case EViewType::ColorPrint:
{
return m_layers.layer_contains_colorprint_options(static_cast<size_t>(v.layer_id)) ? DUMMY_COLOR :
m_tool_colors[static_cast<size_t>(v.color_id) % m_tool_colors.size()];
m_color_print_colors[static_cast<size_t>(v.color_id) % m_color_print_colors.size()];
}
default: { break; }
}
@ -997,6 +1000,12 @@ void ViewerImpl::set_tool_colors(const Palette& colors)
m_settings.update_colors = true;
}
void ViewerImpl::set_color_print_colors(const Palette& colors)
{
m_color_print_colors = colors;
m_settings.update_colors = true;
}
const Color& ViewerImpl::get_extrusion_role_color(EGCodeExtrusionRole role) const
{
const auto it = m_extrusion_roles_colors.find(role);

View File

@ -143,6 +143,10 @@ public:
const Palette& get_tool_colors() const { return m_tool_colors; }
void set_tool_colors(const Palette& colors);
size_t get_color_print_colors_count() const { return m_color_print_colors.size(); }
const Palette& get_color_print_colors() const { return m_color_print_colors; }
void set_color_print_colors(const Palette& colors);
const Color& get_extrusion_role_color(EGCodeExtrusionRole role) const;
void set_extrusion_role_color(EGCodeExtrusionRole role, const Color& color);
void reset_default_extrusion_roles_colors();
@ -278,6 +282,7 @@ private:
ColorRange(EColorRangeType::Linear), ColorRange(EColorRangeType::Logarithmic)
};
Palette m_tool_colors;
Palette m_color_print_colors;
//
// OpenGL shaders ids
//

View File

@ -1110,37 +1110,33 @@ void GCodeViewer::init()
}
#if ENABLE_NEW_GCODE_VIEWER
void GCodeViewer::load_as_gcode(const GCodeProcessorResult& gcode_result, const Print& print, const std::vector<std::string>& str_tool_colors)
void GCodeViewer::load_as_gcode(const GCodeProcessorResult& gcode_result, const Print& print, const std::vector<std::string>& str_tool_colors,
const std::vector<std::string>& str_color_print_colors)
{
const bool current_top_layer_only = m_viewer.is_top_layer_only_view_range();
const bool required_top_layer_only = get_app_config()->get_bool("seq_top_layer_only");
if (current_top_layer_only != required_top_layer_only)
m_viewer.toggle_top_layer_only_view_range();
std::vector<ColorRGBA> tool_colors;
if (m_viewer.get_view_type() == libvgcode::EViewType::Tool && !gcode_result.extruder_colors.empty())
// update tool colors from config stored in the gcode
decode_colors(gcode_result.extruder_colors, tool_colors);
else
// update tool colors
decode_colors(str_tool_colors, tool_colors);
// ensure there are enough colors defined
const ColorRGBA default_color = { 1.0f, 0.5f, 0.0f, 1.0f }; // "#FF8000"
while (tool_colors.size() < std::max<size_t>(1, gcode_result.extruders_count)) {
tool_colors.push_back(default_color);
}
std::vector<libvgcode::Color> colors;
colors.reserve(tool_colors.size());
for (const ColorRGBA& color : tool_colors) {
colors.emplace_back(libvgcode::convert(color));
}
m_viewer.set_tool_colors(colors);
// avoid processing if called with the same gcode_result
if (m_last_result_id == gcode_result.id)
if (m_last_result_id == gcode_result.id && wxGetApp().is_editor()) {
// collect tool colors
libvgcode::Palette tools_colors;
tools_colors.reserve(str_tool_colors.size());
for (const std::string& color : str_tool_colors) {
tools_colors.emplace_back(libvgcode::convert(color));
}
m_viewer.set_tool_colors(tools_colors);
// collect color print colors
libvgcode::Palette color_print_colors;
color_print_colors.reserve(str_color_print_colors.size());
for (const std::string& color : str_color_print_colors) {
color_print_colors.emplace_back(libvgcode::convert(color));
}
m_viewer.set_color_print_colors(color_print_colors);
return;
}
m_last_result_id = gcode_result.id;
@ -1148,7 +1144,7 @@ void GCodeViewer::load_as_gcode(const GCodeProcessorResult& gcode_result, const
reset();
// convert data from PrusaSlicer format to libvgcode format
libvgcode::GCodeInputData data = libvgcode::convert(gcode_result, m_viewer.get_travels_radius(), m_viewer.get_wipes_radius());
libvgcode::GCodeInputData data = libvgcode::convert(gcode_result, str_tool_colors, str_color_print_colors, m_viewer);
// send data to the viewer
m_viewer.reset_default_extrusion_roles_colors();
@ -1291,19 +1287,8 @@ void GCodeViewer::load(const GCodeProcessorResult& gcode_result, const Print & p
}
#if ENABLE_NEW_GCODE_VIEWER
void GCodeViewer::load_as_preview(libvgcode::GCodeInputData&& data, const std::vector<std::string>& str_tool_colors)
void GCodeViewer::load_as_preview(libvgcode::GCodeInputData&& data)
{
if (!str_tool_colors.empty()) {
std::vector<ColorRGBA> tool_colors;
decode_colors(str_tool_colors, tool_colors);
std::vector<libvgcode::Color> colors;
colors.reserve(tool_colors.size());
for (const ColorRGBA& color : tool_colors) {
colors.emplace_back(libvgcode::convert(color));
}
m_viewer.set_tool_colors(colors);
}
m_viewer.set_extrusion_role_color(libvgcode::EGCodeExtrusionRole::Skirt, { 127, 255, 127 });
m_viewer.set_extrusion_role_color(libvgcode::EGCodeExtrusionRole::ExternalPerimeter, { 255, 255, 0 });
m_viewer.set_extrusion_role_color(libvgcode::EGCodeExtrusionRole::SupportMaterial, { 127, 255, 127 });

View File

@ -913,8 +913,9 @@ public:
// extract rendering data from the given parameters
#if ENABLE_NEW_GCODE_VIEWER
void load_as_gcode(const GCodeProcessorResult& gcode_result, const Print& print, const std::vector<std::string>& str_tool_colors);
void load_as_preview(libvgcode::GCodeInputData&& data, const std::vector<std::string>& str_tool_colors);
void load_as_gcode(const GCodeProcessorResult& gcode_result, const Print& print, const std::vector<std::string>& str_tool_colors,
const std::vector<std::string>& str_color_print_colors);
void load_as_preview(libvgcode::GCodeInputData&& data);
#else
void load(const GCodeProcessorResult& gcode_result, const Print& print);
// recalculate ranges in dependence of what is visible and sets tool/print colors
@ -1024,7 +1025,9 @@ public:
void toggle_gcode_window_visibility() { m_sequential_view.gcode_window.toggle_visibility(); }
#if !ENABLE_NEW_GCODE_VIEWER
std::vector<CustomGCode::Item>& get_custom_gcode_per_print_z() { return m_custom_gcode_per_print_z; }
#endif // !ENABLE_NEW_GCODE_VIEWER
size_t get_extruders_count() { return m_extruders_count; }
void invalidate_legend() { m_legend_resizer.reset(); }

View File

@ -2687,19 +2687,21 @@ void GLCanvas3D::load_gcode_shells()
m_gcode_viewer.set_force_shells_visible(true);
}
void GLCanvas3D::load_gcode_preview(const GCodeProcessorResult& gcode_result, const std::vector<std::string>& str_tool_colors)
{
#if ENABLE_NEW_GCODE_VIEWER
void GLCanvas3D::load_gcode_preview(const GCodeProcessorResult& gcode_result, const std::vector<std::string>& str_tool_colors,
const std::vector<std::string>& str_color_print_colors)
{
m_gcode_viewer.enable_legend(true);
m_gcode_viewer.enable_view_type_cache_write(true);
m_gcode_viewer.enable_view_type_cache_load(true);
m_gcode_viewer.set_view_type(m_gcode_viewer.get_view_type());
m_gcode_viewer.load_as_gcode(gcode_result, *this->fff_print(), str_tool_colors);
m_gcode_viewer.load_as_gcode(gcode_result, *this->fff_print(), str_tool_colors, str_color_print_colors);
m_gcode_layers_times_cache = m_gcode_viewer.get_layers_times();
#else
void GLCanvas3D::load_gcode_preview(const GCodeProcessorResult& gcode_result, const std::vector<std::string>& str_tool_colors)
{
m_gcode_viewer.load(gcode_result, *this->fff_print());
#endif // ENABLE_NEW_GCODE_VIEWER
if (wxGetApp().is_editor()) {
_set_warning_notification_if_needed(EWarning::ToolpathOutside);
_set_warning_notification_if_needed(EWarning::GCodeConflict);
@ -2735,7 +2737,12 @@ void GLCanvas3D::load_sla_preview()
}
}
#if ENABLE_NEW_GCODE_VIEWER
void GLCanvas3D::load_preview(const std::vector<std::string>& str_tool_colors, const std::vector<std::string>& str_color_print_colors,
const std::vector<CustomGCode::Item>& color_print_values)
#else
void GLCanvas3D::load_preview(const std::vector<std::string>& str_tool_colors, const std::vector<CustomGCode::Item>& color_print_values)
#endif // ENABLE_NEW_GCODE_VIEWER
{
const Print *print = this->fff_print();
if (print == nullptr)
@ -2744,14 +2751,15 @@ void GLCanvas3D::load_preview(const std::vector<std::string>& str_tool_colors, c
_set_current();
#if ENABLE_NEW_GCODE_VIEWER
libvgcode::GCodeInputData data = libvgcode::convert(*print, str_tool_colors, color_print_values, static_cast<size_t>(wxGetApp().extruders_edited_cnt()));
libvgcode::GCodeInputData data = libvgcode::convert(*print, str_tool_colors, str_color_print_colors, color_print_values,
static_cast<size_t>(wxGetApp().extruders_edited_cnt()));
// send data to the viewer
m_gcode_viewer.enable_legend(false);
m_gcode_viewer.enable_view_type_cache_write(false);
m_gcode_viewer.enable_view_type_cache_load(false);
m_gcode_viewer.set_view_type(libvgcode::EViewType::FeatureType);
m_gcode_viewer.load_as_preview(std::move(data), str_tool_colors);
m_gcode_viewer.load_as_preview(std::move(data));
#else
// Release OpenGL data before generating new data.
this->reset_volumes();

View File

@ -857,7 +857,9 @@ public:
#endif // ENABLE_NEW_GCODE_VIEWER
void set_volumes_z_range(const std::array<double, 2>& range);
void set_toolpaths_z_range(const std::array<unsigned int, 2>& range);
#if !ENABLE_NEW_GCODE_VIEWER
std::vector<CustomGCode::Item>& get_custom_gcode_per_print_z() { return m_gcode_viewer.get_custom_gcode_per_print_z(); }
#endif // !ENABLE_NEW_GCODE_VIEWER
size_t get_gcode_extruders_count() { return m_gcode_viewer.get_extruders_count(); }
std::vector<int> load_object(const ModelObject& model_object, int obj_idx, std::vector<int> instance_idxs);
@ -868,21 +870,27 @@ public:
void reload_scene(bool refresh_immediately, bool force_full_scene_refresh = false);
void load_gcode_shells();
void load_gcode_preview(const GCodeProcessorResult& gcode_result, const std::vector<std::string>& str_tool_colors);
#if ENABLE_NEW_GCODE_VIEWER
void load_gcode_preview(const GCodeProcessorResult& gcode_result, const std::vector<std::string>& str_tool_colors,
const std::vector<std::string>& str_color_print_colors);
void set_gcode_view_type(libvgcode::EViewType type) { return m_gcode_viewer.set_view_type(type); }
libvgcode::EViewType get_gcode_view_type() const { return m_gcode_viewer.get_view_type(); }
void enable_gcode_view_type_cache_load(bool enable) { m_gcode_viewer.enable_view_type_cache_load(enable); }
void enable_gcode_view_type_cache_write(bool enable) { m_gcode_viewer.enable_view_type_cache_write(enable); }
bool is_gcode_view_type_cache_load_enabled() const { return m_gcode_viewer.is_view_type_cache_load_enabled(); }
bool is_gcode_view_type_cache_write_enabled() const { return m_gcode_viewer.is_view_type_cache_write_enabled(); }
void load_preview(const std::vector<std::string>& str_tool_colors, const std::vector<std::string>& str_color_print_colors,
const std::vector<CustomGCode::Item>& color_print_values);
#else
void load_gcode_preview(const GCodeProcessorResult& gcode_result, const std::vector<std::string>& str_tool_colors);
void refresh_gcode_preview_render_paths(bool keep_sequential_current_first, bool keep_sequential_current_last);
void set_gcode_view_preview_type(GCodeViewer::EViewType type) { return m_gcode_viewer.set_view_type(type); }
GCodeViewer::EViewType get_gcode_view_preview_type() const { return m_gcode_viewer.get_view_type(); }
void load_preview(const std::vector<std::string>& str_tool_colors, const std::vector<CustomGCode::Item>& color_print_values);
#endif // ENABLE_NEW_GCODE_VIEWER
void load_sla_preview();
void load_preview(const std::vector<std::string>& str_tool_colors, const std::vector<CustomGCode::Item>& color_print_values);
void bind_event_handlers();
void unbind_event_handlers();

View File

@ -526,7 +526,11 @@ void Preview::update_layers_slider(const std::vector<double>& layers_z, bool kee
ticks_info_from_model = plater->model().custom_gcode_per_print_z;
else {
ticks_info_from_model.mode = CustomGCode::Mode::SingleExtruder;
#if ENABLE_NEW_GCODE_VIEWER
ticks_info_from_model.gcodes = m_gcode_result->custom_gcode_per_print_z;
#else
ticks_info_from_model.gcodes = m_canvas->get_custom_gcode_per_print_z();
#endif // ENABLE_NEW_GCODE_VIEWER
}
check_layers_slider_values(ticks_info_from_model.gcodes, layers_z);
@ -874,20 +878,23 @@ void Preview::load_print_as_fff(bool keep_z_range)
libvgcode::EViewType gcode_view_type = m_canvas->get_gcode_view_type();
const bool gcode_preview_data_valid = !m_gcode_result->moves.empty();
const bool is_pregcode_preview = !gcode_preview_data_valid && wxGetApp().is_editor();
const std::vector<std::string> tool_colors = wxGetApp().plater()->get_extruder_colors_from_plater_config(m_gcode_result);
const std::vector<CustomGCode::Item>& color_print_values = wxGetApp().is_editor() ?
wxGetApp().plater()->model().custom_gcode_per_print_z.gcodes : m_gcode_result->custom_gcode_per_print_z;
std::vector<std::string> color_print_colors;
if (!color_print_values.empty()) {
color_print_colors = wxGetApp().plater()->get_colors_for_color_print(m_gcode_result);
color_print_colors.push_back("#808080"); // gray color for pause print or custom G-code
}
#else
GCodeViewer::EViewType gcode_view_type = m_canvas->get_gcode_view_preview_type();
const bool gcode_preview_data_valid = !m_gcode_result->moves.empty();
#endif // ENABLE_NEW_GCODE_VIEWER
// Collect colors per extruder.
std::vector<std::string> colors;
std::vector<CustomGCode::Item> color_print_values = {};
// set color print values, if it is selected "ColorPrint" view type
#if ENABLE_NEW_GCODE_VIEWER
if (gcode_view_type == libvgcode::EViewType::ColorPrint || is_pregcode_preview) {
#else
if (gcode_view_type == GCodeViewer::EViewType::ColorPrint) {
#endif // ENABLE_NEW_GCODE_VIEWER
colors = wxGetApp().plater()->get_colors_for_color_print(m_gcode_result);
if (!gcode_preview_data_valid) {
@ -898,25 +905,25 @@ void Preview::load_print_as_fff(bool keep_z_range)
colors.push_back("#808080"); // gray color for pause print or custom G-code
}
}
#if ENABLE_NEW_GCODE_VIEWER
else if (gcode_preview_data_valid || gcode_view_type == libvgcode::EViewType::Tool) {
#else
else if (gcode_preview_data_valid || gcode_view_type == GCodeViewer::EViewType::Tool) {
#endif // ENABLE_NEW_GCODE_VIEWER
colors = wxGetApp().plater()->get_extruder_colors_from_plater_config(m_gcode_result);
color_print_values.clear();
}
#endif // ENABLE_NEW_GCODE_VIEWER
std::vector<double> zs;
if (IsShown()) {
m_canvas->set_selected_extruder(0);
if (gcode_preview_data_valid) {
// Load the real G-code preview.
m_canvas->load_gcode_preview(*m_gcode_result, colors);
#if ENABLE_NEW_GCODE_VIEWER
// Load the real G-code preview.
m_canvas->load_gcode_preview(*m_gcode_result, tool_colors, color_print_colors);
// the view type may have been changed by the call m_canvas->load_gcode_preview()
gcode_view_type = m_canvas->get_gcode_view_type();
#else
// Load the real G-code preview.
m_canvas->load_gcode_preview(*m_gcode_result, colors);
#endif // ENABLE_NEW_GCODE_VIEWER
m_left_sizer->Layout();
Refresh();
@ -927,15 +934,15 @@ void Preview::load_print_as_fff(bool keep_z_range)
}
#if ENABLE_NEW_GCODE_VIEWER
else if (is_pregcode_preview) {
#else
else if (wxGetApp().is_editor()) {
#endif // ENABLE_NEW_GCODE_VIEWER
// Load the initial preview based on slices, not the final G-code.
m_canvas->load_preview(colors, color_print_values);
#if ENABLE_NEW_GCODE_VIEWER
m_canvas->load_preview(tool_colors, color_print_colors, color_print_values);
// the view type has been changed by the call m_canvas->load_gcode_preview()
if (gcode_view_type == libvgcode::EViewType::ColorPrint && !color_print_values.empty())
m_canvas->set_gcode_view_type(gcode_view_type);
#else
else if (wxGetApp().is_editor()) {
// Load the initial preview based on slices, not the final G-code.
m_canvas->load_preview(colors, color_print_values);
#endif // ENABLE_NEW_GCODE_VIEWER
m_left_sizer->Hide(m_bottom_toolbar_panel);
m_left_sizer->Layout();
@ -954,18 +961,19 @@ void Preview::load_print_as_fff(bool keep_z_range)
if (!zs.empty() && !m_keep_current_preview_type) {
const unsigned int number_extruders = wxGetApp().is_editor() ?
(unsigned int)print->extruders().size() :
m_canvas->get_gcode_extruders_count();
(unsigned int)print->extruders().size() : m_canvas->get_gcode_extruders_count();
#if ENABLE_NEW_GCODE_VIEWER
const bool contains_color_gcodes = std::any_of(std::begin(color_print_values), std::end(color_print_values),
[](auto const& item) { return item.type == CustomGCode::Type::ColorChange; });
const libvgcode::EViewType choice = contains_color_gcodes ?
libvgcode::EViewType::ColorPrint :
(number_extruders > 1) ? libvgcode::EViewType::Tool : libvgcode::EViewType::FeatureType;
#else
const std::vector<Item> gcodes = wxGetApp().is_editor() ?
wxGetApp().plater()->model().custom_gcode_per_print_z.gcodes :
m_canvas->get_custom_gcode_per_print_z();
const bool contains_color_gcodes = std::any_of(std::begin(gcodes), std::end(gcodes),
[](auto const& item) { return item.type == CustomGCode::Type::ColorChange || item.type == CustomGCode::Type::ToolChange; });
#if ENABLE_NEW_GCODE_VIEWER
const libvgcode::EViewType choice = contains_color_gcodes ?
libvgcode::EViewType::ColorPrint :
(number_extruders > 1) ? libvgcode::EViewType::Tool : libvgcode::EViewType::FeatureType;
#else
const GCodeViewer::EViewType choice = contains_color_gcodes ?
GCodeViewer::EViewType::ColorPrint :
(number_extruders > 1) ? GCodeViewer::EViewType::Tool : GCodeViewer::EViewType::FeatureType;

View File

@ -7,6 +7,7 @@
#if ENABLE_NEW_GCODE_VIEWER
#include "libslic3r/Print.hpp"
#include "libslic3r/Color.hpp"
namespace libvgcode {
@ -38,6 +39,12 @@ Color convert(const Slic3r::ColorRGBA& c)
return { static_cast<uint8_t>(c.r() * 255.0f), static_cast<uint8_t>(c.g() * 255.0f), static_cast<uint8_t>(c.b() * 255.0f) };
}
Color convert(const std::string& color_str)
{
Slic3r::ColorRGBA color_rgba;
return decode_color(color_str, color_rgba) ? convert(color_rgba) : DUMMY_COLOR;
}
Slic3r::GCodeExtrusionRole convert(EGCodeExtrusionRole role)
{
switch (role)
@ -147,10 +154,28 @@ Slic3r::PrintEstimatedStatistics::ETimeMode convert(const ETimeMode& mode)
}
}
GCodeInputData convert(const Slic3r::GCodeProcessorResult& result, float travels_radius, float wipes_radius)
GCodeInputData convert(const Slic3r::GCodeProcessorResult& result, const std::vector<std::string>& str_tool_colors,
const std::vector<std::string>& str_color_print_colors, const Viewer& viewer)
{
const std::vector<Slic3r::GCodeProcessorResult::MoveVertex>& moves = result.moves;
GCodeInputData ret;
// collect tool colors
ret.tools_colors.reserve(str_tool_colors.size());
for (const std::string& color : str_tool_colors) {
ret.tools_colors.emplace_back(convert(color));
}
// collect color print colors
const std::vector<std::string>& str_colors = str_color_print_colors.empty() ? str_tool_colors : str_color_print_colors;
ret.color_print_colors.reserve(str_colors.size());
for (const std::string& color : str_colors) {
ret.color_print_colors.emplace_back(convert(color));
}
const float travels_radius = viewer.get_travels_radius();
const float wipes_radius = viewer.get_wipes_radius();
const std::vector<Slic3r::GCodeProcessorResult::MoveVertex>& moves = result.moves;
ret.vertices.reserve(2 * moves.size());
for (size_t i = 1; i < moves.size(); ++i) {
const Slic3r::GCodeProcessorResult::MoveVertex& curr = moves[i];
@ -498,37 +523,40 @@ static void convert_wipe_tower_to_vertices(const Slic3r::Print& print, const std
class ObjectHelper
{
public:
ObjectHelper(const std::vector<Slic3r::CustomGCode::Item>& color_print_values, size_t tool_colors_count, size_t extruders_count)
ObjectHelper(const std::vector<Slic3r::CustomGCode::Item>& color_print_values, size_t tool_colors_count, size_t color_print_colors_count, size_t extruders_count)
: m_color_print_values(color_print_values)
, m_tool_colors_count(tool_colors_count)
, m_extruders_count(extruders_count)
{
, m_color_print_colors_count(color_print_colors_count)
, m_extruders_count(extruders_count) {
}
uint8_t color_id(size_t layer_id, size_t extruder_id) const
{
return !m_color_print_values.empty() ? color_print_color_id(layer_id, extruder_id) :
(m_tool_colors_count > 0) ? std::min<uint8_t>(m_tool_colors_count - 1, static_cast<uint8_t>(extruder_id)) : 0;
uint8_t color_id(double print_z, size_t extruder_id) const {
if (!m_color_print_values.empty())
return color_print_color_id(print_z, extruder_id);
else {
if (m_tool_colors_count > 0)
return std::min<uint8_t>(m_tool_colors_count - 1, static_cast<uint8_t>(extruder_id));
else
return 0;
}
}
private:
const std::vector<Slic3r::CustomGCode::Item>& m_color_print_values;
size_t m_tool_colors_count{ 0 };
size_t m_color_print_colors_count{ 0 };
size_t m_extruders_count{ 0 };
uint8_t color_print_color_id(double print_z, size_t extruder_id) const
{
uint8_t color_print_color_id(double print_z, size_t extruder_id) const {
auto it = std::find_if(m_color_print_values.begin(), m_color_print_values.end(),
[print_z](const Slic3r::CustomGCode::Item& code) {
return std::fabs(code.print_z - print_z) < EPSILON;
});
if (it != m_color_print_values.end()) {
if (it != m_color_print_values.end()) {
Slic3r::CustomGCode::Type type = it->type;
// pause print or custom Gcode
if (type == Slic3r::CustomGCode::PausePrint ||
(type != Slic3r::CustomGCode::ColorChange && type != Slic3r::CustomGCode::ToolChange))
return static_cast<uint8_t>(m_tool_colors_count - 1); // last color item is a gray color for pause print or custom G-code
if (type == Slic3r::CustomGCode::PausePrint || type == Slic3r::CustomGCode::Custom || type == Slic3r::CustomGCode::Template)
return static_cast<uint8_t>(m_color_print_colors_count - 1); // last color item is a gray color for pause print or custom G-code
switch (it->type) {
// change color for current extruder
case Slic3r::CustomGCode::ColorChange: { return color_change_color_id(it, extruder_id); }
@ -554,8 +582,7 @@ private:
return std::min<uint8_t>(m_extruders_count - 1, static_cast<uint8_t>(extruder_id));
}
uint8_t color_change_color_id(std::vector<Slic3r::CustomGCode::Item>::const_iterator it, size_t extruder_id) const
{
uint8_t color_change_color_id(std::vector<Slic3r::CustomGCode::Item>::const_iterator it, size_t extruder_id) const {
if (m_extruders_count == 1)
return m600_color_id(it);
@ -577,8 +604,7 @@ private:
return 0;
}
uint8_t tool_change_color_id(std::vector<Slic3r::CustomGCode::Item>::const_iterator it, size_t extruder_id) const
{
uint8_t tool_change_color_id(std::vector<Slic3r::CustomGCode::Item>::const_iterator it, size_t extruder_id) const {
const int current_extruder = it->extruder == 0 ? static_cast<int>(extruder_id + 1) : it->extruder;
if (m_tool_colors_count == m_extruders_count + 1) // there is no one "M600"
return std::min<uint8_t>(m_extruders_count - 1, std::max<uint8_t>(current_extruder - 1, 0));
@ -593,8 +619,7 @@ private:
return std::min<uint8_t>(m_extruders_count - 1, std::max<uint8_t>(current_extruder - 1, 0));
}
uint8_t m600_color_id(std::vector<Slic3r::CustomGCode::Item>::const_iterator it) const
{
uint8_t m600_color_id(std::vector<Slic3r::CustomGCode::Item>::const_iterator it) const {
uint8_t shift = 0;
while (it != m_color_print_values.begin()) {
--it;
@ -606,7 +631,8 @@ private:
};
static void convert_object_to_vertices(const Slic3r::PrintObject& object, const std::vector<std::string>& str_tool_colors,
const std::vector<Slic3r::CustomGCode::Item>& color_print_values, size_t extruders_count, VerticesData& data)
const std::vector<std::string>& str_color_print_colors, const std::vector<Slic3r::CustomGCode::Item>& color_print_values,
size_t extruders_count, VerticesData& data)
{
const bool has_perimeters = object.is_step_done(Slic3r::posPerimeters);
const bool has_infill = object.is_step_done(Slic3r::posInfill);
@ -624,7 +650,7 @@ static void convert_object_to_vertices(const Slic3r::PrintObject& object, const
}
std::sort(layers.begin(), layers.end(), [](const Slic3r::Layer* l1, const Slic3r::Layer* l2) { return l1->print_z < l2->print_z; });
ObjectHelper object_helper(color_print_values, str_tool_colors.size(), extruders_count);
ObjectHelper object_helper(color_print_values, str_tool_colors.size(), str_color_print_colors.size(), extruders_count);
data.layers_zs.reserve(layers.size());
for (const Slic3r::Layer* layer : layers) {
@ -681,19 +707,21 @@ static void convert_object_to_vertices(const Slic3r::PrintObject& object, const
}
static void convert_objects_to_vertices(const Slic3r::SpanOfConstPtrs<Slic3r::PrintObject>& objects, const std::vector<std::string>& str_tool_colors,
const std::vector<Slic3r::CustomGCode::Item>& color_print_values, size_t extruders_count, std::vector<VerticesData>& data)
const std::vector<std::string>& str_color_print_colors, const std::vector<Slic3r::CustomGCode::Item>& color_print_values, size_t extruders_count,
std::vector<VerticesData>& data)
{
// extract vertices and layers zs object by object
data.reserve(data.size() + objects.size());
for (size_t i = 0; i < objects.size(); ++i) {
data.emplace_back(VerticesData());
convert_object_to_vertices(*objects[i], str_tool_colors, color_print_values, extruders_count, data.back());
convert_object_to_vertices(*objects[i], str_tool_colors, str_color_print_colors, color_print_values, extruders_count, data.back());
}
}
// mapping from Slic3r::Print to libvgcode::GCodeInputData
GCodeInputData convert(const Slic3r::Print& print, const std::vector<std::string>& str_tool_colors,
const std::vector<Slic3r::CustomGCode::Item>& color_print_values, size_t extruders_count)
const std::vector<std::string>& str_color_print_colors, const std::vector<Slic3r::CustomGCode::Item>& color_print_values,
size_t extruders_count)
{
GCodeInputData ret;
std::vector<VerticesData> data;
@ -704,7 +732,7 @@ GCodeInputData convert(const Slic3r::Print& print, const std::vector<std::string
// extract vertices and layers zs from wipe tower
convert_wipe_tower_to_vertices(print, str_tool_colors, data);
// extract vertices and layers zs from objects
convert_objects_to_vertices(print.objects(), str_tool_colors, color_print_values, extruders_count, data);
convert_objects_to_vertices(print.objects(), str_tool_colors, str_color_print_colors, color_print_values, extruders_count, data);
// collect layers zs
std::vector<float> layers;
@ -723,6 +751,19 @@ GCodeInputData convert(const Slic3r::Print& print, const std::vector<std::string
}
min_z = z;
}
// collect tool colors
ret.tools_colors.reserve(str_tool_colors.size());
for (const std::string& color : str_tool_colors) {
ret.tools_colors.emplace_back(convert(color));
}
// collect color print colors
ret.color_print_colors.reserve(str_color_print_colors.size());
for (const std::string& color : str_color_print_colors) {
ret.color_print_colors.emplace_back(convert(color));
}
return ret;
}

View File

@ -36,6 +36,9 @@ extern Slic3r::ColorRGBA convert(const Color& c);
// mapping from Slic3r::ColorRGBA to libvgcode::Color
extern Color convert(const Slic3r::ColorRGBA& c);
// mapping from encoded color to libvgcode::Color
extern Color convert(const std::string& color_str);
// mapping from libvgcode::EGCodeExtrusionRole to Slic3r::GCodeExtrusionRole
extern Slic3r::GCodeExtrusionRole convert(EGCodeExtrusionRole role);
@ -55,12 +58,13 @@ extern ETimeMode convert(const Slic3r::PrintEstimatedStatistics::ETimeMode& mode
extern Slic3r::PrintEstimatedStatistics::ETimeMode convert(const ETimeMode& mode);
// mapping from Slic3r::GCodeProcessorResult to libvgcode::GCodeInputData
extern GCodeInputData convert(const Slic3r::GCodeProcessorResult& result, float travels_radius = DEFAULT_TRAVELS_RADIUS_MM,
float wipes_radius = DEFAULT_WIPES_RADIUS_MM);
extern GCodeInputData convert(const Slic3r::GCodeProcessorResult& result, const std::vector<std::string>& str_tool_colors,
const std::vector<std::string>& str_color_print_colors, const Viewer& viewer);
// mapping from Slic3r::Print to libvgcode::GCodeInputData
extern GCodeInputData convert(const Slic3r::Print& print, const std::vector<std::string>& str_tool_colors,
const std::vector<Slic3r::CustomGCode::Item>& color_print_values, size_t extruders_count);
const std::vector<std::string>& str_color_print_colors, const std::vector<Slic3r::CustomGCode::Item>& color_print_values,
size_t extruders_count);
} // namespace libvgcode