CreoOtkPluging/CreoManager.h
sladro a9f290367f 新增层级统计分析接口 - 支持装配体组件数量统计
主要特性:
- 新接口 /api/analysis/hierarchy-statistics 统计每个层级的组件数量
- 层级0固定为1(根装配体),其他层级统计实际组件数量
- 自动移除值为0的空层级,优化返回数据
- 支持装配体和零件双模式,零件返回单层级结果
- 完善的异常处理和错误信息

技术实现:
- 新增 HierarchyStatisticsAnalyzer 类处理层级统计逻辑
- 修改 CreoManager.h 暴露必要的公共接口
- 递归遍历装配体结构,正确统计各层级组件数量
- 所有注释改为英文,避免UTF-8编码冲突

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-08-28 19:24:14 +08:00

357 lines
12 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#pragma once
// 基础OTK头文件 - 确保正确的包含顺序
#include <pfcGlobal.h>
#include <pfcSession.h>
#include <wfcSession.h>
#include <wfcGlobal.h>
#include <pfcModel.h>
#include <pfcExceptions.h>
#include <pfcExport.h>
#include <pfcFeature.h>
#include <pfcComponentFeat.h>
#include <pfcFeature_s.h>
#include <pfcSolid.h>
#include <wfcSolid.h>
#include <wfcSolidInstructions.h>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <cmath>
// Creo状态信息结构
struct CreoStatus {
bool is_connected = false;
std::string version;
std::string build;
std::string working_directory;
int session_id = 0;
};
// 模型状态信息结构
struct ModelStatus {
bool has_model = false;
std::string name;
std::string filename;
std::string type;
std::string software; // 从OTK API获取不设默认值
std::string version; // 从OTK API获取不设默认值
std::string connection_time;
bool is_assembly = false;
int total_parts = 0;
int assembly_levels = 0;
std::string file_size;
std::string connection_status;
std::string open_time;
bool is_modified = false; // 模型是否已修改(未保存)
};
// 导出结果结构
struct ExportResult {
bool success = false;
std::string export_path;
std::string file_size;
std::string format;
std::string export_time;
std::string software;
std::string original_file;
std::string dirname;
std::string filename;
std::string error_message;
};
// 保存结果结构
struct SaveResult {
bool success = false;
std::string file_size;
std::string save_time;
std::string software;
std::string original_file;
std::string error_message;
};
// 关闭结果结构
struct CloseResult {
bool success = false;
std::string model_name;
bool was_modified = false;
std::string close_time;
std::string error_message;
};
// 打开结果结构
struct OpenResult {
bool success = false;
std::string model_name;
std::string model_type;
std::string file_path;
std::string file_size;
std::string open_time;
bool is_assembly = false;
int total_parts = 0;
bool model_in_session = false; // 验证模型是否真的在会话中
bool window_model_match = false; // 验证窗口是否正确关联模型
std::string error_message;
};
// 层级分析组件信息结构
struct ComponentInfo {
std::string id;
std::string name;
std::string type; // "assembly" 或 "part"
int level;
int children_count;
std::string path;
std::string file_size;
std::string deletion_safety; // "forbidden", "risky", "moderate"
bool is_visible;
std::string model_type; // "MDL_ASSEMBLY", "MDL_PART", "MDL_DRAWING" 等
std::string full_path; // 完整的组件路径
};
// 层级分析请求结构
struct HierarchyAnalysisRequest {
std::string software_type;
std::string project_name;
int max_depth;
bool include_geometry;
int target_level = -1; // 新增:指定返回的层级,-1表示返回所有
std::map<std::string, std::string> analysis_options;
};
// 删除建议结构
struct DeletionRecommendation {
std::string component_id;
std::string component_name;
int level;
std::string reason;
std::vector<std::string> risk_factors;
double confidence;
};
// 层级分析结果结构
struct HierarchyAnalysisResult {
bool success = false;
std::string message;
std::string project_name;
int total_levels;
int total_components;
std::vector<std::vector<ComponentInfo>> hierarchy;
std::vector<DeletionRecommendation> safe_deletions;
std::vector<DeletionRecommendation> risky_deletions;
std::string error_message;
};
// Creo管理器类
class CreoManager {
public:
// 会话管理(避免重复代码)
struct SessionInfo {
pfcSession_ptr session;
wfcWSession_ptr wSession;
bool is_valid;
std::string version;
std::string build;
};
static CreoManager& Instance();
// 状态检测
CreoStatus GetCreoStatus();
ModelStatus GetModelStatus();
// 基础操作
bool ShowMessage(const std::string& message);
// 导出功能
ExportResult ExportModelToSTEP(const std::string& export_path, const std::string& geom_flags = "solids");
// 保存功能
SaveResult SaveModel();
// 关闭功能
CloseResult CloseModel(bool force_close = false);
// 打开功能
OpenResult OpenModel(const std::string& file_path, const std::string& open_mode = "active");
// 层级分析功能
HierarchyAnalysisResult AnalyzeModelHierarchy(const HierarchyAnalysisRequest& request);
// 层级删除功能
struct HierarchyDeleteResult {
bool success = false;
std::string message;
int original_levels;
int target_level;
int final_levels;
std::map<int, std::vector<std::string>> deleted_components;
int total_deleted;
int successful;
int failed;
std::string error_message;
};
HierarchyDeleteResult DeleteHierarchyComponents(const std::string& project_name, int target_level);
// 薄壳化分析功能
struct ShellAnalysisRequest {
std::string software_type;
std::string project_name = "current_model";
std::string analysis_type = "surface_shell";
bool preserve_external_surfaces = true;
double min_wall_thickness = 1.0;
double confidence_threshold = 0.7;
};
struct FeatureDeletion {
int id;
std::string name;
std::string type;
std::string reason;
double confidence;
int volume_reduction;
std::string part_file;
std::string part_path;
std::string component_type = "FEATURE";
};
struct EstimatedReduction {
std::string volume_reduction;
std::string file_size_reduction;
std::string performance_improvement;
};
struct HierarchyAnalysisInfo {
bool enabled = true;
int total_parts = 0;
int outer_parts = 0;
int internal_parts = 0;
int containment_relationships = 0;
std::map<std::string, std::string> performance_stats;
};
struct ShellAnalysisParameters {
bool preserve_external_surfaces;
double min_wall_thickness;
double confidence_threshold;
int total_features;
int deletable_features;
int preserved_features;
bool assembly_analysis;
std::string analysis_strategy = "bbox_surface_ownership_feature_analysis_optimized";
int surface_count;
int shell_surfaces;
int internal_surfaces;
int shell_feature_whitelist;
HierarchyAnalysisInfo hierarchy_analysis;
};
struct ShellAnalysisResult {
bool success = false;
std::vector<FeatureDeletion> safe_deletions;
std::vector<FeatureDeletion> suggested_deletions;
std::vector<FeatureDeletion> preserve_list;
EstimatedReduction estimated_reduction;
ShellAnalysisParameters analysis_parameters;
std::string error_message;
};
ShellAnalysisResult AnalyzeShellFeatures(const ShellAnalysisRequest& request);
// 薄壳化分析辅助方法
std::string GetFeatureTypeName(pfcFeatureType feat_type);
bool IsExternalSurface(pfcFeature_ptr feature, bool preserve_external);
bool CheckWallThickness(pfcFeature_ptr feature, double min_thickness);
// 薄壳化算法辅助方法基于真实OTK数据
bool IsStandardPart(const std::string& part_name);
bool IsInternalPart(pfcFeature_ptr feature, pfcModel_ptr model);
// 辅助功能
std::string GetCurrentTimeString();
std::string GetCurrentTimeStringISO();
std::string GetFileSize(const std::string& filepath);
int SafeCalculateAssemblyLevels(wfcWAssembly_ptr assembly);
// 会话信息获取
SessionInfo GetSessionInfo();
// 字符串转换辅助函数
std::string XStringToString(const xstring& xstr);
// 文件大小统计
std::string GetModelFileSize(pfcModel_ptr model);
std::string CalculateAssemblyTotalSize(pfcModel_ptr model);
double ParseMBFromSizeString(const std::string& size_str);
private:
CreoManager(); // 需要自定义构造函数来设置配置
~CreoManager() = default;
CreoManager(const CreoManager&) = delete;
CreoManager& operator=(const CreoManager&) = delete;
// 辅助函数
xstring StringToXString(const std::string& str);
std::pair<std::string, std::string> ParseFilePath(const std::string& file_path);
// 层级分析私有方法 (新SOTA算法)
void AnalyzeAssemblyNode(wfcWAssembly_ptr assembly,
int level,
const std::string& parentName,
const std::string& currentPath,
HierarchyAnalysisResult& result,
int target_level = -1); // 新增参数
ComponentInfo CreateComponentFromFeature(pfcComponentFeat_ptr compFeat,
int level,
const std::string& parentName,
const std::string& currentPath,
pfcModel_ptr preloadedModel = nullptr);
pfcModel_ptr LoadComponentModel(pfcComponentFeat_ptr compFeat);
// 薄壳化分析递归方法
void CollectAllComponentsForShellAnalysis(wfcWAssembly_ptr assembly,
const std::string& parentPath,
std::vector<std::pair<pfcFeature_ptr, std::string>>& allComponents);
// 真实几何分析方法
bool AnalyzeFeatureGeometry(pfcFeature_ptr feature, pfcOutline3D_ptr globalOutline, double tolerance);
// 旧方法 (保留用于兼容)
void TraverseAssemblyLevels(wfcWAssembly_ptr assembly,
int current_level,
const std::string& parent_path,
std::vector<std::vector<ComponentInfo>>& hierarchy_levels,
std::vector<ComponentInfo>& all_components);
void ClearStaticVisitedModels();
ComponentInfo CreateComponentInfo(wfcWComponentPath_ptr component_path,
int level, const std::string& parent_path);
// 新增CREOSON风格的安全方法
ComponentInfo CreateComponentInfoSafe(wfcWComponentPath_ptr component_path,
int level, const std::string& parent_path);
bool CheckCanRecurseSafely(wfcWComponentPath_ptr component_path);
wfcWAssembly_ptr GetSubAssemblySafely(wfcWComponentPath_ptr component_path);
std::string GetComponentTypeSafe(wfcWComponentPath_ptr component_path);
std::string GenerateComponentPathId(wfcWComponentPath_ptr component_path);
std::string EvaluateDeletionSafety(const ComponentInfo& component);
std::string GetComponentFileSize(wfcWComponentPath_ptr component_path);
std::string GetModelTypeString(pfcModelType model_type);
int CountChildComponents(wfcWComponentPath_ptr component_path);
};