diff options
Diffstat (limited to 'xs/src/libslic3r/GCode/ToolOrdering.cpp')
-rw-r--r-- | xs/src/libslic3r/GCode/ToolOrdering.cpp | 327 |
1 files changed, 314 insertions, 13 deletions
diff --git a/xs/src/libslic3r/GCode/ToolOrdering.cpp b/xs/src/libslic3r/GCode/ToolOrdering.cpp index 271b75ef3..189a94d49 100644 --- a/xs/src/libslic3r/GCode/ToolOrdering.cpp +++ b/xs/src/libslic3r/GCode/ToolOrdering.cpp @@ -15,6 +15,24 @@ namespace Slic3r { + +// Returns true in case that extruder a comes before b (b does not have to be present). False otherwise. +bool LayerTools::is_extruder_order(unsigned int a, unsigned int b) const +{ + if (a==b) + return false; + + for (auto extruder : extruders) { + if (extruder == a) + return true; + if (extruder == b) + return false; + } + + return false; +} + + // For the use case when each object is printed separately // (print.config.complete_objects is true). ToolOrdering::ToolOrdering(const PrintObject &object, unsigned int first_extruder, bool prime_multi_material) @@ -48,11 +66,14 @@ ToolOrdering::ToolOrdering(const PrintObject &object, unsigned int first_extrude // (print.config.complete_objects is false). ToolOrdering::ToolOrdering(const Print &print, unsigned int first_extruder, bool prime_multi_material) { + m_print_config_ptr = &print.config; + + PrintObjectPtrs objects = print.get_printable_objects(); // Initialize the print layers for all objects and all layers. coordf_t object_bottom_z = 0.; { std::vector<coordf_t> zs; - for (auto object : print.objects) { + for (auto object : objects) { zs.reserve(zs.size() + object->layers.size() + object->support_layers.size()); for (auto layer : object->layers) zs.emplace_back(layer->print_z); @@ -65,7 +86,7 @@ ToolOrdering::ToolOrdering(const Print &print, unsigned int first_extruder, bool } // Collect extruders reuqired to print the layers. - for (auto object : print.objects) + for (auto object : objects) this->collect_extruders(*object); // Reorder the extruders to minimize tool switches. @@ -76,9 +97,10 @@ ToolOrdering::ToolOrdering(const Print &print, unsigned int first_extruder, bool this->collect_extruder_statistics(prime_multi_material); } -ToolOrdering::LayerTools& ToolOrdering::tools_for_layer(coordf_t print_z) + +LayerTools& ToolOrdering::tools_for_layer(coordf_t print_z) { - auto it_layer_tools = std::lower_bound(m_layer_tools.begin(), m_layer_tools.end(), ToolOrdering::LayerTools(print_z - EPSILON)); + auto it_layer_tools = std::lower_bound(m_layer_tools.begin(), m_layer_tools.end(), LayerTools(print_z - EPSILON)); assert(it_layer_tools != m_layer_tools.end()); coordf_t dist_min = std::abs(it_layer_tools->print_z - print_z); for (++ it_layer_tools; it_layer_tools != m_layer_tools.end(); ++it_layer_tools) { @@ -102,7 +124,7 @@ void ToolOrdering::initialize_layers(std::vector<coordf_t> &zs) coordf_t zmax = zs[i] + EPSILON; for (; j < zs.size() && zs[j] <= zmax; ++ j) ; // Assign an average print_z to the set of layers with nearly equal print_z. - m_layer_tools.emplace_back(LayerTools(0.5 * (zs[i] + zs[j-1]))); + m_layer_tools.emplace_back(LayerTools(0.5 * (zs[i] + zs[j-1]), m_print_config_ptr)); i = j; } } @@ -134,12 +156,29 @@ void ToolOrdering::collect_extruders(const PrintObject &object) if (layerm == nullptr) continue; const PrintRegion ®ion = *object.print()->regions[region_id]; + if (! layerm->perimeters.entities.empty()) { - layer_tools.extruders.push_back(region.config.perimeter_extruder.value); + bool something_nonoverriddable = true; + + if (m_print_config_ptr) { // in this case complete_objects is false (see ToolOrdering constructors) + something_nonoverriddable = false; + for (const auto& eec : layerm->perimeters.entities) // let's check if there are nonoverriddable entities + if (!layer_tools.wiping_extrusions().is_overriddable(dynamic_cast<const ExtrusionEntityCollection&>(*eec), *m_print_config_ptr, object, region)) { + something_nonoverriddable = true; + break; + } + } + + if (something_nonoverriddable) + layer_tools.extruders.push_back(region.config.perimeter_extruder.value); + layer_tools.has_object = true; } + + bool has_infill = false; bool has_solid_infill = false; + bool something_nonoverriddable = false; for (const ExtrusionEntity *ee : layerm->fills.entities) { // fill represents infill extrusions of a single island. const auto *fill = dynamic_cast<const ExtrusionEntityCollection*>(ee); @@ -148,19 +187,33 @@ void ToolOrdering::collect_extruders(const PrintObject &object) has_solid_infill = true; else if (role != erNone) has_infill = true; + + if (m_print_config_ptr) { + if (!something_nonoverriddable && !layer_tools.wiping_extrusions().is_overriddable(*fill, *m_print_config_ptr, object, region)) + something_nonoverriddable = true; + } + } + + if (something_nonoverriddable || !m_print_config_ptr) + { + if (has_solid_infill) + layer_tools.extruders.push_back(region.config.solid_infill_extruder); + if (has_infill) + layer_tools.extruders.push_back(region.config.infill_extruder); } - if (has_solid_infill) - layer_tools.extruders.push_back(region.config.solid_infill_extruder); - if (has_infill) - layer_tools.extruders.push_back(region.config.infill_extruder); if (has_solid_infill || has_infill) layer_tools.has_object = true; } } - // Sort and remove duplicates - for (LayerTools < : m_layer_tools) - sort_remove_duplicates(lt.extruders); + for (auto& layer : m_layer_tools) { + // Sort and remove duplicates + sort_remove_duplicates(layer.extruders); + + // make sure that there are some tools for each object layer (e.g. tall wiping object will result in empty extruders vector) + if (layer.extruders.empty() && layer.has_object) + layer.extruders.push_back(0); // 0="dontcare" extruder - it will be taken care of in reorder_extruders + } } // Reorder extruders to minimize layer changes. @@ -217,6 +270,8 @@ void ToolOrdering::reorder_extruders(unsigned int last_extruder_id) } } + + void ToolOrdering::fill_wipe_tower_partitions(const PrintConfig &config, coordf_t object_bottom_z) { if (m_layer_tools.empty()) @@ -327,4 +382,250 @@ void ToolOrdering::collect_extruder_statistics(bool prime_multi_material) } } + + +// This function is called from Print::mark_wiping_extrusions and sets extruder this entity should be printed with (-1 .. as usual) +void WipingExtrusions::set_extruder_override(const ExtrusionEntity* entity, unsigned int copy_id, int extruder, unsigned int num_of_copies) +{ + something_overridden = true; + + auto entity_map_it = (entity_map.insert(std::make_pair(entity, std::vector<int>()))).first; // (add and) return iterator + auto& copies_vector = entity_map_it->second; + if (copies_vector.size() < num_of_copies) + copies_vector.resize(num_of_copies, -1); + + if (copies_vector[copy_id] != -1) + std::cout << "ERROR: Entity extruder overriden multiple times!!!\n"; // A debugging message - this must never happen. + + copies_vector[copy_id] = extruder; +} + + +// Finds first non-soluble extruder on the layer +int WipingExtrusions::first_nonsoluble_extruder_on_layer(const PrintConfig& print_config) const +{ + const LayerTools& lt = *m_layer_tools; + for (auto extruders_it = lt.extruders.begin(); extruders_it != lt.extruders.end(); ++extruders_it) + if (!print_config.filament_soluble.get_at(*extruders_it)) + return (*extruders_it); + + return (-1); +} + +// Finds last non-soluble extruder on the layer +int WipingExtrusions::last_nonsoluble_extruder_on_layer(const PrintConfig& print_config) const +{ + const LayerTools& lt = *m_layer_tools; + for (auto extruders_it = lt.extruders.rbegin(); extruders_it != lt.extruders.rend(); ++extruders_it) + if (!print_config.filament_soluble.get_at(*extruders_it)) + return (*extruders_it); + + return (-1); +} + + +// Decides whether this entity could be overridden +bool WipingExtrusions::is_overriddable(const ExtrusionEntityCollection& eec, const PrintConfig& print_config, const PrintObject& object, const PrintRegion& region) const +{ + if (print_config.filament_soluble.get_at(Print::get_extruder(eec, region))) + return false; + + if (object.config.wipe_into_objects) + return true; + + if (!region.config.wipe_into_infill || eec.role() != erInternalInfill) + return false; + + return true; +} + + +// Following function iterates through all extrusions on the layer, remembers those that could be used for wiping after toolchange +// and returns volume that is left to be wiped on the wipe tower. +float WipingExtrusions::mark_wiping_extrusions(const Print& print, unsigned int old_extruder, unsigned int new_extruder, float volume_to_wipe) +{ + const LayerTools& lt = *m_layer_tools; + const float min_infill_volume = 0.f; // ignore infill with smaller volume than this + + if (print.config.filament_soluble.get_at(old_extruder) || print.config.filament_soluble.get_at(new_extruder)) + return volume_to_wipe; // Soluble filament cannot be wiped in a random infill, neither the filament after it + + // we will sort objects so that dedicated for wiping are at the beginning: + PrintObjectPtrs object_list = print.get_printable_objects(); + std::sort(object_list.begin(), object_list.end(), [](const PrintObject* a, const PrintObject* b) { return a->config.wipe_into_objects; }); + + // We will now iterate through + // - first the dedicated objects to mark perimeters or infills (depending on infill_first) + // - second through the dedicated ones again to mark infills or perimeters (depending on infill_first) + // - then all the others to mark infills (in case that !infill_first, we must also check that the perimeter is finished already + // this is controlled by the following variable: + bool perimeters_done = false; + + for (int i=0 ; i<(int)object_list.size() + (perimeters_done ? 0 : 1); ++i) { + if (!perimeters_done && (i==(int)object_list.size() || !object_list[i]->config.wipe_into_objects)) { // we passed the last dedicated object in list + perimeters_done = true; + i=-1; // let's go from the start again + continue; + } + + const auto& object = object_list[i]; + + // Finds this layer: + auto this_layer_it = std::find_if(object->layers.begin(), object->layers.end(), [<](const Layer* lay) { return std::abs(lt.print_z - lay->print_z)<EPSILON; }); + if (this_layer_it == object->layers.end()) + continue; + const Layer* this_layer = *this_layer_it; + unsigned int num_of_copies = object->_shifted_copies.size(); + + for (unsigned int copy = 0; copy < num_of_copies; ++copy) { // iterate through copies first, so that we mark neighbouring infills to minimize travel moves + + for (size_t region_id = 0; region_id < object->print()->regions.size(); ++ region_id) { + const auto& region = *object->print()->regions[region_id]; + + if (!region.config.wipe_into_infill && !object->config.wipe_into_objects) + continue; + + + if ((!print.config.infill_first ? perimeters_done : !perimeters_done) || (!object->config.wipe_into_objects && region.config.wipe_into_infill)) { + for (const ExtrusionEntity* ee : this_layer->regions[region_id]->fills.entities) { // iterate through all infill Collections + auto* fill = dynamic_cast<const ExtrusionEntityCollection*>(ee); + + if (!is_overriddable(*fill, print.config, *object, region)) + continue; + + // What extruder would this normally be printed with? + unsigned int correct_extruder = Print::get_extruder(*fill, region); + + if (volume_to_wipe<=0) + continue; + + if (!object->config.wipe_into_objects && !print.config.infill_first && region.config.wipe_into_infill) + // In this case we must check that the original extruder is used on this layer before the one we are overridding + // (and the perimeters will be finished before the infill is printed): + if (!lt.is_extruder_order(region.config.perimeter_extruder - 1, new_extruder)) + continue; + + if ((!is_entity_overridden(fill, copy) && fill->total_volume() > min_infill_volume)) { // this infill will be used to wipe this extruder + set_extruder_override(fill, copy, new_extruder, num_of_copies); + volume_to_wipe -= fill->total_volume(); + } + } + } + + // Now the same for perimeters - see comments above for explanation: + if (object->config.wipe_into_objects && (print.config.infill_first ? perimeters_done : !perimeters_done)) + { + for (const ExtrusionEntity* ee : this_layer->regions[region_id]->perimeters.entities) { + auto* fill = dynamic_cast<const ExtrusionEntityCollection*>(ee); + if (!is_overriddable(*fill, print.config, *object, region)) + continue; + + if (volume_to_wipe<=0) + continue; + + if ((!is_entity_overridden(fill, copy) && fill->total_volume() > min_infill_volume)) { + set_extruder_override(fill, copy, new_extruder, num_of_copies); + volume_to_wipe -= fill->total_volume(); + } + } + } + } + } + } + return std::max(0.f, volume_to_wipe); +} + + + +// Called after all toolchanges on a layer were mark_infill_overridden. There might still be overridable entities, +// that were not actually overridden. If they are part of a dedicated object, printing them with the extruder +// they were initially assigned to might mean violating the perimeter-infill order. We will therefore go through +// them again and make sure we override it. +void WipingExtrusions::ensure_perimeters_infills_order(const Print& print) +{ + const LayerTools& lt = *m_layer_tools; + unsigned int first_nonsoluble_extruder = first_nonsoluble_extruder_on_layer(print.config); + unsigned int last_nonsoluble_extruder = last_nonsoluble_extruder_on_layer(print.config); + + PrintObjectPtrs printable_objects = print.get_printable_objects(); + for (const PrintObject* object : printable_objects) { + // Finds this layer: + auto this_layer_it = std::find_if(object->layers.begin(), object->layers.end(), [<](const Layer* lay) { return std::abs(lt.print_z - lay->print_z)<EPSILON; }); + if (this_layer_it == object->layers.end()) + continue; + const Layer* this_layer = *this_layer_it; + unsigned int num_of_copies = object->_shifted_copies.size(); + + for (unsigned int copy = 0; copy < num_of_copies; ++copy) { // iterate through copies first, so that we mark neighbouring infills to minimize travel moves + for (size_t region_id = 0; region_id < object->print()->regions.size(); ++ region_id) { + const auto& region = *object->print()->regions[region_id]; + + if (!region.config.wipe_into_infill && !object->config.wipe_into_objects) + continue; + + for (const ExtrusionEntity* ee : this_layer->regions[region_id]->fills.entities) { // iterate through all infill Collections + auto* fill = dynamic_cast<const ExtrusionEntityCollection*>(ee); + + if (!is_overriddable(*fill, print.config, *object, region) + || is_entity_overridden(fill, copy) ) + continue; + + // This infill could have been overridden but was not - unless we do something, it could be + // printed before its perimeter, or not be printed at all (in case its original extruder has + // not been added to LayerTools + // Either way, we will now force-override it with something suitable: + if (print.config.infill_first + || object->config.wipe_into_objects // in this case the perimeter is overridden, so we can override by the last one safely + || lt.is_extruder_order(region.config.perimeter_extruder - 1, last_nonsoluble_extruder // !infill_first, but perimeter is already printed when last extruder prints + || std::find(lt.extruders.begin(), lt.extruders.end(), region.config.infill_extruder - 1) == lt.extruders.end()) // we have to force override - this could violate infill_first (FIXME) + ) + set_extruder_override(fill, copy, (print.config.infill_first ? first_nonsoluble_extruder : last_nonsoluble_extruder), num_of_copies); + else { + // In this case we can (and should) leave it to be printed normally. + // Force overriding would mean it gets printed before its perimeter. + } + } + + // Now the same for perimeters - see comments above for explanation: + for (const ExtrusionEntity* ee : this_layer->regions[region_id]->perimeters.entities) { // iterate through all perimeter Collections + auto* fill = dynamic_cast<const ExtrusionEntityCollection*>(ee); + if (!is_overriddable(*fill, print.config, *object, region) + || is_entity_overridden(fill, copy) ) + continue; + + set_extruder_override(fill, copy, (print.config.infill_first ? last_nonsoluble_extruder : first_nonsoluble_extruder), num_of_copies); + } + } + } + } +} + + + + + + + +// Following function is called from process_layer and returns pointer to vector with information about which extruders should be used for given copy of this entity. +// It first makes sure the pointer is valid (creates the vector if it does not exist) and contains a record for each copy +// It also modifies the vector in place and changes all -1 to correct_extruder_id (at the time the overrides were created, correct extruders were not known, +// so -1 was used as "print as usual". +// The resulting vector has to keep track of which extrusions are the ones that were overridden and which were not. In the extruder is used as overridden, +// its number is saved as it is (zero-based index). Usual extrusions are saved as -number-1 (unfortunately there is no negative zero). +const std::vector<int>* WipingExtrusions::get_extruder_overrides(const ExtrusionEntity* entity, int correct_extruder_id, int num_of_copies) +{ + auto entity_map_it = entity_map.find(entity); + if (entity_map_it == entity_map.end()) + entity_map_it = (entity_map.insert(std::make_pair(entity, std::vector<int>()))).first; + + // Now the entity_map_it should be valid, let's make sure the vector is long enough: + entity_map_it->second.resize(num_of_copies, -1); + + // Each -1 now means "print as usual" - we will replace it with actual extruder id (shifted it so we don't lose that information): + std::replace(entity_map_it->second.begin(), entity_map_it->second.end(), -1, -correct_extruder_id-1); + + return &(entity_map_it->second); +} + + } // namespace Slic3r |