1689 lines
51 KiB
Dart
1689 lines
51 KiB
Dart
/// 架构图模块数据模型:图形(矩形 / 圆 / 云 / 圆柱……)、手绘墨迹与连线。
|
||
///
|
||
/// 独立于时序图(条形 / 圆周 / 流程)三视图:数据存放在 [ArchDoc],
|
||
/// 由 `Project.arch` 挂在同一份工程文件里随存随取,**暂不与时序图互通**
|
||
/// (不引用工位 / 动作,也不参与节拍计算)。
|
||
///
|
||
/// 约定:
|
||
/// - 颜色以 32 位 ARGB 整数保存,序列化为 8 位十六进制 `#aarrggbb`(保留透明度,
|
||
/// 因为架构图要用「半透明填充 + 实色描边」的画法);
|
||
/// - 所有几何均为「世界坐标」(画布坐标),缩放 / 平移由 [ArchDoc.zoom]/[ArchDoc.panX]
|
||
/// / [ArchDoc.panY] 描述;
|
||
/// - 所有模型提供 copyWith / toJson / fromJson,保证 JSON 往返。
|
||
library;
|
||
|
||
import 'dart:convert';
|
||
import 'dart:math' as math;
|
||
import 'dart:ui' show Offset, Path, Rect, Size;
|
||
|
||
import '../sketch/sketch_models.dart';
|
||
|
||
/// 架构图画布当前工具。
|
||
enum ArchTool {
|
||
/// 选择 / 移动 / 缩放。
|
||
select,
|
||
|
||
/// 手绘(松开自动识别成标准图形)。
|
||
pen,
|
||
|
||
/// 连线(从一个图形拖到另一个图形,自动吸附并跟随移动)。
|
||
connect,
|
||
|
||
/// 橡皮(拖过图形 / 墨迹 / 连线即擦除;一次连续擦除 = 一步撤销)。
|
||
eraser,
|
||
}
|
||
|
||
/// 连线走线方式。
|
||
enum ArchRoute {
|
||
/// 直线。
|
||
straight,
|
||
|
||
/// 正交折线(横平竖直走线)。
|
||
ortho,
|
||
}
|
||
|
||
/// 对齐 / 均布方式(PPT 风格;作用于多选图形,以选中范围为参照)。
|
||
enum ArchAlign {
|
||
/// 左对齐。
|
||
left,
|
||
|
||
/// 横向居中对齐。
|
||
hCenter,
|
||
|
||
/// 右对齐。
|
||
right,
|
||
|
||
/// 顶对齐。
|
||
top,
|
||
|
||
/// 纵向居中对齐。
|
||
vCenter,
|
||
|
||
/// 底对齐。
|
||
bottom,
|
||
|
||
/// 横向均布(至少 3 个)。
|
||
distributeH,
|
||
|
||
/// 纵向均布(至少 3 个)。
|
||
distributeV,
|
||
}
|
||
|
||
/// 架构图标准图形类型。
|
||
enum ArchShapeType {
|
||
/// 矩形。
|
||
rect,
|
||
|
||
/// 圆角矩形。
|
||
roundRect,
|
||
|
||
/// 椭圆。
|
||
ellipse,
|
||
|
||
/// 圆形。
|
||
circle,
|
||
|
||
/// 三角形([ArchShape.dir] 控制朝向:0 上 / 1 右 / 2 下 / 3 左)。
|
||
triangle,
|
||
|
||
/// 菱形。
|
||
diamond,
|
||
|
||
/// 圆柱(数据库 / 存储的标准画法)。
|
||
cylinder,
|
||
|
||
/// 云(外部系统 / 云端)。
|
||
cloud,
|
||
|
||
/// 六边形。
|
||
hexagon,
|
||
|
||
/// 纯文本(无边框)。
|
||
text,
|
||
|
||
/// 直线 / 箭头(用 [ArchShape.a]、[ArchShape.b]、[ArchShape.arrow] 描述)。
|
||
line,
|
||
|
||
/// 折线(用 [ArchShape.pts] 描述顶点;近水平 / 垂直的段会自动扶正)。
|
||
polyline,
|
||
|
||
/// 曲线(样条;用 [ArchShape.pts] 描述通过点,绘制时平滑)。
|
||
curve,
|
||
|
||
/// 服务器(IT 系统设计:机架抽屉式)。
|
||
server,
|
||
|
||
/// 客户端 / PC(显示器 + 底座)。
|
||
client,
|
||
|
||
/// 移动端(手机外形)。
|
||
mobile,
|
||
|
||
/// 防火墙(砖墙)。
|
||
firewall,
|
||
|
||
/// 交换机(网络设备:端口方块)。
|
||
netSwitch,
|
||
|
||
/// 消息队列 / 数据流(管道 + 箭头)。
|
||
queue,
|
||
|
||
/// 图元库符号(可扩展;[ArchShape.symbolId] 指向图元库条目)。
|
||
symbol,
|
||
}
|
||
|
||
/// 可编辑尺寸类型(识别后图形的直接尺寸;不走约束求解器)。
|
||
enum ArchDimKind {
|
||
/// 宽度(水平尺寸)。
|
||
width,
|
||
|
||
/// 高度(垂直尺寸)。
|
||
height,
|
||
|
||
/// 长度(线类图形的路径长度)。
|
||
length,
|
||
|
||
/// 直径(圆)。
|
||
diameter,
|
||
}
|
||
|
||
/// 尺寸类型 JSON 名称。
|
||
String archDimKindName(ArchDimKind k) => switch (k) {
|
||
ArchDimKind.width => 'width',
|
||
ArchDimKind.height => 'height',
|
||
ArchDimKind.length => 'length',
|
||
ArchDimKind.diameter => 'diameter',
|
||
};
|
||
|
||
/// JSON 名称 → 尺寸类型。
|
||
ArchDimKind archDimKindFrom(String s) => switch (s) {
|
||
'height' => ArchDimKind.height,
|
||
'length' => ArchDimKind.length,
|
||
'diameter' => ArchDimKind.diameter,
|
||
_ => ArchDimKind.width,
|
||
};
|
||
|
||
/// 尺寸类型中文名。
|
||
String archDimKindLabel(ArchDimKind k) => switch (k) {
|
||
ArchDimKind.width => '宽度',
|
||
ArchDimKind.height => '高度',
|
||
ArchDimKind.length => '长度',
|
||
ArchDimKind.diameter => '直径',
|
||
};
|
||
|
||
/// 图形适用的尺寸类型(右栏编辑 / 画布标注用)。
|
||
List<ArchDimKind> archShapeDimKinds(ArchShape s) {
|
||
if (s.isLinear) {
|
||
return const <ArchDimKind>[
|
||
ArchDimKind.length,
|
||
ArchDimKind.width,
|
||
ArchDimKind.height,
|
||
];
|
||
}
|
||
if (s.type == ArchShapeType.circle) {
|
||
return const <ArchDimKind>[ArchDimKind.diameter];
|
||
}
|
||
return const <ArchDimKind>[ArchDimKind.width, ArchDimKind.height];
|
||
}
|
||
|
||
/// 线类图形的外接矩形(pts / a-b)。
|
||
Rect archLinearBounds(ArchShape s) {
|
||
if (s.type == ArchShapeType.line && s.a != null && s.b != null) {
|
||
return Rect.fromPoints(s.a!, s.b!);
|
||
}
|
||
if (s.pts.isEmpty) return s.rect;
|
||
double l = s.pts.first.dx, r = l, t = s.pts.first.dy, b = t;
|
||
for (final Offset p in s.pts) {
|
||
if (p.dx < l) l = p.dx;
|
||
if (p.dx > r) r = p.dx;
|
||
if (p.dy < t) t = p.dy;
|
||
if (p.dy > b) b = p.dy;
|
||
}
|
||
return Rect.fromLTRB(l, t, r, b);
|
||
}
|
||
|
||
/// 线类图形的路径长度(直线 / 折线 / 曲线按顶点折线近似)。
|
||
double archShapeLength(ArchShape s) {
|
||
if (s.type == ArchShapeType.line && s.a != null && s.b != null) {
|
||
return (s.b! - s.a!).distance;
|
||
}
|
||
double len = 0;
|
||
for (int i = 1; i < s.pts.length; i++) {
|
||
len += (s.pts[i] - s.pts[i - 1]).distance;
|
||
}
|
||
return len;
|
||
}
|
||
|
||
/// 测量图形当前尺寸值。
|
||
double archShapeDimValue(ArchShape s, ArchDimKind k) {
|
||
switch (k) {
|
||
case ArchDimKind.length:
|
||
return archShapeLength(s);
|
||
case ArchDimKind.diameter:
|
||
return s.rect.width;
|
||
case ArchDimKind.width:
|
||
return s.isLinear ? archLinearBounds(s).width : s.rect.width;
|
||
case ArchDimKind.height:
|
||
return s.isLinear ? archLinearBounds(s).height : s.rect.height;
|
||
}
|
||
}
|
||
|
||
/// 线类图形按比例缩放(绕外接框中心;[fx]/[fy] 为横纵缩放系数)。
|
||
ArchShape _scaleLinear(ArchShape s, double fx, double fy) {
|
||
final Rect b = archLinearBounds(s);
|
||
final Offset c0 = b.center;
|
||
Offset map(Offset p) =>
|
||
Offset(c0.dx + (p.dx - c0.dx) * fx, c0.dy + (p.dy - c0.dy) * fy);
|
||
if (s.type == ArchShapeType.line && s.a != null && s.b != null) {
|
||
final Offset a2 = map(s.a!);
|
||
final Offset b2 = map(s.b!);
|
||
return s.copyWith(a: a2, b: b2, rect: Rect.fromPoints(a2, b2));
|
||
}
|
||
final List<Offset> pts = <Offset>[for (final Offset p in s.pts) map(p)];
|
||
if (pts.isEmpty) return s;
|
||
double l = pts.first.dx, r = l, t = pts.first.dy, bo = t;
|
||
for (final Offset p in pts) {
|
||
if (p.dx < l) l = p.dx;
|
||
if (p.dx > r) r = p.dx;
|
||
if (p.dy < t) t = p.dy;
|
||
if (p.dy > bo) bo = p.dy;
|
||
}
|
||
return s.copyWith(pts: pts, rect: Rect.fromLTRB(l, t, r, bo));
|
||
}
|
||
|
||
/// 应用尺寸(直接缩放几何,中心保持;值 <= 0.1 时按 0.1 处理)。
|
||
ArchShape archShapeApplyDim(ArchShape s, ArchDimKind k, double value) {
|
||
final double v = value <= 0.1 ? 0.1 : value;
|
||
switch (k) {
|
||
case ArchDimKind.width:
|
||
if (s.isLinear) {
|
||
final double w = archLinearBounds(s).width;
|
||
return _scaleLinear(s, w <= 1e-6 ? 1 : v / w, 1);
|
||
}
|
||
return s.copyWith(
|
||
rect: Rect.fromCenter(
|
||
center: s.rect.center, width: v, height: s.rect.height));
|
||
case ArchDimKind.height:
|
||
if (s.isLinear) {
|
||
final double h = archLinearBounds(s).height;
|
||
return _scaleLinear(s, 1, h <= 1e-6 ? 1 : v / h);
|
||
}
|
||
return s.copyWith(
|
||
rect: Rect.fromCenter(
|
||
center: s.rect.center, width: s.rect.width, height: v));
|
||
case ArchDimKind.diameter:
|
||
return s.copyWith(
|
||
rect: Rect.fromCenter(center: s.rect.center, width: v, height: v));
|
||
case ArchDimKind.length:
|
||
final double len = archShapeLength(s);
|
||
if (len <= 1e-6) return s;
|
||
final double f = v / len;
|
||
return _scaleLinear(s, f, f);
|
||
}
|
||
}
|
||
|
||
/// 尺寸标签锚点(世界坐标;屏幕恒定间距由 [zoom] 折算)。
|
||
Offset archShapeDimLabelPos(
|
||
ArchShape s,
|
||
ArchDimKind k,
|
||
int index,
|
||
double zoom,
|
||
) {
|
||
final double z = zoom <= 0.05 ? 1 : zoom;
|
||
final double gap = 17 / z;
|
||
final Rect b = s.isLinear ? archLinearBounds(s) : s.rect;
|
||
final bool wide =
|
||
s.type == ArchShapeType.line || s.type == ArchShapeType.curve;
|
||
return Offset(
|
||
b.center.dx + (wide ? gap * 0.9 : 0),
|
||
b.bottom + gap * (index + 0.8),
|
||
);
|
||
}
|
||
|
||
/// 命中的尺寸标签(双击编辑用);未命中返回 null。
|
||
(ArchShape, ArchDimKind)? archShapeDimHitAt(
|
||
ArchDoc doc,
|
||
Offset p,
|
||
double zoom,
|
||
) {
|
||
final double z = zoom <= 0.05 ? 1 : zoom;
|
||
final double tol = 14 / z + 6;
|
||
for (final ArchShape s in doc.shapes) {
|
||
if (!doc.isLayerVisible(s.layerId)) continue;
|
||
final List<ArchDimKind> kinds = <ArchDimKind>[
|
||
for (final ArchDimKind k in archShapeDimKinds(s))
|
||
if (s.dims.contains(k)) k,
|
||
];
|
||
for (int i = 0; i < kinds.length; i++) {
|
||
final Offset a = archShapeDimLabelPos(s, kinds[i], i, z);
|
||
if ((p - a).distance <= tol) return (s, kinds[i]);
|
||
}
|
||
}
|
||
return null;
|
||
}
|
||
|
||
/// 把折线正交化:保留顶点位置,但所有段强制「横平竖直」。
|
||
///
|
||
/// 用于手绘连线的轨迹:笔迹认出的折点仍被尊重,但不会出现斜线
|
||
///(斜段自动拆成 横 + 竖 或 竖 + 横 两段台阶)。
|
||
List<Offset> orthoPolyline(List<Offset> pts) {
|
||
if (pts.length < 2) return List<Offset>.of(pts);
|
||
final List<Offset> out = <Offset>[pts.first];
|
||
for (int i = 1; i < pts.length; i++) {
|
||
final Offset p = pts[i];
|
||
final Offset last = out.last;
|
||
final Offset d = p - last;
|
||
if (d.distance < 1e-3) continue;
|
||
if (d.dx.abs() >= d.dy.abs()) {
|
||
out.add(Offset(p.dx, last.dy));
|
||
if (d.dy.abs() > 6) out.add(p);
|
||
} else {
|
||
out.add(Offset(last.dx, p.dy));
|
||
if (d.dx.abs() > 6) out.add(p);
|
||
}
|
||
}
|
||
// 去掉相邻重复点。
|
||
final List<Offset> clean = <Offset>[out.first];
|
||
for (int i = 1; i < out.length; i++) {
|
||
if ((out[i] - clean.last).distance < 2) continue;
|
||
clean.add(out[i]);
|
||
}
|
||
// 去掉共线冗余点(中间点与前后同横或同竖)。
|
||
for (int i = clean.length - 2; i >= 1; i--) {
|
||
final Offset a = clean[i - 1], b = clean[i], cc = clean[i + 1];
|
||
final bool h = (a.dy - b.dy).abs() < 1 && (b.dy - cc.dy).abs() < 1;
|
||
final bool v = (a.dx - b.dx).abs() < 1 && (b.dx - cc.dx).abs() < 1;
|
||
if (h || v) clean.removeAt(i);
|
||
}
|
||
return clean;
|
||
}
|
||
|
||
/// 默认画布(图纸)尺寸:A0 横放(1189 × 841 mm;1 世界单位 = 1 mm)。
|
||
const Size kArchPageSize = Size(1189, 841);
|
||
|
||
/// 图形类型的中文名(界面展示用)。
|
||
String archShapeTypeLabel(ArchShapeType t) {
|
||
switch (t) {
|
||
case ArchShapeType.rect:
|
||
return '矩形';
|
||
case ArchShapeType.roundRect:
|
||
return '圆角矩形';
|
||
case ArchShapeType.ellipse:
|
||
return '椭圆';
|
||
case ArchShapeType.circle:
|
||
return '圆形';
|
||
case ArchShapeType.triangle:
|
||
return '三角形';
|
||
case ArchShapeType.diamond:
|
||
return '菱形';
|
||
case ArchShapeType.cylinder:
|
||
return '圆柱';
|
||
case ArchShapeType.cloud:
|
||
return '云';
|
||
case ArchShapeType.hexagon:
|
||
return '六边形';
|
||
case ArchShapeType.text:
|
||
return '文本';
|
||
case ArchShapeType.line:
|
||
return '直线';
|
||
case ArchShapeType.polyline:
|
||
return '折线';
|
||
case ArchShapeType.curve:
|
||
return '曲线';
|
||
case ArchShapeType.server:
|
||
return '服务器';
|
||
case ArchShapeType.client:
|
||
return '客户端(PC)';
|
||
case ArchShapeType.mobile:
|
||
return '移动端';
|
||
case ArchShapeType.firewall:
|
||
return '防火墙';
|
||
case ArchShapeType.netSwitch:
|
||
return '交换机';
|
||
case ArchShapeType.queue:
|
||
return '消息队列';
|
||
case ArchShapeType.symbol:
|
||
return '图元';
|
||
}
|
||
}
|
||
|
||
/// 图形库中新图形的默认尺寸(世界坐标 px)。
|
||
Size archDefaultSize(ArchShapeType t) {
|
||
switch (t) {
|
||
case ArchShapeType.circle:
|
||
return const Size(120, 120);
|
||
case ArchShapeType.ellipse:
|
||
return const Size(160, 96);
|
||
case ArchShapeType.triangle:
|
||
return const Size(140, 116);
|
||
case ArchShapeType.diamond:
|
||
return const Size(160, 116);
|
||
case ArchShapeType.cylinder:
|
||
return const Size(132, 136);
|
||
case ArchShapeType.cloud:
|
||
return const Size(184, 126);
|
||
case ArchShapeType.text:
|
||
return const Size(200, 48);
|
||
case ArchShapeType.line:
|
||
return const Size(180, 0);
|
||
case ArchShapeType.polyline:
|
||
case ArchShapeType.curve:
|
||
return const Size(180, 80);
|
||
case ArchShapeType.rect:
|
||
case ArchShapeType.roundRect:
|
||
case ArchShapeType.hexagon:
|
||
return const Size(176, 88);
|
||
case ArchShapeType.server:
|
||
return const Size(104, 132);
|
||
case ArchShapeType.client:
|
||
return const Size(152, 118);
|
||
case ArchShapeType.mobile:
|
||
return const Size(76, 132);
|
||
case ArchShapeType.firewall:
|
||
return const Size(160, 76);
|
||
case ArchShapeType.netSwitch:
|
||
return const Size(176, 68);
|
||
case ArchShapeType.queue:
|
||
return const Size(184, 72);
|
||
case ArchShapeType.symbol:
|
||
return const Size(120, 120);
|
||
}
|
||
}
|
||
|
||
/// 架构图默认调色板(与整体应用视觉一致,ARGB)。
|
||
const List<int> kArchPalette = <int>[
|
||
0xFF4CC9F0, // 钢蓝
|
||
0xFF34D399, // 绿
|
||
0xFFFFD166, // 琥珀
|
||
0xFFFF8A5C, // 橙
|
||
0xFFF472B6, // 粉
|
||
0xFFA78BFA, // 紫
|
||
0xFF38BDF8, // 天蓝
|
||
0xFF94A3B8, // 灰蓝
|
||
];
|
||
|
||
/// 架构图连线的默认颜色(中性蓝灰,深浅主题下都清晰)。
|
||
const int kArchConnectorColor = 0xFF7C8DA6;
|
||
|
||
/// 把 32 位 ARGB 序列化为 `#aarrggbb`(保留透明度通道)。
|
||
String archColorHex(int argb) =>
|
||
'#${(argb & 0xFFFFFFFF).toRadixString(16).padLeft(8, '0')}';
|
||
|
||
/// 解析 `#aarrggbb` / `#rrggbb` / `#rgb` 为 ARGB 整数;失败时回退 [fallback]。
|
||
int archHexColor(Object? hex, {int fallback = 0xFF4CC9F0}) {
|
||
if (hex is! String) return fallback;
|
||
final String h = hex.replaceAll('#', '').trim();
|
||
final int? v = int.tryParse(h, radix: 16);
|
||
if (v == null) return fallback;
|
||
if (h.length <= 6) return 0xFF000000 | (v & 0xFFFFFF);
|
||
return v & 0xFFFFFFFF;
|
||
}
|
||
|
||
/// 由主色派生默认填充色(低透明度,深浅主题下都呈「浅色块 + 实色描边」)。
|
||
int archFillOf(int color, {int alpha = 0x26}) =>
|
||
(alpha << 24) | (color & 0xFFFFFF);
|
||
|
||
/// 生成架构图内不重复的 id(如 `S3` / `C2` / `K5`)。
|
||
String archNewId(String prefix, Iterable<String> existing) {
|
||
final Set<String> used = existing.toSet();
|
||
int n = 1;
|
||
while (used.contains('$prefix$n')) {
|
||
n++;
|
||
}
|
||
return '$prefix$n';
|
||
}
|
||
|
||
/// 点到线段距离(命中测试用)。
|
||
double archDistToSegment(Offset p, Offset a, Offset b) {
|
||
final Offset ab = b - a;
|
||
final double len2 = ab.dx * ab.dx + ab.dy * ab.dy;
|
||
if (len2 <= 0.0001) return (p - a).distance;
|
||
double t = ((p.dx - a.dx) * ab.dx + (p.dy - a.dy) * ab.dy) / len2;
|
||
t = t.clamp(0.0, 1.0);
|
||
return (p - (a + ab * t)).distance;
|
||
}
|
||
|
||
/// 从图形边框上取一点:连线从图形中心指向 [target] 时与边框的交点。
|
||
///
|
||
/// 椭圆 / 圆用归一化椭圆求交;其余图形统一按外接矩形求交(三角形 / 菱形 /
|
||
/// 云的边界近似为矩形,视觉上可接受且实现简单)。
|
||
Offset archBorderPoint(ArchShape s, Offset target) {
|
||
final Offset c = s.rect.center;
|
||
Offset d = target - c;
|
||
if (d.distance < 0.001) d = const Offset(1, 0);
|
||
if (s.type == ArchShapeType.ellipse || s.type == ArchShapeType.circle) {
|
||
final double rx = math.max(1, s.rect.width / 2);
|
||
final double ry = math.max(1, s.rect.height / 2);
|
||
final double t = 1 / math.sqrt((d.dx / rx) * (d.dx / rx) + (d.dy / ry) * (d.dy / ry));
|
||
return c + d * t;
|
||
}
|
||
final double hw = math.max(1, s.rect.width / 2);
|
||
final double hh = math.max(1, s.rect.height / 2);
|
||
final double tx = d.dx.abs() < 0.001 ? double.infinity : hw / d.dx.abs();
|
||
final double ty = d.dy.abs() < 0.001 ? double.infinity : hh / d.dy.abs();
|
||
final double t = math.min(tx, ty);
|
||
return c + d * t;
|
||
}
|
||
|
||
/// 计算连线当前端点:吸附到图形时按「图形中心连线与边框的交点」实时计算,
|
||
/// 因此移动图形后连线自动跟随([override] 供拖拽预览替换图形几何)。
|
||
(Offset, Offset) archConnectorEnds(
|
||
ArchDoc doc,
|
||
ArchConnector c, {
|
||
ArchShape? Function(ArchShape)? override,
|
||
}) {
|
||
final ArchConnGeometry g = archConnectorGeometry(doc, c, override: override);
|
||
return (g.start, g.end);
|
||
}
|
||
|
||
/// 网格吸附:把点吸附到最近网格点(关闭吸附或粒度 <= 1 时原样返回)。
|
||
Offset archSnapPoint(ArchDoc doc, Offset p) {
|
||
if (!doc.gridSnap || doc.gridSize <= 1) return p;
|
||
final double g = doc.gridSize;
|
||
return Offset(
|
||
(p.dx / g).roundToDouble() * g,
|
||
(p.dy / g).roundToDouble() * g,
|
||
);
|
||
}
|
||
|
||
/// 移动吸附结果:校正后的位移 + 磁力对齐线(世界坐标,null 表示该轴无对齐)。
|
||
typedef ArchMoveSnap = ({Offset delta, double? guideX, double? guideY});
|
||
|
||
/// 计算移动时的吸附位移:
|
||
/// - **磁力对齐**:移动集合(并集外框)的左 / 中 / 右与其它图形的左 / 中 / 右
|
||
/// 在 [tol](世界像素)内接近时,吸附对齐并返回虚线导引位置;
|
||
/// - 未命中的轴回退到**网格吸附**(以主选中图形为准)。
|
||
ArchMoveSnap archSnapMoveDelta({
|
||
required ArchDoc doc,
|
||
required Set<String> movingIds,
|
||
required Rect union,
|
||
required Offset rawDelta,
|
||
required double tol,
|
||
required bool gridSnap,
|
||
}) {
|
||
double? guideX;
|
||
double? guideY;
|
||
double? matchCandX;
|
||
double? matchCandY;
|
||
double bestDX = double.infinity;
|
||
double bestDY = double.infinity;
|
||
for (final ArchShape o in doc.shapes) {
|
||
if (movingIds.contains(o.id)) continue;
|
||
final Rect b = o.bounds;
|
||
for (final double cand in <double>[
|
||
union.left,
|
||
union.center.dx,
|
||
union.right,
|
||
]) {
|
||
for (final double t in <double>[b.left, b.center.dx, b.right]) {
|
||
final double d = (t - cand).abs();
|
||
if (d <= tol && d < bestDX) {
|
||
bestDX = d;
|
||
guideX = t;
|
||
matchCandX = cand;
|
||
}
|
||
}
|
||
}
|
||
for (final double cand in <double>[
|
||
union.top,
|
||
union.center.dy,
|
||
union.bottom,
|
||
]) {
|
||
for (final double t in <double>[b.top, b.center.dy, b.bottom]) {
|
||
final double d = (t - cand).abs();
|
||
if (d <= tol && d < bestDY) {
|
||
bestDY = d;
|
||
guideY = t;
|
||
matchCandY = cand;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
double dx = rawDelta.dx;
|
||
double dy = rawDelta.dy;
|
||
if (guideX != null && matchCandX != null) {
|
||
dx = rawDelta.dx + (guideX - matchCandX);
|
||
}
|
||
if (guideY != null && matchCandY != null) {
|
||
dy = rawDelta.dy + (guideY - matchCandY);
|
||
}
|
||
if (gridSnap) {
|
||
final ArchShape? primary =
|
||
doc.shapeById(movingIds.isEmpty ? null : movingIds.first);
|
||
if (primary != null) {
|
||
final Offset targetTL = primary.rect.topLeft + rawDelta;
|
||
final Offset snappedTL = archSnapPoint(doc, targetTL);
|
||
final Offset snapDelta = snappedTL - primary.rect.topLeft;
|
||
if (guideX == null) dx = snapDelta.dx;
|
||
if (guideY == null) dy = snapDelta.dy;
|
||
}
|
||
}
|
||
return (delta: Offset(dx, dy), guideX: guideX, guideY: guideY);
|
||
}
|
||
|
||
/// 连线走线的几何结果:折点序列 + 平滑路径 + 标签锚点。
|
||
class ArchConnGeometry {
|
||
/// 构造几何结果。
|
||
const ArchConnGeometry(this.pts, this.path, this.label);
|
||
|
||
/// 折点序列(起 → 中间折点 → 止)。
|
||
final List<Offset> pts;
|
||
|
||
/// 绘制路径(正交折线的中间折点带圆角)。
|
||
final Path path;
|
||
|
||
/// 标签锚点(最长段的中点)。
|
||
final Offset label;
|
||
|
||
/// 起点。
|
||
Offset get start => pts.first;
|
||
|
||
/// 终点。
|
||
Offset get end => pts.last;
|
||
}
|
||
|
||
/// 图形边界上距 [p] 最近的点(连线「开始 / 结束」吸附用)。
|
||
///
|
||
/// 椭圆 / 圆按径向投影到边界;矩形类(含圆角矩形 / 多边形 / 云 / 圆柱)
|
||
/// 取外接矩形边界上的最近点(外部点 clamp、内部点投影到最近一条边)。
|
||
Offset archNearestBorderPoint(ArchShape s, Offset p) {
|
||
final Rect r = s.rect;
|
||
if (s.type == ArchShapeType.ellipse || s.type == ArchShapeType.circle) {
|
||
final double rx = math.max(1, r.width / 2);
|
||
final double ry = math.max(1, r.height / 2);
|
||
final Offset d = p - r.center;
|
||
if (d.distance < 0.001) return Offset(r.right, r.center.dy);
|
||
final double t =
|
||
1 / math.sqrt((d.dx / rx) * (d.dx / rx) + (d.dy / ry) * (d.dy / ry));
|
||
return r.center + d * t;
|
||
}
|
||
if (r.contains(p)) {
|
||
final double dl = (p.dx - r.left).abs();
|
||
final double dr = (r.right - p.dx).abs();
|
||
final double dt = (p.dy - r.top).abs();
|
||
final double db = (r.bottom - p.dy).abs();
|
||
final double m = math.min(math.min(dl, dr), math.min(dt, db));
|
||
if (m == dl) return Offset(r.left, p.dy);
|
||
if (m == dr) return Offset(r.right, p.dy);
|
||
if (m == dt) return Offset(p.dx, r.top);
|
||
return Offset(p.dx, r.bottom);
|
||
}
|
||
return Offset(p.dx.clamp(r.left, r.right), p.dy.clamp(r.top, r.bottom));
|
||
}
|
||
|
||
/// 计算连线几何:**一律正交走线**(横平竖直,无斜线)。
|
||
///
|
||
/// - 有折点([ArchConnector.mids],手绘轨迹 / 拖拽过的角点)时:按折点走正交台阶;
|
||
/// - 无折点时:按两图形中心主方向自动生成单拐角正交折线(带圆角);
|
||
/// - 端点吸附图形时从相应方向边框射出。
|
||
ArchConnGeometry archConnectorGeometry(
|
||
ArchDoc doc,
|
||
ArchConnector c, {
|
||
ArchShape? Function(ArchShape)? override,
|
||
}) {
|
||
ArchShape? resolve(String? id) {
|
||
final ArchShape? s = doc.shapeById(id);
|
||
if (s == null) return null;
|
||
return override == null ? s : override(s);
|
||
}
|
||
|
||
final ArchShape? sf = resolve(c.fromId);
|
||
final ArchShape? st = resolve(c.toId);
|
||
final Offset cFrom = sf?.rect.center ?? c.from;
|
||
final Offset cTo = st?.rect.center ?? c.to;
|
||
final (Offset a, Offset b) =
|
||
_orthoEnds(sf, st, cFrom, cTo, c.from, c.to);
|
||
|
||
// 有折点:以折点顺序走正交台阶(角点即折点,可直接拖拽编辑)。
|
||
if (c.mids.isNotEmpty) {
|
||
final List<Offset> raw = orthoPolyline(<Offset>[a, ...c.mids, b]);
|
||
final List<Offset> clean = <Offset>[];
|
||
for (final Offset p in raw) {
|
||
if (clean.isEmpty || (p - clean.last).distance > 0.5) clean.add(p);
|
||
}
|
||
if (clean.length >= 2) {
|
||
return ArchConnGeometry(
|
||
clean,
|
||
_roundedOrthoPath(clean, 8),
|
||
_labelPoint(clean),
|
||
);
|
||
}
|
||
}
|
||
|
||
// 无折点:自动单拐角(先横后竖 / 先竖后横由主方向决定)。
|
||
final bool horizontalFirst =
|
||
(cTo.dx - cFrom.dx).abs() >= (cTo.dy - cFrom.dy).abs();
|
||
final List<Offset> pts = horizontalFirst
|
||
? <Offset>[a, Offset(b.dx, a.dy), b]
|
||
: <Offset>[a, Offset(a.dx, b.dy), b];
|
||
final List<Offset> clean = <Offset>[];
|
||
for (final Offset p in pts) {
|
||
if (clean.isEmpty || (p - clean.last).distance > 0.5) clean.add(p);
|
||
}
|
||
return ArchConnGeometry(
|
||
clean,
|
||
_roundedOrthoPath(clean, 8),
|
||
_labelPoint(clean),
|
||
);
|
||
}
|
||
|
||
/// 正交走线的两端射出点(从图形边框取点;自由端点原样返回)。
|
||
(Offset, Offset) _orthoEnds(
|
||
ArchShape? sf,
|
||
ArchShape? st,
|
||
Offset cFrom,
|
||
Offset cTo,
|
||
Offset freeFrom,
|
||
Offset freeTo,
|
||
) {
|
||
final bool horizontalFirst =
|
||
(cTo.dx - cFrom.dx).abs() >= (cTo.dy - cFrom.dy).abs();
|
||
Offset a;
|
||
Offset b;
|
||
if (horizontalFirst) {
|
||
a = sf == null
|
||
? freeFrom
|
||
: archBorderPoint(
|
||
sf,
|
||
Offset(
|
||
cTo.dx >= cFrom.dx ? sf.rect.right + 1e5 : sf.rect.left - 1e5,
|
||
cFrom.dy,
|
||
),
|
||
);
|
||
b = st == null
|
||
? freeTo
|
||
: archBorderPoint(
|
||
st,
|
||
Offset(
|
||
cTo.dx >= cFrom.dx ? st.rect.left - 1e5 : st.rect.right + 1e5,
|
||
cTo.dy,
|
||
),
|
||
);
|
||
} else {
|
||
a = sf == null
|
||
? freeFrom
|
||
: archBorderPoint(
|
||
sf,
|
||
Offset(
|
||
cFrom.dx,
|
||
cTo.dy >= cFrom.dy ? sf.rect.bottom + 1e5 : sf.rect.top - 1e5,
|
||
),
|
||
);
|
||
b = st == null
|
||
? freeTo
|
||
: archBorderPoint(
|
||
st,
|
||
Offset(
|
||
cTo.dx,
|
||
cTo.dy >= cFrom.dy ? st.rect.top - 1e5 : st.rect.bottom + 1e5,
|
||
),
|
||
);
|
||
}
|
||
return (a, b);
|
||
}
|
||
|
||
/// 标签锚点:最长段的中点。
|
||
Offset _labelPoint(List<Offset> pts) {
|
||
double best = -1;
|
||
Offset mid = pts.first;
|
||
for (int i = 0; i < pts.length - 1; i++) {
|
||
final double len = (pts[i + 1] - pts[i]).distance;
|
||
if (len > best) {
|
||
best = len;
|
||
mid = (pts[i] + pts[i + 1]) / 2;
|
||
}
|
||
}
|
||
return mid;
|
||
}
|
||
|
||
/// 正交折线路径:中间折点用圆角过渡。
|
||
Path _roundedOrthoPath(List<Offset> pts, double radius) {
|
||
final Path path = Path()..moveTo(pts.first.dx, pts.first.dy);
|
||
for (int i = 1; i < pts.length - 1; i++) {
|
||
final Offset prev = pts[i - 1];
|
||
final Offset cur = pts[i];
|
||
final Offset next = pts[i + 1];
|
||
final double d1 = (cur - prev).distance;
|
||
final double d2 = (next - cur).distance;
|
||
if (d1 < 0.001 || d2 < 0.001) continue;
|
||
final double r = math.min(radius, math.min(d1, d2) * 0.4);
|
||
if (r < 0.5) {
|
||
path.lineTo(cur.dx, cur.dy);
|
||
continue;
|
||
}
|
||
final Offset p1 = cur - (cur - prev) / d1 * r;
|
||
final Offset p2 = cur + (next - cur) / d2 * r;
|
||
path.lineTo(p1.dx, p1.dy);
|
||
path.quadraticBezierTo(cur.dx, cur.dy, p2.dx, p2.dy);
|
||
}
|
||
path.lineTo(pts.last.dx, pts.last.dy);
|
||
return path;
|
||
}
|
||
|
||
/// 图形默认归属图层 id:空串表示「未指定」→ 由控制器补充为当前活动图层。
|
||
const String kArchDefaultLayerId = '';
|
||
|
||
/// 架构图图层(AutoCAD 风格):名称 / 颜色 / 可见 / 锁定。
|
||
class ArchLayer {
|
||
/// 构造图层。
|
||
const ArchLayer({
|
||
required this.id,
|
||
required this.name,
|
||
required this.color,
|
||
this.visible = true,
|
||
this.locked = false,
|
||
});
|
||
|
||
/// 唯一标识(如 `L0` / `LY2`)。
|
||
final String id;
|
||
|
||
/// 图层名称。
|
||
final String name;
|
||
|
||
/// 图层颜色(ARGB;用于色块展示与新建图形的默认取色)。
|
||
final int color;
|
||
|
||
/// 是否可见(隐藏后不绘制、不可选、不可吸附)。
|
||
final bool visible;
|
||
|
||
/// 是否锁定(可见但不可选 / 不可移动;仍可作为吸附参照)。
|
||
final bool locked;
|
||
|
||
/// 默认图层(旧文件 / 未指定归属时的兜底)。
|
||
static const ArchLayer fallback = ArchLayer(
|
||
id: 'L0',
|
||
name: '默认图层',
|
||
color: 0xFF4CC9F0,
|
||
);
|
||
|
||
/// 复制并覆盖部分字段。
|
||
ArchLayer copyWith({
|
||
String? id,
|
||
String? name,
|
||
int? color,
|
||
bool? visible,
|
||
bool? locked,
|
||
}) {
|
||
return ArchLayer(
|
||
id: id ?? this.id,
|
||
name: name ?? this.name,
|
||
color: color ?? this.color,
|
||
visible: visible ?? this.visible,
|
||
locked: locked ?? this.locked,
|
||
);
|
||
}
|
||
|
||
/// 序列化为 JSON。
|
||
Map<String, dynamic> toJson() => <String, dynamic>{
|
||
'id': id,
|
||
'name': name,
|
||
'c': archColorHex(color),
|
||
if (!visible) 'vis': false,
|
||
if (locked) 'lok': true,
|
||
};
|
||
|
||
/// 从 JSON 反序列化。
|
||
static ArchLayer fromJson(Map<String, dynamic> j) {
|
||
return ArchLayer(
|
||
id: j['id'] as String? ?? 'L0',
|
||
name: j['name'] as String? ?? '图层',
|
||
color: archHexColor(j['c'], fallback: 0xFF4CC9F0),
|
||
visible: j['vis'] as bool? ?? true,
|
||
locked: j['lok'] as bool? ?? false,
|
||
);
|
||
}
|
||
}
|
||
|
||
/// 正交 / 极轴约束:把移动位移约束到主轴(正交)或极轴角度(极轴)。
|
||
Offset archConstrainDelta({
|
||
required Offset raw,
|
||
required bool ortho,
|
||
required bool polar,
|
||
required double polarDeg,
|
||
}) {
|
||
if (ortho) {
|
||
return raw.dx.abs() >= raw.dy.abs()
|
||
? Offset(raw.dx, 0)
|
||
: Offset(0, raw.dy);
|
||
}
|
||
if (polar) {
|
||
final double len = raw.distance;
|
||
if (len < 1.5) return raw;
|
||
final double step = math.max(1.0, polarDeg) * math.pi / 180;
|
||
final double ang = (math.atan2(raw.dy, raw.dx) / step).round() * step;
|
||
return Offset(math.cos(ang) * len, math.sin(ang) * len);
|
||
}
|
||
return raw;
|
||
}
|
||
|
||
/// 架构图标准图形(矩形 / 圆 / 云 / 圆柱……;[ArchShapeType.line] 直线 / 箭头)。
|
||
class ArchShape {
|
||
/// 构造一个图形。
|
||
const ArchShape({
|
||
required this.id,
|
||
required this.type,
|
||
required this.rect,
|
||
this.radius = 0,
|
||
this.dir = 0,
|
||
this.a,
|
||
this.b,
|
||
this.arrow = false,
|
||
this.fill = 0x264CC9F0,
|
||
this.stroke = 0xFF4CC9F0,
|
||
this.sw = 1.8,
|
||
this.text = '',
|
||
this.pts = const <Offset>[],
|
||
this.layerId = kArchDefaultLayerId,
|
||
this.symbolId = '',
|
||
this.dims = const <ArchDimKind>{},
|
||
});
|
||
|
||
/// 唯一标识(如 `S1`)。
|
||
final String id;
|
||
|
||
/// 图形类型。
|
||
final ArchShapeType type;
|
||
|
||
/// 外接矩形(世界坐标)。
|
||
final Rect rect;
|
||
|
||
/// 圆角半径(仅 [ArchShapeType.roundRect] 使用)。
|
||
final double radius;
|
||
|
||
/// 朝向(仅三角形使用):0 上 / 1 右 / 2 下 / 3 左。
|
||
final int dir;
|
||
|
||
/// 直线起点(仅 [ArchShapeType.line] 使用)。
|
||
final Offset? a;
|
||
|
||
/// 直线终点(仅 [ArchShapeType.line] 使用)。
|
||
final Offset? b;
|
||
|
||
/// 是否为箭头(仅 [ArchShapeType.line] 使用)。
|
||
final bool arrow;
|
||
|
||
/// 填充色(ARGB,默认半透明主色)。
|
||
final int fill;
|
||
|
||
/// 描边色(ARGB)。
|
||
final int stroke;
|
||
|
||
/// 描边宽度(世界像素)。
|
||
final double sw;
|
||
|
||
/// 图内文字(多行以 `\n` 分隔)。
|
||
final String text;
|
||
|
||
/// 折线 / 曲线的通过点(世界坐标;[ArchShapeType.polyline] / [ArchShapeType.curve])。
|
||
final List<Offset> pts;
|
||
|
||
/// 所属图层 id(空串 = 未指定,由控制器补充为当前活动图层)。
|
||
final String layerId;
|
||
|
||
/// 图元库符号 id(仅 [ArchShapeType.symbol] 使用;空串 = 未指定)。
|
||
final String symbolId;
|
||
|
||
/// 画布上标注的尺寸类型(识别后图形可直接改值缩放,见 [archShapeApplyDim])。
|
||
final Set<ArchDimKind> dims;
|
||
|
||
/// 是否为「线类」图形(直线 / 箭头 / 折线 / 曲线)。
|
||
bool get isLinear =>
|
||
type == ArchShapeType.line ||
|
||
type == ArchShapeType.polyline ||
|
||
type == ArchShapeType.curve;
|
||
|
||
/// 命中范围(用于选择与删除判定)。
|
||
Rect get bounds {
|
||
if (type == ArchShapeType.line && a != null && b != null) {
|
||
return Rect.fromPoints(a!, b!).inflate(6);
|
||
}
|
||
if (pts.isNotEmpty) {
|
||
double l = pts.first.dx, r = l, t = pts.first.dy, b = t;
|
||
for (final Offset p in pts) {
|
||
if (p.dx < l) l = p.dx;
|
||
if (p.dx > r) r = p.dx;
|
||
if (p.dy < t) t = p.dy;
|
||
if (p.dy > b) b = p.dy;
|
||
}
|
||
return Rect.fromLTRB(l, t, r, b).inflate(6);
|
||
}
|
||
return rect;
|
||
}
|
||
|
||
/// 点 [p] 是否命中本图形([pad] 为宽容度)。
|
||
bool hitTest(Offset p, {double pad = 2}) {
|
||
if (isLinear) {
|
||
final List<Offset> line = isLinear ? _linePoints() : const <Offset>[];
|
||
if (line.length < 2) return false;
|
||
final double tol = math.max(8, sw + 4) + pad;
|
||
for (int i = 0; i < line.length - 1; i++) {
|
||
if (archDistToSegment(p, line[i], line[i + 1]) <= tol) return true;
|
||
}
|
||
return false;
|
||
}
|
||
if (type == ArchShapeType.ellipse || type == ArchShapeType.circle) {
|
||
final double rx = math.max(1, rect.width / 2);
|
||
final double ry = math.max(1, rect.height / 2);
|
||
final double nx = (p.dx - rect.center.dx) / rx;
|
||
final double ny = (p.dy - rect.center.dy) / ry;
|
||
return nx * nx + ny * ny <= 1.18;
|
||
}
|
||
return rect.inflate(pad).contains(p);
|
||
}
|
||
|
||
/// 线类图形的折线点列(命中测试与范围计算用)。
|
||
List<Offset> _linePoints() {
|
||
if (type == ArchShapeType.line) {
|
||
final Offset? p1 = a;
|
||
final Offset? p2 = b;
|
||
return p1 == null || p2 == null ? const <Offset>[] : <Offset>[p1, p2];
|
||
}
|
||
return pts;
|
||
}
|
||
|
||
/// 复制并覆盖部分字段。
|
||
ArchShape copyWith({
|
||
String? id,
|
||
ArchShapeType? type,
|
||
Rect? rect,
|
||
double? radius,
|
||
int? dir,
|
||
Offset? a,
|
||
Offset? b,
|
||
bool? arrow,
|
||
int? fill,
|
||
int? stroke,
|
||
double? sw,
|
||
String? text,
|
||
List<Offset>? pts,
|
||
String? layerId,
|
||
String? symbolId,
|
||
Set<ArchDimKind>? dims,
|
||
}) {
|
||
return ArchShape(
|
||
id: id ?? this.id,
|
||
type: type ?? this.type,
|
||
rect: rect ?? this.rect,
|
||
radius: radius ?? this.radius,
|
||
dir: dir ?? this.dir,
|
||
a: a ?? this.a,
|
||
b: b ?? this.b,
|
||
arrow: arrow ?? this.arrow,
|
||
fill: fill ?? this.fill,
|
||
stroke: stroke ?? this.stroke,
|
||
sw: sw ?? this.sw,
|
||
text: text ?? this.text,
|
||
pts: pts ?? this.pts,
|
||
layerId: layerId ?? this.layerId,
|
||
symbolId: symbolId ?? this.symbolId,
|
||
dims: dims ?? this.dims,
|
||
);
|
||
}
|
||
|
||
/// 平移图形(线类图形同时平移端点 / 经过点)。
|
||
ArchShape translated(Offset d) {
|
||
if (type == ArchShapeType.line && a != null && b != null) {
|
||
return copyWith(a: a! + d, b: b! + d, rect: rect.shift(d));
|
||
}
|
||
if (pts.isNotEmpty) {
|
||
return copyWith(
|
||
pts: <Offset>[for (final Offset p in pts) p + d],
|
||
rect: rect.shift(d),
|
||
);
|
||
}
|
||
return copyWith(rect: rect.shift(d));
|
||
}
|
||
|
||
/// 序列化为 JSON。
|
||
Map<String, dynamic> toJson() => <String, dynamic>{
|
||
'id': id,
|
||
't': type.name,
|
||
'x': rect.left,
|
||
'y': rect.top,
|
||
'w': rect.width,
|
||
'h': rect.height,
|
||
if (radius != 0) 'r': radius,
|
||
if (dir != 0) 'dir': dir,
|
||
if (a != null) 'ax': a!.dx,
|
||
if (a != null) 'ay': a!.dy,
|
||
if (b != null) 'bx': b!.dx,
|
||
if (b != null) 'by': b!.dy,
|
||
if (arrow) 'arw': true,
|
||
'f': archColorHex(fill),
|
||
's': archColorHex(stroke),
|
||
'sw': sw,
|
||
if (text.isNotEmpty) 'txt': text,
|
||
if (pts.isNotEmpty)
|
||
'pts': <double>[for (final Offset p in pts) ...<double>[p.dx, p.dy]],
|
||
if (layerId.isNotEmpty) 'ly': layerId,
|
||
if (symbolId.isNotEmpty) 'sy': symbolId,
|
||
if (dims.isNotEmpty)
|
||
'dim': <String>[
|
||
for (final ArchDimKind k in dims) archDimKindName(k),
|
||
],
|
||
};
|
||
|
||
/// 从 JSON 反序列化(字段缺失时使用安全默认值)。
|
||
static ArchShape fromJson(Map<String, dynamic> j) {
|
||
final double x = (j['x'] as num?)?.toDouble() ?? 0;
|
||
final double y = (j['y'] as num?)?.toDouble() ?? 0;
|
||
final double w = (j['w'] as num?)?.toDouble() ?? 100;
|
||
final double h = (j['h'] as num?)?.toDouble() ?? 60;
|
||
final ArchShapeType type = ArchShapeType.values.firstWhere(
|
||
(ArchShapeType t) => t.name == j['t'],
|
||
orElse: () => ArchShapeType.rect,
|
||
);
|
||
final List<dynamic> flat = (j['pts'] as List<dynamic>?) ?? <dynamic>[];
|
||
final List<Offset> pts = <Offset>[];
|
||
for (int i = 0; i + 1 < flat.length; i += 2) {
|
||
pts.add(Offset(
|
||
(flat[i] as num).toDouble(),
|
||
(flat[i + 1] as num).toDouble(),
|
||
));
|
||
}
|
||
return ArchShape(
|
||
id: j['id'] as String? ?? 'S?',
|
||
type: type,
|
||
rect: Rect.fromLTWH(x, y, w, h),
|
||
radius: (j['r'] as num?)?.toDouble() ?? 0,
|
||
dir: (j['dir'] as num?)?.toInt() ?? 0,
|
||
a: j['ax'] == null
|
||
? null
|
||
: Offset((j['ax'] as num).toDouble(), (j['ay'] as num?)?.toDouble() ?? 0),
|
||
b: j['bx'] == null
|
||
? null
|
||
: Offset((j['bx'] as num).toDouble(), (j['by'] as num?)?.toDouble() ?? 0),
|
||
arrow: j['arw'] as bool? ?? false,
|
||
fill: archHexColor(j['f'], fallback: 0x264CC9F0),
|
||
stroke: archHexColor(j['s']),
|
||
sw: (j['sw'] as num?)?.toDouble() ?? 1.8,
|
||
text: j['txt'] as String? ?? '',
|
||
pts: pts,
|
||
layerId: j['ly'] as String? ?? kArchDefaultLayerId,
|
||
symbolId: j['sy'] as String? ?? '',
|
||
dims: <ArchDimKind>{
|
||
for (final dynamic e in (j['dim'] as List<dynamic>?) ?? <dynamic>[])
|
||
archDimKindFrom(e.toString()),
|
||
},
|
||
);
|
||
}
|
||
}
|
||
|
||
/// 一段手绘墨迹(未识别或关闭识别时保留的原始笔迹)。
|
||
class ArchStroke {
|
||
/// 构造一段墨迹。
|
||
const ArchStroke({
|
||
required this.id,
|
||
required this.pts,
|
||
this.color = 0xFF4CC9F0,
|
||
this.w = 2.2,
|
||
this.layerId = kArchDefaultLayerId,
|
||
});
|
||
|
||
/// 唯一标识(如 `K1`)。
|
||
final String id;
|
||
|
||
/// 笔迹采样点(世界坐标)。
|
||
final List<Offset> pts;
|
||
|
||
/// 颜色(ARGB)。
|
||
final int color;
|
||
|
||
/// 线宽(世界像素)。
|
||
final double w;
|
||
|
||
/// 所属图层 id(空串 = 未指定)。
|
||
final String layerId;
|
||
|
||
/// 笔迹外接矩形。
|
||
Rect get bounds {
|
||
if (pts.isEmpty) return Rect.zero;
|
||
double l = pts.first.dx, r = l, t = pts.first.dy, b = t;
|
||
for (final Offset p in pts) {
|
||
if (p.dx < l) l = p.dx;
|
||
if (p.dx > r) r = p.dx;
|
||
if (p.dy < t) t = p.dy;
|
||
if (p.dy > b) b = p.dy;
|
||
}
|
||
return Rect.fromLTRB(l, t, r, b);
|
||
}
|
||
|
||
/// 点 [p] 是否命中了这段笔迹。
|
||
bool hitTest(Offset p, {double pad = 4}) {
|
||
if (pts.length < 2) return pts.isNotEmpty && (pts.first - p).distance <= 8;
|
||
for (int i = 0; i < pts.length - 1; i++) {
|
||
if (archDistToSegment(p, pts[i], pts[i + 1]) <= math.max(6, w + pad)) {
|
||
return true;
|
||
}
|
||
}
|
||
return false;
|
||
}
|
||
|
||
/// 复制并覆盖部分字段。
|
||
ArchStroke copyWith({String? id, List<Offset>? pts, int? color, double? w, String? layerId}) {
|
||
return ArchStroke(
|
||
id: id ?? this.id,
|
||
pts: pts ?? this.pts,
|
||
color: color ?? this.color,
|
||
w: w ?? this.w,
|
||
layerId: layerId ?? this.layerId,
|
||
);
|
||
}
|
||
|
||
/// 序列化为 JSON;点列表压平为 `[x1,y1,x2,y2,...]`。
|
||
Map<String, dynamic> toJson() => <String, dynamic>{
|
||
'id': id,
|
||
'pts': <double>[for (final Offset p in pts) ...<double>[p.dx, p.dy]],
|
||
'c': archColorHex(color),
|
||
'w': w,
|
||
if (layerId.isNotEmpty) 'ly': layerId,
|
||
};
|
||
|
||
/// 从 JSON 反序列化。
|
||
static ArchStroke fromJson(Map<String, dynamic> j) {
|
||
final List<dynamic> flat = (j['pts'] as List<dynamic>?) ?? <dynamic>[];
|
||
final List<Offset> pts = <Offset>[];
|
||
for (int i = 0; i + 1 < flat.length; i += 2) {
|
||
pts.add(Offset(
|
||
(flat[i] as num).toDouble(),
|
||
(flat[i + 1] as num).toDouble(),
|
||
));
|
||
}
|
||
return ArchStroke(
|
||
id: j['id'] as String? ?? 'K?',
|
||
pts: pts,
|
||
color: archHexColor(j['c'], fallback: 0xFF4CC9F0),
|
||
w: (j['w'] as num?)?.toDouble() ?? 2.2,
|
||
layerId: j['ly'] as String? ?? kArchDefaultLayerId,
|
||
);
|
||
}
|
||
}
|
||
|
||
/// 架构图连线:连接两个图形(或自由端点),带箭头与可选标签。
|
||
///
|
||
/// [fromId]/[toId] 非空时表示吸附在对应图形上:端点每次绘制时按「图形中心
|
||
/// 连线与边框的交点」实时计算,因此移动图形后连线自动跟随。
|
||
/// [route] 控制走线:直线或正交折线。
|
||
class ArchConnector {
|
||
/// 构造一条连线。
|
||
const ArchConnector({
|
||
required this.id,
|
||
this.fromId,
|
||
required this.from,
|
||
this.toId,
|
||
required this.to,
|
||
this.label = '',
|
||
this.dash = false,
|
||
this.color = kArchConnectorColor,
|
||
this.route = ArchRoute.straight,
|
||
this.mids = const <Offset>[],
|
||
this.layerId = kArchDefaultLayerId,
|
||
});
|
||
|
||
/// 唯一标识(如 `C1`)。
|
||
final String id;
|
||
|
||
/// 起点吸附的图形 id(null 表示自由端点)。
|
||
final String? fromId;
|
||
|
||
/// 起点坐标(自由端点或图形缺失时的回退值)。
|
||
final Offset from;
|
||
|
||
/// 终点吸附的图形 id(null 表示自由端点)。
|
||
final String? toId;
|
||
|
||
/// 终点坐标(自由端点或图形缺失时的回退值)。
|
||
final Offset to;
|
||
|
||
/// 连线标签(可为空)。
|
||
final String label;
|
||
|
||
/// 是否虚线(云 / 外部系统连接常用)。
|
||
final bool dash;
|
||
|
||
/// 颜色(ARGB)。
|
||
final int color;
|
||
|
||
/// 走线方式(直线 / 正交折线)。
|
||
final ArchRoute route;
|
||
|
||
/// 中间折点(手画连线的轨迹识别结果;端点吸附图形时随图形移动,
|
||
/// 折点保持在世界坐标,可继续拖动编辑)。
|
||
final List<Offset> mids;
|
||
|
||
/// 所属图层 id(空串 = 未指定)。
|
||
final String layerId;
|
||
|
||
/// 复制并覆盖部分字段。
|
||
ArchConnector copyWith({
|
||
String? id,
|
||
Object? fromId = _keep,
|
||
Offset? from,
|
||
Object? toId = _keep,
|
||
Offset? to,
|
||
String? label,
|
||
bool? dash,
|
||
int? color,
|
||
ArchRoute? route,
|
||
List<Offset>? mids,
|
||
String? layerId,
|
||
}) {
|
||
return ArchConnector(
|
||
id: id ?? this.id,
|
||
fromId: identical(fromId, _keep) ? this.fromId : fromId as String?,
|
||
from: from ?? this.from,
|
||
toId: identical(toId, _keep) ? this.toId : toId as String?,
|
||
to: to ?? this.to,
|
||
label: label ?? this.label,
|
||
dash: dash ?? this.dash,
|
||
color: color ?? this.color,
|
||
route: route ?? this.route,
|
||
mids: mids ?? this.mids,
|
||
layerId: layerId ?? this.layerId,
|
||
);
|
||
}
|
||
|
||
/// 序列化为 JSON。
|
||
Map<String, dynamic> toJson() => <String, dynamic>{
|
||
'id': id,
|
||
if (fromId != null) 'fi': fromId,
|
||
'fx': from.dx,
|
||
'fy': from.dy,
|
||
if (toId != null) 'ti': toId,
|
||
'tx': to.dx,
|
||
'ty': to.dy,
|
||
if (label.isNotEmpty) 'lb': label,
|
||
if (dash) 'dsh': true,
|
||
'c': archColorHex(color),
|
||
if (route != ArchRoute.straight) 'rt': route.name,
|
||
if (mids.isNotEmpty)
|
||
'mid': <double>[for (final Offset p in mids) ...<double>[p.dx, p.dy]],
|
||
if (layerId.isNotEmpty) 'ly': layerId,
|
||
};
|
||
|
||
/// 从 JSON 反序列化。
|
||
static ArchConnector fromJson(Map<String, dynamic> j) {
|
||
final List<dynamic> flat = (j['mid'] as List<dynamic>?) ?? <dynamic>[];
|
||
final List<Offset> mids = <Offset>[];
|
||
for (int i = 0; i + 1 < flat.length; i += 2) {
|
||
mids.add(Offset(
|
||
(flat[i] as num).toDouble(),
|
||
(flat[i + 1] as num).toDouble(),
|
||
));
|
||
}
|
||
return ArchConnector(
|
||
id: j['id'] as String? ?? 'C?',
|
||
fromId: j['fi'] as String?,
|
||
from: Offset(
|
||
(j['fx'] as num?)?.toDouble() ?? 0,
|
||
(j['fy'] as num?)?.toDouble() ?? 0,
|
||
),
|
||
toId: j['ti'] as String?,
|
||
to: Offset(
|
||
(j['tx'] as num?)?.toDouble() ?? 0,
|
||
(j['ty'] as num?)?.toDouble() ?? 0,
|
||
),
|
||
label: j['lb'] as String? ?? '',
|
||
dash: j['dsh'] as bool? ?? false,
|
||
color: archHexColor(j['c'], fallback: kArchConnectorColor),
|
||
route: (j['rt'] as String?) == 'ortho'
|
||
? ArchRoute.ortho
|
||
: ArchRoute.straight,
|
||
mids: mids,
|
||
layerId: j['ly'] as String? ?? kArchDefaultLayerId,
|
||
);
|
||
}
|
||
}
|
||
|
||
/// 哨兵:用于 [ArchConnector.copyWith] 区分「未传参」与「显式置 null」。
|
||
const Object _keep = Object();
|
||
|
||
/// 架构图文档:图形 + 墨迹 + 连线 + 视口,是架构图模块的整体单元。
|
||
///
|
||
/// 通过 `Project.arch` 挂在工程文件里(暂不与时序图互通)。
|
||
class ArchDoc {
|
||
/// 构造文档。
|
||
const ArchDoc({
|
||
this.shapes = const <ArchShape>[],
|
||
this.strokes = const <ArchStroke>[],
|
||
this.connectors = const <ArchConnector>[],
|
||
this.zoom = 1,
|
||
this.panX = 0,
|
||
this.panY = 0,
|
||
this.gridSnap = true,
|
||
this.gridSize = 20,
|
||
this.layers = const <ArchLayer>[ArchLayer.fallback],
|
||
this.activeLayerId = 'L0',
|
||
this.sketch = SketchDoc.empty,
|
||
});
|
||
|
||
/// 空文档(新建工程 / 首次进入架构图时的初始状态)。
|
||
static const ArchDoc empty = ArchDoc();
|
||
|
||
/// 图形列表(后添加的绘制在上层)。
|
||
final List<ArchShape> shapes;
|
||
|
||
/// 墨迹列表(后添加的绘制在上层)。
|
||
final List<ArchStroke> strokes;
|
||
|
||
/// 连线列表。
|
||
final List<ArchConnector> connectors;
|
||
|
||
/// 视口缩放。
|
||
final double zoom;
|
||
|
||
/// 视口水平平移。
|
||
final double panX;
|
||
|
||
/// 视口垂直平移。
|
||
final double panY;
|
||
|
||
/// 是否吸附网格(默认开启;识别 / 移动 / 缩放时对位到网格)。
|
||
final bool gridSnap;
|
||
|
||
/// 网格粒度(世界像素,控制点阵间距与吸附步长;默认 20)。
|
||
final double gridSize;
|
||
|
||
/// 图层列表(AutoCAD 风格;至少一个)。
|
||
final List<ArchLayer> layers;
|
||
|
||
/// 当前活动图层 id(新建图形 / 墨迹 / 连线的归属)。
|
||
final String activeLayerId;
|
||
|
||
/// 参数化草图(点 / 几何 / 约束 / 驱动尺寸)。
|
||
final SketchDoc sketch;
|
||
|
||
/// 按 id 查找图层;未找到时返回兜底图层。
|
||
ArchLayer layerById(String? id) {
|
||
for (final ArchLayer l in layers) {
|
||
if (l.id == id) return l;
|
||
}
|
||
return layers.isEmpty ? ArchLayer.fallback : layers.first;
|
||
}
|
||
|
||
/// 当前活动图层 id(不合法时回退到第一个图层)。
|
||
String get activeLayer {
|
||
if (activeLayerId.isNotEmpty &&
|
||
layers.any((ArchLayer l) => l.id == activeLayerId)) {
|
||
return activeLayerId;
|
||
}
|
||
return layers.isEmpty ? ArchLayer.fallback.id : layers.first.id;
|
||
}
|
||
|
||
/// 图层是否可见(未指定归属视为可见)。
|
||
bool isLayerVisible(String? id) =>
|
||
id == null || id.isEmpty || layerById(id).visible;
|
||
|
||
/// 图层是否锁定(未指定归属视为未锁定)。
|
||
bool isLayerLocked(String? id) =>
|
||
id != null && id.isNotEmpty && layerById(id).locked;
|
||
|
||
/// 是否为空文档(无任何内容)。
|
||
bool get isEmpty =>
|
||
shapes.isEmpty &&
|
||
strokes.isEmpty &&
|
||
connectors.isEmpty &&
|
||
sketch.isEmpty;
|
||
|
||
/// 元素总数(供状态栏展示)。
|
||
int get elementCount => shapes.length + strokes.length + connectors.length;
|
||
|
||
/// 按 id 查找图形。
|
||
ArchShape? shapeById(String? id) {
|
||
if (id == null) return null;
|
||
for (final ArchShape s in shapes) {
|
||
if (s.id == id) return s;
|
||
}
|
||
return null;
|
||
}
|
||
|
||
/// 按 id 查找墨迹。
|
||
ArchStroke? strokeById(String? id) {
|
||
if (id == null) return null;
|
||
for (final ArchStroke s in strokes) {
|
||
if (s.id == id) return s;
|
||
}
|
||
return null;
|
||
}
|
||
|
||
/// 按 id 查找连线。
|
||
ArchConnector? connectorById(String? id) {
|
||
if (id == null) return null;
|
||
for (final ArchConnector c in connectors) {
|
||
if (c.id == id) return c;
|
||
}
|
||
return null;
|
||
}
|
||
|
||
/// 所有已用 id(生成新 id 时避免冲突)。
|
||
Iterable<String> get allIds => <String>[
|
||
for (final ArchShape s in shapes) s.id,
|
||
for (final ArchStroke s in strokes) s.id,
|
||
for (final ArchConnector c in connectors) c.id,
|
||
];
|
||
|
||
/// 内容外接矩形(「适应视图」用);隐藏图层不计入。
|
||
Rect contentBounds() {
|
||
double? l, t, r, b;
|
||
void include(Rect x) {
|
||
l = (l == null || x.left < l!) ? x.left : l;
|
||
t = (t == null || x.top < t!) ? x.top : t;
|
||
r = (r == null || x.right > r!) ? x.right : r;
|
||
b = (b == null || x.bottom > b!) ? x.bottom : b;
|
||
}
|
||
|
||
for (final ArchShape s in shapes) {
|
||
if (!isLayerVisible(s.layerId)) continue;
|
||
include(s.bounds);
|
||
}
|
||
for (final ArchStroke s in strokes) {
|
||
if (!isLayerVisible(s.layerId)) continue;
|
||
include(s.bounds);
|
||
}
|
||
for (final ArchConnector c in connectors) {
|
||
if (!isLayerVisible(c.layerId)) continue;
|
||
include(Rect.fromPoints(c.from, c.to));
|
||
}
|
||
final Rect? sk = sketch.contentBounds();
|
||
if (sk != null) include(sk);
|
||
if (l == null || t == null || r == null || b == null) {
|
||
// 空文档默认按 A0 图纸(1 世界单位 = 1 mm)。
|
||
return Rect.fromLTWH(0, 0, kArchPageSize.width, kArchPageSize.height);
|
||
}
|
||
return Rect.fromLTRB(l!, t!, r!, b!);
|
||
}
|
||
|
||
/// 复制并覆盖部分字段。
|
||
ArchDoc copyWith({
|
||
List<ArchShape>? shapes,
|
||
List<ArchStroke>? strokes,
|
||
List<ArchConnector>? connectors,
|
||
double? zoom,
|
||
double? panX,
|
||
double? panY,
|
||
bool? gridSnap,
|
||
double? gridSize,
|
||
List<ArchLayer>? layers,
|
||
String? activeLayerId,
|
||
SketchDoc? sketch,
|
||
}) {
|
||
return ArchDoc(
|
||
shapes: shapes ?? this.shapes,
|
||
strokes: strokes ?? this.strokes,
|
||
connectors: connectors ?? this.connectors,
|
||
zoom: zoom ?? this.zoom,
|
||
panX: panX ?? this.panX,
|
||
panY: panY ?? this.panY,
|
||
gridSnap: gridSnap ?? this.gridSnap,
|
||
gridSize: gridSize ?? this.gridSize,
|
||
layers: layers ?? this.layers,
|
||
activeLayerId: activeLayerId ?? this.activeLayerId,
|
||
sketch: sketch ?? this.sketch,
|
||
);
|
||
}
|
||
|
||
/// 移除若干 id:图形被移除时,吸附在它上面的连线一并移除。
|
||
ArchDoc removeIds(Set<String> ids) {
|
||
final List<ArchShape> nextShapes =
|
||
shapes.where((ArchShape s) => !ids.contains(s.id)).toList();
|
||
final Set<String> gone = <String>{
|
||
for (final ArchShape s in shapes)
|
||
if (ids.contains(s.id)) s.id,
|
||
};
|
||
final List<ArchStroke> nextStrokes =
|
||
strokes.where((ArchStroke s) => !ids.contains(s.id)).toList();
|
||
final List<ArchConnector> nextConnectors = connectors
|
||
.where((ArchConnector c) =>
|
||
!ids.contains(c.id) &&
|
||
!gone.contains(c.fromId) &&
|
||
!gone.contains(c.toId))
|
||
.toList();
|
||
return copyWith(
|
||
shapes: nextShapes,
|
||
strokes: nextStrokes,
|
||
connectors: nextConnectors,
|
||
);
|
||
}
|
||
|
||
/// 替换图形(id 相同则原位替换,否则追加到末尾)。
|
||
ArchDoc replaceShape(ArchShape s) {
|
||
bool found = false;
|
||
final List<ArchShape> next = <ArchShape>[];
|
||
for (final ArchShape x in shapes) {
|
||
if (x.id == s.id) {
|
||
next.add(s);
|
||
found = true;
|
||
} else {
|
||
next.add(x);
|
||
}
|
||
}
|
||
if (!found) next.add(s);
|
||
return copyWith(shapes: next);
|
||
}
|
||
|
||
/// 序列化为 JSON。
|
||
Map<String, dynamic> toJson() => <String, dynamic>{
|
||
'shapes': <Map<String, dynamic>>[
|
||
for (final ArchShape s in shapes) s.toJson(),
|
||
],
|
||
'strokes': <Map<String, dynamic>>[
|
||
for (final ArchStroke s in strokes) s.toJson(),
|
||
],
|
||
'connectors': <Map<String, dynamic>>[
|
||
for (final ArchConnector c in connectors) c.toJson(),
|
||
],
|
||
'zoom': zoom,
|
||
'pan': <double>[panX, panY],
|
||
'gridSnap': gridSnap,
|
||
'gridSize': gridSize,
|
||
'layers': <Map<String, dynamic>>[
|
||
for (final ArchLayer l in layers) l.toJson(),
|
||
],
|
||
'activeLayer': activeLayerId,
|
||
'sketch': sketch.toJson(),
|
||
};
|
||
|
||
/// 序列化为带两空格缩进的 JSON 文本(架构图文档 `*.arch.json` 用)。
|
||
String encode() => const JsonEncoder.withIndent(' ').convert(toJson());
|
||
|
||
/// 从 JSON 文本解码为 [ArchDoc]。
|
||
static ArchDoc decode(String source) =>
|
||
ArchDoc.fromJson(jsonDecode(source) as Map<String, dynamic>);
|
||
|
||
/// 从 JSON 反序列化(缺失字段使用安全默认值,保证旧文件可加载)。
|
||
static ArchDoc fromJson(Map<String, dynamic> j) {
|
||
final List<dynamic> sh = (j['shapes'] as List<dynamic>?) ?? <dynamic>[];
|
||
final List<dynamic> st = (j['strokes'] as List<dynamic>?) ?? <dynamic>[];
|
||
final List<dynamic> cn =
|
||
(j['connectors'] as List<dynamic>?) ?? <dynamic>[];
|
||
final List<dynamic> pan = (j['pan'] as List<dynamic>?) ?? <dynamic>[0, 0];
|
||
return ArchDoc(
|
||
shapes: sh
|
||
.map((dynamic e) => ArchShape.fromJson(e as Map<String, dynamic>))
|
||
.toList(),
|
||
strokes: st
|
||
.map((dynamic e) => ArchStroke.fromJson(e as Map<String, dynamic>))
|
||
.toList(),
|
||
connectors: cn
|
||
.map((dynamic e) => ArchConnector.fromJson(e as Map<String, dynamic>))
|
||
.toList(),
|
||
zoom: (j['zoom'] as num?)?.toDouble() ?? 1,
|
||
panX: (pan.isNotEmpty ? (pan[0] as num?)?.toDouble() : 0) ?? 0,
|
||
panY: (pan.length > 1 ? (pan[1] as num?)?.toDouble() : 0) ?? 0,
|
||
gridSnap: j['gridSnap'] as bool? ?? true,
|
||
gridSize: (j['gridSize'] as num?)?.toDouble() ?? 20,
|
||
layers: (() {
|
||
final List<dynamic> ls = (j['layers'] as List<dynamic>?) ?? <dynamic>[];
|
||
final List<ArchLayer> out = ls
|
||
.map((dynamic e) => ArchLayer.fromJson(e as Map<String, dynamic>))
|
||
.toList();
|
||
return out.isEmpty ? const <ArchLayer>[ArchLayer.fallback] : out;
|
||
})(),
|
||
activeLayerId: j['activeLayer'] as String? ?? 'L0',
|
||
sketch: j['sketch'] == null
|
||
? SketchDoc.empty
|
||
: SketchDoc.fromJson(j['sketch'] as Map<String, dynamic>),
|
||
);
|
||
}
|
||
}
|