新增ModelSearchEngine和ModelSearchHandler模块,提供零件和装配体名称搜索功能: • 支持prefix、contains、fuzzy三种匹配模式 • 从根装配体构建完整模型树层级路径 • 智能去重算法自动去除重复结果,保留最长路径 • 递归搜索支持多层级装配体遍历 • 向后兼容,不影响现有功能 解决搜索重复结果和缺少层级路径问题,提升用户体验。 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
181 lines
6.9 KiB
C++
181 lines
6.9 KiB
C++
#pragma once
|
|
|
|
// 基础OTK头文件
|
|
#include <pfcGlobal.h>
|
|
#include <pfcSession.h>
|
|
#include <wfcSession.h>
|
|
#include <wfcGlobal.h>
|
|
#include <pfcModel.h>
|
|
#include <pfcExceptions.h>
|
|
#include <pfcFeature.h>
|
|
#include <pfcComponentFeat.h>
|
|
#include <pfcSolid.h>
|
|
#include <wfcSolid.h>
|
|
#include <pfcAssembly.h>
|
|
#include <wfcAssembly.h>
|
|
#include <string>
|
|
#include <vector>
|
|
#include <set>
|
|
#include <map>
|
|
#include <algorithm>
|
|
#include <cmath>
|
|
#include <memory>
|
|
#include <chrono>
|
|
|
|
// 搜索请求结构
|
|
struct ModelSearchRequest {
|
|
std::string query; // 搜索关键词
|
|
std::string match_mode; // 匹配模式: "prefix", "contains", "fuzzy"
|
|
std::string search_scope; // 搜索范围: "session", "current_model", "all"
|
|
std::vector<std::string> 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 display_name; // 显示名称
|
|
std::string full_path; // 模型树中的完整路径
|
|
std::string file_path; // 文件路径 (GetOrigin)
|
|
std::string model_type; // 模型类型 ("PART", "ASSEMBLY", "DRAWING")
|
|
std::string component_path; // 组件路径(装配体中的位置)
|
|
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::string file_size; // 文件大小
|
|
std::string last_modified; // 最后修改时间
|
|
std::vector<std::string> matched_keywords; // 匹配的关键词
|
|
|
|
// 构造函数
|
|
SearchResultItem() :
|
|
match_score(0.0),
|
|
is_in_session(false),
|
|
is_assembly(false),
|
|
component_count(0)
|
|
{
|
|
}
|
|
};
|
|
|
|
// 搜索结果结构
|
|
struct ModelSearchResult {
|
|
bool success; // 搜索是否成功
|
|
std::vector<SearchResultItem> 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<SearchResultItem>& results);
|
|
void SearchCurrentModel(const ModelSearchRequest& request, std::vector<SearchResultItem>& results);
|
|
void SearchFromRootAssemblies(const ModelSearchRequest& request, std::vector<SearchResultItem>& results);
|
|
void SearchAssemblyComponents(const ModelSearchRequest& request,
|
|
pfcAssembly_ptr assembly,
|
|
std::vector<SearchResultItem>& results,
|
|
std::set<std::string>& processed_models,
|
|
const std::string& parent_path = "");
|
|
void SearchAssemblyComponentsWithDedup(const ModelSearchRequest& request,
|
|
pfcAssembly_ptr assembly,
|
|
std::map<std::string, SearchResultItem>& best_results,
|
|
std::set<std::string>& 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<SearchResultItem>& 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<std::string>& type_filters);
|
|
|
|
// 字符串处理工具
|
|
static std::string ToLowerCase(const std::string& str);
|
|
static std::vector<std::string> 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<std::string> processed_models_;
|
|
|
|
// 性能计时
|
|
std::chrono::high_resolution_clock::time_point search_start_time_;
|
|
void StartTimer();
|
|
std::string GetElapsedTimeMs();
|
|
};
|