ENH: support recommended filament map when print by object
1. support recommended filament map when print by object 2. placeholder_parser support function filament_change 3. extruder_id of filament_map is start from 1 Change-Id: Ide8019cd4a165a25972f22706ff685c3005aa031
This commit is contained in:
parent
b08ed80f82
commit
b42d94e1d0
|
@ -22,7 +22,7 @@ unsigned int Extruder::extruder_id() const
|
|||
{
|
||||
assert(m_config);
|
||||
if (m_id < m_config->filament_map.size()) {
|
||||
return m_config->filament_map.get_at(m_id);
|
||||
return m_config->filament_map.get_at(m_id) - 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -2390,12 +2390,8 @@ size_t GCode::cur_extruder_index() const
|
|||
size_t GCode::get_extruder_id(unsigned int filament_id) const
|
||||
{
|
||||
if (m_print) {
|
||||
std::vector<int> filament_maps = m_print->get_filament_maps();
|
||||
if (filament_id < filament_maps.size()) {
|
||||
return filament_maps[filament_id];
|
||||
}
|
||||
return m_print->get_extruder_id(filament_id);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
|
@ -1045,6 +1045,146 @@ std::vector<int> ToolOrdering::get_recommended_filament_maps()
|
|||
return recommended_filament_maps;
|
||||
}
|
||||
|
||||
// for print by object
|
||||
std::vector<int> ToolOrdering::get_recommended_filament_maps(const std::vector<std::vector<unsigned int>> all_layer_filaments, const PrintConfig *print_config)
|
||||
{
|
||||
//if (!print_config && m_print_object_ptr) { print_config = &(m_print_object_ptr->print()->config()); }
|
||||
|
||||
if (!print_config || all_layer_filaments.empty())
|
||||
return std::vector<int>();
|
||||
|
||||
const unsigned int number_of_extruders = (unsigned int) (print_config->filament_colour.values.size() + EPSILON);
|
||||
|
||||
// get flush matrix
|
||||
std::vector<FlushMatrix> nozzle_flush_mtx;
|
||||
size_t nozzle_nums = print_config->nozzle_diameter.values.size();
|
||||
for (size_t nozzle_id = 0; nozzle_id < nozzle_nums; ++nozzle_id) {
|
||||
std::vector<float> flush_matrix(cast<float>(get_flush_volumes_matrix(print_config->flush_volumes_matrix.values, nozzle_id, nozzle_nums)));
|
||||
std::vector<std::vector<float>> wipe_volumes;
|
||||
for (unsigned int i = 0; i < number_of_extruders; ++i)
|
||||
wipe_volumes.push_back(std::vector<float>(flush_matrix.begin() + i * number_of_extruders, flush_matrix.begin() + (i + 1) * number_of_extruders));
|
||||
|
||||
nozzle_flush_mtx.emplace_back(wipe_volumes);
|
||||
}
|
||||
|
||||
auto extruders_to_hash_key = [](const std::vector<unsigned int> &extruders, std::optional<unsigned int> initial_extruder_id) -> uint32_t {
|
||||
uint32_t hash_key = 0;
|
||||
// high 16 bit define initial extruder ,low 16 bit define extruder set
|
||||
if (initial_extruder_id) hash_key |= (1 << (16 + *initial_extruder_id));
|
||||
for (auto item : extruders) hash_key |= (1 << item);
|
||||
return hash_key;
|
||||
};
|
||||
|
||||
std::vector<LayerPrintSequence> other_layers_seqs;
|
||||
const ConfigOptionInts * other_layers_print_sequence_op = print_config->option<ConfigOptionInts>("other_layers_print_sequence");
|
||||
const ConfigOptionInt * other_layers_print_sequence_nums_op = print_config->option<ConfigOptionInt>("other_layers_print_sequence_nums");
|
||||
if (other_layers_print_sequence_op && other_layers_print_sequence_nums_op) {
|
||||
const std::vector<int> &print_sequence = other_layers_print_sequence_op->values;
|
||||
int sequence_nums = other_layers_print_sequence_nums_op->value;
|
||||
other_layers_seqs = get_other_layers_print_sequence(sequence_nums, print_sequence);
|
||||
}
|
||||
|
||||
// other_layers_seq: the layer_idx and extruder_idx are base on 1
|
||||
auto get_custom_seq = [&other_layers_seqs](int layer_idx, std::vector<int> &out_seq) -> bool {
|
||||
for (size_t idx = other_layers_seqs.size() - 1; idx != size_t(-1); --idx) {
|
||||
const auto &other_layers_seq = other_layers_seqs[idx];
|
||||
if (layer_idx + 1 >= other_layers_seq.first.first && layer_idx + 1 <= other_layers_seq.first.second) {
|
||||
out_seq = other_layers_seq.second;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
std::set<unsigned int> filaments;
|
||||
for (int i = 0; i < all_layer_filaments.size(); ++i) {
|
||||
for (unsigned int filament : all_layer_filaments[i])
|
||||
filaments.insert(filament);
|
||||
}
|
||||
|
||||
auto extruder_group = generate_combinations(std::vector<unsigned int>(filaments.begin(), filaments.end()));
|
||||
|
||||
std::vector<int> recommended_filament_maps;
|
||||
float min_flush_volume = std::numeric_limits<float>::max();
|
||||
for (auto iter = extruder_group.begin(); iter != extruder_group.end(); ++iter) {
|
||||
std::vector<int> filament_maps;
|
||||
filament_maps.resize(number_of_extruders);
|
||||
for (unsigned int e : iter->first) {
|
||||
filament_maps[e] = 0;
|
||||
}
|
||||
for (unsigned int e : iter->second) {
|
||||
filament_maps[e] = 1;
|
||||
}
|
||||
|
||||
std::optional<unsigned int> current_extruder_id;
|
||||
std::vector<std::optional<unsigned int>> nozzle_to_cur_filaments;
|
||||
nozzle_to_cur_filaments.resize(nozzle_nums);
|
||||
|
||||
float flush_volume_cost = 0;
|
||||
for (int i = 0; i < all_layer_filaments.size(); ++i) {
|
||||
std::vector<unsigned int> layer_filaments = all_layer_filaments[i];
|
||||
if (layer_filaments.empty())
|
||||
continue;
|
||||
|
||||
std::vector<int> custom_extruder_seq;
|
||||
if (get_custom_seq(i, custom_extruder_seq) && !custom_extruder_seq.empty()) {
|
||||
std::vector<unsigned int> unsign_custom_extruder_seq;
|
||||
for (int extruder : custom_extruder_seq) {
|
||||
unsigned int unsign_extruder = static_cast<unsigned int>(extruder) - 1;
|
||||
auto it = std::find(layer_filaments.begin(), layer_filaments.end(), unsign_extruder);
|
||||
if (it != layer_filaments.end()) {
|
||||
unsign_custom_extruder_seq.emplace_back(unsign_extruder);
|
||||
nozzle_to_cur_filaments[filament_maps[unsign_extruder]] = unsign_extruder;
|
||||
}
|
||||
}
|
||||
|
||||
layer_filaments = unsign_custom_extruder_seq;
|
||||
current_extruder_id = layer_filaments.back();
|
||||
flush_volume_cost += get_flush_volume(filament_maps, layer_filaments, nozzle_flush_mtx, nozzle_nums);
|
||||
continue;
|
||||
}
|
||||
|
||||
// The algorithm complexity is O(n2*2^n)
|
||||
if (i != 0) {
|
||||
std::vector<std::vector<unsigned int>> nozzle_filaments;
|
||||
nozzle_filaments.resize(nozzle_nums);
|
||||
|
||||
for (unsigned int filament_id : layer_filaments)
|
||||
{
|
||||
nozzle_filaments[filament_maps[filament_id]].emplace_back(filament_id);
|
||||
}
|
||||
|
||||
for (size_t nozzle_id = 0; nozzle_id < nozzle_nums; ++nozzle_id) {
|
||||
float f_cost = 0;
|
||||
nozzle_filaments[nozzle_id] = get_extruders_order(nozzle_flush_mtx[nozzle_id], nozzle_filaments[nozzle_id], nozzle_to_cur_filaments[nozzle_id], &f_cost);
|
||||
std::vector<uint8_t> hash_val;
|
||||
hash_val.reserve(nozzle_filaments[nozzle_id].size());
|
||||
for (auto item : nozzle_filaments[nozzle_id]) hash_val.emplace_back(static_cast<uint8_t>(item));
|
||||
flush_volume_cost += f_cost;
|
||||
nozzle_to_cur_filaments[nozzle_id] = nozzle_filaments[nozzle_id].back();
|
||||
}
|
||||
|
||||
//layer_filaments.clear();
|
||||
//for (size_t nozzle_id = 0; nozzle_id < nozzle_nums; ++nozzle_id) {
|
||||
// layer_filaments.insert(layer_filaments.end(), nozzle_filaments[nozzle_id].begin(), nozzle_filaments[nozzle_id].end());
|
||||
//}
|
||||
}
|
||||
current_extruder_id = layer_filaments.back();
|
||||
}
|
||||
|
||||
if (flush_volume_cost == 0) {
|
||||
recommended_filament_maps = filament_maps;
|
||||
break;
|
||||
}
|
||||
|
||||
if (flush_volume_cost < min_flush_volume) {
|
||||
min_flush_volume = flush_volume_cost;
|
||||
recommended_filament_maps = filament_maps;
|
||||
}
|
||||
}
|
||||
return recommended_filament_maps;
|
||||
}
|
||||
|
||||
void ToolOrdering::reorder_extruders_for_minimum_flush_volume()
|
||||
{
|
||||
const PrintConfig *print_config = m_print_config_ptr;
|
||||
|
@ -1057,12 +1197,19 @@ void ToolOrdering::reorder_extruders_for_minimum_flush_volume()
|
|||
|
||||
size_t nozzle_nums = print_config->nozzle_diameter.values.size();
|
||||
if (nozzle_nums > 1) {
|
||||
std::vector<int> filament_maps = get_recommended_filament_maps();
|
||||
if (filament_maps.empty()) // multi-extruder and one-color
|
||||
return;
|
||||
std::vector<int> filament_maps = m_print->get_filament_maps();
|
||||
|
||||
if (print_config->print_sequence != PrintSequence::ByObject) {
|
||||
filament_maps = get_recommended_filament_maps();
|
||||
if (filament_maps.empty()) // multi-extruder and one-color
|
||||
return;
|
||||
|
||||
std::transform(filament_maps.begin(), filament_maps.end(), filament_maps.begin(), [](int value) { return value + 1; });
|
||||
m_print->update_filament_maps_to_config(filament_maps);
|
||||
}
|
||||
|
||||
std::transform(filament_maps.begin(), filament_maps.end(), filament_maps.begin(), [](int value) { return value - 1; });
|
||||
reorder_extruders_for_minimum_flush_volume_multi_extruder(filament_maps);
|
||||
m_print->update_filament_maps_to_config(filament_maps);
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
|
@ -187,6 +187,8 @@ public:
|
|||
std::vector<LayerTools>& layer_tools() { return m_layer_tools; }
|
||||
bool has_wipe_tower() const { return ! m_layer_tools.empty() && m_first_printing_extruder != (unsigned int)-1 && m_layer_tools.front().has_wipe_tower; }
|
||||
|
||||
static std::vector<int> get_recommended_filament_maps(const std::vector<std::vector<unsigned int>> all_layer_filaments, const PrintConfig *print_config);
|
||||
|
||||
private:
|
||||
void initialize_layers(std::vector<coordf_t> &zs);
|
||||
void collect_extruders(const PrintObject &object, const std::vector<std::pair<double, unsigned int>> &per_layer_extruder_switches);
|
||||
|
@ -196,7 +198,7 @@ private:
|
|||
void fill_wipe_tower_partitions(const PrintConfig &config, coordf_t object_bottom_z, coordf_t max_layer_height);
|
||||
void mark_skirt_layers(const PrintConfig &config, coordf_t max_layer_height);
|
||||
void collect_extruder_statistics(bool prime_multi_material);
|
||||
std::vector<int> ToolOrdering::get_recommended_filament_maps();
|
||||
std::vector<int> get_recommended_filament_maps();
|
||||
void reorder_extruders_for_minimum_flush_volume();
|
||||
void reorder_extruders_for_minimum_flush_volume_multi_extruder(const std::vector<int> &filament_maps);
|
||||
|
||||
|
|
|
@ -401,11 +401,7 @@ coordf_t Layer::get_sparse_infill_max_void_area()
|
|||
|
||||
size_t Layer::get_extruder_id(unsigned int filament_id) const
|
||||
{
|
||||
std::vector<int> filament_map = m_object->print()->get_filament_maps();
|
||||
if (filament_id < filament_map.size()) {
|
||||
return filament_map[filament_id];
|
||||
}
|
||||
return 0;
|
||||
return m_object->print()->get_extruder_id(filament_id);
|
||||
}
|
||||
|
||||
BoundingBox get_extents(const LayerRegion &layer_region)
|
||||
|
|
|
@ -971,6 +971,13 @@ namespace client
|
|||
expr<Iterator>::random(param1, param2, ctx->context_data->rng);
|
||||
}
|
||||
|
||||
template<typename Iterator>
|
||||
static void filament_change(const MyContext* ctx, expr<Iterator>& param)
|
||||
{
|
||||
MyContext *context = const_cast<MyContext *>(ctx);
|
||||
context->current_extruder_id = param.as_i();
|
||||
}
|
||||
|
||||
template <typename Iterator>
|
||||
static void throw_exception(const std::string &msg, const boost::iterator_range<Iterator> &it_range)
|
||||
{
|
||||
|
@ -1330,6 +1337,7 @@ namespace client
|
|||
[ px::bind(&expr<Iterator>::max, _val, _2) ]
|
||||
| (kw["random"] > '(' > conditional_expression(_r1) [_val = _1] > ',' > conditional_expression(_r1) > ')')
|
||||
[ px::bind(&MyContext::random<Iterator>, _r1, _val, _2) ]
|
||||
| (kw["filament_change"] > '(' > conditional_expression(_r1) > ')') [ px::bind(&MyContext::filament_change<Iterator>, _r1, _1) ]
|
||||
| (kw["digits"] > '(' > conditional_expression(_r1) [_val = _1] > ',' > conditional_expression(_r1) > optional_parameter(_r1))
|
||||
[ px::bind(&expr<Iterator>::template digits<false>, _val, _2, _3) ]
|
||||
| (kw["zdigits"] > '(' > conditional_expression(_r1) [_val = _1] > ',' > conditional_expression(_r1) > optional_parameter(_r1))
|
||||
|
@ -1380,6 +1388,7 @@ namespace client
|
|||
("min")
|
||||
("max")
|
||||
("random")
|
||||
("filament_change")
|
||||
("round")
|
||||
("not")
|
||||
("or")
|
||||
|
|
|
@ -1846,6 +1846,21 @@ void Print::process(std::unordered_map<std::string, long long>* slice_time, bool
|
|||
if (this->config().print_sequence == PrintSequence::ByObject) {
|
||||
// Order object instances for sequential print.
|
||||
print_object_instances_ordering = sort_object_instances_by_model_order(*this);
|
||||
|
||||
// get recommended filament map
|
||||
{
|
||||
std::vector<std::vector<unsigned int>> all_filaments;
|
||||
print_object_instance_sequential_active = print_object_instances_ordering.begin();
|
||||
for (; print_object_instance_sequential_active != print_object_instances_ordering.end(); ++print_object_instance_sequential_active) {
|
||||
tool_ordering = ToolOrdering(*(*print_object_instance_sequential_active)->print_object, initial_extruder_id);
|
||||
for (const auto &layer_tool : tool_ordering.layer_tools()) { all_filaments.emplace_back(layer_tool.extruders); }
|
||||
}
|
||||
|
||||
std::vector<int> recomended_maps = ToolOrdering::get_recommended_filament_maps(all_filaments, &config());
|
||||
std::transform(recomended_maps.begin(), recomended_maps.end(), recomended_maps.begin(), [](int value) { return value + 1; });
|
||||
update_filament_maps_to_config(recomended_maps);
|
||||
}
|
||||
|
||||
// print_object_instances_ordering = sort_object_instances_by_max_z(print);
|
||||
print_object_instance_sequential_active = print_object_instances_ordering.begin();
|
||||
for (; print_object_instance_sequential_active != print_object_instances_ordering.end(); ++print_object_instance_sequential_active) {
|
||||
|
@ -2326,7 +2341,7 @@ size_t Print::get_extruder_id(unsigned int filament_id) const
|
|||
{
|
||||
std::vector<int> filament_map = get_filament_maps();
|
||||
if (filament_id < filament_map.size()) {
|
||||
return filament_map[filament_id];
|
||||
return filament_map[filament_id] - 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue