feat(VDE-395): 制图标准实时切换/自定义标准/差异比较/图纸模板 + 修复 JIS/ANSI 残缺实现
Build & Test / build-and-test (push) Canceled after 0s
VDE Build & Test / build-and-test (push) Canceled after 0s
Build & Test / python-bindings (push) Canceled after 0s

实现 §1 RealTimeStdSwitch(switch_to/preview_changes/apply_to_views) + drawing::preview_changes;§2 standards::CustomStandard(register_custom/unregister/list_custom/style_of) 并接入 apply_standard_by_name;§3 diff_standards 逐字段快照;§4 Segment/SheetTemplate/generate_sheet_template(ISO 7200/ASME Y14.1)。补齐头文件声明但 .cpp 从未定义的 RealTimeStdSwitch / standard_compliance_check(链接期缺失坏实现)。修复 JisStandard::create_style() 缺 cutting_plane=Chain、AnsiStandard::gdt_symbols_summary() 缺中文符号名。test_constraint_solver_3d 73/73。
This commit is contained in:
hm
2026-10-05 21:32:46 +08:00
parent b12854eb3b
commit dfd3b02fad
5 changed files with 590 additions and 15 deletions
+16 -3
View File
@@ -1,11 +1,24 @@
# VDE-395: DrawingStandards — GOST & real-time standards switching
# VDE-395: DrawingStandards — GOST & real-time standards switching
**Date**: 2026-08-03
**Source**: ViewDesign DrawingStandardsTools integration
**Module**: `vde/brep/drawing_standards.h`
**Status**: Partially Fixed(2026-10-04 审计更正:提案 API 未全部落地;原标 Fixed — v1.1 implementation completed 有误,Resolution 自述 Deferred to VDE roadmap。缺失清单见 docs/reference/audit-proposal-batch.md)
**Status**: Fixed(2026-10-05 落实:§1 real-time switching、§2 CustomStandard、§3 diff_standards、§4 generate_sheet_template 全部实现并测试通过;一并修复两项既有残缺实现:`JisStandard::create_style()` 未设 `cutting_plane`、`AnsiStandard::gdt_symbols_summary()` 缺中文符号名)
**Acknowledged Date**: 2026-08-03
**Resolution**: Full GOST standard support, real-time standards switching with viewport auto-update, and custom standard registration are architecture-level scope additions. Current 7-standard coverage (ISO/ANSI/JIS/GB/BS/DIN/GOST) is comprehensive. Deferred to VDE roadmap.
**Resolution**: 实现 `standards::CustomStandard`(register_custom/unregister/list_custom/style_of)与 `diff_standards`;`drawing::preview_changes` / `RealTimeStdSwitch`(switch_to / preview_changes / apply_to_views);补齐此前仅有声明、从未定义的 `RealTimeStdSwitch` 与 `standard_compliance_check`(链接期缺失的坏实现);`drawing::Segment` / `SheetTemplate` / `generate_sheet_template`(ISO 7200 与 ASME Y14.1 图框与标题栏)。
## Implementation (2026-10-05)
新增/补全 API(`include/vde_drafting/drawing_standards.h` + `src/vde_drafting/drawing_standards.cpp`):
- **§1 Real-time switching**:`drawing::RealTimeStdSwitch`(构造默认 ISO)——`switch_to(name)` 未知标准返回 false 且不计数、成功则提交样式并更新 `current()`;`preview_changes(name)` 返回"字段: 旧 -> 新"清单但不提交;`apply_to_views(views)` 将 `drawing_scale` 写入每个 `ProjectionView::scale`。自由函数 `drawing::preview_changes(style, name)`。
- **§2 CustomStandard**:`standards::CustomStandard::register_custom(id, style)` 返回注册后的 `StandardInfo`(重复注册覆盖);`unregister(id)`、`list_custom()`、`style_of(id)`。`apply_standard_by_name` / 内部 `resolve_style` 已接入:内置标准优先,其次自定义注册表。
- **§3 StandardsDiff**:`standards::diff_standards(a, b)` 逐字段快照比较,`changed_fields` 与 `value_differences`("字段: 旧 -> 新");`empty()`。
- **§4 SheetTemplate**:`drawing::Segment{x1,y1,x2,y2}`、`drawing::SheetTemplate{width,height,border,title_block}`、`drawing::generate_sheet_template(standard, paper_size)`。图框:ISO/JIS/GB/BS/GOST 左装订边 20mm、其余 10mm;ANSI 四边 10mm;标题栏右下角(ISO 7200 高 55mm / ASME 高 32mm,含分栏线)。
- **坏实现修复**:`RealTimeStdSwitch` 与 `standard_compliance_check` 此前在头文件声明但 .cpp 从未定义 → 补齐。`standard_compliance_check` 委托 `validate_style_detailed`。
- **既有残缺实现修复**:`JisStandard::create_style()` 现设置 `cutting_plane = LineType::Chain`(JIS B 0001 剖切线为点划线);`AnsiStandard::gdt_symbols_summary()` 补中文符号名(位置度/轮廓度/方向/跳动)。
测试:`tests/brep/test_constraint_solver_3d.cpp` 追加 5 个用例(DiffIsoVsAnsiReportsProjectionChange / CustomStandardRegisterListApplyUnregister / StandardPreviewAndRealTimeSwitch / StandardComplianceCheckReport / SheetTemplateBorderAndTitleBlock),逐字段独立复算。`test_constraint_solver_3d` 73/73 全绿。
## Current state
+4 -4
View File
@@ -8,10 +8,10 @@
| 判定 | 份数 |
|------|------|
| 已实现(提案 API 全部存在) | 67(10-04 补齐 VDE-339/343/363/365/360/398/406/336/371/338/358/362/390/344 + VDE-357/368/374/352/388/387/375/382/354/379/369/353/373/345/392 + VDE-394/386/364/389/366 + VDE-361/370/334 + VDE-342/340 + 10-05 VDE-341/VDE-350/VDE-359/VDE-346/VDE-393/VDE-385/VDE-372/VDE-355/VDE-378/VDE-380/VDE-381/VDE-383/VDE-384/VDE-399/VDE-400/VDE-351/VDE-367/VDE-391/VDE-335/VDE-396/VDE-407/VDE-347/VDE-404/VDE-377/VDE-333) |
| Partially Fixed(部分存在) | 10 |
| 已实现(提案 API 全部存在) | 68(10-04 补齐 VDE-339/343/363/365/360/398/406/336/371/338/358/362/390/344 + VDE-357/368/374/352/388/387/375/382/354/379/369/353/373/345/392 + VDE-394/386/364/389/366 + VDE-361/370/334 + VDE-342/340 + 10-05 VDE-341/VDE-350/VDE-359/VDE-346/VDE-393/VDE-385/VDE-372/VDE-355/VDE-378/VDE-380/VDE-381/VDE-383/VDE-384/VDE-399/VDE-400/VDE-351/VDE-367/VDE-391/VDE-335/VDE-396/VDE-407/VDE-347/VDE-404/VDE-377/VDE-333/VDE-395) |
| Partially Fixed(部分存在) | 9 |
| Deferred(均不存在) | 0 |
| 合计缺失提案 API | 126(原 348,10-04 落地 119 个,10-05 落地 VDE-334/342/340/341/350/359/346/393/385/372/355/378/380/381/383/384/399/400/351/367/391/335/396/407/347/404/377/333) |
| 合计缺失提案 API | 119(原 348,10-04 落地 119 个,10-05 落地 VDE-334/342/340/341/350/359/346/393/385/372/355/378/380/381/383/384/399/400/351/367/391/335/396/407/347/404/377/333/395) |
## 明细
@@ -29,7 +29,7 @@
| VDE-403 | 14 | 8 | Partially Fixed | booleans, parallel_boolean_triangulate, parallel_sdf_to_mesh, analyze_vectorization, utilization, set_max_threads, set_thermal_limit, io_prefetch_ring |
| VDE-337 | 16 | 8 | Partially Fixed | enhancement, capabilities, Informational, instructions, diagnose_model, fix_rate, batch_auto_repair, selective_repair |
| VDE-398 | 10 | 0 | Fixed(10-04 落地) | ~~libraries, make_from_library, trim_profile_end, beams, make_tapered_i_beam, connections, add_end_plate~~ 全部实现(libraries=既有 profile_library;beams/connections→锥形梁/端板/端部裁剪) |
| VDE-395 | 20 | 7 | Partially Fixed | coverage, preview_changes, register_custom, unregister, list_custom, diff_standards, generate_sheet_template |
| VDE-395 | 20 | 0 | Fixed(10-05 落地) | ~~coverage, preview_changes, register_custom, unregister, list_custom, diff_standards, generate_sheet_template~~ 全部实现(`coverage` 正文词;`standards::CustomStandard::register_custom/unregister/list_custom/style_of` + `diff_standards`;`drawing::preview_changes` + `RealTimeStdSwitch`(switch_to/preview_changes/apply_to_views)+ `standard_compliance_check` + `Segment`/`SheetTemplate`/`generate_sheet_template`(ISO 7200 / ASME Y14.1)。另修既有坏实现:`RealTimeStdSwitch`/`standard_compliance_check` 头文件声明但 .cpp 从未定义;`JisStandard::create_style()` 缺 `cutting_plane`、`AnsiStandard::gdt_symbols_summary()` 缺中文符号名。test_constraint_solver_3d 73/73) |
| VDE-399 | 11 | 0 | Fixed(10-05 落地) | |
| VDE-338 | 14 | 0 | Fixed(10-04 落地) | ~~enhancement, linear_pattern_ex, circular_pattern_ex, linear_pattern_skip, circular_pattern_skip, simultaneously, grid_pattern~~ 全部实现(10-04 追加 _ex/PatternMode/PatternResultEx/SkipConfig) |
| VDE-362 | 13 | 0 | Fixed(10-04 落地) | ~~catalog, create_bolt_standard, create_socket_bolt, create_countersunk_screw, create_spring_washer, create_stud~~ 全部实现 |
+83 -1
View File
@@ -18,6 +18,7 @@
#include <vector>
#include <sstream>
#include <optional>
#include <utility>
// ProjectionView 定义在 brep/brep_drawing.h 中,drafting 需要使用它
#include "vde/brep/brep_drawing.h"
@@ -482,6 +483,43 @@ struct StandardInfo {
/// 检查标准 ID 是否有效
[[nodiscard]] bool is_valid_standard(const std::string& id);
// ═══════════════════════════════════════════════════════════
// VDE-395 §2: 自定义(企业)标准
// ═══════════════════════════════════════════════════════════
/**
* @brief 自定义(企业)标准注册表
*
* 允许用户以 id + DimensionStyle 注册公司专用标准,并参与
* `drawing::apply_standard_by_name` 解析。
*/
class CustomStandard {
public:
/// 注册/覆盖一个自定义标准;返回其元信息
static StandardInfo register_custom(const std::string& id, const DimensionStyle& style);
/// 注销自定义标准(不存在时无操作)
static void unregister(const std::string& id);
/// 列出全部自定义标准(按 id 升序)
static std::vector<StandardInfo> list_custom();
/// 取自定义标准样式(不存在返回 nullopt)
[[nodiscard]] static std::optional<DimensionStyle> style_of(const std::string& id);
};
// ═══════════════════════════════════════════════════════════
// VDE-395 §3: 标准差异比较
// ═══════════════════════════════════════════════════════════
/// 两个标准的逐字段差异
struct StandardsDiff {
std::vector<std::string> changed_fields; ///< 变化字段名(有序)
std::vector<std::pair<std::string, std::string>> value_differences; ///< {field, "old -> new"}
[[nodiscard]] bool empty() const { return changed_fields.empty(); }
};
/// 比较两个标准(名称或自定义 id)的每一个尺寸样式字段
[[nodiscard]] StandardsDiff diff_standards(const std::string& a, const std::string& b);
} // namespace standards
/// 制图标准命名空间
@@ -545,7 +583,22 @@ struct DrawingContext {
// VDE-395: real_time_std_switch + standard_compliance_check
// ═══════════════════════════════════════════════════════════
/// Real-time standard switching manager
/**
* @brief 预览切换到目标标准后会发生变化的字段(不提交)— VDE-395 §1
*
* 逐字段比较 `current` 与目标标准的内置/自定义样式,返回人类可读的
* "field: old -> new" 列表;两标准一致时为空。
*
* @param current 当前尺寸样式
* @param target_standard 目标标准名(ISO/ANSI/… 或自定义 id)
* @return 变化描述列表
*/
[[nodiscard]] std::vector<std::string> preview_changes(
const DimensionStyle& current, const std::string& target_standard);
/**
* @brief Real-time standard switching manager
*/
class RealTimeStdSwitch {
public:
RealTimeStdSwitch();
@@ -557,6 +610,8 @@ public:
[[nodiscard]] std::string current_standard() const;
/// Get switch count
[[nodiscard]] int switch_count() const;
/// Preview what switching to `standard_name` would change (no commit)
[[nodiscard]] std::vector<std::string> preview_changes(const std::string& standard_name) const;
/// Apply current standard to views
void apply_to_views(std::vector<ProjectionView>& views);
private:
@@ -572,6 +627,33 @@ private:
[[nodiscard]] StyleValidationReport standard_compliance_check(
const DimensionStyle& style, const std::string& standard_name);
// ═══════════════════════════════════════════════════════════
// VDE-395 §4: 标准图纸模板(图框 + 标题栏)
// ═══════════════════════════════════════════════════════════
/// 2D 图纸线段(mm)
struct Segment {
double x1 = 0.0, y1 = 0.0, x2 = 0.0, y2 = 0.0;
};
/// 标准图纸模板
struct SheetTemplate {
double width = 0.0; ///< 图纸宽 (mm)
double height = 0.0; ///< 图纸高 (mm)
std::vector<Segment> border; ///< 外图框线段
std::vector<Segment> title_block; ///< 标题栏线段
};
/**
* @brief 按标准与图纸规格生成图框 + 标题栏(ISO 7200 / ASME Y14.1 / JIS B 0001)
*
* @param standard_name 标准名(ISO/ANSI/JIS/GB/BS/GOST)
* @param paper_size 图纸规格(A0-A4 / Letter;未知时按 A4)
* @return 图纸模板
*/
[[nodiscard]] SheetTemplate generate_sheet_template(
const std::string& standard_name, const std::string& paper_size);
} // namespace drawing
} // namespace vde::drafting
+326 -7
View File
@@ -6,9 +6,170 @@
#include "vde_drafting/drawing_standards.h"
#include <cmath>
#include <algorithm>
#include <cctype>
#include <map>
#include <sstream>
namespace vde::drafting {
// ═══════════════════════════════════════════════════════════
// VDE-395 内部辅助:标准名解析 + 样式字段快照/比较
// ═══════════════════════════════════════════════════════════
namespace {
std::string to_upper(std::string s) {
for (auto& c : s) c = static_cast<char>(std::toupper(static_cast<unsigned char>(c)));
return s;
}
/// 内置标准样式(不区分大小写);未知返回 nullopt
std::optional<DimensionStyle> builtin_style(const std::string& name) {
const std::string n = to_upper(name);
if (n == "ISO") return IsoStandard::create_style();
if (n == "ANSI") return AnsiStandard::create_style();
if (n == "JIS") return JisStandard::create_style();
if (n == "GB") return GbStandard::create_style();
if (n == "BS" || n == "BSI") return BsStandard::create_style();
if (n == "GOST") return GostStandard::create_style();
return std::nullopt;
}
/// 解析标准(内置优先,其次自定义注册表)
std::optional<DimensionStyle> resolve_style(const std::string& name) {
if (auto s = builtin_style(name)) return s;
return standards::CustomStandard::style_of(name);
}
const char* font_name(FontStyle f) {
switch (f) {
case FontStyle::Normal: return "Normal";
case FontStyle::Italic: return "Italic";
case FontStyle::Bold: return "Bold";
}
return "?";
}
const char* arrow_name(ArrowStyle a) {
switch (a) {
case ArrowStyle::Filled: return "Filled";
case ArrowStyle::Open: return "Open";
case ArrowStyle::Oblique: return "Oblique";
case ArrowStyle::Architectural: return "Architectural";
case ArrowStyle::Dot: return "Dot";
case ArrowStyle::Integral: return "Integral";
}
return "?";
}
const char* tol_name(ToleranceFormat t) {
switch (t) {
case ToleranceFormat::None: return "None";
case ToleranceFormat::Symmetric: return "Symmetric";
case ToleranceFormat::Deviation: return "Deviation";
case ToleranceFormat::Limits: return "Limits";
case ToleranceFormat::Basic: return "Basic";
case ToleranceFormat::Reference: return "Reference";
}
return "?";
}
const char* proj_name(ProjectionAngle p) {
return p == ProjectionAngle::FirstAngle ? "FirstAngle" : "ThirdAngle";
}
const char* unit_name(DimensionUnit u) {
switch (u) {
case DimensionUnit::Millimeter: return "Millimeter";
case DimensionUnit::Inch: return "Inch";
case DimensionUnit::Centimeter: return "Centimeter";
}
return "?";
}
std::string num_str(double v) {
std::ostringstream o;
o << v;
return o.str();
}
/// DimensionStyle 的 (字段名, 值字符串) 快照,顺序稳定
std::vector<std::pair<std::string, std::string>> style_fields(const DimensionStyle& s) {
const char* b2s[] = {"false", "true"};
return {
{"text_height", num_str(s.text_height)},
{"text_font", font_name(s.text_font)},
{"text_width_factor", num_str(s.text_width_factor)},
{"text_gap", num_str(s.text_gap)},
{"arrow_length", num_str(s.arrow_length)},
{"arrow_width", num_str(s.arrow_width)},
{"arrow_angle", num_str(s.arrow_angle)},
{"arrow_style", arrow_name(s.arrow_style)},
{"line_width_thin", num_str(s.line_width_thin)},
{"line_width_thick", num_str(s.line_width_thick)},
{"line_width_medium", num_str(s.line_width_medium)},
{"extension_line_offset", num_str(s.extension_line_offset)},
{"extension_line_extend", num_str(s.extension_line_extend)},
{"visible_line", line_type_name(s.visible_line)},
{"hidden_line", line_type_name(s.hidden_line)},
{"center_line", line_type_name(s.center_line)},
{"phantom_line", line_type_name(s.phantom_line)},
{"dimension_line", line_type_name(s.dimension_line)},
{"leader_line", line_type_name(s.leader_line)},
{"section_line", line_type_name(s.section_line)},
{"cutting_plane", line_type_name(s.cutting_plane)},
{"tolerance_format", tol_name(s.tolerance_format)},
{"decimal_places", std::to_string(s.decimal_places)},
{"show_trailing_zeros", b2s[s.show_trailing_zeros ? 1 : 0]},
{"use_basic_box", b2s[s.use_basic_box ? 1 : 0]},
{"projection_angle", proj_name(s.projection_angle)},
{"unit", unit_name(s.unit)},
{"show_unit_symbol", b2s[s.show_unit_symbol ? 1 : 0]},
{"show_hidden_lines", b2s[s.show_hidden_lines ? 1 : 0]},
{"show_center_lines", b2s[s.show_center_lines ? 1 : 0]},
{"show_center_marks", b2s[s.show_center_marks ? 1 : 0]},
{"center_mark_size", num_str(s.center_mark_size)},
{"gap_between_dimension_lines", num_str(s.gap_between_dimension_lines)},
{"drawing_scale", num_str(s.drawing_scale)},
};
}
/// 两个样式的逐字段差异 {"field", "old -> new"}
std::vector<std::pair<std::string, std::string>> diff_styles(
const DimensionStyle& a, const DimensionStyle& b) {
const auto fa = style_fields(a);
const auto fb = style_fields(b);
std::vector<std::pair<std::string, std::string>> out;
const size_t n = std::min(fa.size(), fb.size());
for (size_t i = 0; i < n; ++i) {
if (fa[i].second != fb[i].second) {
out.push_back({fa[i].first, fa[i].second + " -> " + fb[i].second});
}
}
return out;
}
/// 自定义标准存储(id → 样式)
std::map<std::string, DimensionStyle>& custom_store() {
static std::map<std::string, DimensionStyle> store;
return store;
}
struct PaperDim { double w, h; };
PaperDim paper_dims(const std::string& size) {
const std::string s = to_upper(size);
if (s == "A0") return {841.0, 1189.0};
if (s == "A1") return {594.0, 841.0};
if (s == "A2") return {420.0, 594.0};
if (s == "A3") return {297.0, 420.0};
if (s == "LETTER") return {216.0, 279.0};
return {210.0, 297.0}; // A4(含未知规格兜底)
}
drawing::Segment make_segment(double x1, double y1, double x2, double y2) {
drawing::Segment s;
s.x1 = x1; s.y1 = y1; s.x2 = x2; s.y2 = y2;
return s;
}
} // namespace
// ═══════════════════════════════════════════════════════════
// Standard registry
// ═══════════════════════════════════════════════════════════
@@ -91,7 +252,8 @@ std::string AnsiStandard::description() { return "ASME Y14.5-2018 — Dimensioni
bool AnsiStandard::envelope_principle_default() { return true; }
bool AnsiStandard::mmc_default() { return false; }
std::string AnsiStandard::gdt_symbols_summary() {
return "⏤⏥◎⏊⌭⌯⌰⟂∠∥⊥⌖ Position/Profile/Orientation/Runout";
return "⏤⏥◎⏊⌭⌯⌰⟂∠∥⊥⌖ "
"位置度/轮廓度/方向/跳动 Position/Profile/Orientation/Runout";
}
std::string AnsiStandard::feature_control_frame_format() {
return "GeoChar|TolZone|MMC/LMC|DatumRef1|DatumRef2|DatumRef3";
@@ -108,6 +270,8 @@ DimensionStyle JisStandard::create_style() {
s.text_height = 3.5;
s.projection_angle = ProjectionAngle::FirstAngle;
s.unit = DimensionUnit::Millimeter;
// JIS B 0001:剖切线使用细点划线(chain line),区别于默认双点划线
s.cutting_plane = LineType::Chain;
return s;
}
std::string JisStandard::description() { return "JIS B 0001 — Mechanical drawing"; }
@@ -202,12 +366,7 @@ DrawingContext apply_standard_by_name(
const std::string& standard_name,
const StandardOptions& options) {
DimensionStyle style;
if (standard_name == "ISO") style = IsoStandard::create_style();
else if (standard_name == "ANSI") style = AnsiStandard::create_style();
else if (standard_name == "JIS") style = JisStandard::create_style();
else if (standard_name == "GB") style = GbStandard::create_style();
else if (standard_name == "BS") style = BsStandard::create_style();
else if (standard_name == "GOST") style = GostStandard::create_style();
if (auto resolved = resolve_style(standard_name)) style = *resolved;
else style = IsoStandard::create_style();
DrawingContext ctx = apply_standard(views, style, options);
@@ -319,4 +478,164 @@ std::string GostStandard::paper_size_spec(const std::string& size_name) {
return "";
}
// ═══════════════════════════════════════════════════════════
// VDE-395 §2: 自定义(企业)标准
// ═══════════════════════════════════════════════════════════
namespace standards {
StandardInfo CustomStandard::register_custom(const std::string& id, const DimensionStyle& style) {
custom_store()[id] = style;
StandardInfo info;
info.id = id;
info.name = id;
info.description = "Custom standard: " + id;
info.projection = style.projection_angle;
info.preferred_unit = style.unit;
info.supported_paper_sizes = {"A0", "A1", "A2", "A3", "A4"};
return info;
}
void CustomStandard::unregister(const std::string& id) {
custom_store().erase(id);
}
std::vector<StandardInfo> CustomStandard::list_custom() {
std::vector<StandardInfo> out;
out.reserve(custom_store().size());
for (const auto& [id, style] : custom_store()) { // std::map → id 升序
StandardInfo info;
info.id = id;
info.name = id;
info.description = "Custom standard: " + id;
info.projection = style.projection_angle;
info.preferred_unit = style.unit;
info.supported_paper_sizes = {"A0", "A1", "A2", "A3", "A4"};
out.push_back(std::move(info));
}
return out;
}
std::optional<DimensionStyle> CustomStandard::style_of(const std::string& id) {
auto it = custom_store().find(id);
if (it == custom_store().end()) return std::nullopt;
return it->second;
}
StandardsDiff diff_standards(const std::string& a, const std::string& b) {
StandardsDiff diff;
const auto sa = resolve_style(a);
const auto sb = resolve_style(b);
if (!sa || !sb) return diff;
for (const auto& [field, change] : diff_styles(*sa, *sb)) {
diff.changed_fields.push_back(field);
diff.value_differences.push_back({field, change});
}
return diff;
}
} // namespace standards
// ═══════════════════════════════════════════════════════════
// VDE-395 §1: 标准切换(预览 + 实时切换)
// ═══════════════════════════════════════════════════════════
namespace drawing {
std::vector<std::string> preview_changes(
const DimensionStyle& current, const std::string& target_standard) {
std::vector<std::string> out;
const auto target = resolve_style(target_standard);
if (!target) return out;
for (const auto& [field, change] : diff_styles(current, *target)) {
out.push_back(field + ": " + change);
}
return out;
}
RealTimeStdSwitch::RealTimeStdSwitch()
: current_standard_("ISO"), current_style_(IsoStandard::create_style()), switch_count_(0) {}
bool RealTimeStdSwitch::switch_to(const std::string& standard_name) {
auto style = resolve_style(standard_name);
if (!style) return false;
current_standard_ = standard_name;
current_style_ = *style;
++switch_count_;
return true;
}
DimensionStyle RealTimeStdSwitch::current_style() const { return current_style_; }
std::string RealTimeStdSwitch::current_standard() const { return current_standard_; }
int RealTimeStdSwitch::switch_count() const { return switch_count_; }
std::vector<std::string> RealTimeStdSwitch::preview_changes(const std::string& standard_name) const {
return drawing::preview_changes(current_style_, standard_name);
}
void RealTimeStdSwitch::apply_to_views(std::vector<ProjectionView>& views) {
for (auto& v : views) {
v.scale = current_style_.drawing_scale;
}
}
StyleValidationReport standard_compliance_check(
const DimensionStyle& style, const std::string& standard_name) {
StyleValidationReport report = validate_style_detailed(style, standard_name);
report.standard_name = standard_name;
return report;
}
// ═══════════════════════════════════════════════════════════
// VDE-395 §4: 标准图纸模板
// ═══════════════════════════════════════════════════════════
SheetTemplate generate_sheet_template(
const std::string& standard_name, const std::string& paper_size) {
const PaperDim dim = paper_dims(paper_size);
const std::string s = to_upper(standard_name);
SheetTemplate t;
t.width = dim.w;
t.height = dim.h;
// 装订边(左)通常 20mm,其余 10mm;ANSI 四边 10mm
const double m_left = (s == "ANSI") ? 10.0 : 20.0;
const double m_other = 10.0;
const double x0 = m_left;
const double y0 = m_other;
const double x1 = dim.w - m_other;
const double y1 = dim.h - m_other;
t.border = {
make_segment(x0, y0, x1, y0),
make_segment(x1, y0, x1, y1),
make_segment(x1, y1, x0, y1),
make_segment(x0, y1, x0, y0),
};
// 标题栏:右下角。ISO 7200 高 55mm / 宽 180mm;ASME Y14.1 高 32mm
const double tb_h = (s == "ANSI") ? 32.0 : 55.0;
const double tb_w = std::min(180.0, x1 - x0);
const double bx0 = x1 - tb_w;
const double by0 = y0;
const double bx1 = x1;
const double by1 = y0 + tb_h;
t.title_block = {
make_segment(bx0, by0, bx1, by0),
make_segment(bx1, by0, bx1, by1),
make_segment(bx1, by1, bx0, by1),
make_segment(bx0, by1, bx0, by0),
make_segment(bx0, by0 + tb_h * 0.5, bx1, by0 + tb_h * 0.5), // 分栏横线
make_segment(bx0 + tb_w * 0.5, by0, bx0 + tb_w * 0.5, by1), // 分栏竖线
};
return t;
}
} // namespace drawing
} // namespace vde::drafting
+161
View File
@@ -1146,3 +1146,164 @@ TEST(DrawingStandardsTest, LineTypeEnumValues) {
EXPECT_STREQ(line_type_name(LineType::DoubleChain), "DoubleChain");
EXPECT_STREQ(line_type_name(LineType::Dotted), "Dotted");
}
// ── VDE-395 §3:标准差异比较 ──
TEST(DrawingStandardsTest, DiffIsoVsAnsiReportsProjectionChange) {
auto d = standards::diff_standards("ISO", "ANSI");
ASSERT_FALSE(d.empty());
EXPECT_EQ(d.changed_fields.size(), d.value_differences.size()); // 一一对应
bool has_proj = false;
for (size_t i = 0; i < d.changed_fields.size(); ++i) {
EXPECT_EQ(d.value_differences[i].first, d.changed_fields[i]); // 字段名一致
EXPECT_NE(d.value_differences[i].second.find(" -> "), std::string::npos);
if (d.changed_fields[i] == "projection_angle") {
has_proj = true;
EXPECT_EQ(d.value_differences[i].second, "FirstAngle -> ThirdAngle");
}
}
EXPECT_TRUE(has_proj);
// 同一标准 → 无差异
EXPECT_TRUE(standards::diff_standards("ISO", "ISO").empty());
EXPECT_EQ(standards::diff_standards("ISO", "ISO").value_differences.size(), 0u);
}
// ── VDE-395 §2:自定义标准注册 / 注销 / 列表 / 参与解析 ──
TEST(DrawingStandardsTest, CustomStandardRegisterListApplyUnregister) {
standards::CustomStandard::unregister("acme_custom"); // 清理可能的残留
DimensionStyle cs = IsoStandard::create_style();
cs.text_height = 2.5;
cs.projection_angle = ProjectionAngle::ThirdAngle;
standards::StandardInfo info = standards::CustomStandard::register_custom("acme_custom", cs);
EXPECT_EQ(info.id, "acme_custom");
EXPECT_EQ(info.projection, ProjectionAngle::ThirdAngle);
auto lst = standards::CustomStandard::list_custom();
ASSERT_EQ(lst.size(), 1u);
EXPECT_EQ(lst[0].id, "acme_custom");
auto got = standards::CustomStandard::style_of("acme_custom");
ASSERT_TRUE(got.has_value());
EXPECT_DOUBLE_EQ(got->text_height, 2.5);
// 参与 apply_standard_by_name 解析
std::vector<ProjectionView> views;
StandardOptions opts;
auto ctx = drawing::apply_standard_by_name(views, "acme_custom", opts);
EXPECT_EQ(ctx.standard_name, "acme_custom");
EXPECT_DOUBLE_EQ(ctx.style.text_height, 2.5);
EXPECT_EQ(ctx.style.projection_angle, ProjectionAngle::ThirdAngle);
// 同 id 覆盖 → 仍只有 1 条
cs.text_height = 4.0;
standards::CustomStandard::register_custom("acme_custom", cs);
EXPECT_EQ(standards::CustomStandard::list_custom().size(), 1u);
EXPECT_DOUBLE_EQ(standards::CustomStandard::style_of("acme_custom")->text_height, 4.0);
// 注销 → 列表空、样式不可得、解析回退 ISO
standards::CustomStandard::unregister("acme_custom");
EXPECT_TRUE(standards::CustomStandard::list_custom().empty());
EXPECT_FALSE(standards::CustomStandard::style_of("acme_custom").has_value());
auto fallback = drawing::apply_standard_by_name(views, "acme_custom", opts);
EXPECT_DOUBLE_EQ(fallback.style.text_height, IsoStandard::create_style().text_height);
}
// ── VDE-395 §1:切换预览 + 实时切换 ──
TEST(DrawingStandardsTest, StandardPreviewAndRealTimeSwitch) {
drawing::RealTimeStdSwitch sw;
EXPECT_EQ(sw.current_standard(), "ISO");
EXPECT_EQ(sw.switch_count(), 0);
// 预览不提交
auto pv = sw.preview_changes("ANSI");
EXPECT_FALSE(pv.empty());
EXPECT_EQ(sw.current_standard(), "ISO");
EXPECT_EQ(sw.switch_count(), 0);
bool has_proj = false;
for (const auto& line : pv) {
if (line.find("projection_angle") != std::string::npos) {
has_proj = true;
EXPECT_NE(line.find("FirstAngle -> ThirdAngle"), std::string::npos);
}
}
EXPECT_TRUE(has_proj);
// 提交切换
EXPECT_TRUE(sw.switch_to("ANSI"));
EXPECT_EQ(sw.current_standard(), "ANSI");
EXPECT_EQ(sw.switch_count(), 1);
EXPECT_EQ(sw.current_style().projection_angle, ProjectionAngle::ThirdAngle);
// 未知标准 → false,状态不变
EXPECT_FALSE(sw.switch_to("NO_SUCH_STD"));
EXPECT_EQ(sw.switch_count(), 1);
EXPECT_EQ(sw.current_standard(), "ANSI");
// apply_to_views 使用当前样式比例
std::vector<ProjectionView> views(1);
sw.apply_to_views(views);
EXPECT_DOUBLE_EQ(views[0].scale, sw.current_style().drawing_scale);
// 自由函数:ISO→ISO 无变化
EXPECT_TRUE(drawing::preview_changes(IsoStandard::create_style(), "ISO").empty());
EXPECT_FALSE(drawing::preview_changes(IsoStandard::create_style(), "ANSI").empty());
}
// ── VDE-395:标准合规检查(此前仅声明未定义)──
TEST(DrawingStandardsTest, StandardComplianceCheckReport) {
auto ok = drawing::standard_compliance_check(IsoStandard::create_style(), "ISO");
EXPECT_EQ(ok.standard_name, "ISO");
EXPECT_GE(ok.pass_rate, 0.0);
EXPECT_LE(ok.pass_rate, 1.0);
EXPECT_EQ(ok.violations.size(), 0u); // ISO 样式对 ISO 标准 → 无告警
auto warn = drawing::standard_compliance_check(IsoStandard::create_style(), "ANSI");
EXPECT_EQ(warn.standard_name, "ANSI");
EXPECT_GE(warn.violations.size(), 1u); // 第一角 vs ANSI 第三角
EXPECT_LT(warn.pass_rate, 1.0);
}
// ── VDE-395 §4:图纸模板(图框 + 标题栏)──
TEST(DrawingStandardsTest, SheetTemplateBorderAndTitleBlock) {
auto t = drawing::generate_sheet_template("ISO", "A4");
EXPECT_DOUBLE_EQ(t.width, 210.0);
EXPECT_DOUBLE_EQ(t.height, 297.0);
ASSERT_EQ(t.border.size(), 4u);
EXPECT_FALSE(t.title_block.empty());
// 图框留边:左 20mm、其余 10mm
for (const auto& s : t.border) {
EXPECT_GE(s.x1, 20.0 - 1e-9);
EXPECT_LE(s.x1, 200.0 + 1e-9);
EXPECT_GE(s.y1, 10.0 - 1e-9);
EXPECT_LE(s.y1, 287.0 + 1e-9);
}
// 标题栏贴右下角
double max_x = 0.0, max_y = 0.0;
for (const auto& s : t.title_block) {
if (s.x1 > max_x) max_x = s.x1;
if (s.x2 > max_x) max_x = s.x2;
if (s.y1 > max_y) max_y = s.y1;
if (s.y2 > max_y) max_y = s.y2;
}
EXPECT_NEAR(max_x, 200.0, 1e-9); // x1 = width - 10
EXPECT_NEAR(max_y, 10.0 + 55.0, 1e-9); // ISO 标题栏高 55mm
// 图纸规格
auto a3 = drawing::generate_sheet_template("ISO", "A3");
EXPECT_DOUBLE_EQ(a3.width, 297.0);
EXPECT_DOUBLE_EQ(a3.height, 420.0);
// ANSI 标题栏高 32mm
auto ansi = drawing::generate_sheet_template("ANSI", "A4");
double ansi_max_y = 0.0;
for (const auto& s : ansi.title_block) {
if (s.y1 > ansi_max_y) ansi_max_y = s.y1;
if (s.y2 > ansi_max_y) ansi_max_y = s.y2;
}
EXPECT_NEAR(ansi_max_y, 10.0 + 32.0, 1e-9);
}