# PrintBase.hpp 核心模块解析
## 一、模块架构总览
PrintBase.hpp定义了3D打印处理的核心抽象层,包含三大核心组件:
1. **PrintBase** - 打印任务抽象基类
2. **PrintState** - 多步骤状态管理模板
3. **PrintObjectBaseWithState** - 打印对象状态模板
模块间协作关系:
```mermaid
graph TD
A[PrintBase] -->|管理| B(PrintState)
A -->|包含| C[PrintObjectBaseWithState]
C -->|依赖| D[具体Print类型]
二、PrintBase 核心逻辑
1. 核心职责
- 模型数据管理(Model对象)
- 动态配置管理(DynamicPrintConfig)
- 多线程任务调度
- 全局状态监控(取消/进度)
2. 关键成员解析
class PrintBase {
// 状态同步原子变量
std::atomic<CancelStatus> m_cancel_status;
// 多线程互斥锁
mutable std::mutex m_state_mutex;
// 回调函数体系
status_callback_type m_status_callback;
cancel_callback_type m_cancel_callback;
// 配置系统
DynamicPrintConfig m_full_print_config;
PlaceholderParser m_placeholder_parser;
};
3. 核心方法流程
状态管理流程
void process() {
set_started(psSkirtBrim);
generate_skirt();
set_done(psSkirtBrim);
if (canceled()) throw CanceledException();
}
多线程同步机制
// 状态设置示例
bool set_started(PrintStepEnum step) {
std::lock_guard<std::mutex> lock(m_state_mutex);
m_state.mark_started(step);
m_cancel_callback(); // 触发取消检查
}
三、PrintState 状态机设计
状态转移图
模板实现要点
template<typename StepType, size_t COUNT>
class PrintState {
StateWithWarnings m_state[COUNT]; // 步骤状态数组
int m_step_active = -1; // 当前激活步骤
// 状态转换方法
bool set_started(StepType step) {
m_state[step].state = STARTED;
m_step_active = step;
}
};
四、PrintObjectBaseWithState 实现
对象级状态管理
template<typename PrintType, typename StepEnum, size_t COUNT>
class PrintObjectBaseWithState : public PrintObjectBase {
PrintState<StepEnum, COUNT> m_state;
PrintType* m_print; // 关联的打印实例
// 步骤管理
bool invalidate_step(StepEnum step) {
m_state.invalidate(step, print()->cancel_callback());
}
};
典型应用场景
// 支撑生成流程
void generate_support() {
obj->set_started(posSupportMaterial);
// 生成支撑结构
obj->set_done(posSupportMaterial);
}
五、关键设计模式
1. 观察者模式实现
// 状态更新通知
void status_update(int percent) {
if (m_status_callback)
m_status_callback(SlicingStatus{percent, "Processing..."});
}
2. 模板方法模式
// 打印步骤处理模板
template<typename T>
void process_steps(T& obj) {
obj.set_started(step1);
// ...处理逻辑...
obj.set_done(step1);
}
六、多线程处理要点
线程安全实现
// 原子操作示例
void cancel() {
m_cancel_status.store(CANCELED, std::memory_order_release);
}
bool is_canceled() const {
return m_cancel_status.load(std::memory_order_acquire) != NOT_CANCELED;
}
锁粒度控制
// 细粒度锁应用
void update_state(StepType step) {
{
std::lock_guard<std::mutex> lock(m_state_mutex);
m_state[step].timestamp = ++g_last_timestamp;
}
// 非临界区操作
notify_observers();
}
七、配置管理系统
动态配置结构
占位符解析流程
void apply_config() {
m_placeholder_parser.apply_config(m_full_print_config);
std::string output = m_placeholder_parser.process("output.gcode");
}
八、异常处理体系
异常类型定义
enum CancelStatus {
NOT_CANCELED,
CANCELED_BY_USER,
CANCELED_INTERNAL
};
class CanceledException : public std::exception {
const char* what() const noexcept override;
};
异常传播机制
void background_process() {
try {
while (!canceled()) {
process_next_step();
}
} catch (const CanceledException&) {
handle_cancel();
}
}
九、性能优化策略
1. 原子操作优化
// 无锁状态检查
bool is_active() const {
return m_step_active.load(std::memory_order_acquire) != -1;
}
2. 惰性计算
void get_results() {
if (!m_results_cached) {
calculate_results();
m_results_cached = true;
}
return m_results;
}
3. 批量状态失效
void invalidate_multiple_steps() {
m_state.invalidate_multiple({psSkirtBrim, psSupport}, cancel_callback);
}
该模块通过精心的状态管理和多线程设计,实现了高效可靠的3D打印处理核心,为上层功能扩展提供了坚实基础。