Files
OrcaSlicer/src/libslic3r/PrintBase.cpp
T
HanifKoh 8a6377f087 Add Missing Includes Across src/libslic3r (#16068)
* Add Missing Includes Across src/libslic3r

Every libslic3r source and header now directly includes the headers declaring what it uses, rather than relying on the precompiled header or transitive includes. Generated with clang-tidy misc-include-cleaner, with libslic3r headers spelled libslic3r/... so they resolve outside the library's private include paths. MultiMaterialSegmentation.hpp, Support/SupportParameters.hpp and Format/STEP.hpp are made self-contained by hand.

* Make the libslic3r Headers Compile on Their Own

Each now includes, or forward-declares, what it uses instead of relying on what its includers happened to include first. Left out: I18N.hpp, which errors on purpose when included from GUI code, and VoxelizeCSGMesh.hpp and SLA/bicubic.h, which nothing includes and which no longer compile at all.

* Add the Includes Missing From the Hand-Fixed libslic3r Headers

clang-tidy would not edit these headers while they failed to compile on their own, so the first pass skipped them. With the headers now self-contained, a second pass adds the rest.

* Keep Windows Setup Ahead of the Added libslic3r Includes

Print.cpp and Thread.cpp open with a _WIN32 block that has to come first; without the precompiled header, Print.cpp otherwise reaches windows.h through OCCT with NONLS defined and boost/regex fails. OpenVDBUtils.cpp and SLA/SupportTreeBuilder.cpp had includes inside #ifndef NOMINMAX, which libslic3r defines on Windows, so those were skipped there. .clang-tidy also ignores the MSVC STL and UCRT internals, Boost.Multiprecision's fwd.hpp and CPython's Windows include directory.

* Re-Add libslic3r Includes After the Clipper2 2.0.1 Migration

Rebasing onto main took main's version of the files the Clipper2 migration rewrote, so their added includes are restored here, along with includes for main's new code. Clipper2's individual headers are now ignored by clang-tidy: they only build the Z variant through clipper2_z.hpp, which defines USINGZ first, so including clipper.core.h and the like directly broke ClipperZUtils.cpp.
2026-10-03 15:31:11 +08:00

172 lines
6.9 KiB
C++

#include "Config.hpp"
#include "Exception.hpp"
#include "PrintBase.hpp"
#include <boost/filesystem.hpp>
#include <boost/filesystem/operations.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/log/trivial.hpp>
#include <cstddef>
#include <string>
#include <vector>
#include <stdexcept>
#include <mutex>
#include <functional>
#include "I18N.hpp"
#include "Model.hpp"
#include "libslic3r.h"
#include "libslic3r_version.h"
#include "PlaceholderParser.hpp"
//! macro used to mark string used at localization,
//! return same string
#define L(s) Slic3r::I18N::translate(s)
namespace Slic3r
{
void PrintTryCancel::operator()()
{
m_print->throw_if_canceled();
}
size_t PrintStateBase::g_last_timestamp = 0;
// Update "scale", "input_filename", "input_filename_base", "first_object_name" placeholders from the current m_objects.
void PrintBase::update_object_placeholders(DynamicConfig &config, const std::string &default_ext) const
{
// get the first input file name
std::string input_file;
std::string first_object_name;
std::vector<std::string> v_scale;
int num_objects = 0;
int num_instances = 0;
for (const ModelObject *model_object : m_model.objects) {
ModelInstance *printable = nullptr;
for (ModelInstance *model_instance : model_object->instances)
if (model_instance->is_printable()) {
printable = model_instance;
++ num_instances;
}
if (printable) {
++ num_objects;
if (num_objects == 1)
first_object_name = model_object->name;
// CHECK_ME -> Is the following correct ?
v_scale.push_back("x:" + boost::lexical_cast<std::string>(printable->get_scaling_factor(X) * 100) +
"% y:" + boost::lexical_cast<std::string>(printable->get_scaling_factor(Y) * 100) +
"% z:" + boost::lexical_cast<std::string>(printable->get_scaling_factor(Z) * 100) + "%");
if (input_file.empty())
input_file = model_object->name.empty() ? model_object->input_file : model_object->name;
}
}
config.set_key_value("num_objects", new ConfigOptionInt(num_objects));
config.set_key_value("num_instances", new ConfigOptionInt(num_instances));
config.set_key_value("scale", new ConfigOptionStrings(v_scale));
config.set_key_value("first_object_name", new ConfigOptionString(first_object_name));
if (! input_file.empty()) {
// get basename with and without suffix
const std::string input_filename = boost::filesystem::path(input_file).filename().string();
const std::string input_filename_base = input_filename.substr(0, input_filename.find_last_of("."));
config.set_key_value("input_filename", new ConfigOptionString(input_filename_base + default_ext));
config.set_key_value("input_filename_base", new ConfigOptionString(input_filename_base));
}
}
// Generate an output file name based on the format template, default extension, and template parameters
// (timestamps, object placeholders derived from the model, current placeholder prameters, print statistics - config_override)
std::string PrintBase::output_filename(const std::string &format, const std::string &default_ext, const std::string &filename_base, const DynamicConfig *config_override) const
{
DynamicConfig cfg;
if (config_override != nullptr)
cfg = *config_override;
cfg.set_key_value("version", new ConfigOptionString(std::string(SoftFever_VERSION)));
PlaceholderParser::update_timestamp(cfg);
PlaceholderParser::update_user_name(cfg);
this->update_object_placeholders(cfg, default_ext);
if (! filename_base.empty()) {
cfg.set_key_value("input_filename", new ConfigOptionString(filename_base + default_ext));
cfg.set_key_value("input_filename_base", new ConfigOptionString(filename_base));
}
try {
boost::filesystem::path filename = format.empty() ?
cfg.opt_string("input_filename_base") + default_ext :
this->placeholder_parser().process(format, 0, &cfg);
if (filename.extension().empty())
filename.replace_extension(default_ext);
return filename.string();
} catch (std::runtime_error &err) {
throw Slic3r::PlaceholderParserError(L("Processing of the filename_format template failed.") + "\n" + err.what());
}
}
std::string PrintBase::output_filepath(const std::string &path, const std::string &filename_base) const
{
// if we were supplied no path, generate an automatic one based on our first object's input file
if (path.empty())
// get the first input file name
return (boost::filesystem::path(m_model.propose_export_file_name_and_path()).parent_path() / this->output_filename(filename_base)).make_preferred().string();
// if we were supplied a directory, use it and append our automatically generated filename
boost::filesystem::path p(path);
if (boost::filesystem::is_directory(p))
return (p / this->output_filename(filename_base)).make_preferred().string();
// if we were supplied a file which is not a directory, use it
return path;
}
//BBS: move set_status from hpp to cpp
void PrintBase::set_status(int percent, const std::string &message, unsigned int flags, int warning_step) const
{
if (m_status_callback)
m_status_callback(SlicingStatus(percent, message, flags, warning_step));
else
BOOST_LOG_TRIVIAL(debug) <<boost::format("Percent %1%: %2%\n")%percent %message.c_str();
}
void PrintBase::status_update_warnings(int step, PrintStateBase::WarningLevel warning_level,
const std::string &message, const PrintObjectBase* print_object, PrintStateBase::SlicingNotificationType message_id)
{
if (this->m_status_callback) {
auto status = print_object ? SlicingStatus(*print_object, step, message, message_id, warning_level) : SlicingStatus(*this, step, message, message_id, warning_level);
m_status_callback(status);
}
else if (! message.empty())
BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << boost::format(", Print warning: %1%\n")% message.c_str();
}
//BBS: add PrintObject id into slicing status
void PrintBase::status_update_warnings(int step, PrintStateBase::WarningLevel warning_level,
const std::string& message, PrintObjectBase &object, PrintStateBase::SlicingNotificationType message_id)
{
//BBS: add object it into slicing status
if (this->m_status_callback) {
m_status_callback(SlicingStatus(object, step, message, message_id, warning_level));
}
else if (!message.empty())
BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << boost::format(", PrintObject warning: %1%\n")% message.c_str();
}
std::mutex& PrintObjectBase::state_mutex(PrintBase *print)
{
return print->state_mutex();
}
std::function<void()> PrintObjectBase::cancel_callback(PrintBase *print)
{
return print->cancel_callback();
}
void PrintObjectBase::status_update_warnings(PrintBase *print, int step, PrintStateBase::WarningLevel warning_level,
const std::string &message, PrintStateBase::SlicingNotificationType message_id)
{
print->status_update_warnings(step, warning_level, message, this, message_id);
}
} // namespace Slic3r