#pragma once // 基础OTK头文件 #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include // 搜索请求结构 struct ModelSearchRequest { std::string query; // 搜索关键词 std::string match_mode; // 匹配模式: "prefix", "contains", "fuzzy" std::string search_scope; // 搜索范围: "session", "current_model", "all" std::vector model_types; // 模型类型过滤: ["MDL_PART", "MDL_ASSEMBLY"] int max_results; // 最大返回结果数 double similarity_threshold; // 模糊匹配相似度阈值 (0.0-1.0) bool include_components; // 是否包含装配体组件搜索 bool include_features; // 是否包含特征名称搜索 // 构造函数 ModelSearchRequest() : match_mode("contains"), search_scope("session"), max_results(50), similarity_threshold(0.3), include_components(true), include_features(false) { } }; // 搜索结果项结构 struct SearchResultItem { std::string model_name; // 模型名称 std::string full_path; // 模型树中的完整路径 std::string file_path; // 文件路径 (GetOrigin) std::string model_type; // 模型类型 ("PART", "ASSEMBLY", "DRAWING") double match_score; // 匹配分数 (0.0-1.0) std::string match_reason; // 匹配原因说明 std::string match_type; // 匹配类型 ("name", "component", "feature") bool is_in_session; // 是否在当前会话中 bool is_assembly; // 是否是装配体 int component_count; // 组件数量(装配体) std::vector matched_keywords; // 匹配的关键词 // 构造函数 SearchResultItem() : match_score(0.0), is_in_session(false), is_assembly(false), component_count(0) { } }; // 搜索结果结构 struct ModelSearchResult { bool success; // 搜索是否成功 std::vector results; // 搜索结果列表 int total_found; // 总找到数量 int returned_count; // 返回数量 std::string search_time_ms; // 搜索耗时(毫秒) std::string error_message; // 错误信息 // 搜索统计 struct SearchStats { int session_models_count; // 会话模型数量 int components_searched; // 搜索的组件数量 int features_searched; // 搜索的特征数量 std::string search_scope_used; // 实际使用的搜索范围 } stats; // 构造函数 ModelSearchResult() : success(false), total_found(0), returned_count(0) { stats.session_models_count = 0; stats.components_searched = 0; stats.features_searched = 0; } }; // 模型搜索引擎类 class ModelSearchEngine { public: // 单例模式 static ModelSearchEngine& Instance(); // 主要搜索接口 ModelSearchResult SearchModels(const ModelSearchRequest& request); // 模糊匹配算法 static double CalculateFuzzyMatch(const std::string& query, const std::string& target); // 字符串匹配工具 static bool MatchesPrefix(const std::string& text, const std::string& prefix); static bool MatchesContains(const std::string& text, const std::string& substring); static double CalculateEditDistance(const std::string& s1, const std::string& s2); private: // 构造函数(私有) ModelSearchEngine() = default; ~ModelSearchEngine() = default; // 删除拷贝构造 ModelSearchEngine(const ModelSearchEngine&) = delete; ModelSearchEngine& operator=(const ModelSearchEngine&) = delete; // 核心搜索方法 void SearchSessionModels(const ModelSearchRequest& request, std::vector& results); void SearchCurrentModel(const ModelSearchRequest& request, std::vector& results); void SearchFromRootAssemblies(const ModelSearchRequest& request, std::vector& results); void SearchAssemblyComponents(const ModelSearchRequest& request, pfcAssembly_ptr assembly, std::vector& results, std::set& processed_models, const std::string& parent_path = ""); void SearchAssemblyComponentsWithDedup(const ModelSearchRequest& request, pfcAssembly_ptr assembly, std::map& best_results, std::set& processed_models, const std::string& parent_path = ""); // 匹配评分方法 double CalculateMatchScore(const ModelSearchRequest& request, const std::string& target_name, const std::string& match_type); // 结果处理方法 void SortAndFilterResults(std::vector& results, const ModelSearchRequest& request); // 辅助方法 std::string GetModelTypeString(pfcModel_ptr model); std::string GetModelDisplayName(pfcModel_ptr model); std::string GetModelFilePath(pfcModel_ptr model); std::string GetModelFullPath(pfcModel_ptr model); bool ShouldIncludeModelType(const std::string& model_type, const std::vector& type_filters); // 字符串处理工具 static std::string ToLowerCase(const std::string& str); static std::vector SplitString(const std::string& str, char delimiter); static std::string TrimString(const std::string& str); // 组件路径构建 std::string BuildComponentPath(const std::string& parent_path, const std::string& component_name); // 去重处理 std::set processed_models_; // 性能计时 std::chrono::high_resolution_clock::time_point search_start_time_; void StartTimer(); std::string GetElapsedTimeMs(); };