# SolidWorks 主视图候选生成与排序 这个工具用于从 SolidWorks 零件或装配体生成 24 个主视图候选,并把每个候选单独导出为 DWG。后续再用 AutoCAD 提取这些 DWG 的线条签名,与学生二维图主视图进行匹配。 ## 候选视角定义 候选视角共 24 个: - 6 个观察方向:`+X`、`-X`、`+Y`、`-Y`、`+Z`、`-Z` - 每个方向绕屏幕法向旋转:`0`、`90`、`180`、`270` 度 顺序固定为: - 01-04:`+X`,roll `0/90/180/270` - 05-08:`-X`,roll `0/90/180/270` - 09-12:`+Y`,roll `0/90/180/270` - 13-16:`-Y`,roll `0/90/180/270` - 17-20:`+Z`,roll `0/90/180/270` - 21-24:`-Z`,roll `0/90/180/270` ## 导出 24 个候选 DWG 使用前先在 SolidWorks 中打开模型和工程图。工程图只需要一个打开的图纸,不要手动创建 24 个工程图窗口。 ```powershell dotnet run --project tools\drawing-diagnostics\SolidWorksViewCandidateGenerator\SolidWorksViewCandidateGenerator.csproj -- ` --model "D:\Desktop\新建文件夹\毕设\建模\小臂肘关节装配\小臂肘关节.SLDASM" ` --use-active-drawing ` --export-candidate-dwgs-sequential ` --output-dir "D:\CSharpProjects\agent4\runtime\sw_candidate_dwg_1to1" ``` 行为: - 先在模型端创建 24 个临时候选命名视图。 - 在工程图中插入第 1 个候选视图。 - 按 `1:1` 比例保存为 DWG。 - 删除当前候选视图。 - 再插入下一个候选视图,直到 24 个全部完成。 如果某个候选 DWG 保存失败,工具会重新插入、重建、再次保存。默认不直接跳过失败候选。 输出: - `candidate-result.json`:完整执行结果。 - `candidate-views.json`:候选方向、旋转角、DWG 路径、保存状态。 - `candidate-dwgs/`:24 个候选 DWG。 ## 提取候选 DWG 导出完成后,用 AutoCAD 串行提取候选 DWG: ```powershell powershell -ExecutionPolicy Bypass -File tools\drawing-diagnostics\SolidWorksViewCandidateGenerator\ExtractCandidateDwgsSequential.ps1 ` -CandidateViewsJson "D:\CSharpProjects\agent4\runtime\sw_candidate_dwg_1to1\candidate-views.json" ` -OutputRoot "D:\CSharpProjects\agent4\runtime\sw_candidate_dwg_1to1\autocad-extractions-relative" ` -Visible ``` 脚本会构建一次 `AutoCadDwgExtractor`,然后对每个候选 DWG 运行编译后的 DLL。每次只打开一个 DWG,提取完成后关闭,避免 AutoCAD 同时打开大量窗口。 候选 DWG 提取时使用: ```text --main-view-seed-mode solidworks-candidate ``` 该模式会先移除图框和标题栏连通块,再把剩余几何作为候选视图。 ## 排序 学生二维图主视图签名和 24 个候选签名都准备好后,运行: ```powershell powershell -ExecutionPolicy Bypass -File tools\drawing-diagnostics\SolidWorksViewCandidateGenerator\RankExtractedCandidateDwgs.ps1 ` -TargetSignaturePath "D:\CSharpProjects\agent4\runtime\dwg-main-view\A2_肘关节装配_relative_signature\main-view-signature.json" ` -CandidateExtractionRoot "D:\CSharpProjects\agent4\runtime\sw_candidate_dwg_1to1\autocad-extractions-relative" ` -CandidateViewsJson "D:\CSharpProjects\agent4\runtime\sw_candidate_dwg_1to1\candidate-views.json" ``` 输出: - `candidate-ranking-relative.json` 当前打分特征: - 图元类型数量。 - 水平、竖直、斜线方向分布。 - 线段长度直方图。 - 圆半径、圆弧半径、圆弧长度。 - 相对中点坐标直方图。 - 相对坐标加线长/方向的组合特征。 本次验证结果: ```text rank 1: agent4_candidate_04_px_r270, direction=px, roll=270, score=0.421348 rank 2: agent4_candidate_02_px_r90, direction=px, roll=90, score=0.385758 ``` ## 放置最优主视图 得到排序结果后,可以把第一名候选视角作为 SolidWorks 工程图中的主视图。要求 SolidWorks 中已经打开模型和工程图: ```powershell dotnet run --project tools\drawing-diagnostics\SolidWorksViewCandidateGenerator\SolidWorksViewCandidateGenerator.csproj -- ` --model "D:\Desktop\新建文件夹\毕设\建模\小臂肘关节装配\小臂肘关节.SLDASM" ` --use-active-drawing ` --place-main-view ` --ranking-json "D:\CSharpProjects\agent4\runtime\sw_candidate_dwg_1to1\autocad-extractions-relative\candidate-ranking-relative.json" ` --output-dir "D:\CSharpProjects\agent4\runtime\sw_candidate_dwg_1to1\placed-main-view" ``` 也可以不用排序文件,直接指定候选编号: ```powershell dotnet run --project tools\drawing-diagnostics\SolidWorksViewCandidateGenerator\SolidWorksViewCandidateGenerator.csproj -- ` --model "D:\Desktop\新建文件夹\毕设\建模\小臂肘关节装配\小臂肘关节.SLDASM" ` --use-active-drawing ` --place-main-view ` --candidate-index 4 ` --output-dir "D:\CSharpProjects\agent4\runtime\sw_candidate_dwg_1to1\placed-main-view" ``` 默认行为: - 自动读取 rank 1 的候选编号,或使用 `--candidate-index` 指定的编号。 - 创建命名视图,例如 `agent4_main_view_04_px_r270`。 - 在当前工程图中插入该视图。 - 默认比例是 `1:1`。 - 默认放置在图纸中心;可用 `--single-view-x-m` 和 `--single-view-y-m` 指定位置,单位是米。 - 默认删除此前由 `agent4_main_view` 前缀创建的旧主视图;如果想保留旧主视图,传 `--keep-existing-main-view`。 ## 对齐 SolidWorks 主视图到学生图主视图原点 当学生 DWG 已经整体平移到 SolidWorks 图框中心后,还需要把 SolidWorks 工程图里的主视图整体平移,使两边主视图的相对坐标原点一致。这里的原点规则和签名提取一致:主视图中最左侧竖直线段的最上点。 输入是两个 `main-view-signature.json`: - `--student-signature`:移动后的学生 DWG 主视图签名。 - `--reference-signature`:最优 SolidWorks 候选 DWG 的主视图签名,例如候选 04。 运行: ```powershell dotnet run --project tools\drawing-diagnostics\SolidWorksViewCandidateGenerator\SolidWorksViewCandidateGenerator.csproj -- ` --model "D:\Desktop\新建文件夹\毕设\建模\小臂肘关节装配\小臂肘关节.SLDASM" ` --use-active-drawing ` --align-main-view-origin ` --student-signature "D:\CSharpProjects\agent4\runtime\aligned-student-dwg\extraction-check\main-view-signature.json" ` --reference-signature "D:\CSharpProjects\agent4\runtime\sw_candidate_dwg_1to1\autocad-extractions-relative\04_agent4_candidate_04_px_r270\main-view-signature.json" ` --main-view-orientation-name "agent4_main_view_04_px_r270" ` --output-dir "D:\CSharpProjects\agent4\runtime\sw_candidate_dwg_1to1\aligned-main-view-origin" ``` 本次实测结果: - 学生原点:`(45.990114407105125, 338.275336225917)` mm。 - SolidWorks 候选 04 原点:`(229.50000000000037, 260.00000000000057)` mm。 - 主视图整体平移量:`dx=-183.50988559289524 mm, dy=78.27533622591642 mm`。 - SolidWorks 视图位置从 `(0.297, 0.21)` m 移到 `(0.11349011440710474, 0.2882753362259164)` m。 - 输出报告:`runtime\sw_candidate_dwg_1to1\aligned-main-view-origin\candidate-result.json`。 ## 不再使用的方法 - 不再把 24 个候选视图同时放到同一个工程图里做匹配。 - 不再依赖候选视图在图纸上的绝对坐标。 - 不再使用中心点种子线扩展候选视图。 原因是最终学生图和候选图都应通过 AutoCAD DWG 提取,保证线条类型、长度、方向、合并和相对坐标规则一致。 # Current-drawing alignment rule `--align-main-view-origin` must compare the aligned student DWG signature with the signature extracted from a freshly exported DWG of the current SolidWorks drawing. Do not pass a signature from `runtime/sw_candidate_dwg_*/candidate-dwgs` or `autocad-extractions-relative` as the final `--reference-signature`. Candidate DWGs are only for choosing the main-view orientation. # Section-view center alignment Use `AutoCadDwgExtractor` to extract section-view regions from both DWGs before aligning SolidWorks section drawing views: ```powershell # Student DWG: section title A-A/B-B is above the section view; probe downward. dotnet run --project tools\drawing-diagnostics\AutoCadDwgExtractor\AutoCadDwgExtractor.csproj -- ` --drawing-path "D:\path\student-aligned.dwg" ` --output-dir "D:\CSharpProjects\agent4\runtime\student-section-regions" ` --section-view-probe-direction down # SolidWorks exported DWG: section title A-A/B-B is below the section view; probe upward. dotnet run --project tools\drawing-diagnostics\AutoCadDwgExtractor\AutoCadDwgExtractor.csproj -- ` --drawing-path "D:\path\solidworks-current.dwg" ` --output-dir "D:\CSharpProjects\agent4\runtime\sw-section-regions" ` --section-view-probe-direction up ``` Then align the matching SolidWorks section drawing views by the center of each matched `section_views[].bbox`: ```powershell dotnet run --project tools\drawing-diagnostics\SolidWorksViewCandidateGenerator\SolidWorksViewCandidateGenerator.csproj -- ` --use-active-drawing ` --align-section-views-to-regions ` --student-view-regions "D:\CSharpProjects\agent4\runtime\student-section-regions\view-regions.json" ` --reference-view-regions "D:\CSharpProjects\agent4\runtime\sw-section-regions\view-regions.json" ` --output-dir "D:\CSharpProjects\agent4\runtime\aligned-section-views" ``` The computed offset is `student_center - solidworks_center`, averaged across matching labels. Use `--section-label A` to align only one section label.