From 0516ec6ac7d5a603ef4b5e7686951c3668483389 Mon Sep 17 00:00:00 2001 From: xiange Date: Tue, 16 Apr 2024 22:37:43 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20Optimized=20snap=20prompt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/chili/src/snap/interfaces.ts | 1 + packages/chili/src/snap/objectSnap.ts | 10 ++++++++-- .../src/snap/snapEventHandler/snapEventHandler.ts | 2 +- .../snap/snapEventHandler/snapPointEventHandler.ts | 14 ++++++++++---- packages/chili/src/snap/tracking/trackingSnap.ts | 5 ++++- packages/chili/src/step/pointStep.ts | 2 +- 6 files changed, 25 insertions(+), 9 deletions(-) diff --git a/packages/chili/src/snap/interfaces.ts b/packages/chili/src/snap/interfaces.ts index 9090439b..220ee4a6 100644 --- a/packages/chili/src/snap/interfaces.ts +++ b/packages/chili/src/snap/interfaces.ts @@ -10,6 +10,7 @@ export interface SnapedData { view: IView; point?: XYZ; info?: string; + refPoint?: XYZ; shapes: VisualShapeData[]; models?: IModel[]; } diff --git a/packages/chili/src/snap/objectSnap.ts b/packages/chili/src/snap/objectSnap.ts index 968c1d29..d1920f62 100644 --- a/packages/chili/src/snap/objectSnap.ts +++ b/packages/chili/src/snap/objectSnap.ts @@ -80,11 +80,17 @@ export class ObjectSnap implements ISnapper { } snap(data: MouseAndDetected): SnapedData | undefined { + let snap: SnapedData | undefined; if (data.shapes.length > 0) { this.showInvisibleSnaps(data.view, data.shapes[0]); - return this.snapOnShape(data.view, data.mx, data.my, data.shapes); + snap = this.snapOnShape(data.view, data.mx, data.my, data.shapes); + } else { + snap = this.snapeInvisible(data.view, data.mx, data.my); + } + if (this.referencePoint && snap?.point) { + snap.info += " -> " + this.referencePoint?.().distanceTo(snap.point).toFixed(2); } - return this.snapeInvisible(data.view, data.mx, data.my); + return snap; } private snapOnShape(view: IView, x: number, y: number, shapes: VisualShapeData[]) { diff --git a/packages/chili/src/snap/snapEventHandler/snapEventHandler.ts b/packages/chili/src/snap/snapEventHandler/snapEventHandler.ts index 47c64299..e2cda638 100644 --- a/packages/chili/src/snap/snapEventHandler/snapEventHandler.ts +++ b/packages/chili/src/snap/snapEventHandler/snapEventHandler.ts @@ -78,7 +78,7 @@ export abstract class SnapEventHandler implements IEventHandler { this.removeTempObject(); this.setSnaped(view, event); if (this._snaped !== undefined) { - this.switchSnapedPrompt(this.data.prompt?.(this._snaped) ?? this.snaped?.info); + this.switchSnapedPrompt(this.data.prompt?.(this._snaped) ?? this._snaped.info); } else { this.clearSnapTip(); } diff --git a/packages/chili/src/snap/snapEventHandler/snapPointEventHandler.ts b/packages/chili/src/snap/snapEventHandler/snapPointEventHandler.ts index cd44339c..36f552e2 100644 --- a/packages/chili/src/snap/snapEventHandler/snapPointEventHandler.ts +++ b/packages/chili/src/snap/snapEventHandler/snapPointEventHandler.ts @@ -25,10 +25,11 @@ export class SnapPointEventHandler extends SnapEventHandler { protected getPointFromInput(view: IView, text: string): XYZ { let dims = text.split(",").map((x) => Number(x)); - let result = this.pointData.refPoint?.() ?? XYZ.zero; + let refPoint = this.getRefPoint(); + let result = refPoint ?? XYZ.zero; let end = this._snaped!.point!; if (dims.length === 1 && end !== undefined) { - let vector = end.sub(this.pointData.refPoint!()).normalize()!; + let vector = end.sub(refPoint!).normalize()!; result = result.add(vector.multiply(dims[0])); } else if (dims.length > 1) { let plane = this.pointData.plane?.() ?? view.workplane; @@ -43,20 +44,25 @@ export class SnapPointEventHandler extends SnapEventHandler { protected inputError(text: string): I18nKeys | undefined { let dims = text.split(",").map((x) => Number(x)); let dimension = Dimension.from(dims.length); + let refPoint = this.getRefPoint(); if (!Dimension.contains(this.pointData.dimension!, dimension)) { return "error.input.unsupportedInputs"; } else if (dims.some((x) => Number.isNaN(x))) { return "error.input.invalidNumber"; - } else if (this.pointData.refPoint === undefined) { + } else if (refPoint === undefined) { if (dims.length !== 3) { return "error.input.threeNumberCanBeInput"; } } else if ( dims.length === 1 && - (this._snaped === undefined || this._snaped.point!.isEqualTo(this.pointData.refPoint())) + (this._snaped === undefined || this._snaped.point!.isEqualTo(refPoint)) ) { return "error.input.cannotInputANumber"; } return undefined; } + + private getRefPoint(): XYZ | undefined { + return this.pointData.refPoint?.() ?? this._snaped?.refPoint; + } } diff --git a/packages/chili/src/snap/tracking/trackingSnap.ts b/packages/chili/src/snap/tracking/trackingSnap.ts index de1b1868..046dd584 100644 --- a/packages/chili/src/snap/tracking/trackingSnap.ts +++ b/packages/chili/src/snap/tracking/trackingSnap.ts @@ -84,8 +84,11 @@ export class TrackingSnap implements ISnapper { if (trackingDatas.length === 1) { let distance = point.distanceTo(trackingDatas[0].axis.location); info = `${trackingDatas[0].axis.name}: ${distance.toFixed(2)}`; + } else if (trackingDatas.length === 2) { + info = I18n.translate("snap.intersection"); } - return { view, point, info, shapes: [] }; + let refPoint = trackingDatas[0].axis.location; + return { view, point, info, shapes: [], refPoint }; } private showTempLine(view: IView, start: XYZ, end: XYZ): number | undefined { diff --git a/packages/chili/src/step/pointStep.ts b/packages/chili/src/step/pointStep.ts index 8c9fd968..f304ae2f 100644 --- a/packages/chili/src/step/pointStep.ts +++ b/packages/chili/src/step/pointStep.ts @@ -6,7 +6,7 @@ import { StepBase } from "./step"; function defaultSnapedData(): SnapPointData { return { - dimension: Dimension.D1D2D3, + dimension: Dimension.D1 | Dimension.D1D2D3, }; }