217 lines
8.8 KiB
C++
217 lines
8.8 KiB
C++
#pragma once
|
||
/**
|
||
* @file format_detect.h
|
||
* @brief 公共文件格式探测 API(魔数/容器头优先 + 扩展名回退)—— Phase4C
|
||
*
|
||
* 解决审计 UNKNOWN `detect.unknown_extension`:此前无公共 `detect_format` API,
|
||
* 各导入器自行按扩展名/魔数判定。本 API 提供统一、只读、无异常的文件格式识别:
|
||
* - 输入内存字节(可为空/含 NUL)+ 可选路径提示;
|
||
* - 先按 **魔数/容器头** 识别(STEP/IFC、IGES、DXF、DWG、STL ascii/binary、PLY、
|
||
* glTF(.gltf/.glb)、3MF(ZIP)、PDF、VRML、COLLADA、AMF、XT、SAT/SAB、JT、
|
||
* LAS/LAZ、PTX、E57、VDE、OBJ、G-code、Mesh 容器);识别不到时按扩展名回退;
|
||
* - 返回 `DetectedFormat{format, confidence, reason}`:魔数命中 confidence 接近 1,
|
||
* 扩展名回退 confidence 较低;未知格式返回 `FileFormat::Unknown` + reason,
|
||
* **不抛异常**(文件打不开也只是 Unknown + reason)。
|
||
*
|
||
* 设计约定:
|
||
* - **魔数/容器头优先于扩展名**;两者冲突时保留魔数结果,reason 中记录冲突;
|
||
* - confidence ∈ [0,1],0 表示 Unknown;
|
||
* - 只读取前缀(最多约 2–8 MB,见实现),不修改输入;对 NUL/二进制安全;
|
||
* - 本 API 只做格式识别,不做深度合法性校验(识别成功不保证文件可完整导入)。
|
||
*
|
||
* @ingroup foundation
|
||
*/
|
||
#include <cstdint>
|
||
#include <string>
|
||
#include <vector>
|
||
|
||
namespace vde::io {
|
||
|
||
/// 可识别的文件格式(导入/导出基础设施支持的范围;未知为 Unknown)
|
||
enum class FileFormat {
|
||
Unknown = 0, ///< 无法识别
|
||
Step, ///< ISO 10303-21(AP203/AP209/AP214/AP242)
|
||
Iges, ///< IGES 5.x
|
||
Dxf, ///< AutoCAD DXF(ASCII / Binary DXF)
|
||
Dwg, ///< AutoCAD DWG(AC10xx 魔数)
|
||
Stl, ///< STL(ASCII 或 binary)
|
||
Obj, ///< Wavefront OBJ
|
||
Ply, ///< Stanford PLY
|
||
ThreeMf, ///< 3MF(ZIP 容器)
|
||
Gltf, ///< glTF 2.0(.gltf JSON 或 .glb 二进制)
|
||
Vrml, ///< VRML 2.0
|
||
Collada, ///< COLLADA (.dae)
|
||
Amf, ///< AMF(1.1 XML 或 ZIP 容器)
|
||
Pdf3d, ///< PDF(含 PDF 3D U3D/PRC 流)
|
||
Ifc, ///< IFC(ISO 16739,STEP 物理文件 + IFC schema)
|
||
Xt, ///< Parasolid XT(.x_t / .x_b)
|
||
Sat, ///< ACIS SAT/SAB
|
||
Jt, ///< JT(ISO 14306)
|
||
Las, ///< ASPRS LAS 点云
|
||
Laz, ///< LASzip 压缩 LAS 点云
|
||
Ptx, ///< Leica PTX 点云
|
||
E57, ///< ASTM E2807 E57 点云
|
||
Vde, ///< VDE 自有二进制格式(magic "VDE1")
|
||
Mesh, ///< 通用网格容器(.mesh/.msh/.off,仅扩展名回退)
|
||
Gcode, ///< G-code / ISO 6983
|
||
Dwt, ///< AutoCAD 图形模板(DWG 容器,按 DWG 读取)
|
||
Dws, ///< AutoCAD 图形标准(DWG 容器,按 DWG 读取)
|
||
Dwf, ///< Autodesk Design Web Format(DWF/DWFx)
|
||
ThreeDXml, ///< Dassault 3DXML(ZIP + XML)
|
||
Prt, ///< 通用/歧义 PRT(NX / Creo / Solid Edge)
|
||
ThreeDm, ///< Rhino 3DM
|
||
Model, ///< CATIA V4 MODEL
|
||
Vda, ///< VDA-FS 曲面
|
||
Sldprt, ///< SolidWorks 零件
|
||
Sldasm, ///< SolidWorks 装配
|
||
Par, ///< Solid Edge 零件
|
||
Asm, ///< 通用/歧义 ASM(Solid Edge / Creo)
|
||
Psm, ///< Solid Edge 钣金
|
||
CatPart, ///< CATIA V5 零件
|
||
CatProduct, ///< CATIA V5 装配
|
||
Cgr, ///< CATIA CGR
|
||
Ipt, ///< Autodesk Inventor 零件
|
||
Iam, ///< Autodesk Inventor 装配
|
||
Rvt, ///< Autodesk Revit 项目
|
||
};
|
||
|
||
/// 格式探测结果
|
||
struct DetectedFormat {
|
||
FileFormat format = FileFormat::Unknown; ///< 识别出的格式
|
||
double confidence = 0.0; ///< 置信度 [0,1];Unknown 为 0
|
||
std::string reason; ///< 判定依据/冲突/失败原因(人类可读)
|
||
|
||
[[nodiscard]] bool is_known() const noexcept { return format != FileFormat::Unknown; }
|
||
};
|
||
|
||
/**
|
||
* @brief 探测内存中字节的格式(魔数/容器头优先,扩展名回退)
|
||
*
|
||
* @param bytes 文件前缀或完整内容(可空、可含 NUL;实现只读前若干 MB)
|
||
* @param path_hint 可选路径/文件名,仅用于扩展名回退与冲突记录(可为空)
|
||
* @return 探测结果;无法识别返回 Unknown + reason(不抛异常)
|
||
*/
|
||
[[nodiscard]] DetectedFormat detect_format(const std::vector<uint8_t>& bytes,
|
||
const std::string& path_hint = {});
|
||
|
||
/**
|
||
* @brief 探测磁盘文件的格式(读取文件前缀后调用 `detect_format`)
|
||
*
|
||
* @param path 文件路径(同时作为 path_hint 使用)
|
||
* @return 探测结果;文件打不开/为空/未知格式均返回 Unknown + reason(不抛异常)
|
||
*/
|
||
[[nodiscard]] DetectedFormat detect_format_file(const std::string& path);
|
||
|
||
/**
|
||
* @brief 格式枚举的稳定文本名("STEP" / "IGES" / ... / "Unknown")
|
||
*
|
||
* 用于日志/审计探针;返回值生命周期为静态存储。
|
||
*/
|
||
[[nodiscard]] const char* format_name(FileFormat format) noexcept;
|
||
|
||
// ===================================================================
|
||
// 统一格式能力矩阵(2026-10-27)
|
||
// ===================================================================
|
||
|
||
/// 格式所属族(用于 UI 分组/筛选)
|
||
enum class FormatFamily {
|
||
Mesh,
|
||
Brep,
|
||
Drawing,
|
||
Bim,
|
||
PointCloud,
|
||
Manufacturing,
|
||
Document,
|
||
Unknown,
|
||
};
|
||
|
||
/// 本引擎对该格式的能力等级
|
||
enum class FormatAccess {
|
||
NativeReadWrite,
|
||
NativeRead,
|
||
NativeWrite,
|
||
ConverterOnly,
|
||
Unsupported,
|
||
};
|
||
|
||
/// 单个格式的注册信息
|
||
struct CadFormatInfo {
|
||
FileFormat format = FileFormat::Unknown;
|
||
const char* name = "Unknown";
|
||
FormatFamily family = FormatFamily::Unknown;
|
||
FormatAccess access = FormatAccess::Unsupported;
|
||
std::vector<const char*> extensions;
|
||
const char* note = "";
|
||
|
||
[[nodiscard]] bool can_read() const noexcept {
|
||
return access == FormatAccess::NativeReadWrite ||
|
||
access == FormatAccess::NativeRead ||
|
||
access == FormatAccess::ConverterOnly;
|
||
}
|
||
[[nodiscard]] bool can_write() const noexcept {
|
||
return access == FormatAccess::NativeReadWrite ||
|
||
access == FormatAccess::NativeWrite;
|
||
}
|
||
[[nodiscard]] bool native() const noexcept {
|
||
return access == FormatAccess::NativeReadWrite ||
|
||
access == FormatAccess::NativeRead ||
|
||
access == FormatAccess::NativeWrite;
|
||
}
|
||
};
|
||
|
||
/// 全量注册表(覆盖本引擎声明支持的所有扩展名,含仅转换器/仅识别的私有格式)
|
||
[[nodiscard]] const std::vector<CadFormatInfo>& cad_format_registry();
|
||
|
||
/// 按枚举查注册信息;未注册返回 nullptr
|
||
[[nodiscard]] const CadFormatInfo* cad_format_info(FileFormat format) noexcept;
|
||
|
||
/// 按路径/文件名查注册信息;支持 .prt.1 / .asm.2 复合扩展名
|
||
[[nodiscard]] const CadFormatInfo* cad_format_info_for_path(
|
||
const std::string& path) noexcept;
|
||
|
||
|
||
// ===================================================================
|
||
// 统一转换器能力查询(VDE-1277)
|
||
// ===================================================================
|
||
|
||
/// 转换器可用状态
|
||
struct ConverterStatus {
|
||
bool available = false; ///< 当前是否可执行该格式转换
|
||
bool native = false; ///< 是否内核原生支持,无需外部转换器
|
||
std::string executable; ///< 注册的转换器可执行文件/插件标识(可空)
|
||
std::string version; ///< 注册的转换器版本(可空)
|
||
std::string reason; ///< 可用/不可用原因(人类可读)
|
||
};
|
||
|
||
/// 单个格式的完整能力(注册表 + 转换器状态)
|
||
struct FormatCapability {
|
||
FileFormat format = FileFormat::Unknown;
|
||
const char* name = "Unknown";
|
||
FormatFamily family = FormatFamily::Unknown;
|
||
FormatAccess access = FormatAccess::Unsupported;
|
||
bool can_read = false;
|
||
bool can_write = false;
|
||
std::vector<std::string> extensions;
|
||
ConverterStatus converter;
|
||
};
|
||
|
||
/**
|
||
* @brief 注册外部转换器(宿主的格式桥接层调用)
|
||
*
|
||
* 内核不主动探测/启动外部程序;宿主确认转换器可用后注册,
|
||
* 后续统一查询即可返回 executable/version。
|
||
*/
|
||
void register_format_converter(FileFormat format, std::string executable,
|
||
std::string version = {});
|
||
|
||
/// 清除全部外部转换器注册(测试/宿主重载配置用)
|
||
void clear_format_converters();
|
||
|
||
/// 查询单个格式的转换器状态;未注册返回 available=false + reason
|
||
[[nodiscard]] ConverterStatus query_format_converter(FileFormat format);
|
||
|
||
/// 全量能力列表:注册表 + 当前转换器状态
|
||
[[nodiscard]] std::vector<FormatCapability> format_capabilities();
|
||
|
||
} // namespace vde::io
|