GCode Preview - Customizable extrusion role colors by editing 3DPreview.pm

This commit is contained in:
Enrico Turri 2018-02-13 13:16:23 +01:00
parent 3f006dc11a
commit f4522cd2fc
6 changed files with 87 additions and 0 deletions

View file

@ -233,6 +233,24 @@ sub new {
# init canvas
$self->print($print);
# sets colors for gcode preview extrusion roles
my @extrusion_roles_colors = (
'Perimeter' => 'FF0000',
'External perimeter' => '00FF00',
'Overhang perimeter' => '0000FF',
'Internal infill' => 'FFFF00',
'Solid infill' => 'FF00FF',
'Top solid infill' => '00FFFF',
'Bridge infill' => '7F7F7F',
'Gap fill' => 'FFFFFF',
'Skirt' => '7F0000',
'Support material' => '007F00',
'Support material interface' => '00007F',
'Wipe tower' => 'B3E3AB',
);
$self->print->set_gcode_extrusion_paths_colors(\@extrusion_roles_colors);
$self->reload_print;
return $self;

View file

@ -347,6 +347,18 @@ const GCodeAnalyzer::PreviewData::Color& GCodeAnalyzer::PreviewData::get_extrusi
return extrusion.ranges.feedrate.get_color_at(feedrate);
}
void GCodeAnalyzer::PreviewData::set_extrusion_role_color(const std::string& role_name, float red, float green, float blue, float alpha)
{
for (unsigned int i = 0; i < Extrusion::Num_Extrusion_Roles; ++i)
{
if (role_name == extrusion.role_names[i])
{
extrusion.role_colors[i] = Color(red, green, blue, alpha);
break;
}
}
}
std::string GCodeAnalyzer::PreviewData::get_legend_title() const
{
switch (extrusion.view_type)

View file

@ -279,6 +279,8 @@ public:
const Color& get_extrusion_width_color(float width) const;
const Color& get_extrusion_feedrate_color(float feedrate) const;
void set_extrusion_role_color(const std::string& role_name, float red, float green, float blue, float alpha);
std::string get_legend_title() const;
LegendItemsList get_legend_items(const std::vector<float>& tool_colors) const;
};

View file

@ -107,6 +107,48 @@ void Print::set_gcode_preview_shells_visible(bool visible)
gcode_preview.shell.is_visible = visible;
}
void Print::set_gcode_extrusion_paths_colors(const std::vector<std::string>& colors)
{
unsigned int size = (unsigned int)colors.size();
if (size % 2 != 0)
return;
for (unsigned int i = 0; i < size; i += 2)
{
const std::string& color_str = colors[i + 1];
if (color_str.size() == 6)
{
bool valid = true;
for (int c = 0; c < 6; ++c)
{
if (::isxdigit(color_str[c]) == 0)
{
valid = false;
break;
}
}
if (valid)
{
unsigned int color;
std::stringstream ss;
ss << std::hex << color_str;
ss >> color;
float den = 1.0f / 255.0f;
float r = (float)((color & 0xFF0000) >> 16) * den;
float g = (float)((color & 0x00FF00) >> 8) * den;
float b = (float)(color & 0x0000FF) * den;
gcode_preview.set_extrusion_role_color(colors[i], r, g, b, 1.0f);
}
}
}
}
PrintRegion* Print::add_region()
{
regions.push_back(new PrintRegion(this));

View file

@ -264,6 +264,18 @@ public:
void set_gcode_preview_unretractions_visible(bool visible);
void set_gcode_preview_shells_visible(bool visible);
// Sets the extrusion path colors from the given strings vector.
// Data in the vector should be formatted as follows:
// std::vector<std::string> role_colors =
// { <role_1>, <color_1>,
// <role_2>, <color_2>,
// <role_3>, <color_3>,
// ...
// <role_N>, <color_N> };
// where <role_X> should be a string from GCodeAnalyzer::PreviewData::Extrusion::Default_Extrusion_Role_Names[]
// and <color_X> an RGB color in hex format (i.e. red = FF0000)
void set_gcode_extrusion_paths_colors(const std::vector<std::string>& colors);
// methods for handling regions
PrintRegion* get_region(size_t idx) { return regions.at(idx); }
const PrintRegion* get_region(size_t idx) const { return regions.at(idx); }

View file

@ -172,6 +172,7 @@ _constant()
void set_gcode_preview_retractions_visible(bool visible);
void set_gcode_preview_unretractions_visible(bool visible);
void set_gcode_preview_shells_visible(bool visible);
void set_gcode_extrusion_paths_colors(std::vector<std::string> colors);
PrintRegionPtrs* regions()
%code%{ RETVAL = &THIS->regions; %};