/// 数据模型层:定义序列图(Sequence Diagram)的领域模型与序列化协议。 /// /// 本文件是整个应用的“单一事实来源”,不依赖任何 UI 或持久化实现, /// 由 layout / state / persistence 等上层模块共同引用: /// - [Project] 聚合工位、动作、节拍、单位与视图状态; /// - 所有模型均提供 copyWith / toJson / fromJson,保证不可变更新与 /// JSON 往返(round-trip)序列化; /// - 动作之间的时序约束由 [Dependency] 描述,供关键路径计算使用。 library; import 'dart:convert'; /// 主视图模式。 enum ViewMode { /// 条形(甘特式)节拍视图。 bar, /// 圆周(环形)节拍视图。 circular, } /// 时间单位。 enum TimeUnit { /// 毫秒。 ms, /// 秒。 s, /// 分钟。 min, } /// 相位单位。 enum PhaseUnit { /// 角度(度)。 deg, /// 百分比(相对整圈 360° 的比例)。 percent, } /// 动作依赖类型(前驱/后继的开始或结束关系)。 enum DepType { /// Finish-to-Start:前驱结束之后,后继才能开始。 fs, /// Start-to-Start:前驱开始之后,后继才能开始。 ss, /// Finish-to-Finish:前驱结束后,后继才能结束。 ff, /// Start-to-Finish:前驱开始后,后继才能结束。 sf, } /// 圆周视图的旋转方向。 enum CycleDirection { /// 顺时针。 cw, /// 逆时针。 ccw, } /// 圆周视图的 0° 起始位置。 enum CycleOrigin { /// 十二点钟方向为起点。 twelve, /// 三点钟方向为起点。 three, } /// 修改循环时长时对动作相位的处理策略。 enum CycleInputPolicy { /// 保持角度不变(仅改变时长与时间换算)。 keepAngle, /// 保持时间不变(按比例缩放角度)。 keepTime, } /// 新增动作时循环取用的默认配色板(ARGB 颜色值)。 const List kActionPalette = [ 0xFF4CC9F0, 0xFF7BD88F, 0xFFFFD166, 0xFFFF8A5C, 0xFFF472B6, 0xFFA78BFA, 0xFF38BDF8, 0xFF34D399, ]; /// 将 ARGB 整数颜色序列化为 `#rrggbb` 十六进制字符串(丢弃透明度通道)。 /// /// 参数 [argb]:32 位 ARGB 颜色值。 /// 返回:形如 `#4cc9f0` 的字符串,用于 JSON 持久化。 String colorToHex(int argb) { final int rgb = argb & 0xFFFFFF; return '#${rgb.toRadixString(16).padLeft(6, '0')}'; } /// 将 `#rrggbb`(可带 `#`,大小写均可)解析为不透明的 ARGB 整数。 /// /// 参数 [hex]:十六进制颜色字符串。 /// 返回:`0xFF000000 | rgb`;解析失败时回退到默认色 `0x4CC9F0`。 int hexToColor(String hex) { final String h = hex.replaceAll('#', '').trim(); final int rgb = int.tryParse(h, radix: 16) ?? 0x4CC9F0; return 0xFF000000 | (rgb & 0xFFFFFF); } /// 动作之间的时序依赖:由 [fromId] 指向 [toId],描述二者的开始/结束约束。 /// /// 依赖会被关键路径算法(见 AppController._recomputeCritical)用来计算 /// 各动作的最早开始时间,并高亮关键链。 class Dependency { /// 构造一条依赖。 /// /// 参数 [fromId]:前驱动作 id;[toId]:后继动作 id; /// [type]:依赖类型,默认 [DepType.fs];[offsetAngle]:附加相位偏移(度)。 const Dependency({ required this.fromId, required this.toId, this.type = DepType.fs, this.offsetAngle = 0, }); /// 前驱动作 id。 final String fromId; /// 后继动作 id。 final String toId; /// 依赖类型(FS/SS/FF/SF)。 final DepType type; /// 依赖的附加角度偏移,单位:度。 final double offsetAngle; /// 基于当前实例创建副本,未提供的参数保持原值。 Dependency copyWith({ String? fromId, String? toId, DepType? type, double? offsetAngle, }) { return Dependency( fromId: fromId ?? this.fromId, toId: toId ?? this.toId, type: type ?? this.type, offsetAngle: offsetAngle ?? this.offsetAngle, ); } /// 序列化为 JSON;依赖类型以大写形式(如 `FS`)写出,便于跨版本读取。 Map toJson() => { 'fromId': fromId, 'toId': toId, 'type': type.name.toUpperCase(), 'offsetAngle': offsetAngle, }; /// 从 JSON 反序列化;缺失字段使用安全默认值,保证旧文件可加载。 static Dependency fromJson(Map j) { return Dependency( fromId: j['fromId'] as String? ?? '', toId: j['toId'] as String? ?? '', type: _depTypeFrom(j['type'] as String?), offsetAngle: (j['offsetAngle'] as num?)?.toDouble() ?? 0, ); } /// 将字符串(不区分大小写)解析为 [DepType],无法识别时回退为 FS。 static DepType _depTypeFrom(String? s) { switch ((s ?? 'FS').toUpperCase()) { case 'SS': return DepType.ss; case 'FF': return DepType.ff; case 'SF': return DepType.sf; default: return DepType.fs; } } } /// 单个动作(工序):序列图的核心实体,用角度表示其在节拍周期内的区间。 /// /// 不变式:`0 <= startAngle`,`durationAngle > 0`,且二者共同构成一个 /// 角度区间 `[startAngle, startAngle + durationAngle]`;当结束角超过 360° /// 时视为跨圈(见 [wrap])。 class Action { /// 构造一个动作。 /// /// 参数 [id]:唯一标识;[name]:显示名称;[stationId]:所属工位; /// [driver]:执行元件/驱动描述;[startAngle]/[durationAngle]:起始角与持续角; /// [layer]:圆周视图中的层(半径序号);[color]:ARGB 颜色; /// [deps]:该动作作为后继的依赖列表(即指向本动作的边)。 const Action({ required this.id, required this.name, required this.stationId, required this.driver, required this.startAngle, required this.durationAngle, required this.layer, required this.color, this.row = -1, this.deps = const [], }); /// 动作唯一标识,如 `A1`。 final String id; /// 动作显示名称。 final String name; /// 所属工位 id,对应 [Station.id]。 final String stationId; /// 执行元件/驱动的描述文本。 final String driver; /// 起始相位角,单位:度。 final double startAngle; /// 持续相位角(区间宽度),单位:度。 final double durationAngle; /// 圆周视图中的层号(越大半径越大)。 final int layer; /// 动作颜色(ARGB)。 final int color; /// 条形视图中的行序(自上而下,从 0 开始)。 /// /// `-1` 表示“未指定”,此时由 [BarLayout] 按工位与起始角自动排序; /// 一旦用户上下拖拽过行,就写入显式行序,行位置不再随相位变化而跳动。 final int row; /// 指向本动作的依赖集合(本动作作为后继)。 final List deps; /// 结束相位角 = 起始角 + 持续角,单位:度。 double get endAngle => startAngle + durationAngle; /// 是否跨圈(结束角超过 360°,预留微小浮点容差)。 bool get wrap => endAngle > 360.0001; /// 基于当前实例创建副本,未提供的参数保持原值。 Action copyWith({ String? id, String? name, String? stationId, String? driver, double? startAngle, double? durationAngle, int? layer, int? color, int? row, List? deps, }) { return Action( id: id ?? this.id, name: name ?? this.name, stationId: stationId ?? this.stationId, driver: driver ?? this.driver, startAngle: startAngle ?? this.startAngle, durationAngle: durationAngle ?? this.durationAngle, layer: layer ?? this.layer, color: color ?? this.color, row: row ?? this.row, deps: deps ?? this.deps, ); } /// 序列化为 JSON;颜色以 `#rrggbb` 字符串保存,依赖递归序列化。 Map toJson() => { 'id': id, 'name': name, 'stationId': stationId, 'driver': driver, 'startAngle': startAngle, 'durationAngle': durationAngle, 'layer': layer, 'color': colorToHex(color), 'row': row, 'deps': deps.map((Dependency d) => d.toJson()).toList(), }; /// 从 JSON 反序列化;字段缺失或类型异常时使用默认值以保证健壮性。 static Action fromJson(Map j) { return Action( id: j['id'] as String? ?? 'A?', name: j['name'] as String? ?? '动作', stationId: j['stationId'] as String? ?? 'ST1', driver: j['driver'] as String? ?? '', startAngle: (j['startAngle'] as num?)?.toDouble() ?? 0, durationAngle: (j['durationAngle'] as num?)?.toDouble() ?? 0, layer: (j['layer'] as num?)?.toInt() ?? 0, color: hexToColor(j['color'] as String? ?? '#4cc9f0'), row: (j['row'] as num?)?.toInt() ?? -1, deps: ((j['deps'] as List?) ?? []) .map((dynamic e) => Dependency.fromJson(e as Map)) .toList(), ); } } /// 工位:动作的归属单元,决定条形视图中动作的分组与排序。 class Station { /// 构造一个工位。 /// /// 参数 [id]:唯一标识;[name]:显示名称;[order]:排序权重(越小越靠前); /// [color]:ARGB 颜色。 const Station({ required this.id, required this.name, required this.order, required this.color, }); /// 工位唯一标识,如 `ST1`。 final String id; /// 工位显示名称。 final String name; /// 排序权重,用于条形视图对动作排序。 final int order; /// 工位颜色(ARGB)。 final int color; /// 基于当前实例创建副本,未提供的参数保持原值。 Station copyWith({String? id, String? name, int? order, int? color}) { return Station( id: id ?? this.id, name: name ?? this.name, order: order ?? this.order, color: color ?? this.color, ); } /// 序列化为 JSON;颜色以 `#rrggbb` 字符串保存。 Map toJson() => { 'id': id, 'name': name, 'order': order, 'color': colorToHex(color), }; /// 从 JSON 反序列化;缺失字段使用默认值。 static Station fromJson(Map j) { return Station( id: j['id'] as String? ?? 'ST1', name: j['name'] as String? ?? '工位', order: (j['order'] as num?)?.toInt() ?? 0, color: hexToColor(j['color'] as String? ?? '#4cc9f0'), ); } } /// 节拍(循环)参数:定义整圈对应的时长、起始角、旋转方向与吸附精度。 /// /// 不变式:`durationMs > 0`,`snapDeg >= 0`(为 0 表示不吸附)。 class Cycle { /// 构造节拍参数。 /// /// 参数 [durationMs]:整圈时长(毫秒),默认 2400;[startAngle]:起始相位角; /// [direction]:旋转方向;[origin]:0° 基准位置;[snapDeg]:角度吸附步长(度)。 const Cycle({ this.durationMs = 3600, this.startAngle = 0, this.direction = CycleDirection.cw, this.origin = CycleOrigin.twelve, this.snapDeg = 1, }); /// 整圈对应的时长,单位:毫秒。 final double durationMs; /// 循环起始相位角,单位:度。 final double startAngle; /// 旋转方向(顺/逆时针)。 final CycleDirection direction; /// 0° 基准位置。 final CycleOrigin origin; /// 角度吸附步长,单位:度;0 表示禁用吸附。 final double snapDeg; /// 基于当前实例创建副本,未提供的参数保持原值。 Cycle copyWith({ double? durationMs, double? startAngle, CycleDirection? direction, CycleOrigin? origin, double? snapDeg, }) { return Cycle( durationMs: durationMs ?? this.durationMs, startAngle: startAngle ?? this.startAngle, direction: direction ?? this.direction, origin: origin ?? this.origin, snapDeg: snapDeg ?? this.snapDeg, ); } /// 序列化为 JSON;枚举以 name 字符串保存。 Map toJson() => { 'durationMs': durationMs, 'startAngle': startAngle, 'direction': direction.name, 'origin': origin.name, 'snapDeg': snapDeg, }; /// 从 JSON 反序列化;未知枚举值回退为默认(顺时针、十二点钟方向)。 static Cycle fromJson(Map j) { return Cycle( durationMs: (j['durationMs'] as num?)?.toDouble() ?? 2400, startAngle: (j['startAngle'] as num?)?.toDouble() ?? 0, direction: (j['direction'] as String?) == 'ccw' ? CycleDirection.ccw : CycleDirection.cw, origin: (j['origin'] as String?) == '3o' ? CycleOrigin.three : CycleOrigin.twelve, snapDeg: (j['snapDeg'] as num?)?.toDouble() ?? 1, ); } } /// 单位与显示设置:控制时间/相位的显示格式、吸附步长与网格行为。 /// /// 该设置属于项目数据的一部分,会随项目一起持久化。 class UnitSettings { /// 构造单位设置。 /// /// 参数 [timeUnit]/[phaseUnit]:时间与相位单位;[precision]:小数位数, /// 负数表示自动;[snapAngle]/[snapTime]:角度与时间吸附步长; /// [cycleInputPolicy]:修改循环时长时保持角度还是保持时间; /// [showGrid]/[snapGrid]:网格显示与吸附开关;[autoSplitWrap]:跨圈自动拆分; /// [preset]:预设方案标识。 const UnitSettings({ this.timeUnit = TimeUnit.s, this.phaseUnit = PhaseUnit.deg, this.precision = -1, this.snapAngle = 1, this.snapTime = 0.1, this.cycleInputPolicy = CycleInputPolicy.keepAngle, this.showGrid = true, this.snapGrid = true, this.autoSplitWrap = true, this.preset = 'assembly', }); /// 时间显示单位。 final TimeUnit timeUnit; /// 相位显示单位。 final PhaseUnit phaseUnit; /// 小数位数;负数表示按单位自动决定(见 units.dart 的 `_dec`)。 final int precision; /// 角度吸附步长,单位:度。 final double snapAngle; /// 时间吸附步长,单位:毫秒(由调用方换算)。 final double snapTime; /// 修改循环时长时对动作相位的处理策略。 final CycleInputPolicy cycleInputPolicy; /// 是否显示网格。 final bool showGrid; /// 是否吸附到网格。 final bool snapGrid; /// 跨圈动作是否自动拆分显示。 final bool autoSplitWrap; /// 当前单位预设标识。 final String preset; /// 基于当前实例创建副本,未提供的参数保持原值。 UnitSettings copyWith({ TimeUnit? timeUnit, PhaseUnit? phaseUnit, int? precision, double? snapAngle, double? snapTime, CycleInputPolicy? cycleInputPolicy, bool? showGrid, bool? snapGrid, bool? autoSplitWrap, String? preset, }) { return UnitSettings( timeUnit: timeUnit ?? this.timeUnit, phaseUnit: phaseUnit ?? this.phaseUnit, precision: precision ?? this.precision, snapAngle: snapAngle ?? this.snapAngle, snapTime: snapTime ?? this.snapTime, cycleInputPolicy: cycleInputPolicy ?? this.cycleInputPolicy, showGrid: showGrid ?? this.showGrid, snapGrid: snapGrid ?? this.snapGrid, autoSplitWrap: autoSplitWrap ?? this.autoSplitWrap, preset: preset ?? this.preset, ); } /// 序列化为 JSON。 Map toJson() => { 'timeUnit': timeUnit.name, 'phaseUnit': phaseUnit.name, 'precision': precision, 'snapAngle': snapAngle, 'snapTime': snapTime, 'cycleInputPolicy': cycleInputPolicy.name, 'showGrid': showGrid, 'snapGrid': snapGrid, 'autoSplitWrap': autoSplitWrap, 'preset': preset, }; /// 从 JSON 反序列化;枚举按 name 匹配,无法匹配时回退到默认值。 static UnitSettings fromJson(Map j) { return UnitSettings( timeUnit: TimeUnit.values.firstWhere( (TimeUnit u) => u.name == j['timeUnit'], orElse: () => TimeUnit.s, ), phaseUnit: PhaseUnit.values.firstWhere( (PhaseUnit u) => u.name == j['phaseUnit'], orElse: () => PhaseUnit.deg, ), precision: (j['precision'] as num?)?.toInt() ?? -1, snapAngle: (j['snapAngle'] as num?)?.toDouble() ?? 1, snapTime: (j['snapTime'] as num?)?.toDouble() ?? 0.1, cycleInputPolicy: (j['cycleInputPolicy'] as String?) == 'keepTime' ? CycleInputPolicy.keepTime : CycleInputPolicy.keepAngle, showGrid: j['showGrid'] as bool? ?? true, snapGrid: j['snapGrid'] as bool? ?? true, autoSplitWrap: j['autoSplitWrap'] as bool? ?? true, preset: j['preset'] as String? ?? 'assembly', ); } } /// 视图状态:记录当前视图模式、缩放/平移以及各类显示开关和选中项。 /// /// 不变式:[zoom] 由 AppController.setZoom 限制在 `[0.25, 4.0]`; /// [selectedId] 为 null 表示无选中。该状态随项目持久化,但不参与业务计算。 class ViewState { /// 构造视图状态。 /// /// 参数 [mode]:视图模式;[zoom]:缩放系数;[panX]/[panY]:平移量; /// [showDeps]/[showRuler]/[showPhaseBands]/[showRingBg]/[showLayerLabels]: /// 各类显示开关;[selectedId]:当前选中动作 id。 const ViewState({ this.mode = ViewMode.bar, this.zoom = 1.0, this.panX = 0, this.panY = 0, this.showDeps = true, this.showRuler = true, this.showPhaseBands = true, this.showRingBg = true, this.showLayerLabels = true, this.selectedIds = const [], }); /// 当前视图模式(条形/圆周)。 final ViewMode mode; /// 缩放系数。 final double zoom; /// 水平平移量(像素)。 final double panX; /// 垂直平移量(像素)。 final double panY; /// 是否显示依赖连线。 final bool showDeps; /// 是否显示刻度尺。 final bool showRuler; /// 是否显示相位分区色带。 final bool showPhaseBands; /// 是否显示圆周背景环。 final bool showRingBg; /// 是否显示层标签。 final bool showLayerLabels; /// 当前选中的动作 id 列表(支持 Ctrl 多选);空表示无选中。 /// /// 约定:**第一个元素为主选中项**(右侧属性面板展示它)。 final List selectedIds; /// 主选中动作 id(列表首项);空表示无选中。 String? get selectedId => selectedIds.isEmpty ? null : selectedIds.first; /// 判断动作 [id] 是否被选中。 bool isSelected(String id) => selectedIds.contains(id); /// 基于当前实例创建副本。 ViewState copyWith({ ViewMode? mode, double? zoom, double? panX, double? panY, bool? showDeps, bool? showRuler, bool? showPhaseBands, bool? showRingBg, bool? showLayerLabels, List? selectedIds, }) { return ViewState( mode: mode ?? this.mode, zoom: zoom ?? this.zoom, panX: panX ?? this.panX, panY: panY ?? this.panY, showDeps: showDeps ?? this.showDeps, showRuler: showRuler ?? this.showRuler, showPhaseBands: showPhaseBands ?? this.showPhaseBands, showRingBg: showRingBg ?? this.showRingBg, showLayerLabels: showLayerLabels ?? this.showLayerLabels, selectedIds: selectedIds ?? this.selectedIds, ); } /// 序列化为 JSON;平移量以二元数组 `[panX, panY]` 保存。 Map toJson() => { 'mode': mode.name, 'zoom': zoom, 'pan': [panX, panY], 'showDeps': showDeps, 'showRuler': showRuler, 'showPhaseBands': showPhaseBands, 'showRingBg': showRingBg, 'showLayerLabels': showLayerLabels, 'selectedIds': selectedIds, }; /// 从 JSON 反序列化;选中项用于界面恢复,缺失时默认无选中。 static ViewState fromJson(Map j) { final List pan = (j['pan'] as List?) ?? [0, 0]; final List sel = (j['selectedIds'] as List?) ?? []; return ViewState( mode: (j['mode'] as String?) == 'circular' ? ViewMode.circular : ViewMode.bar, 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, showDeps: j['showDeps'] as bool? ?? true, showRuler: j['showRuler'] as bool? ?? true, showPhaseBands: j['showPhaseBands'] as bool? ?? true, showRingBg: j['showRingBg'] as bool? ?? true, showLayerLabels: j['showLayerLabels'] as bool? ?? true, selectedIds: sel.map((dynamic e) => e as String).toList(), ); } } /// 相位分区:循环内的一个命名角度区间(例如“上料相位 0°–70°”)。 /// /// 用于在条形节拍视图顶部绘制工艺阶段标注带,也用于属性面板的“相位归属”判断。 /// 区间允许 [endAngle] 超过 360° 以表示跨循环(例如“下料相位 335°–385°”)。 class PhaseZone { /// 构造相位分区。 const PhaseZone({ required this.name, required this.startAngle, required this.endAngle, }); /// 分区名称(如“加工相位”)。 final String name; /// 起始相位角(度,0° = 12 点钟方向)。 final double startAngle; /// 结束相位角(度,可大于 360° 表示跨圈)。 final double endAngle; /// 该分区的角度跨度。 double get span => endAngle - startAngle; /// 判断给定角度 [deg] 是否落在本分区内(自动处理跨圈)。 bool contains(double deg) { final double n = deg % 360; if (endAngle <= 360) { return n >= startAngle - 0.001 && n <= endAngle + 0.001; } return n >= startAngle - 0.001 || n <= (endAngle - 360) + 0.001; } /// 创建副本,未提供的参数保持原值。 PhaseZone copyWith({String? name, double? startAngle, double? endAngle}) { return PhaseZone( name: name ?? this.name, startAngle: startAngle ?? this.startAngle, endAngle: endAngle ?? this.endAngle, ); } /// 序列化为 JSON。 Map toJson() => { 'name': name, 'startAngle': startAngle, 'endAngle': endAngle, }; /// 从 JSON 反序列化。 static PhaseZone fromJson(Map j) { return PhaseZone( name: j['name'] as String? ?? '相位分区', startAngle: (j['startAngle'] as num?)?.toDouble() ?? 0, endAngle: (j['endAngle'] as num?)?.toDouble() ?? 0, ); } /// 默认相位分区:四等分的通用分区(分区 1–4)。 /// /// 用户可在工艺参数中改名、改起止角、增删数量,也可在条形图顶部直接拖拽边界。 static const List defaults = [ PhaseZone(name: '分区 1', startAngle: 0, endAngle: 90), PhaseZone(name: '分区 2', startAngle: 90, endAngle: 180), PhaseZone(name: '分区 3', startAngle: 180, endAngle: 270), PhaseZone(name: '分区 4', startAngle: 270, endAngle: 360), ]; } /// 项目根聚合:包含节拍、工位、动作、单位与视图状态,是保存/加载的整体单元。 /// /// 不变式:[actions] 中的 id 唯一;动作的 [Action.stationId] 通常应存在于 /// [stations] 中;依赖的 fromId/toId 应指向存在的动作。 class Project { /// 构造项目。 /// /// 参数 [name]:项目名称;[schema]:数据协议版本标识; /// [cycle]:节拍参数;[stations]:工位列表;[actions]:动作列表; /// [units]:单位设置;[viewState]:视图状态。 const Project({ required this.name, this.schema = 'seqdiagram/1.2', required this.cycle, required this.stations, required this.actions, required this.units, required this.viewState, this.phaseZones = PhaseZone.defaults, }); /// 项目名称。 final String name; /// 数据协议版本标识,用于兼容性判断。 final String schema; /// 节拍参数。 final Cycle cycle; /// 工位列表。 final List stations; /// 动作列表。 final List actions; /// 单位与显示设置。 final UnitSettings units; /// 视图状态。 final ViewState viewState; /// 相位分区(命名角度区间),用于条形视图顶部工艺阶段标注。 final List phaseZones; /// 按 id 查找动作;[id] 为 null 或未找到时返回 null。 Action? actionById(String? id) { if (id == null) return null; for (final Action a in actions) { if (a.id == id) return a; } return null; } /// 按 id 查找工位;未找到时返回 null。 Station? stationById(String id) { for (final Station s in stations) { if (s.id == id) return s; } return null; } /// 所有动作中的最大层号;无动作时返回 0。 int get maxLayer { int m = 0; for (final Action a in actions) { if (a.layer > m) m = a.layer; } return m; } /// 基于当前实例创建副本,未提供的参数保持原值。 Project copyWith({ String? name, String? schema, Cycle? cycle, List? stations, List? actions, UnitSettings? units, ViewState? viewState, List? phaseZones, }) { return Project( name: name ?? this.name, schema: schema ?? this.schema, cycle: cycle ?? this.cycle, stations: stations ?? this.stations, actions: actions ?? this.actions, units: units ?? this.units, viewState: viewState ?? this.viewState, phaseZones: phaseZones ?? this.phaseZones, ); } /// 序列化为 JSON。外层包含 `schema` 版本号,实际数据位于 `project` 子对象。 Map toJson() => { 'schema': schema, 'project': { 'name': name, 'units': units.toJson(), 'cycle': cycle.toJson(), 'stations': stations.map((Station s) => s.toJson()).toList(), 'actions': actions.map((Action a) => a.toJson()).toList(), 'viewState': viewState.toJson(), 'phaseZones': phaseZones.map((PhaseZone z) => z.toJson()).toList(), }, }; /// 序列化为带两空格缩进的 JSON 文本,便于人工阅读与 diff。 String encode() => const JsonEncoder.withIndent(' ').convert(toJson()); /// 从 JSON 反序列化。 /// /// 兼容两种结构:带 `project` 子对象的正式格式,以及直接平铺的旧格式 /// (当不存在 `project` 键时把根对象当作数据体)。 static Project fromJson(Map j) { final Map p = (j['project'] as Map?) ?? j; final List st = (p['stations'] as List?) ?? []; final List ac = (p['actions'] as List?) ?? []; final List pz = (p['phaseZones'] as List?) ?? []; return Project( name: p['name'] as String? ?? '未命名', schema: j['schema'] as String? ?? 'seqdiagram/1.2', cycle: Cycle.fromJson((p['cycle'] as Map?) ?? {}), stations: st.map((dynamic e) => Station.fromJson(e as Map)).toList(), actions: ac.map((dynamic e) => Action.fromJson(e as Map)).toList(), units: UnitSettings.fromJson((p['units'] as Map?) ?? {}), viewState: ViewState.fromJson((p['viewState'] as Map?) ?? {}), phaseZones: pz.isEmpty ? PhaseZone.defaults : pz.map((dynamic e) => PhaseZone.fromJson(e as Map)).toList(), ); } /// 从 JSON 文本解码为 [Project]。 static Project decode(String source) { return Project.fromJson(jsonDecode(source) as Map); } /// 构造内置演示项目(装配线节拍方案)。 /// /// 返回一个包含 2 个工位、2 个动作(动作 1 / 动作 2)的示例 [Project], /// 用于首次启动展示。刻意保持精简,避免预设内容过多干扰用户。 static Project demo() { final List stations = [ const Station(id: 'ST1', name: '工位 1', order: 0, color: 0xFF4CC9F0), const Station(id: 'ST2', name: '工位 2', order: 1, color: 0xFFFFD166), ]; final List actions = [ Action( id: 'A1', name: '动作 1', stationId: 'ST1', driver: '气缸 Y1', startAngle: 0, durationAngle: 70, layer: 0, row: 0, color: kActionPalette[0], ), Action( id: 'A2', name: '动作 2', stationId: 'ST2', driver: '气缸 Y2', startAngle: 80, durationAngle: 60, layer: 1, row: 1, color: kActionPalette[1], deps: const [ Dependency(fromId: 'A1', toId: 'A2', type: DepType.fs), ], ), ]; return Project( name: '未命名节拍', cycle: const Cycle(durationMs: 3600), stations: stations, actions: actions, units: const UnitSettings(), viewState: const ViewState(mode: ViewMode.bar, selectedIds: ['A1']), ); } }