diff --git a/src/ApplicationServices/Application.ts b/src/ApplicationServices/Application.ts index ca0aad42b..e7c22e827 100644 --- a/src/ApplicationServices/Application.ts +++ b/src/ApplicationServices/Application.ts @@ -200,6 +200,12 @@ export class ApplicationService if (changeObjects.length > 0 || createObjects.length > 0 || cmdName === "PU") this.Saved = false; }); + + app.CommandReactor.OnCommandEnd((cmdName, changeObjects, createObjects) => + { + this.Editor.transCtrl.UpdateMtx(); + }); + // this._autoCuttingReactor = new AutoCuttingReactor(); this._hardwareCuttingReactor = new HardwareCuttingReactor(); new RelevanceCuttingReactor(this); diff --git a/src/Common/CommandNames.ts b/src/Common/CommandNames.ts index de4ac3b77..f1a8dcbe0 100644 --- a/src/Common/CommandNames.ts +++ b/src/Common/CommandNames.ts @@ -22,6 +22,7 @@ export enum CommandNames Scale = "SCALE", Convert2Polyline = "CONVERT2POLYLINE", Move = "MOVE", + MoveAxis = "MOVEAXIS", Rotate = "ROTATE", RotateRefer = "ROTATEREFER", Revolve = "REVOLVE", diff --git a/src/Editor/SnapServices.ts b/src/Editor/SnapServices.ts index a8e610f5d..9b6180dc2 100644 --- a/src/Editor/SnapServices.ts +++ b/src/Editor/SnapServices.ts @@ -61,6 +61,12 @@ interface SupportSnapPoint Entitys?: SupportSnapEntity[]; } +export enum SnapType +{ + Entity = 0, + Axis = 1, +} + //正交捕捉轴 const OrthoAxis = [XAxis, YAxis, ZAxis]; const PolarAxis = [XAxis, new Vector3(1, 1).normalize(), YAxis, new Vector3(-1, 1).normalize(), ZAxis]; @@ -178,13 +184,22 @@ export class SnapServices app.Viewer.PreViewer.UpdateScreen(); } + _SnapType: SnapType; /** * 返回合适的捕捉点,如果有的话. * @returns 捕捉点或空 */ GetSnapPoint(): Vector3 | undefined { - this.SnapPoint = this.GetEntitySnapPoint() || this.GetAxisSnapPoint(); + this.SnapPoint = this.GetEntitySnapPoint(); + if (this.SnapPoint) + this._SnapType = SnapType.Entity; + else + { + this.SnapPoint = this.GetAxisSnapPoint(); + if (this.SnapPoint) + this._SnapType = SnapType.Axis; + } this.UpdateCursort(); return this.SnapPoint; diff --git a/src/Editor/TranstrolControl/CoorAxes.ts b/src/Editor/TranstrolControl/CoorAxes.ts index 0d5bd41e0..0c3cef64b 100644 --- a/src/Editor/TranstrolControl/CoorAxes.ts +++ b/src/Editor/TranstrolControl/CoorAxes.ts @@ -1,5 +1,4 @@ -import * as THREE from 'three'; - +import { Group } from 'three'; import { ColorMaterial } from '../../Common/ColorPalette'; import { Axes } from './Axes'; @@ -13,32 +12,32 @@ export enum AxisType //坐标轴的threejs对象,内部有3个子对象均为 axis 均存在 useData = AxisType -export class CoorAxes extends THREE.Group +export class CoorAxes extends Group { - private m_Axis_X: Axes; - private m_Axis_Y: Axes; - private m_Axis_Z: Axes; + private _Axis_X: Axes; + private _Axis_Y: Axes; + private _Axis_Z: Axes; constructor() { super(); - this.m_Axis_X = new Axes(); - this.m_Axis_X.rotateY(Math.PI / 2); - this.m_Axis_X.updateMatrix(); - this.m_Axis_X.userData.Axis = AxisType.X; + this._Axis_X = new Axes(); + this._Axis_X.rotateY(Math.PI / 2); + this._Axis_X.updateMatrix(); + this._Axis_X.userData.Axis = AxisType.X; - this.m_Axis_Y = new Axes(); - this.m_Axis_Y.rotateX(Math.PI / -2); - this.m_Axis_Y.Material = ColorMaterial.GetLineMaterial(3); - this.m_Axis_Y.updateMatrix(); - this.m_Axis_Y.userData.Axis = AxisType.Y; + this._Axis_Y = new Axes(); + this._Axis_Y.rotateX(Math.PI / -2); + this._Axis_Y.Material = ColorMaterial.GetLineMaterial(3); + this._Axis_Y.updateMatrix(); + this._Axis_Y.userData.Axis = AxisType.Y; - this.m_Axis_Z = new Axes(); - this.m_Axis_Z.Material = ColorMaterial.GetLineMaterial(5); - this.m_Axis_Z.updateMatrix(); - this.m_Axis_Z.userData.Axis = AxisType.Z; + this._Axis_Z = new Axes(); + this._Axis_Z.Material = ColorMaterial.GetLineMaterial(5); + this._Axis_Z.updateMatrix(); + this._Axis_Z.userData.Axis = AxisType.Z; - this.add(this.m_Axis_X, this.m_Axis_Y, this.m_Axis_Z); + this.add(this._Axis_X, this._Axis_Y, this._Axis_Z); } //还原实体的材质状态 diff --git a/src/Editor/TranstrolControl/TransformServices.ts b/src/Editor/TranstrolControl/TransformServices.ts index 5c8717141..c34ae5ce8 100644 --- a/src/Editor/TranstrolControl/TransformServices.ts +++ b/src/Editor/TranstrolControl/TransformServices.ts @@ -1,16 +1,19 @@ import { Matrix4, Vector3 } from 'three'; import { end } from 'xaop'; import { app } from '../../ApplicationServices/Application'; +import { CommandNames } from '../../Common/CommandNames'; import { InputState } from '../../Common/InputState'; import { MouseKey } from '../../Common/KeyEnum'; +import { Curve } from '../../DatabaseServices/Entity/Curve'; import { Entity } from '../../DatabaseServices/Entity/Entity'; -import { MoveMatrix } from '../../Geometry/GeUtils'; +import { equalv3, ZeroVec } from '../../Geometry/GeUtils'; import { PreViewer } from '../../GraphicsSystem/PreViewer'; import { CommandWrap } from '../CommandMachine'; import { Editor, EditorService } from '../Editor'; import { JigUtils } from '../JigUtils'; import { PointPickOneObject } from '../PointPick'; import { PromptStatus } from '../PromptResult'; +import { AxisSnapMode } from '../SnapServices'; import { MatrixToPreViewMat, UCSPsotion } from '../UCSServices'; import { AxisType } from './CoorAxes'; import { RotateAxes } from './RotateAxes'; @@ -80,36 +83,87 @@ export class TransformServicess implements EditorService return; let axes = this.CurAxes; - if (axes.AxtiveIndex > 0) + let index = axes.AxtiveIndex; + if (index > 0) { await CommandWrap(async () => { app.Viewer.GripScene.visible = false; + + let bakUCS = app.Editor.UCSMatrix; + app.Editor.UCSMatrix = this._Matrix;//合理的UCS是关键,当用户转动视图时,我们也应该给予正确的UCS UP:看起来又不需要? 因为我们有Z捕捉 + + let bakSnapMode = app.Editor.GetPointServices.snapServices.SnapMode; + app.Editor.GetPointServices.snapServices.SnapMode = 0; + + let bakSnapAxisMode = app.Editor.GetPointServices.snapServices.AxisSnapMode;//TODO:按下F3 F8时,坑爹的userconfig.Upload会刷新捕捉模式,导致自定义捕捉模式失效 + app.Editor.GetPointServices.snapServices.AxisSnapMode = AxisSnapMode.Custom; + + let timer = setTimeout(() => + { + app.Editor.MouseCtrl.EnableMouseUpDoit = true; //响应鼠标抬起就绘制 + }, 500); + + let axis = new Vector3().setFromMatrixColumn(this._Matrix, index - 1); + app.Editor.GetPointServices.snapServices.CustomAxis = [axis]; + let jigEns = this._Ents.map(e => JigUtils.Draw(e)); - let base = new Vector3().setFromMatrixColumn(this._Matrix, 3); + let mouseP = app.Editor.MouseCtrl._CurMousePointWCS.clone(); + + let mtxInv = new Matrix4().getInverse(this._Matrix); + mouseP.applyMatrix4(mtxInv); + for (let i = 0; i < 3; i++) + if (i !== index - 1) + mouseP.setComponent(i, 0); + mouseP.applyMatrix4(this._Matrix); + let ptLast = mouseP.clone(); + + let m = new Matrix4; let ptRes = await this._Editor.GetPoint({ Msg: "请点击下一个点:", - BasePoint: base, + BasePoint: mouseP, AllowDrawRubberBand: true, Callback: (newP) => { - JigUtils.Restore(); - let m = MoveMatrix(newP.clone().sub(base)); - for (let e of jigEns) - e.ApplyMatrix(m); - app.Editor.UpdateScreen(); + newP = newP.clone(); + // if (app.Editor.GetPointServices.IsSnap && app.Editor.GetPointServices.snapServices._SnapType === SnapType.Entity) + // 不管在什么时候 我们都强制它在这个轴移动 + { + newP.applyMatrix4(mtxInv); + for (let i = 0; i < 3; i++) + if (i !== index - 1) + newP.setComponent(i, 0); + newP.applyMatrix4(this._Matrix); + } + + ptLast.subVectors(newP, ptLast); + if (!equalv3(ptLast, ZeroVec)) + { + m.setPosition(ptLast); + for (let e of jigEns) + e.ApplyMatrix(m); + } + + ptLast.copy(newP); } }); + + clearTimeout(timer); + app.Editor.MouseCtrl.EnableMouseUpDoit = false; //响应鼠标抬起就绘制 + if (ptRes.Status === PromptStatus.OK) { - let m = MoveMatrix(ptRes.Point.sub(base)); + m.setPosition(ptRes.Point.sub(mouseP)); for (let e of this._Ents) e.ApplyMatrix(m); } app.Viewer.GripScene.visible = true; - }, "_move"); + app.Editor.GetPointServices.snapServices.AxisSnapMode = bakSnapAxisMode; + app.Editor.GetPointServices.snapServices.SnapMode = bakSnapMode; + app.Editor.UCSMatrix = bakUCS; + }, CommandNames.MoveAxis); } return true; } @@ -142,9 +196,21 @@ export class TransformServicess implements EditorService this._Editor.App.Viewer.PreViewer.UpdateScreen(); } - set Matrix(mat: Matrix4) + set Matrix(mtx: Matrix4) + { + this._Matrix.copy(mtx); + this.UpdateAxesMatrix(); + } + + UpdateMtx() { - this._Matrix.copy(mat); + let ent = this._Ents[0]; + if (!ent) return; + if (ent instanceof Curve) + this._Matrix.copy(ent.OCSNoClone).setPosition(ent.BoundingBox.getCenter(new Vector3)); + else if (ent instanceof Entity) + this._Matrix.copy(ent.OCSNoClone); + this.UpdateAxesMatrix(); } diff --git a/src/Editor/TranstrolControl/TranslateAxes.ts b/src/Editor/TranstrolControl/TranslateAxes.ts index 3cbdee6ec..7fdc663e7 100644 --- a/src/Editor/TranstrolControl/TranslateAxes.ts +++ b/src/Editor/TranstrolControl/TranslateAxes.ts @@ -1,46 +1,46 @@ -import * as THREE from 'three'; - +import { Mesh, MeshBasicMaterial, OctahedronGeometry } from 'three'; import { ColorMaterial } from '../../Common/ColorPalette'; import { Axes } from './Axes'; import { AxisType, CoorAxes } from './CoorAxes'; + export class TranslateAxex extends CoorAxes { - m_ActiveIndex: number = 0; - private m_OriginFace: THREE.Mesh; + _ActiveIndex: number = 0; + private _OriginFace: Mesh; constructor() { super(); //原点面. - let orignGeo = new THREE.OctahedronGeometry(0.1, 0); - let originMat = new THREE.MeshBasicMaterial({ + let orignGeo = new OctahedronGeometry(0.1, 0); + let originMat = new MeshBasicMaterial({ transparent: true, color: ColorMaterial.GetColor(2), opacity: 0.1 }); - this.m_OriginFace = new THREE.Mesh(orignGeo, originMat); - this.m_OriginFace.userData.Axis = AxisType.Origin; - this.add(this.m_OriginFace); + this._OriginFace = new Mesh(orignGeo, originMat); + this._OriginFace.userData.Axis = AxisType.Origin; + this.add(this._OriginFace); } set AxtiveIndex(v: number) { - this.m_ActiveIndex = v; + this._ActiveIndex = v; let cir = this.children[v - 1] as Axes; cir.Material = ColorMaterial.GetLineMaterial(2); } get AxtiveIndex() { - return this.m_ActiveIndex; + return this._ActiveIndex; } Rest() { super.Rest(); - this.m_ActiveIndex = 0; - let mat = this.m_OriginFace.material as THREE.MeshBasicMaterial; + this._ActiveIndex = 0; + let mat = this._OriginFace.material as MeshBasicMaterial; mat.transparent = true; } }