309 lines
12 KiB
C++
309 lines
12 KiB
C++
/**
|
||
* @file test_drafting_intelligence.cpp
|
||
* @brief VDE B-5:高级制图智能化(自动排布 / 公差链 / 标准合规 / 图纸一致性)
|
||
*/
|
||
#include <gtest/gtest.h>
|
||
|
||
#include "vde/brep/auto_dimensioning.h"
|
||
#include "vde/brep/brep_drawing.h"
|
||
#include "vde_drafting/drawing_sheet.h"
|
||
#include "vde_drafting/drawing_standards.h"
|
||
|
||
#include <algorithm>
|
||
#include <cmath>
|
||
#include <iostream>
|
||
#include <string>
|
||
#include <vector>
|
||
|
||
using namespace vde;
|
||
using namespace vde::drafting;
|
||
|
||
namespace {
|
||
|
||
// ── B-5.1 自动排布辅助 ──
|
||
brep::Dimension make_dim(int i, double x, double y) {
|
||
brep::Dimension d;
|
||
d.type = brep::DimensionType::Linear;
|
||
d.value = 5.0 + static_cast<double>(i);
|
||
d.text_position = core::Point3D(x, y, 0.0);
|
||
d.view_name = "front";
|
||
return d;
|
||
}
|
||
|
||
std::vector<brep::Dimension> clustered_dims(int n) {
|
||
std::vector<brep::Dimension> dims;
|
||
dims.reserve(static_cast<size_t>(n));
|
||
// 5 列 × (n/5) 行,锚点间距 6mm(文字框宽 ~10mm → 必然重叠)
|
||
for (int i = 0; i < n; ++i) {
|
||
const double x = 40.0 + 6.0 * (i % 5);
|
||
const double y = 40.0 + 6.0 * (i / 5);
|
||
dims.push_back(make_dim(i, x, y));
|
||
}
|
||
return dims;
|
||
}
|
||
|
||
// ── B-5.3 合规样本辅助 ──
|
||
DraftingStandardSample compliant_sample() {
|
||
DraftingStandardSample s;
|
||
s.standard = 4; // GB
|
||
DraftingLayerDef dim;
|
||
dim.name = "DIM";
|
||
dim.role = "dimension";
|
||
dim.line_type = "Continuous";
|
||
dim.line_width = 0.25;
|
||
s.layers.push_back(dim);
|
||
DraftingDimensionEntry d;
|
||
d.id = "d1";
|
||
d.x = 60.0;
|
||
d.y = 40.0;
|
||
d.text = "25";
|
||
d.text_height = 3.5;
|
||
d.angle_deg = 0.0;
|
||
d.arrow_length = 3.5;
|
||
d.arrow_width = 1.0;
|
||
d.line_spacing_to_prev = 10.0;
|
||
d.layer = "DIM";
|
||
s.dimensions.push_back(d);
|
||
return s;
|
||
}
|
||
|
||
int count_rule(const DraftingComplianceReport& r, DraftingRuleId id) {
|
||
int n = 0;
|
||
for (const auto& v : r.violations)
|
||
if (v.rule == id) ++n;
|
||
return n;
|
||
}
|
||
|
||
} // namespace
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// B-5.1 尺寸自动排布
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
TEST(DraftingB5Test, AutoLayout_20Dimensions_NoOverlapNoOverflowStable) {
|
||
const auto input = clustered_dims(20);
|
||
brep::AutoLayoutOptions opts;
|
||
const int before20 = brep::count_dimension_overlaps(input, opts);
|
||
std::cout << "[B5] layout20 before=" << before20;
|
||
EXPECT_GT(before20, 0) << "before: clustered anchors should overlap";
|
||
const auto res = brep::auto_layout_dimensions(input, opts);
|
||
std::cout << " after=" << res.overlap_count << "/oob="
|
||
<< res.out_of_bounds_count << " stable=" << res.stable << "\n";
|
||
EXPECT_EQ(res.overlap_count, 0) << res.to_string();
|
||
EXPECT_EQ(res.out_of_bounds_count, 0) << res.to_string();
|
||
EXPECT_TRUE(res.stable) << res.to_string();
|
||
EXPECT_TRUE(res.ok());
|
||
EXPECT_EQ(res.dimensions.size(), input.size());
|
||
// 全部尺寸落在可读区域(同视图)
|
||
EXPECT_TRUE(brep::dimensions_within_bounds(res.dimensions, opts));
|
||
// 重复排布逐位一致
|
||
const auto res2 = brep::auto_layout_dimensions(input, opts);
|
||
for (size_t i = 0; i < res.dimensions.size(); ++i) {
|
||
EXPECT_DOUBLE_EQ(res.dimensions[i].text_position.x(),
|
||
res2.dimensions[i].text_position.x());
|
||
EXPECT_DOUBLE_EQ(res.dimensions[i].text_position.y(),
|
||
res2.dimensions[i].text_position.y());
|
||
}
|
||
}
|
||
|
||
TEST(DraftingB5Test, AutoLayout_50Dimensions_NoOverlapNoOverflowStable) {
|
||
const auto input = clustered_dims(50);
|
||
brep::AutoLayoutOptions opts;
|
||
const int before50 = brep::count_dimension_overlaps(input, opts);
|
||
std::cout << " [B5] layout50 before=" << before50;
|
||
EXPECT_GT(before50, 0);
|
||
const auto res = brep::auto_layout_dimensions(input, opts);
|
||
EXPECT_EQ(res.overlap_count, 0) << res.to_string();
|
||
EXPECT_EQ(res.out_of_bounds_count, 0) << res.to_string();
|
||
EXPECT_TRUE(res.stable) << res.to_string();
|
||
EXPECT_EQ(res.dimensions.size(), input.size());
|
||
}
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// B-5.3 制图标准合规(10 类违规 10/10,合规 0 误报)
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
TEST(DraftingB5Test, Compliance_CompliantSample_NoFalsePositive) {
|
||
for (int std : {4, 0, 1}) {
|
||
auto s = compliant_sample();
|
||
s.standard = std;
|
||
const auto rep = check_drafting_compliance(s);
|
||
EXPECT_TRUE(rep.ok()) << rep.to_string();
|
||
EXPECT_EQ(rep.violations.size(), 0u);
|
||
EXPECT_EQ(rep.checked_rules, 10);
|
||
}
|
||
}
|
||
|
||
TEST(DraftingB5Test, Compliance_TenViolationClasses_AllDetected) {
|
||
const std::vector<DraftingRuleId> rules{
|
||
DraftingRuleId::DimensionLineSpacing, DraftingRuleId::ArrowLength,
|
||
DraftingRuleId::ArrowWidthRatio, DraftingRuleId::TextHeightSeries,
|
||
DraftingRuleId::TextOrientation, DraftingRuleId::MissingLayer,
|
||
DraftingRuleId::LineWidthMapping, DraftingRuleId::LineTypeMapping,
|
||
DraftingRuleId::UnitSuffix, DraftingRuleId::TextEmpty};
|
||
int detected = 0;
|
||
for (size_t k = 0; k < rules.size(); ++k) {
|
||
auto s = compliant_sample();
|
||
switch (rules[k]) {
|
||
case DraftingRuleId::DimensionLineSpacing:
|
||
s.dimensions[0].line_spacing_to_prev = 3.0; break;
|
||
case DraftingRuleId::ArrowLength:
|
||
s.dimensions[0].arrow_length = 6.0; break;
|
||
case DraftingRuleId::ArrowWidthRatio:
|
||
s.dimensions[0].arrow_width = 2.5; break;
|
||
case DraftingRuleId::TextHeightSeries:
|
||
s.dimensions[0].text_height = 4.2; break;
|
||
case DraftingRuleId::TextOrientation:
|
||
s.dimensions[0].angle_deg = 45.0; break;
|
||
case DraftingRuleId::MissingLayer:
|
||
s.dimensions[0].layer = "UNKNOWN"; break;
|
||
case DraftingRuleId::LineWidthMapping:
|
||
s.layers[0].line_width = 0.50; break;
|
||
case DraftingRuleId::LineTypeMapping:
|
||
s.layers[0].line_type = "Dashed"; break;
|
||
case DraftingRuleId::UnitSuffix:
|
||
s.unit_mm = false; // 文本 "25" 无 in 后缀
|
||
break;
|
||
case DraftingRuleId::TextEmpty:
|
||
s.dimensions[0].text.clear(); break;
|
||
}
|
||
const auto rep = check_drafting_compliance(s);
|
||
const int n = count_rule(rep, rules[k]);
|
||
EXPECT_EQ(n, 1) << "rule " << drafting_rule_id_name(rules[k]) << "\n"
|
||
<< rep.to_string();
|
||
if (n == 1) ++detected;
|
||
}
|
||
EXPECT_EQ(detected, 10);
|
||
}
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// B-5.4 图纸一致性
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
namespace {
|
||
|
||
ViewLayout::ViewPlacement placement(const std::string& name, double x,
|
||
double y, double w, double h,
|
||
bool with_geometry = true) {
|
||
ViewLayout::ViewPlacement p;
|
||
p.view_name = name;
|
||
p.pos_x = x;
|
||
p.pos_y = y;
|
||
p.width = w;
|
||
p.height = h;
|
||
if (with_geometry)
|
||
p.view.segments.push_back(brep::DrawSegment{0.0, 0.0, 10.0, 0.0, false});
|
||
return p;
|
||
}
|
||
|
||
DrawingSheet a3_sheet() {
|
||
DrawingSheet s;
|
||
s.size = SheetSize::A3;
|
||
s.orientation = SheetOrientation::Landscape;
|
||
s.margin_left = 20.0;
|
||
s.margin_right = 10.0;
|
||
s.margin_top = 10.0;
|
||
s.margin_bottom = 10.0;
|
||
return s;
|
||
}
|
||
|
||
AnnotationView ann_view(const std::string& view_name, const std::string& text,
|
||
const std::string& style = "DIM") {
|
||
AnnotationView av;
|
||
av.view_name = view_name;
|
||
Annotation a;
|
||
a.type = AnnotationType::Dimension;
|
||
a.text = text;
|
||
a.style = style;
|
||
av.annotations.push_back(a);
|
||
return av;
|
||
}
|
||
|
||
ViewLayout consistent_layout() {
|
||
ViewLayout layout;
|
||
layout.sheet = a3_sheet();
|
||
layout.placements.push_back(placement("front", 60.0, 60.0, 150.0, 100.0));
|
||
layout.placements.push_back(placement("top", 60.0, 180.0, 150.0, 80.0));
|
||
layout.annotations.push_back(ann_view("front", "25"));
|
||
return layout;
|
||
}
|
||
|
||
int count_issue(const DrawingConsistencyReport& r, const std::string& rule) {
|
||
int n = 0;
|
||
for (const auto& i : r.issues)
|
||
if (i.rule == rule) ++n;
|
||
return n;
|
||
}
|
||
|
||
} // namespace
|
||
|
||
TEST(DraftingB5Test, Consistency_CompliantLayout_NoIssue) {
|
||
const auto layout = consistent_layout();
|
||
DrawingBom bom;
|
||
bom.items.push_back(BomItem{"P-001", "plate", 2, {"front"}});
|
||
const auto rep = check_drawing_consistency(layout, {"DIM"}, &bom);
|
||
EXPECT_TRUE(rep.ok()) << rep.to_string();
|
||
}
|
||
|
||
TEST(DraftingB5Test, Consistency_DetectsAllIssueClasses) {
|
||
// C1 重复视图名
|
||
{
|
||
auto layout = consistent_layout();
|
||
layout.placements.push_back(
|
||
placement("front", 240.0, 60.0, 100.0, 80.0));
|
||
const auto rep = check_drawing_consistency(layout, {"DIM"}, nullptr);
|
||
EXPECT_EQ(count_issue(rep, "duplicate_view"), 1) << rep.to_string();
|
||
}
|
||
// C2 可见视图无几何
|
||
{
|
||
auto layout = consistent_layout();
|
||
layout.placements[0].view.segments.clear();
|
||
const auto rep = check_drawing_consistency(layout, {"DIM"}, nullptr);
|
||
EXPECT_EQ(count_issue(rep, "empty_view_geometry"), 1) << rep.to_string();
|
||
}
|
||
// C3 孤立标注
|
||
{
|
||
auto layout = consistent_layout();
|
||
layout.annotations.push_back(ann_view("missing", "R5"));
|
||
const auto rep = check_drawing_consistency(layout, {"DIM"}, nullptr);
|
||
EXPECT_EQ(count_issue(rep, "annotation_orphan"), 1) << rep.to_string();
|
||
}
|
||
// C4 标注空文本
|
||
{
|
||
auto layout = consistent_layout();
|
||
layout.annotations[0].annotations[0].text.clear();
|
||
const auto rep = check_drawing_consistency(layout, {"DIM"}, nullptr);
|
||
EXPECT_EQ(count_issue(rep, "annotation_empty_text"), 1)
|
||
<< rep.to_string();
|
||
}
|
||
// C5 标注引用未定义图层
|
||
{
|
||
auto layout = consistent_layout();
|
||
layout.annotations.push_back(ann_view("front", "25", "MISSING"));
|
||
const auto rep = check_drawing_consistency(layout, {"DIM"}, nullptr);
|
||
EXPECT_EQ(count_issue(rep, "annotation_layer"), 1) << rep.to_string();
|
||
}
|
||
// C6/C7/C8/C9 BOM
|
||
{
|
||
auto layout = consistent_layout();
|
||
DrawingBom bom;
|
||
bom.items.push_back(BomItem{"P-001", "a", 2, {"missing"}}); // C6
|
||
bom.items.push_back(BomItem{"P-002", "b", 0, {"front"}}); // C7
|
||
bom.items.push_back(BomItem{"P-001", "c", 1, {"front"}}); // C8
|
||
bom.items.push_back(BomItem{"P-003", "d", 1, {}}); // C9
|
||
const auto rep = check_drawing_consistency(layout, {"DIM"}, &bom);
|
||
EXPECT_EQ(count_issue(rep, "bom_view_missing"), 1) << rep.to_string();
|
||
EXPECT_EQ(count_issue(rep, "bom_quantity"), 1) << rep.to_string();
|
||
EXPECT_EQ(count_issue(rep, "bom_duplicate_part"), 1) << rep.to_string();
|
||
EXPECT_EQ(count_issue(rep, "bom_item_no_view"), 1) << rep.to_string();
|
||
}
|
||
// C10 视图越出图幅
|
||
{
|
||
auto layout = consistent_layout();
|
||
layout.placements.push_back(
|
||
placement("outside", 380.0, 60.0, 100.0, 80.0));
|
||
const auto rep = check_drawing_consistency(layout, {"DIM"}, nullptr);
|
||
EXPECT_EQ(count_issue(rep, "view_outside_sheet"), 1) << rep.to_string();
|
||
}
|
||
}
|