feat(libslic3r): multi-nozzle slicing engine for H2C/A2L

Port BambuStudio's dual-nozzle slicing core: H2C-era config keys, filament-to-nozzle grouping with per-layer dynamic regrouping, filament/nozzle/hotend gcode placeholder vocabulary, multi-nozzle wipe tower pre-heat/pre-cool, the two-pass pre-cooling injector, and corexy farthest-point timelapse.
This commit is contained in:
SoftFever
2026-07-09 00:06:26 +08:00
parent 781ecdc2c1
commit 237ef41b06
36 changed files with 7622 additions and 708 deletions

View File

@@ -347,6 +347,7 @@ static constexpr const char* OTHER_LAYERS_PRINT_SEQUENCE_NUMS_ATTR = "other_laye
static constexpr const char* SPIRAL_VASE_MODE = "spiral_mode";
static constexpr const char* FILAMENT_MAP_MODE_ATTR = "filament_map_mode";
static constexpr const char* FILAMENT_MAP_ATTR = "filament_maps";
static constexpr const char* FILAMENT_VOL_MAP_ATTR = "filament_volume_maps";
static constexpr const char* LIMIT_FILAMENT_MAP_ATTR = "limit_filament_maps";
static constexpr const char* GCODE_FILE_ATTR = "gcode_file";
static constexpr const char* THUMBNAIL_FILE_ATTR = "thumbnail_file";
@@ -699,6 +700,43 @@ void PlateData::parse_filament_info(GCodeProcessorResult *result)
info.id = it->first;
info.used_g = used_filament_g;
info.used_m = used_filament_m;
// Stamp each filament's logical-nozzle assignment onto the saved 3mf so the
// device/monitor can reconstruct it. NOTE: this block runs for the whole fleet, not just
// multi-nozzle prints: reorder_extruders_for_minimum_flush_volume runs unconditionally and
// set_nozzle_group_result stores a (non-null) 1-nozzle result even for a single-extruder print,
// so result->nozzle_group_result is non-null here for X1/P1/A1/H2S too. Saved-3mf byte-identity
// vs the older filament_maps path therefore holds by VALUE-COINCIDENCE, not by skipping: for a
// standard 0.4/0.2/0.6/0.8 nozzle the stamped group_id/nozzle_diameter/volume_type render the same
// bytes as the filament_maps-derived fallback below (empirically confirmed on an X1C 0.4 saved 3mf:
// slice_info.config emits group_id="0" nozzle_diameter="0.40" volume_type="Standard" and
// <nozzle id="0" extruder_id="1" nozzle_diameter="0.4" volume_type="Standard"> == the else-path).
// The one divergence is a non-standard nozzle (e.g. 0.3/0.5): format_diameter_to_str snaps the
// stamped diameter to the nearest of {0.2,0.4,0.6,0.8} (0.5 -> "0.4"), whereas the older path wrote
// the raw config value — a metadata-only carry. No g-code effect; the raw nozzle_diameters plate
// metadata is unchanged.
if (result && result->nozzle_group_result) {
auto nozzles_for_filament = result->nozzle_group_result->get_nozzles_for_filament(it->first);
if (!nozzles_for_filament.empty()) {
info.group_id.reserve(nozzles_for_filament.size());
std::set<double> diameters;
std::set<NozzleVolumeType> volume_types;
for (const auto& nozzle : nozzles_for_filament) {
info.group_id.emplace_back(nozzle.group_id);
diameters.insert(string_to_double_decimal_point(nozzle.diameter));
volume_types.insert(nozzle.volume_type);
}
std::sort(info.group_id.begin(), info.group_id.end());
info.group_id.erase(std::unique(info.group_id.begin(), info.group_id.end()), info.group_id.end());
if (!diameters.empty())
info.nozzle_diameter = *diameters.begin();
if (volume_types.size() > 1)
info.nozzle_volume_type = get_nozzle_volume_type_string(nvtHybrid);
else if (!volume_types.empty())
info.nozzle_volume_type = get_nozzle_volume_type_string(*volume_types.begin());
}
}
auto model_volume_it = ps.model_volumes_per_extruder.find(it->first);
auto support_volume_it = ps.support_volumes_per_extruder.find(it->first);
info.used_for_object = model_volume_it != ps.model_volumes_per_extruder.end() && model_volume_it->second > EPSILON;
@@ -706,6 +744,13 @@ void PlateData::parse_filament_info(GCodeProcessorResult *result)
slice_filaments_info.push_back(info);
}
// Carry the layer-aware grouping result into the plate so the 3mf writer can emit the <nozzle> tags
// and the enable_filament_dynamic_map flag. Only a LayeredNozzleGroupResult (the slicer output) is
// stored; a device-side StaticNozzleGroupResult loaded from a 3mf is not re-serialized here.
auto layered_group_result = std::dynamic_pointer_cast<MultiNozzleUtils::LayeredNozzleGroupResult>(result->nozzle_group_result);
if (layered_group_result)
nozzle_group_result = *layered_group_result;
/* only for test
GCodeProcessorResult::SliceWarning sw;
sw.msg = BED_TEMP_TOO_HIGH_THAN_FILAMENT;
@@ -1283,6 +1328,9 @@ void PlateData::parse_filament_info(GCodeProcessorResult *result)
bool _handle_start_config_warning(const char** attributes, unsigned int num_attributes);
bool _handle_end_config_warning();
bool _handle_start_config_nozzle(const char** attributes, unsigned int num_attributes);
bool _handle_end_config_nozzle();
//BBS: add plater config parse functions
bool _handle_start_config_plater(const char** attributes, unsigned int num_attributes);
bool _handle_end_config_plater();
@@ -1618,8 +1666,10 @@ void PlateData::parse_filament_info(GCodeProcessorResult *result)
plate->is_label_object_enabled = it->second->is_label_object_enabled;
plate->skipped_objects = it->second->skipped_objects;
plate->slice_filaments_info = it->second->slice_filaments_info;
plate->nozzles_info = it->second->nozzles_info;
plate->printer_model_id = it->second->printer_model_id;
plate->nozzle_diameters = it->second->nozzle_diameters;
plate->nozzle_volume_types = it->second->nozzle_volume_types;
plate->filament_maps = it->second->filament_maps;
plate->filament_change_sequence = it->second->filament_change_sequence;
plate->nozzle_change_sequence = it->second->nozzle_change_sequence;
@@ -2289,9 +2339,11 @@ void PlateData::parse_filament_info(GCodeProcessorResult *result)
plate_data_list[it->first-1]->is_support_used = it->second->is_support_used;
plate_data_list[it->first-1]->is_label_object_enabled = it->second->is_label_object_enabled;
plate_data_list[it->first-1]->slice_filaments_info = it->second->slice_filaments_info;
plate_data_list[it->first-1]->nozzles_info = it->second->nozzles_info;
plate_data_list[it->first-1]->skipped_objects = it->second->skipped_objects;
plate_data_list[it->first-1]->printer_model_id = it->second->printer_model_id;
plate_data_list[it->first-1]->nozzle_diameters = it->second->nozzle_diameters;
plate_data_list[it->first-1]->nozzle_volume_types = it->second->nozzle_volume_types;
plate_data_list[it->first-1]->filament_maps = it->second->filament_maps;
plate_data_list[it->first-1]->filament_change_sequence = it->second->filament_change_sequence;
plate_data_list[it->first-1]->nozzle_change_sequence = it->second->nozzle_change_sequence;
@@ -3469,6 +3521,8 @@ void PlateData::parse_filament_info(GCodeProcessorResult *result)
res = _handle_start_config_filament(attributes, num_attributes);
else if (::strcmp(SLICE_WARNING_TAG, name) == 0)
res = _handle_start_config_warning(attributes, num_attributes);
else if (::strcmp(NOZZLE_TAG, name) == 0)
res = _handle_start_config_nozzle(attributes, num_attributes);
else if (::strcmp(ASSEMBLE_TAG, name) == 0)
res = _handle_start_assemble(attributes, num_attributes);
else if (::strcmp(ASSEMBLE_ITEM_TAG, name) == 0)
@@ -3503,6 +3557,8 @@ void PlateData::parse_filament_info(GCodeProcessorResult *result)
res = _handle_end_config_plater();
else if (::strcmp(FILAMENT_TAG, name) == 0)
res = _handle_end_config_filament();
else if (::strcmp(NOZZLE_TAG, name) == 0)
res = _handle_end_config_nozzle();
else if (::strcmp(INSTANCE_TAG, name) == 0)
res = _handle_end_config_plater_instance();
else if (::strcmp(ASSEMBLE_TAG, name) == 0)
@@ -4460,6 +4516,19 @@ void PlateData::parse_filament_info(GCodeProcessorResult *result)
m_curr_plater->config.set_key_value("filament_map", new ConfigOptionInts(filament_map));
}
}
else if (key == FILAMENT_VOL_MAP_ATTR) {
if (m_curr_plater){
auto filament_volume_map = get_vector_from_string(value);
for (size_t idx = 0; idx < filament_volume_map.size(); ++idx) {
// Only Standard(0)/High Flow(1) are currently supported; clamp any higher
// volume-type (TPU High Flow/Hybrid) back to Standard until they are wired in.
if (filament_volume_map[idx] > 1) {
filament_volume_map[idx] = 0;
}
}
m_curr_plater->config.set_key_value("filament_volume_map", new ConfigOptionInts(filament_volume_map));
}
}
else if (key == GCODE_FILE_ATTR)
{
m_curr_plater->gcode_file = value;
@@ -4569,6 +4638,11 @@ void PlateData::parse_filament_info(GCodeProcessorResult *result)
if (m_curr_plater)
m_curr_plater->printer_model_id = value;
}
else if (key == NOZZLE_VOLUME_TYPE_ATTR)
{
if (m_curr_plater)
m_curr_plater->nozzle_volume_types = value;
}
else if (key == NOZZLE_DIAMETERS_ATTR)
{
if (m_curr_plater)
@@ -4622,6 +4696,41 @@ void PlateData::parse_filament_info(GCodeProcessorResult *result)
return true;
}
bool _BBS_3MF_Importer::_handle_start_config_nozzle(const char** attributes, unsigned int num_attributes)
{
// Read the per-plate <nozzle> tags. Older 3mf without <nozzle> tags leave nozzles_info
// empty; load_nozzle_infos_with_compatibility then rebuilds the list from the per-filament
// group_id / filament_map on the device side.
if (m_curr_plater) {
// id="0" extruder_id="1" nozzle_diameter="0.4" volume_type="Standard"
std::string id = bbs_get_attribute_value_string(attributes, num_attributes, "id");
std::string extruder_id = bbs_get_attribute_value_string(attributes, num_attributes, "extruder_id");
std::string nozzle_diameter= bbs_get_attribute_value_string(attributes, num_attributes, "nozzle_diameter");
std::string volume_type = bbs_get_attribute_value_string(attributes, num_attributes, "volume_type");
auto volume_type_str_to_enum = ConfigOptionEnum<NozzleVolumeType>::get_enum_values();
MultiNozzleUtils::NozzleInfo nozzle_info;
nozzle_info.group_id = atoi(id.c_str());
nozzle_info.extruder_id = atoi(extruder_id.c_str()) - 1;
nozzle_info.diameter = nozzle_diameter;
if (volume_type_str_to_enum.count(volume_type))
nozzle_info.volume_type = NozzleVolumeType(volume_type_str_to_enum.at(volume_type));
else
nozzle_info.volume_type = NozzleVolumeType::nvtStandard;
m_curr_plater->nozzles_info.push_back(nozzle_info);
}
return true;
}
bool _BBS_3MF_Importer::_handle_end_config_nozzle()
{
// do nothing
return true;
}
bool _BBS_3MF_Importer::_handle_start_config_warning(const char** attributes, unsigned int num_attributes)
{
if (m_curr_plater) {
@@ -7980,6 +8089,18 @@ void PlateData::parse_filament_info(GCodeProcessorResult *result)
stream << "\"/>\n";
}
ConfigOptionInts* filament_volume_maps_opt = plate_data->config.option<ConfigOptionInts>("filament_volume_map");
if (filament_map_mode_opt != nullptr && filament_volume_maps_opt != nullptr) {
stream << " <" << METADATA_TAG << " " << KEY_ATTR << "=\"" << FILAMENT_VOL_MAP_ATTR << "\" " << VALUE_ATTR << "=\"";
const std::vector<int>& volume_values = filament_volume_maps_opt->values;
for (int i = 0; i < volume_values.size(); ++i) {
stream << volume_values[i];
if (i != (volume_values.size() - 1))
stream << " ";
}
stream << "\"/>\n";
}
if (save_gcode)
stream << " <" << METADATA_TAG << " " << KEY_ATTR << "=\"" << GCODE_FILE_ATTR << "\" " << VALUE_ATTR << "=\"" << std::boolalpha << xml_escape(plate_data->gcode_file) << "\"/>\n";
if (!plate_data->gcode_file.empty()) {
@@ -8119,7 +8240,12 @@ void PlateData::parse_filament_info(GCodeProcessorResult *result)
[](unsigned int filament_id) { return filament_id + 1; });
const std::string plate_key = "plate_" + std::to_string(idx + 1);
sequence_json[plate_key]["sequence"] = filament_sequence;
// Dynamic-map plates write the sequence under "filament_sequence"; every other plate (the
// whole shipping fleet + H2C static mode) keeps the "sequence" key, so the saved 3mf is
// byte-identical to the older format. The reader accepts both.
const bool enable_dynamic_map = plate_data->nozzle_group_result && plate_data->nozzle_group_result->is_support_dynamic_nozzle_map();
const std::string seq_key = enable_dynamic_map ? "filament_sequence" : "sequence";
sequence_json[plate_key][seq_key] = filament_sequence;
sequence_json[plate_key]["nozzle_sequence"] = plate_data->nozzle_change_sequence;
sequence_json[plate_key]["optimal_assignment"] = plate_data->optimal_assignment;
}
@@ -8214,7 +8340,15 @@ void PlateData::parse_filament_info(GCodeProcessorResult *result)
stream << " <" << METADATA_TAG << " " << KEY_ATTR << "=\"" << OUTSIDE_ATTR << "\" " << VALUE_ATTR << "=\"" << std::boolalpha<< plate_data->toolpath_outside << "\"/>\n";
stream << " <" << METADATA_TAG << " " << KEY_ATTR << "=\"" << SUPPORT_USED_ATTR << "\" " << VALUE_ATTR << "=\"" << std::boolalpha<< plate_data->is_support_used << "\"/>\n";
stream << " <" << METADATA_TAG << " " << KEY_ATTR << "=\"" << LABEL_OBJECT_ENABLED_ATTR << "\" " << VALUE_ATTR << "=\"" << std::boolalpha<< plate_data->is_label_object_enabled << "\"/>\n";
stream << " <" << METADATA_TAG << " " << KEY_ATTR << "=\"" << ENABLE_FILAMENT_DYNAMIC_MAP_ATTR << "\" " << VALUE_ATTR << "=\"" << std::boolalpha << false << "\"/>\n";
// Report the plate's dynamic-map state from the grouping result. The result is present
// for the whole fleet (a static single-nozzle result too), so this if-branch is normally
// taken; is_support_dynamic_nozzle_map() is false for any non-dynamic (static /
// single-extruder) result ⇒ byte-identical to the previously hard-coded value. The else
// is a defensive fallback for a missing result.
if (plate_data && plate_data->nozzle_group_result)
stream << " <" << METADATA_TAG << " " << KEY_ATTR << "=\"" << ENABLE_FILAMENT_DYNAMIC_MAP_ATTR << "\" " << VALUE_ATTR << "=\"" << std::boolalpha << plate_data->nozzle_group_result->is_support_dynamic_nozzle_map() << "\"/>\n";
else
stream << " <" << METADATA_TAG << " " << KEY_ATTR << "=\"" << ENABLE_FILAMENT_DYNAMIC_MAP_ATTR << "\" " << VALUE_ATTR << "=\"" << std::boolalpha << false << "\"/>\n";
{
bool has_filament_switcher = config.has("has_filament_switcher") ? config.opt_bool("has_filament_switcher") : false;
stream << " <" << METADATA_TAG << " " << KEY_ATTR << "=\"" << HAS_FILAMENT_SWITCHER_ATTR << "\" " << VALUE_ATTR << "=\"" << std::boolalpha << has_filament_switcher << "\"/>\n";
@@ -8349,12 +8483,27 @@ void PlateData::parse_filament_info(GCodeProcessorResult *result)
stream << " <" << SLICE_WARNING_TAG << " msg=\"" << it->msg << "\" level=\"" << std::to_string(it->level) << "\" error_code =\"" << it->error_code << "\" />\n";
}
for (int nozzle_group_id : used_nozzle_groups) {
stream << " <" << NOZZLE_TAG << " "
<< "id=\"" << nozzle_group_id << "\" "
<< "extruder_id=\"" << nozzle_group_id + 1 << "\" "
<< "nozzle_diameter=\"" << get_nozzle_diameter_str(nozzle_group_id) << "\" "
<< "volume_type=\"" << get_nozzle_volume_type(nozzle_group_id) << "\"/>\n";
// Emit the <nozzle> tags from the grouping result when present. The result is present for
// the whole fleet (see the value-coincidence note in parse_filament_info), so this if-branch
// is normally taken; for single-nozzle-per-extruder prints get_used_nozzles_in_extruder()
// yields one nozzle per used extruder whose serialize() output is byte-identical to the
// filament_maps-derived else-path below for a standard nozzle diameter (a non-standard
// diameter snaps via format_diameter_to_str — the metadata-only carry). Only genuinely
// multi-nozzle prints emit per-nozzle tags the extruder map cannot express. The else
// is a defensive fallback for a missing result.
if (plate_data->nozzle_group_result) {
auto used_nozzle_list = plate_data->nozzle_group_result->get_used_nozzles_in_extruder();
for (auto& used_nozzle : used_nozzle_list) {
stream << " <" << NOZZLE_TAG << " " << used_nozzle.serialize() << "/>\n";
}
} else {
for (int nozzle_group_id : used_nozzle_groups) {
stream << " <" << NOZZLE_TAG << " "
<< "id=\"" << nozzle_group_id << "\" "
<< "extruder_id=\"" << nozzle_group_id + 1 << "\" "
<< "nozzle_diameter=\"" << get_nozzle_diameter_str(nozzle_group_id) << "\" "
<< "volume_type=\"" << get_nozzle_volume_type(nozzle_group_id) << "\"/>\n";
}
}
if (!plate_data->layer_filaments.empty()) {

View File

@@ -73,6 +73,7 @@ struct PlateData
std::map<int, std::pair<int, int>> obj_inst_map;
std::string printer_model_id;
std::string nozzle_diameters;
std::string nozzle_volume_types;
std::string gcode_file;
std::string gcode_file_md5;
std::string thumbnail_file;
@@ -102,6 +103,13 @@ struct PlateData
std::vector<unsigned int> nozzle_change_sequence;
std::vector<int> optimal_assignment;
// Multi-nozzle grouping surface. nozzles_info accumulates the <nozzle> tags read from a
// gcode.3mf; nozzle_group_result is the slicer's per-filament→nozzle assignment carried into the
// saved 3mf metadata (write) and reconstructed on load. Both are empty/nullopt for single-nozzle
// prints, so the saved-3mf output for single-nozzle printers is byte-identical.
std::vector<MultiNozzleUtils::NozzleInfo> nozzles_info;
std::optional<MultiNozzleUtils::LayeredNozzleGroupResult> nozzle_group_result;
// Hexadecimal number,
// the 0th digit corresponds to extruder 1
// the 1th digit corresponds to extruder 2