263 lines
12 KiB
C++
263 lines
12 KiB
C++
#pragma once
|
||
/**
|
||
* @file drawing_sheet.h
|
||
* @brief 工程图纸图幅与标注定义(VDE-380)
|
||
*
|
||
* 从 brep::brep_drawing 中拆分出的"图纸级"概念:
|
||
* - DrawingSheet: 图幅尺寸 / 页边距 / 标题栏 / 作者 / 修订
|
||
* - Annotation / AnnotationType: 视图上的标注(尺寸 / 注释 / 指引线 / 粗糙度符号等)
|
||
* - ViewLayout / ViewPlacement: 视图在图纸上的布局
|
||
* - AnnotationView: 带标注的投影视图
|
||
*
|
||
* 纯几何投影算法(generate_views / section_view / offset_section_view /
|
||
* export_dxf)仍保留在内核 vde::brep::brep_drawing 中;本头文件通过
|
||
* using brep::ProjectionView 复用内核的投影视图类型。
|
||
*
|
||
* @ingroup drafting
|
||
*/
|
||
#include "vde/brep/brep.h"
|
||
#include "vde/brep/brep_drawing.h"
|
||
#include "vde/core/point.h"
|
||
#include <vector>
|
||
#include <string>
|
||
|
||
namespace vde::drafting {
|
||
|
||
// 复用内核的投影视图与 B-Rep 模型类型
|
||
using brep::ProjectionView;
|
||
using brep::BrepModel;
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// 图幅定义
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
/// Sheet size enumeration
|
||
enum class SheetSize { A0, A1, A2, A3, A4, Letter, Tabloid, Custom };
|
||
|
||
/// Sheet orientation
|
||
enum class SheetOrientation { Portrait, Landscape };
|
||
|
||
/// Drawing sheet definition
|
||
struct DrawingSheet {
|
||
SheetSize size = SheetSize::A3;
|
||
SheetOrientation orientation = SheetOrientation::Landscape;
|
||
double custom_width = 0.0;
|
||
double custom_height = 0.0;
|
||
double margin_left = 20.0;
|
||
double margin_right = 10.0;
|
||
double margin_top = 10.0;
|
||
double margin_bottom = 10.0;
|
||
std::string title;
|
||
std::string author;
|
||
std::string revision = "A";
|
||
double scale = 1.0;
|
||
bool third_angle = true; ///< true = 第三角投影 (ASME Y14.3),false = 第一角投影 (ISO 128/GB)
|
||
std::string material; ///< 标题栏材料
|
||
std::string date; ///< 标题栏日期
|
||
};
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// 标注
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
/// Annotation types for drawing views
|
||
enum class AnnotationType { Dimension, Note, Leader, SurfaceFinish, WeldSymbol, Datum, FeatureControlFrame };
|
||
|
||
/// A single annotation on a drawing view
|
||
struct Annotation {
|
||
AnnotationType type = AnnotationType::Note;
|
||
std::string text;
|
||
double x = 0.0, y = 0.0; ///< position in view coordinates
|
||
double angle = 0.0; ///< text rotation angle (degrees)
|
||
double text_height = 3.5;
|
||
std::string style; ///< dimension style name
|
||
};
|
||
|
||
/// Annotation view: a projection view with annotations
|
||
struct AnnotationView {
|
||
ProjectionView base_view;
|
||
std::vector<Annotation> annotations;
|
||
std::string view_name;
|
||
};
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// 视图布局
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
/// Viewport layout: where each view sits on the sheet
|
||
struct ViewLayout {
|
||
struct ViewPlacement {
|
||
std::string view_name; ///< "front", "top", "right", "iso"
|
||
double pos_x = 0.0; ///< position on sheet (mm from bottom-left)
|
||
double pos_y = 0.0;
|
||
double width = 0.0; ///< viewport size (mm)
|
||
double height = 0.0;
|
||
double scale = 1.0;
|
||
bool visible = true;
|
||
ProjectionView view; ///< 该位置上的投影几何(线段为局部二维坐标)
|
||
};
|
||
std::vector<ViewPlacement> placements;
|
||
DrawingSheet sheet;
|
||
std::vector<AnnotationView> annotations; ///< 与 placements 对应的带标注视图
|
||
};
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// 标题栏
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
/// Title block fields (ISO 7200 / ASME Y14.1)
|
||
struct TitleBlock {
|
||
std::string drawing_name;
|
||
std::string material;
|
||
std::string revision = "A";
|
||
double scale = 1.0;
|
||
std::string author;
|
||
std::string date;
|
||
};
|
||
|
||
// ── ISO 7200 完整标题栏字段(VDE-441)───────────────────────
|
||
|
||
/// ISO 8601 日历日期(标题栏"批准日期"等)
|
||
struct Date {
|
||
int year = 0; ///< 年(如 2026)
|
||
int month = 0; ///< 月(1-12)
|
||
int day = 0; ///< 日(1-31)
|
||
|
||
/// 是否为合法日期(含闰年与月长校验)
|
||
[[nodiscard]] bool is_valid() const;
|
||
/// ISO 8601 字符串 "YYYY-MM-DD"(非法日期返回空串)
|
||
[[nodiscard]] std::string to_iso_string() const;
|
||
};
|
||
|
||
/**
|
||
* @brief ISO 7200 完整标题栏字段(VDE-441)
|
||
*
|
||
* 覆盖 ISO 7200 规定的图号、名称、材料、表面处理、比例、质量、批准日期、
|
||
* 未注公差(ISO 2768)与投影符号等数据字段。
|
||
*/
|
||
struct TitleBlockFields {
|
||
std::string drawing_number; ///< 图号
|
||
std::string title; ///< 图样名称
|
||
std::string material; ///< 材料
|
||
std::string surface_treatment; ///< 表面处理
|
||
double scale = 1.0; ///< 比例
|
||
double mass_kg = 0.0; ///< 质量(kg)
|
||
Date approval_date; ///< 批准日期
|
||
std::string general_tolerance; ///< 未注公差(如 "ISO 2768-mK")
|
||
std::string projection_symbol; ///< 投影符号(第一角/第三角)
|
||
|
||
/// 必填字段(图号、名称、比例、合法日期)是否齐备
|
||
[[nodiscard]] bool is_complete() const;
|
||
};
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// 图纸布局 API
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
/// Create a standard view layout on a drawing sheet
|
||
/// @param views The projection views to place
|
||
/// @param sheet_size Sheet size (default A3)
|
||
/// @return ViewLayout with auto-positioned views
|
||
[[nodiscard]] ViewLayout view_layout(const std::vector<ProjectionView>& views,
|
||
SheetSize sheet_size = SheetSize::A3);
|
||
|
||
/// Create an annotation view with automatically generated dimensions
|
||
/// @param view Base projection view
|
||
/// @param body B-Rep model (for dimension extraction)
|
||
/// @return AnnotationView with dimensions, notes, and center marks
|
||
[[nodiscard]] AnnotationView annotation_view(const ProjectionView& view,
|
||
const BrepModel& body);
|
||
|
||
/// Setup a complete drawing sheet with views, layout, title block
|
||
/// @param views All projection views
|
||
/// @param body B-Rep model
|
||
/// @param sheet Drawing sheet configuration
|
||
/// @return Fully configured ViewLayout with annotations
|
||
[[nodiscard]] ViewLayout drawing_sheet_setup(const std::vector<ProjectionView>& views,
|
||
const BrepModel& body,
|
||
const DrawingSheet& sheet);
|
||
|
||
// ── VDE-380 提案 API ────────────────────────────────────────
|
||
|
||
/// Auto-position views on a sheet following ISO 128 / ASME Y14.3 conventions.
|
||
///
|
||
/// 第一角投影(third_angle=false,ISO/GB):顶视图置于前视图下方、右视图置于左侧;
|
||
/// 第三角投影(third_angle=true,ASME):顶视图在前视图上方、右视图在右侧。
|
||
/// 未知视图名(section/detail 等)沿图纸右侧竖向排布。
|
||
/// @param views The projection views to place
|
||
/// @param sheet Sheet configuration (margins / scale / projection angle)
|
||
/// @return ViewPlacement list (含几何),坐标单位为 mm,原点在图幅左下角
|
||
[[nodiscard]] std::vector<ViewLayout::ViewPlacement>
|
||
layout_views_on_sheet(const std::vector<ProjectionView>& views,
|
||
const DrawingSheet& sheet);
|
||
|
||
/// Apply a title block to a drawing sheet (copies fields into the sheet).
|
||
void add_title_block(DrawingSheet& sheet, const TitleBlock& block);
|
||
|
||
/// Export a laid-out drawing sheet to a valid PDF file (PDF 1.7).
|
||
///
|
||
/// 页面大小取自 layout.sheet;图框、标题栏、各视图的可见线(实线)与
|
||
/// 隐藏线(虚线)按坐标写入内容流。
|
||
/// @param filepath Output file path (.pdf)
|
||
/// @param layout Layout produced by view_layout / layout_views_on_sheet
|
||
/// @return true on success
|
||
bool export_pdf(const std::string& filepath, const ViewLayout& layout);
|
||
|
||
// ═══════════════════════════════════════════════════════════
|
||
// VDE B-5:图纸一致性检查(视图 / 标注 / 图层 / BOM)
|
||
// ═══════════════════════════════════════════════════════════
|
||
|
||
/// BOM 条目(图纸装配件清单)
|
||
struct BomItem {
|
||
std::string part_number;
|
||
std::string description;
|
||
int quantity = 0;
|
||
std::vector<std::string> view_names; ///< 该零件对应视图名
|
||
};
|
||
|
||
/// 图纸 BOM
|
||
struct DrawingBom {
|
||
std::vector<BomItem> items;
|
||
};
|
||
|
||
/// 单条一致性问题(规则 id + 可执行诊断)
|
||
struct DrawingConsistencyIssue {
|
||
std::string rule; ///< 规则 id(见 check_drawing_consistency 注释)
|
||
std::string element; ///< 涉及视图/标注/BOM 项
|
||
std::string message;
|
||
std::string suggestion;
|
||
};
|
||
|
||
/// 图纸一致性报告
|
||
struct DrawingConsistencyReport {
|
||
std::vector<DrawingConsistencyIssue> issues;
|
||
[[nodiscard]] bool ok() const { return issues.empty(); }
|
||
[[nodiscard]] std::string to_string() const;
|
||
};
|
||
|
||
/**
|
||
* @brief 图纸一致性检查(B-5)
|
||
*
|
||
* 检查规则(每条返回可执行诊断):
|
||
* C1 duplicate_view :视图名重复
|
||
* C2 empty_view_geometry :可见视图无任何线段(缺失几何)
|
||
* C3 annotation_orphan :标注未挂到任何视图(view_name 不存在/为空)
|
||
* C4 annotation_empty_text :标注文本为空(尺寸/注释)
|
||
* C5 annotation_layer :标注引用未定义图层(drafting style 名)
|
||
* C6 bom_view_missing :BOM 条目引用的视图不存在
|
||
* C7 bom_quantity :BOM 数量非正
|
||
* C8 bom_duplicate_part :BOM 零件号重复
|
||
* C9 bom_item_no_view :BOM 条目未关联任何视图
|
||
* C10 view_outside_sheet :视图放置越出图幅可用区域
|
||
*
|
||
* @param layout 已布局图纸
|
||
* @param defined_layers 已定义图层名(空 = 跳过图层检查)
|
||
* @param bom 可选 BOM
|
||
*/
|
||
[[nodiscard]] DrawingConsistencyReport check_drawing_consistency(
|
||
const ViewLayout& layout,
|
||
const std::vector<std::string>& defined_layers = {},
|
||
const DrawingBom* bom = nullptr);
|
||
|
||
} // namespace vde::drafting
|