架构图增强:橡皮、手动确认识别(平板推荐)、连线一律正交+角点拖拽、手绘/连线职责分离、尺寸标注一键操作;帮助文件大规模更新;修复品牌按钮与 pubspec 描述

This commit is contained in:
2026-10-11 11:22:01 +08:00
parent 1762ee4abf
commit c8147e294d
16 changed files with 767 additions and 614 deletions
+189 -35
View File
@@ -128,6 +128,10 @@ class _ArchViewState extends State<ArchView>
List<Offset> _lastConvPts = const <Offset>[];
DateTime? _lastConvAt;
// ---- 橡皮擦 ----
bool _erasing = false;
int _eraseCount = 0;
// ---- 组合识别缓存(两笔绘图 / 圆柱拼接) ----
String? _lastInkId;
DateTime? _lastInkAt;
@@ -225,7 +229,8 @@ class _ArchViewState extends State<ArchView>
setState(() {});
}
/// 播放「识别演示」:把当前墨迹逐条识别,串行播放转化动画。
/// 播放「识别」:把当前墨迹逐条识别(图元库单笔 → 内置 → 与前一条组合),
/// 串行播放转化动画。手动确认识别模式与「识别 N」按钮共用。
Future<void> _startPlay() async {
if (_playing) return;
_playing = true;
@@ -233,29 +238,112 @@ class _ArchViewState extends State<ArchView>
final List<String> ids = <String>[
for (final ArchStroke s in c.arch.strokes) s.id,
];
void convert(ArchShape shape, List<Offset> pts, Set<String> removeIds) {
_conversions.add(ArchConversion(
pts: pts,
shape: shape,
startMs: DateTime.now().millisecondsSinceEpoch.toDouble(),
undo: false,
removeIds: removeIds,
));
if (!_ticker.isActive) _ticker.start();
setState(() {});
}
Future<void> waitTick() =>
Future<void>.delayed(const Duration(milliseconds: 230));
ArchStroke? prev;
ArchRecognition? prevBuiltin;
Future<void> flushPrev() async {
final ArchStroke? p = prev;
final ArchRecognition? r0 = prevBuiltin;
prev = null;
prevBuiltin = null;
if (p == null || r0 == null) return;
convert(
_snapShapeToGrid(archShapeFromRecognition(r0,
id: archNewId('S', c.arch.allIds), color: c.archColor)),
p.pts,
<String>{p.id},
);
await waitTick();
}
for (final String id in ids) {
if (!mounted || !_playing || _seenPlayToken != token) break;
final ArchStroke? st = c.arch.strokeById(id);
if (st == null) continue;
final ArchRecognition? r = recognizeArchStroke(st.pts);
if (r != null) {
final ArchShape shape = archShapeFromRecognition(
r,
id: archNewId('S', c.arch.allIds),
color: c.archColor,
// 1) 图元库单笔。
final ArchSymbolMatch? m1 =
recognizeArchSymbol(<List<Offset>>[st.pts], SymbolCache.all);
if (m1 != null) {
await flushPrev();
convert(
_symbolShapeFor(m1.symbol, _ptsBounds(st.pts)),
st.pts,
<String>{id},
);
_conversions.add(ArchConversion(
pts: st.pts,
shape: shape,
startMs: DateTime.now().millisecondsSinceEpoch.toDouble(),
undo: false,
removeIds: <String>{id},
));
if (!_ticker.isActive) _ticker.start();
setState(() {});
await waitTick();
continue;
}
await Future<void>.delayed(const Duration(milliseconds: 230));
final ArchRecognition? r = recognizeArchStroke(st.pts);
final bool weak = r == null ||
r.type == ArchShapeType.curve ||
r.type == ArchShapeType.polyline;
// 2) 内置强结果:直接转化。
if (!weak) {
await flushPrev();
convert(
_snapShapeToGrid(archShapeFromRecognition(r,
id: archNewId('S', c.arch.allIds), color: c.archColor)),
st.pts,
<String>{id},
);
await waitTick();
continue;
}
// 3) 弱结果 / 未识别:先与上一条尝试组合(图元库 → 内置两笔)。
if (prev != null) {
final ArchStroke p = prev!;
final ArchSymbolMatch? m2 = recognizeArchSymbol(
<List<Offset>>[p.pts, st.pts], SymbolCache.all);
if (m2 != null) {
prev = null;
prevBuiltin = null;
convert(
_symbolShapeFor(
m2.symbol,
_ptsBounds(p.pts).expandToInclude(_ptsBounds(st.pts)),
),
st.pts,
<String>{p.id, id},
);
await waitTick();
continue;
}
final ArchRecognition? r2 =
recognizeArchStrokes(<List<Offset>>[p.pts, st.pts]);
if (r2 != null && r2.type != ArchShapeType.line) {
prev = null;
prevBuiltin = null;
convert(
_snapShapeToGrid(archShapeFromRecognition(r2,
id: archNewId('S', c.arch.allIds), color: c.archColor)),
st.pts,
<String>{p.id, id},
);
await waitTick();
continue;
}
await flushPrev();
}
prev = st;
prevBuiltin = r;
}
await flushPrev();
_playing = false;
}
@@ -758,6 +846,41 @@ class _ArchViewState extends State<ArchView>
c.setSketchDimValue(d.id, v);
}
/// 橡皮擦:擦除 [world] 附近的图形 / 墨迹 / 连线(不删除锁定图层的对象)。
void _eraseAt(Offset world) {
final ArchDoc doc = c.arch;
final double z = c.arch.zoom <= 0 ? 1 : c.arch.zoom;
final Set<String> ids = <String>{};
for (final ArchStroke s in doc.strokes) {
if (!doc.isLayerVisible(s.layerId) || doc.isLayerLocked(s.layerId)) {
continue;
}
if (s.hitTest(world, pad: 10 / z)) ids.add(s.id);
}
for (final ArchShape s in doc.shapes) {
if (!doc.isLayerVisible(s.layerId) || doc.isLayerLocked(s.layerId)) {
continue;
}
if (s.hitTest(world, pad: 6 / z)) ids.add(s.id);
}
for (final ArchConnector cn in doc.connectors) {
if (!doc.isLayerVisible(cn.layerId) || doc.isLayerLocked(cn.layerId)) {
continue;
}
final ArchConnGeometry g = archConnectorGeometry(doc, cn);
final double tol = 10 / z + 4;
for (int k = 0; k < g.pts.length - 1; k++) {
if (archDistToSegment(world, g.pts[k], g.pts[k + 1]) <= tol) {
ids.add(cn.id);
break;
}
}
}
if (ids.isEmpty) return;
_eraseCount += c.eraseArchIds(ids);
setState(() {});
}
// ===================== 指针事件 =====================
/// 按下:进入捏合 / 平移 / 框选 / 手绘 / 连线 / 选择移动。
@@ -802,6 +925,14 @@ class _ArchViewState extends State<ArchView>
setState(() {});
return;
}
if (tool == ArchTool.eraser) {
// 橡皮:按下即开始擦除;连续拖动 = 一次撤销快照。
_erasing = true;
_eraseCount = 0;
c.beginArchErase();
_eraseAt(world);
return;
}
if (tool == ArchTool.connect) {
// 按住左键画路径:松手后识别为直线 / 折线并吸附到图形边框。
_connFromId = _shapeAt(world)?.id;
@@ -853,7 +984,12 @@ class _ArchViewState extends State<ArchView>
final double tol = 10 / c.arch.zoom;
for (int i = 0; i < g.pts.length; i++) {
if ((g.pts[i] - world).distance <= tol) {
_connVertexOrigin = soleConn;
// 角点编辑:把当前正交路径的全部拐点物化为折点(拖拽自由,
// 路径仍按折点走横平竖直的正交台阶)。
final List<Offset> cornerList = g.pts.length > 2
? g.pts.sublist(1, g.pts.length - 1)
: const <Offset>[];
_connVertexOrigin = soleConn.copyWith(mids: cornerList);
_connVertexIndex = i;
_connVertexPts = List<Offset>.of(g.pts);
_connDragTargetId = null;
@@ -974,6 +1110,10 @@ class _ArchViewState extends State<ArchView>
_sketchMove(world);
return;
}
if (_erasing) {
_eraseAt(world);
return;
}
// 触屏长按(原地停顿)转为框选。
if (_panning && _touchEmpty && !_marquee) {
@@ -1128,6 +1268,14 @@ class _ArchViewState extends State<ArchView>
_sketchUp(world, moved);
return;
}
if (_erasing) {
_erasing = false;
if (_eraseCount > 0) {
c.setStatus('橡皮:已擦除 $_eraseCount 项(可一次撤销回退)');
}
setState(() {});
return;
}
if (_draft != null) {
final List<Offset> pts = _draft!;
@@ -1354,8 +1502,6 @@ class _ArchViewState extends State<ArchView>
if (v.length < 2) return;
final Offset p0 = v.first;
final Offset p1 = v.last;
final List<Offset> mids =
v.length > 2 ? v.sublist(1, v.length - 1) : const <Offset>[];
// 端点吸附:起点优先用按下时命中的图形;终点按松手位置命中。
final ArchShape? sf = c.arch.shapeById(_connFromId) ?? _shapeAt(p0);
final ArchShape? st = _shapeAt(p1);
@@ -1368,6 +1514,15 @@ class _ArchViewState extends State<ArchView>
: (snap ? archSnapPoint(c.arch, p1) : p1);
_connFromId = null;
if ((to - from).distance < 12) return;
// 连线一律正交:把手绘轨迹正交化(保留折点位置,所有段横平竖直)。
final List<Offset> ortho = orthoPolyline(<Offset>[
from,
...v.sublist(1, v.length - 1),
to,
]);
final List<Offset> mids = ortho.length > 2
? ortho.sublist(1, ortho.length - 1)
: const <Offset>[];
c.addArchConnector(ArchConnector(
id: archNewId('C', c.arch.allIds),
fromId: sf?.id,
@@ -1375,6 +1530,7 @@ class _ArchViewState extends State<ArchView>
toId: st?.id,
to: to,
mids: mids,
route: ArchRoute.ortho,
));
}
@@ -1455,6 +1611,18 @@ class _ArchViewState extends State<ArchView>
}
if (len < 24) return;
// 手动确认识别(平板推荐):只保留墨迹,点顶部「识别 N」按钮统一处理。
if (!c.archAutoRecognize) {
final ArchStroke st = ArchStroke(
id: archNewId('K', c.arch.allIds),
pts: pts,
color: c.archColor,
);
c.addArchStroke(st);
c.setStatus('墨迹已保留(${c.arch.strokes.length} 笔待识别)· 点顶部「识别」确认');
return;
}
// 图元库组合(回溯):2.5 秒内新笔迹先尝试与「上一步刚生成的图形」
// 合成图元(如 圆 + 叉 = 指示灯)——即使第一笔已被即时识别成标准图形。
final String? convId = _lastConvId;
@@ -1542,21 +1710,7 @@ class _ArchViewState extends State<ArchView>
_lastInkId = null;
_lastInkAt = null;
// 手绘箭头两端都落在图形上 → 直接生成吸附连线。
if (r.type == ArchShapeType.line && r.a != null && r.b != null) {
final ArchShape? sf = _shapeAt(r.a!);
final ArchShape? st = _shapeAt(r.b!);
if (sf != null && st != null && sf.id != st.id) {
c.addArchConnector(ArchConnector(
id: archNewId('C', c.arch.allIds),
fromId: sf.id,
from: archNearestBorderPoint(sf, r.a!),
toId: st.id,
to: archNearestBorderPoint(st, r.b!),
));
return;
}
}
// 规则:手绘模式只出图形——连线请用「连线」工具(不再从箭头自动生成连线)。
final ArchShape shape =
_snapShapeToGrid(archShapeFromRecognition(