Code refactoring for Color change implementation

This commit is contained in:
YuSanka 2019-12-17 08:37:50 +01:00
parent 7b1e96fe7f
commit afcc6bbb08
16 changed files with 139 additions and 148 deletions

View file

@ -51,7 +51,7 @@ const std::string MODEL_CONFIG_FILE = "Metadata/Slic3r_PE_model.config";
const std::string LAYER_HEIGHTS_PROFILE_FILE = "Metadata/Slic3r_PE_layer_heights_profile.txt";
const std::string LAYER_CONFIG_RANGES_FILE = "Metadata/Prusa_Slicer_layer_config_ranges.xml";
const std::string SLA_SUPPORT_POINTS_FILE = "Metadata/Slic3r_PE_sla_support_points.txt";
const std::string CUSTOM_GCODE_PER_HEIGHT_FILE = "Metadata/Prusa_Slicer_custom_gcode_per_height.xml";
const std::string CUSTOM_GCODE_PER_PRINT_Z_FILE = "Metadata/Prusa_Slicer_custom_gcode_per_print_z.xml";
const char* MODEL_TAG = "model";
const char* RESOURCES_TAG = "resources";
@ -418,7 +418,7 @@ namespace Slic3r {
void _extract_layer_config_ranges_from_archive(mz_zip_archive& archive, const mz_zip_archive_file_stat& stat);
void _extract_sla_support_points_from_archive(mz_zip_archive& archive, const mz_zip_archive_file_stat& stat);
void _extract_custom_gcode_per_height_from_archive(mz_zip_archive& archive, const mz_zip_archive_file_stat& stat);
void _extract_custom_gcode_per_print_z_from_archive(mz_zip_archive& archive, const mz_zip_archive_file_stat& stat);
void _extract_print_config_from_archive(mz_zip_archive& archive, const mz_zip_archive_file_stat& stat, DynamicPrintConfig& config, const std::string& archive_filename);
bool _extract_model_config_from_archive(mz_zip_archive& archive, const mz_zip_archive_file_stat& stat, Model& model);
@ -629,10 +629,10 @@ namespace Slic3r {
// extract slic3r print config file
_extract_print_config_from_archive(archive, stat, config, filename);
}
if (boost::algorithm::iequals(name, CUSTOM_GCODE_PER_HEIGHT_FILE))
if (boost::algorithm::iequals(name, CUSTOM_GCODE_PER_PRINT_Z_FILE))
{
// extract slic3r layer config ranges file
_extract_custom_gcode_per_height_from_archive(archive, stat);
_extract_custom_gcode_per_print_z_from_archive(archive, stat);
}
else if (boost::algorithm::iequals(name, MODEL_CONFIG_FILE))
{
@ -1064,7 +1064,7 @@ namespace Slic3r {
return true;
}
void _3MF_Importer::_extract_custom_gcode_per_height_from_archive(::mz_zip_archive &archive, const mz_zip_archive_file_stat &stat)
void _3MF_Importer::_extract_custom_gcode_per_print_z_from_archive(::mz_zip_archive &archive, const mz_zip_archive_file_stat &stat)
{
if (stat.m_uncomp_size > 0)
{
@ -1079,24 +1079,23 @@ namespace Slic3r {
pt::ptree main_tree;
pt::read_xml(iss, main_tree);
if (main_tree.front().first != "custom_gcodes_per_height")
if (main_tree.front().first != "custom_gcodes_per_print_z")
return;
pt::ptree code_tree = main_tree.front().second;
if (!m_model->custom_gcode_per_height.empty())
m_model->custom_gcode_per_height.clear();
m_model->custom_gcode_per_print_z.clear();
for (const auto& code : code_tree)
{
if (code.first != "code")
continue;
pt::ptree tree = code.second;
double height = tree.get<double>("<xmlattr>.height");
std::string gcode = tree.get<std::string>("<xmlattr>.gcode");
int extruder = tree.get<int>("<xmlattr>.extruder");
std::string color = tree.get<std::string>("<xmlattr>.color");
double print_z = tree.get<double> ("<xmlattr>.print_z" );
std::string gcode = tree.get<std::string> ("<xmlattr>.gcode" );
int extruder = tree.get<int> ("<xmlattr>.extruder" );
std::string color = tree.get<std::string> ("<xmlattr>.color" );
m_model->custom_gcode_per_height.push_back(Model::CustomGCode(height, gcode, extruder, color)) ;
m_model->custom_gcode_per_print_z.push_back(Model::CustomGCode{print_z, gcode, extruder, color}) ;
}
}
}
@ -1885,7 +1884,7 @@ namespace Slic3r {
bool _add_sla_support_points_file_to_archive(mz_zip_archive& archive, Model& model);
bool _add_print_config_file_to_archive(mz_zip_archive& archive, const DynamicPrintConfig &config);
bool _add_model_config_file_to_archive(mz_zip_archive& archive, const Model& model, const IdToObjectDataMap &objects_data);
bool _add_custom_gcode_per_height_file_to_archive(mz_zip_archive& archive, Model& model);
bool _add_custom_gcode_per_print_z_file_to_archive(mz_zip_archive& archive, Model& model);
};
#if ENABLE_THUMBNAIL_GENERATOR
@ -1988,9 +1987,9 @@ namespace Slic3r {
return false;
}
// Adds custom gcode per height file ("Metadata/Prusa_Slicer_custom_gcode_per_height.xml").
// Adds custom gcode per height file ("Metadata/Prusa_Slicer_custom_gcode_per_print_z.xml").
// All custom gcode per height of whole Model are stored here
if (!_add_custom_gcode_per_height_file_to_archive(archive, model))
if (!_add_custom_gcode_per_print_z_file_to_archive(archive, model))
{
close_zip_writer(&archive);
boost::filesystem::remove(filename);
@ -2567,20 +2566,20 @@ namespace Slic3r {
return true;
}
bool _3MF_Exporter::_add_custom_gcode_per_height_file_to_archive( mz_zip_archive& archive, Model& model)
bool _3MF_Exporter::_add_custom_gcode_per_print_z_file_to_archive( mz_zip_archive& archive, Model& model)
{
std::string out = "";
if (!model.custom_gcode_per_height.empty())
if (!model.custom_gcode_per_print_z.empty())
{
pt::ptree tree;
pt::ptree& main_tree = tree.add("custom_gcodes_per_height", "");
pt::ptree& main_tree = tree.add("custom_gcodes_per_print_z", "");
for (const Model::CustomGCode& code : model.custom_gcode_per_height)
for (const Model::CustomGCode& code : model.custom_gcode_per_print_z)
{
pt::ptree& code_tree = main_tree.add("code", "");
// store minX and maxZ
code_tree.put("<xmlattr>.height" , code.height );
code_tree.put("<xmlattr>.print_z" , code.print_z );
code_tree.put("<xmlattr>.gcode" , code.gcode );
code_tree.put("<xmlattr>.extruder" , code.extruder );
code_tree.put("<xmlattr>.color" , code.color );
@ -2599,9 +2598,9 @@ bool _3MF_Exporter::_add_custom_gcode_per_height_file_to_archive( mz_zip_archive
if (!out.empty())
{
if (!mz_zip_writer_add_mem(&archive, CUSTOM_GCODE_PER_HEIGHT_FILE.c_str(), (const void*)out.data(), out.length(), MZ_DEFAULT_COMPRESSION))
if (!mz_zip_writer_add_mem(&archive, CUSTOM_GCODE_PER_PRINT_Z_FILE.c_str(), (const void*)out.data(), out.length(), MZ_DEFAULT_COMPRESSION))
{
add_error("Unable to add custom Gcodes per height file to archive");
add_error("Unable to add custom Gcodes per print_z file to archive");
return false;
}
}

View file

@ -637,7 +637,7 @@ void AMFParserContext::endElement(const char * /* name */)
int extruder = atoi(m_value[2].c_str());
const std::string& color = m_value[3];
m_model.custom_gcode_per_height.push_back(Model::CustomGCode(height, gcode, extruder, color));
m_model.custom_gcode_per_print_z.push_back(Model::CustomGCode{height, gcode, extruder, color});
for (std::string& val: m_value)
val.clear();
@ -1221,21 +1221,21 @@ bool store_amf(const char *path, Model *model, const DynamicPrintConfig *config)
stream << " </constellation>\n";
}
if (!model->custom_gcode_per_height.empty())
if (!model->custom_gcode_per_print_z.empty())
{
std::string out = "";
pt::ptree tree;
pt::ptree& main_tree = tree.add("custom_gcodes_per_height", "");
for (const Model::CustomGCode& code : model->custom_gcode_per_height)
for (const Model::CustomGCode& code : model->custom_gcode_per_print_z)
{
pt::ptree& code_tree = main_tree.add("code", "");
// store minX and maxZ
code_tree.put("<xmlattr>.height", code.height);
code_tree.put("<xmlattr>.gcode", code.gcode);
code_tree.put("<xmlattr>.extruder", code.extruder);
code_tree.put("<xmlattr>.color", code.color);
code_tree.put("<xmlattr>.print_z" , code.print_z );
code_tree.put("<xmlattr>.gcode" , code.gcode );
code_tree.put("<xmlattr>.extruder" , code.extruder );
code_tree.put("<xmlattr>.color" , code.color );
}
if (!tree.empty())

View file

@ -936,7 +936,7 @@ void GCode::_do_export(Print& print, FILE* file)
// Initialize custom gcode
Model* model = print.get_object(0)->model_object()->get_model();
m_custom_g_code_heights = model->custom_gcode_per_height;
m_custom_gcode_per_print_z = model->custom_gcode_per_print_z;
// Initialize autospeed.
{
@ -1125,19 +1125,19 @@ void GCode::_do_export(Print& print, FILE* file)
print.throw_if_canceled();
/* To avoid change filament for non-used extruder for Multi-material,
* check model->custom_gcode_per_height using tool_ordering values
* check model->custom_gcode_per_print_z using tool_ordering values
* */
if (!m_custom_g_code_heights. empty())
if (!m_custom_gcode_per_print_z. empty())
{
bool delete_executed = false;
auto it = m_custom_g_code_heights.end();
while (it != m_custom_g_code_heights.begin())
auto it = m_custom_gcode_per_print_z.end();
while (it != m_custom_gcode_per_print_z.begin())
{
--it;
if (it->gcode != ColorChangeCode)
continue;
auto it_layer_tools = std::lower_bound(tool_ordering.begin(), tool_ordering.end(), LayerTools(it->height));
auto it_layer_tools = std::lower_bound(tool_ordering.begin(), tool_ordering.end(), LayerTools(it->print_z));
bool used_extruder = false;
for (; it_layer_tools != tool_ordering.end(); it_layer_tools++)
@ -1154,14 +1154,14 @@ void GCode::_do_export(Print& print, FILE* file)
/* If we are there, current extruder wouldn't be used,
* so this color change is a redundant move.
* Delete this item from m_custom_g_code_heights
* Delete this item from m_custom_gcode_per_print_z
* */
it = m_custom_g_code_heights.erase(it);
it = m_custom_gcode_per_print_z.erase(it);
delete_executed = true;
}
if (delete_executed)
model->custom_gcode_per_height = m_custom_g_code_heights;
model->custom_gcode_per_print_z = m_custom_gcode_per_print_z;
}
@ -1461,7 +1461,7 @@ void GCode::_do_export(Print& print, FILE* file)
dst.first += buf;
++ dst.second;
};
print.m_print_statistics.filament_stats.insert(std::pair<size_t, float>(extruder.id(), (float)used_filament));
print.m_print_statistics.filament_stats.insert(std::pair<size_t, float>{extruder.id(), (float)used_filament});
append(out_filament_used_mm, "%.1lf", used_filament);
append(out_filament_used_cm3, "%.1lf", extruded_volume * 0.001);
if (filament_weight > 0.) {
@ -1835,15 +1835,15 @@ void GCode::process_layer(
std::string custom_code = "";
std::string pause_print_msg = "";
int m600_before_extruder = -1;
while (!m_custom_g_code_heights.empty() && m_custom_g_code_heights.front().height-EPSILON < layer.print_z) {
custom_code = m_custom_g_code_heights.front().gcode;
while (!m_custom_gcode_per_print_z.empty() && m_custom_gcode_per_print_z.front().print_z - EPSILON < layer.print_z) {
custom_code = m_custom_gcode_per_print_z.front().gcode;
if (custom_code == ColorChangeCode && m_custom_g_code_heights.front().extruder > 0)
m600_before_extruder = m_custom_g_code_heights.front().extruder - 1;
if (custom_code == ColorChangeCode && m_custom_gcode_per_print_z.front().extruder > 0)
m600_before_extruder = m_custom_gcode_per_print_z.front().extruder - 1;
if (custom_code == PausePrintCode)
pause_print_msg = m_custom_g_code_heights.front().color;
pause_print_msg = m_custom_gcode_per_print_z.front().color;
m_custom_g_code_heights.erase(m_custom_g_code_heights.begin());
m_custom_gcode_per_print_z.erase(m_custom_gcode_per_print_z.begin());
colorprint_change = true;
}

View file

@ -367,7 +367,7 @@ protected:
* Updated before the export and erased during the process,
* so no toolchange occurs twice.
* */
std::vector<Model::CustomGCode> m_custom_g_code_heights;
std::vector<Model::CustomGCode> m_custom_gcode_per_print_z;
// Time estimators
GCodeTimeEstimator m_normal_time_estimator;

View file

@ -10,6 +10,11 @@
namespace Slic3r {
// Additional Codes which can be set by user using DoubleSlider
static constexpr char ColorChangeCode[] = "M600";
static constexpr char PausePrintCode[] = "M601";
static constexpr char ExtruderChangeCode[] = "tool_change";
class GCodeWriter {
public:
GCodeConfig config;

View file

@ -18,6 +18,7 @@
#include "SVG.hpp"
#include <Eigen/Dense>
#include "GCodeWriter.hpp"
namespace Slic3r {
@ -43,7 +44,7 @@ Model& Model::assign_copy(const Model &rhs)
}
// copy custom code per height
this->custom_gcode_per_height = rhs.custom_gcode_per_height;
this->custom_gcode_per_print_z = rhs.custom_gcode_per_print_z;
return *this;
}
@ -64,7 +65,7 @@ Model& Model::assign_copy(Model &&rhs)
rhs.objects.clear();
// copy custom code per height
this->custom_gcode_per_height = rhs.custom_gcode_per_height;
this->custom_gcode_per_print_z = rhs.custom_gcode_per_print_z;
return *this;
}
@ -595,16 +596,15 @@ std::string Model::propose_export_file_name_and_path(const std::string &new_exte
std::vector<std::pair<double, DynamicPrintConfig>> Model::get_custom_tool_changes(double default_layer_height, size_t num_extruders) const
{
std::vector<std::pair<double, DynamicPrintConfig>> custom_tool_changes;
if (!custom_gcode_per_height.empty()) {
for (const CustomGCode& custom_gcode : custom_gcode_per_height)
if (custom_gcode.gcode == ExtruderChangeCode) {
DynamicPrintConfig config;
// If extruder count in PrinterSettings was changed, use default (0) extruder for extruders, more than num_extruders
config.set_key_value("extruder", new ConfigOptionInt(custom_gcode.extruder > num_extruders ? 0 : custom_gcode.extruder));
// For correct extruders(tools) changing, we should decrease custom_gcode.height value by one default layer height
custom_tool_changes.push_back({ custom_gcode.height - default_layer_height, config });
}
}
for (const CustomGCode& custom_gcode : custom_gcode_per_print_z)
if (custom_gcode.gcode == ExtruderChangeCode) {
DynamicPrintConfig config;
// If extruder count in PrinterSettings was changed, use default (0) extruder for extruders, more than num_extruders
config.set_key_value("extruder", new ConfigOptionInt(custom_gcode.extruder > num_extruders ? 0 : custom_gcode.extruder));
// For correct extruders(tools) changing, we should decrease custom_gcode.height value by one default layer height
custom_tool_changes.push_back({ custom_gcode.print_z - default_layer_height, config });
}
return custom_tool_changes;
}

View file

@ -749,33 +749,30 @@ public:
// Extensions for color print
struct CustomGCode
{
CustomGCode(double height, const std::string& code, int extruder, const std::string& color) :
height(height), gcode(code), extruder(extruder), color(color) {}
bool operator<(const CustomGCode& other) const { return other.height > this->height; }
bool operator<(const CustomGCode& other) const { return other.print_z > this->print_z; }
bool operator==(const CustomGCode& other) const
{
return (other.height == this->height) &&
(other.gcode == this->gcode) &&
(other.extruder == this->extruder )&&
(other.color == this->color );
return (other.print_z == this->print_z ) &&
(other.gcode == this->gcode ) &&
(other.extruder == this->extruder ) &&
(other.color == this->color );
}
bool operator!=(const CustomGCode& other) const
{
return (other.height != this->height) ||
(other.gcode != this->gcode) ||
(other.extruder != this->extruder )||
(other.color != this->color );
return (other.print_z != this->print_z ) ||
(other.gcode != this->gcode ) ||
(other.extruder != this->extruder ) ||
(other.color != this->color );
}
double height;
double print_z;
std::string gcode;
int extruder; // 0 - "gcode" will be applied for whole print
// else - "gcode" will be applied only for "extruder" print
std::string color; // if gcode is equal to PausePrintCode,
// this field is used for save a short message shown on Printer display
};
std::vector<CustomGCode> custom_gcode_per_height;
std::vector<CustomGCode> custom_gcode_per_print_z;
// Default constructor assigns a new ID to the model.
Model() { assert(this->id().valid()); }
@ -841,7 +838,7 @@ public:
// Propose an output path, replace extension. The new_extension shall contain the initial dot.
std::string propose_export_file_name_and_path(const std::string &new_extension) const;
// from custom_gcode_per_height get just tool_change codes
// from custom_gcode_per_print_z get just tool_change codes
std::vector<std::pair<double, DynamicPrintConfig>> get_custom_tool_changes(double default_layer_height, size_t num_extruders) const;
private:

View file

@ -739,10 +739,10 @@ Print::ApplyStatus Print::apply(const Model &model, DynamicPrintConfig new_full_
model_object_status.emplace(model_object->id(), ModelObjectStatus::Old);
// But if custom gcode per layer height was changed
if (m_model.custom_gcode_per_height != model.custom_gcode_per_height) {
if (m_model.custom_gcode_per_print_z != model.custom_gcode_per_print_z) {
// we should stop background processing
update_apply_status(this->invalidate_step(psGCodeExport));
m_model.custom_gcode_per_height = model.custom_gcode_per_height;
m_model.custom_gcode_per_print_z = model.custom_gcode_per_print_z;
}
} else if (model_object_list_extended(m_model, model)) {
// Add new objects. Their volumes and configs will be synchronized later.

View file

@ -71,12 +71,6 @@ enum SLAPillarConnectionMode {
slapcmDynamic
};
// ys_FIXME ! may be, it's not a best place
// Additional Codes which can be set by user using DoubleSlider
static const std::string ColorChangeCode = "M600";
static const std::string PausePrintCode = "M601";
static const std::string ExtruderChangeCode = "tool_change";
template<> inline const t_config_enum_values& ConfigOptionEnum<PrinterTechnology>::get_enum_values() {
static t_config_enum_values keys_map;
if (keys_map.empty()) {