From 26989e59e10a0dd973430446b5086ab391eac887 Mon Sep 17 00:00:00 2001 From: ChenX Date: Mon, 15 Jan 2018 17:18:29 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84=E6=B8=85=E7=90=86=E7=82=B9?= =?UTF-8?q?=E6=8D=95=E6=8D=89,=20=20=20=E5=88=86=E7=A6=BB=E5=87=BA?= =?UTF-8?q?=E7=82=B9=E6=8D=95=E6=8D=89=E6=9C=8D=E5=8A=A1.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Add-on/DrawLine.ts | 7 +- src/ApplicationServices/Application.ts | 2 - src/DatabaseServices/Circle.ts | 20 +- src/DatabaseServices/Entity.ts | 7 +- src/DatabaseServices/Line.ts | 7 +- src/Editor/Editor.ts | 8 +- src/Editor/GetPointServices.ts | 276 ++----------------------- src/Editor/SelectControls.ts | 1 + src/Editor/SnapServices.ts | 263 +++++++++++++++++++++++ src/GraphicsSystem/PreViewer.ts | 67 +++--- src/GraphicsSystem/SnapServices.ts | 16 -- 11 files changed, 349 insertions(+), 325 deletions(-) create mode 100644 src/Editor/SnapServices.ts delete mode 100644 src/GraphicsSystem/SnapServices.ts diff --git a/src/Add-on/DrawLine.ts b/src/Add-on/DrawLine.ts index fa4c1434c..24019ecd3 100644 --- a/src/Add-on/DrawLine.ts +++ b/src/Add-on/DrawLine.ts @@ -169,7 +169,6 @@ console.log(); export class DrawRect implements Command app.m_Editor.AddNoSnapEntity(l3.Draw(RenderType.Wireframe)); app.m_Editor.AddNoSnapEntity(l4.Draw(RenderType.Wireframe)); - let updateRect = (p1, p2) => { box.setFromPoints([p2, p1]); @@ -197,6 +196,8 @@ console.log(); export class DrawRect implements Command updateRect(p, p1); } }); + + app.m_Editor.ClearSnapEntity(); if (ptRes.Status != PromptStatus.OK) { return; @@ -209,8 +210,6 @@ export class DrawCircle implements Command async exec() { let cir = new Circle(app.m_Editor.m_MouseCtrl.m_CurMousePointWCS.clone(), 1e-3); - app.m_Editor.m_CommandStore.Prompt("指定圆的圆心:"); - app.m_Editor.UpdateScreen(); let ptRes = await app.m_Editor.GetPoint({ Msg: "指定圆的圆心", Callback: (p) => @@ -233,6 +232,8 @@ export class DrawCircle implements Command }); if (disRes.Status === PromptStatus.OK) cir.Radius = disRes.Value; + + app.m_Editor.ClearSnapEntity(); app.m_Database.hm.EndCmd(); } } diff --git a/src/ApplicationServices/Application.ts b/src/ApplicationServices/Application.ts index d3e966ee7..c9ef5e77e 100644 --- a/src/ApplicationServices/Application.ts +++ b/src/ApplicationServices/Application.ts @@ -9,7 +9,6 @@ import { DatGUI } from '../Editor/DebugDatUi'; import { Editor } from '../Editor/Editor'; import { SelectSetBase } from '../Editor/SelectSet'; import { SnapDrag } from '../Editor/SnapDrag'; -import { SnapServices } from '../GraphicsSystem/SnapServices'; import { Viewer } from '../GraphicsSystem/Viewer'; import { layoutOnsizeEvent } from '../UI/Layout/LayoutOnSizeEventManage'; @@ -82,7 +81,6 @@ export class ApplicationService }) regGripEvent(); - new SnapServices(this); new SnapDrag(this); xaop.begin(commandMachine, commandMachine.ExecCommand, () => diff --git a/src/DatabaseServices/Circle.ts b/src/DatabaseServices/Circle.ts index 7579e3590..196bbba59 100644 --- a/src/DatabaseServices/Circle.ts +++ b/src/DatabaseServices/Circle.ts @@ -27,6 +27,7 @@ export class Circle extends Entity { this.WriteAllObjectRecord(); this.m_Center.copy(v); + this.Update(); } get Radius() { @@ -76,9 +77,14 @@ export class Circle extends Entity geo.computeBoundingSphere(); } } + GetSnapPoints(): Array + { + return this.GetStretchPoints(); + } GetStretchPoints(): Array { return [ + this.Center.clone(), this.m_Center.clone().add(new Vector3(0, this.m_Radius)), this.m_Center.clone().add(new Vector3(0, -this.m_Radius)), this.m_Center.clone().add(new Vector3(-this.m_Radius, 0)), @@ -90,11 +96,19 @@ export class Circle extends Entity let pts = this.GetStretchPoints() if (indexList.length > 0) { - let p = pts[indexList[0]]; + let index = indexList[0]; + let p = pts[index]; if (p) { - p.add(vec); - this.Radius = p.distanceTo(this.m_Center); + if (index > 0) + { + p.add(vec); + this.Radius = p.distanceTo(this.m_Center); + } + else + { + this.Center = this.Center.add(vec); + } } } } diff --git a/src/DatabaseServices/Entity.ts b/src/DatabaseServices/Entity.ts index 8e7682343..577735448 100644 --- a/src/DatabaseServices/Entity.ts +++ b/src/DatabaseServices/Entity.ts @@ -65,9 +65,14 @@ export class Entity extends CADObject } } + GetSnapPoints(): Array + { + return []; + } + GetStretchPoints(): Array { - return [] + return []; } MoveStretchPoints(indexList: Array, vec: THREE.Vector3) { diff --git a/src/DatabaseServices/Line.ts b/src/DatabaseServices/Line.ts index 4a0ba9dd6..51182c33f 100644 --- a/src/DatabaseServices/Line.ts +++ b/src/DatabaseServices/Line.ts @@ -52,9 +52,14 @@ export class Line extends Entity } } + GetSnapPoints(): Array + { + return [this.StartPoint, this.EndPoint]; + } + GetStretchPoints(): Array { - return [this.StartPoint, this.EndPoint] + return [this.StartPoint, this.EndPoint]; } MoveStretchPoints(indexList: Array, vec: Vector3) { diff --git a/src/Editor/Editor.ts b/src/Editor/Editor.ts index 269a269c4..defcf748b 100644 --- a/src/Editor/Editor.ts +++ b/src/Editor/Editor.ts @@ -56,11 +56,15 @@ export class Editor } AddNoSnapEntity(e) { - this.m_GetpointServices.noSnapList.add(e); + this.m_GetpointServices.snapServices.notSnapEntity.add(e); } RemoveNoSnapEntity(e) { - this.m_GetpointServices.noSnapList.delete(e); + this.m_GetpointServices.snapServices.notSnapEntity.delete(e); + } + ClearSnapEntity() + { + this.m_GetpointServices.snapServices.notSnapEntity.clear(); } PointToScreen(pt: THREE.Vector3): THREE.Vector2 diff --git a/src/Editor/GetPointServices.ts b/src/Editor/GetPointServices.ts index e02da325f..986d4dda8 100644 --- a/src/Editor/GetPointServices.ts +++ b/src/Editor/GetPointServices.ts @@ -5,12 +5,12 @@ import * as xaop from 'xaop'; import { app } from '../ApplicationServices/Application'; import { GetDistendPrompt, GetPointPrompt, InputState } from '../Common/InputState'; import { KeyBoard, MouseKey } from '../Common/KeyEnum'; -import { equaln, fixAngle, Intersect, midPoint, polar } from '../Geometry/GeUtils'; -import { PreViewer } from '../GraphicsSystem/PreViewer'; +import { midPoint } from '../Geometry/GeUtils'; import { DynamicInputManage } from '../UI/DynamicPrompt/DynamicInputManage'; import { GetDistancePromptBlock } from '../UI/DynamicPrompt/GetDistancePromptBlock'; import { GetPoint2PromptBlock, GetPointPromptBlock } from '../UI/DynamicPrompt/GetPointPromptBlock'; import { PromptDistendResult, PromptPointResult, PromptStatus } from './PromptResult'; +import { SnapServices } from './SnapServices'; /** * 为拾取点提供服务,提供一个类以供Editor引用. @@ -23,13 +23,16 @@ export class GetPointServices private promisResolve: (PromptPointResult) => void;//promis回调 protected removeCalls: Function[] = []; //结束回调 + + public snapServices = new SnapServices(); Doit(prompt?: GetPointPrompt): Promise { prompt = prompt ? prompt : {}; + + //初始化捕捉状态 + this.snapServices.Start(prompt); //程序状态 this.initAppState(); - //点捕捉 - this.initPtSnap(); //消息显示. this.initPrompt(prompt); //动态输入框. @@ -51,8 +54,6 @@ export class GetPointServices return new Promise((resolve, reject) => { this.promisResolve = resolve; - - }); } @@ -61,12 +62,16 @@ export class GetPointServices this._return(); } - private curPoint: Vector3; //更新当前点事件,统一使用该方法注入其他服务的更新(aop在此方法中注入) UpdateCurPointEvent() { - this.curPoint = this.GetNowPoint(); + let wcs = app.m_Editor.m_MouseCtrl.m_CurMousePointWCS; + let snapP = this.snapServices.GetSnapPoint(wcs); + if (snapP) + this.curPoint = snapP; + else + this.curPoint = wcs; } //初始化更新当前点的事件 @@ -96,7 +101,7 @@ export class GetPointServices { if (e.button == MouseKey.Left) { - this._return(this.GetNowPoint()); + this._return(this.curPoint); } })); } @@ -117,7 +122,6 @@ export class GetPointServices { if (prompt.BasePoint && prompt.AllowDrawRubberBand) { - this.m_SnapPtList = [prompt.BasePoint]; if (prompt.AllowDrawRubberBand) { let preView = app.m_Viewer.m_PreViewer; @@ -292,10 +296,13 @@ export class GetPointServices { app.m_Editor.m_InputState = InputState.GetPoint; app.m_Viewer.m_PreViewer.ToGetpoint(); + app.m_Viewer.m_PreViewer.render(); } //还原状态 private restAppState() { + this.snapServices.Stop(); + app.m_Editor.m_InputState = InputState.None; app.m_Viewer.m_PreViewer.ToSelect(); @@ -303,258 +310,9 @@ export class GetPointServices this.removeCalls.forEach(f => f()); this.removeCalls.length = 0; - this.noSnapList.clear(); - app.m_Editor.UpdateScreen(); app.m_Viewer.m_PreViewer.render(); } - - //极轴捕捉的线列表 - m_SnapAxisBlueLine: THREE.Line[] = []; - - private DestroySnapLine() - { - for (let l of this.m_SnapAxisBlueLine) - { - l.parent.remove(l); - l.geometry.dispose(); - } - this.m_SnapAxisBlueLine.length = 0; - } - /** - * - * 获得当前光标所在的点, 先判断捕捉,在判断极轴捕捉 - * - * @private - * @returns {THREE.Vector3} - * @memberof Editor - */ - private GetNowPoint(): THREE.Vector3 - { - let preView = app.m_Viewer.m_PreViewer; - //如果存在捕捉 - if (preView.m_LastSnapPoint) - { - let retP = preView.ptWcs; - app.m_Viewer.ScreenToWorld(retP); - return retP; - } - //清理原先画的辅助线 - this.DestroySnapLine(); - //绘制捕捉光标列表 - this.DrawSnapCross(preView); - - //捕捉轴线列表 - let snapAxisList: { basePt: THREE.Vector3, snapPt: THREE.Vector3 }[] = []; - - for (let i = 0; i < this.m_SnapPtList.length; i++) - { - let baseP = this.m_SnapPtList[i]; - let snapPt = this.AxisSnap(baseP, Math.PI * 0.5); - if (snapPt) - { - snapAxisList.push({ basePt: baseP, snapPt: snapPt }); - } - } - - let wcs = app.m_Editor.m_MouseCtrl.m_CurMousePointWCS; - if (snapAxisList.length > 0) - { - //轴线交点列表 - let axisIntersectList: { - ins: THREE.Vector3, - i1: { basePt: THREE.Vector3, snapPt: THREE.Vector3 }, - i2: { basePt: THREE.Vector3, snapPt: THREE.Vector3 } - }[] = []; - - //检测交点 - for (let i = 0; i < snapAxisList.length; i++) - { - let d1 = snapAxisList[i]; - for (let j = i + 1; j < snapAxisList.length; j++) - { - let d2 = snapAxisList[j]; - let insP = Intersect(d1.basePt, d1.snapPt, d2.basePt, d2.snapPt); - if (insP) - { - axisIntersectList.push({ ins: insP, i1: d1, i2: d2 }) - } - } - } - - if (axisIntersectList.length > 0) - { - axisIntersectList.sort((d1, d2) => - { - return d1.ins.distanceTo(wcs) < d2.ins.distanceTo(wcs) ? -1 : 1 - }); - - let insD = axisIntersectList[0]; - if (insD.ins.distanceToSquared(wcs) < 100) - { - this.DrawLine(insD.i1.basePt, insD.ins, preView.m_BlueDashMaterial); - this.DrawLine(insD.i2.basePt, insD.ins, preView.m_BlueDashMaterial); - return insD.ins.clone(); - } - } - else - { - snapAxisList.sort((d1, d2) => - { - return d1.snapPt.distanceTo(wcs) < d2.snapPt.distanceTo(wcs) ? -1 : 1 - }); - - let snapAxis = snapAxisList[0]; - this.DrawLine(snapAxis.basePt, snapAxis.snapPt, preView.m_BlueDashMaterial); - return snapAxis.snapPt; - } - } - - return wcs.clone(); - } - - //绘制×光标列表 - private DrawSnapCross(preView: PreViewer) - { - for (let snapP of this.m_SnapPtList) - { - let lines = preView.DrawCross(5, preView.m_BlueDashMaterial); - let p = snapP.clone(); - app.m_Viewer.WorldToScreen(p); - preView.ScreenPointToViewerPoint(p); - let move = new THREE.Matrix4(); - move.setPosition(p); - for (let l of lines) - { - l.applyMatrix(move); - this.m_SnapAxisBlueLine.push(l); - preView.Scene.add(l); - } - } - } - - /** - * 在preView中绘制 临时的线段 - * - * @param {THREE.Vector3} p1 - * @param {THREE.Vector3} p2 - * @param {(THREE.LineBasicMaterial | THREE.LineDashedMaterial)} [material] - * @memberof GetPointServices - */ - private DrawLine(p1: THREE.Vector3, p2: THREE.Vector3, material?: THREE.LineBasicMaterial | THREE.LineDashedMaterial) - { - if (p1.distanceToSquared(this.m_SnapPtList[0]) > 0.001) - { - let preView = app.m_Viewer.m_PreViewer; - let l = preView.DrawLineFromWcs(p1.clone(), p2.clone(), material); - this.m_SnapAxisBlueLine.push(l); - preView.Scene.add(l); - } - } - - noSnapList = new Set(); - m_SnapPtList: THREE.Vector3[] = []; - - /** - * 点捕捉服务. 如果有捕捉 将设置光标的捕捉设置 - * - * @returns - * @memberof Editor - */ - initPtSnap() - { - this.m_SnapPtList.length = 0; - let testSnap = () => - { - for (let obj of app.m_Viewer.m_Scene.children) - { - if (this.noSnapList.has(obj)) - continue; - let geo = obj["geometry"]; - let g: THREE.Geometry - if (geo instanceof THREE.Geometry) - g = geo; - else if (geo instanceof THREE.BufferGeometry) - g = new THREE.Geometry().fromBufferGeometry(geo); - else - continue; - for (let i = 0; i < g.vertices.length; i++) - { - let ptC = g.vertices[i].clone(); - ptC.applyMatrix4(obj.matrix); - let ptWcs = ptC.clone(); - app.m_Viewer.WorldToScreen(ptC); - - if (ptC.distanceToSquared(app.m_Editor.m_MouseCtrl.m_CurMousePointVCS) < 100) - { - app.m_Viewer.m_PreViewer.m_LastSnapPoint = ptC; - - app.m_Viewer.m_PreViewer.m_LastEntity = obj; - app.m_Viewer.m_PreViewer.m_LastIndex = i; - - // app.m_Viewer.m_PreViewer.SerCursorPostion(ptC); 设置点为捕捉到的点. - - app.m_Viewer.m_PreViewer.render(); - - for (let p of this.m_SnapPtList) - { - if (p.distanceToSquared(ptWcs) < 0.01) - { - return; - } - } - this.m_SnapPtList.push(ptWcs); - if (this.m_SnapPtList.length > 7) - { - this.m_SnapPtList.shift(); - } - return; - } - } - } - } - testSnap(); - let removeCall = xaop.end(this, this.UpdateCurPointEvent, testSnap); - this.removeCalls.push(() => - { - removeCall(); - this.DestroySnapLine(); - }) - } - - /** - * - * @param {any} basePoint - * @param {number} fixAn - * @returns {THREE.Vector3} 返回捕捉的点 如果捕捉不到 那么返回空 - * @memberof GetPointServices - */ - AxisSnap(basePoint, fixAn: number): THREE.Vector3 - { - let wcs = app.m_Editor.m_MouseCtrl.m_CurMousePointWCS.clone(); - - //相差向量 - let subVec = wcs.clone().sub(basePoint); - let an = Math.atan2(subVec.y, subVec.x); - - if (an < 0) an += Math.PI * 2; - let newan = fixAngle(an, fixAn); - - if (!equaln(newan, an, 0.001)) - { - //dis. - let v0 = new THREE.Vector3(0, 0, 0); - polar(v0, newan, 1); - - let dis = v0.dot(wcs.sub(basePoint)); - - let retP = basePoint.clone(); - polar(retP, newan, dis); - - return retP; - } - return undefined; - } } export class GetDistanceServices diff --git a/src/Editor/SelectControls.ts b/src/Editor/SelectControls.ts index 7bd493250..b5172a62b 100644 --- a/src/Editor/SelectControls.ts +++ b/src/Editor/SelectControls.ts @@ -150,6 +150,7 @@ export class SelectControls let pt = await app.m_Editor.GetPoint({ Msg: "指定下一个点:", BasePoint: lastP.clone(), + AllowDrawRubberBand: true, Callback: (callPt: THREE.Vector3) => { let p = en.GetStretchPoints()[index]; diff --git a/src/Editor/SnapServices.ts b/src/Editor/SnapServices.ts new file mode 100644 index 000000000..f7e0b7254 --- /dev/null +++ b/src/Editor/SnapServices.ts @@ -0,0 +1,263 @@ +import * as THREE from 'three'; + +import { app } from '../ApplicationServices/Application'; +import { ColorMaterial } from '../Common/ColorPalette'; +import { GetPointPrompt } from '../Common/InputState'; +import { Entity } from '../DatabaseServices/Entity'; +import { equaln, fixAngle, Intersect, polar } from '../Geometry/GeUtils'; +import { PreViewer } from '../GraphicsSystem/PreViewer'; + + +//捕捉轴 +interface SnapAxis +{ + BasePoint: THREE.Vector3;//基点 + SnapPoint: THREE.Vector3;//捕捉到的点 +} + +//轴线相交数据 +interface SnapIntersect +{ + IntersectPoint: THREE.Vector3; + Axis1: SnapAxis; + Axis2: SnapAxis; +} + + +//提供点捕捉的服务. +export class SnapServices +{ + private preLines: THREE.Line[] = []; //前视图绘制的线表 + notSnapEntity = new Set();//不参与捕捉的实体列表,这个属性由开发人员维护. + private snapPoints: THREE.Vector3[] = [];//捕捉的点列表 + //开始捕捉 + Start(prompt: GetPointPrompt) + { + if (prompt.BasePoint) this.snapPoints.push(prompt.BasePoint); + } + //结束捕捉服务 + Stop() + { + this.DestroySnapLine(); + this.snapPoints.length = 0; + } + + GetSnapPoint(wcsP: THREE.Vector3) + { + let preView = app.m_Viewer.m_PreViewer; + this.DestroySnapLine(); + this.DrawSnapCross(preView); + + return this.GetEntitySnapPoint() || this.GetAxisSnapPoint(wcsP); + } + + //-------------------计算函数//------------------- + + //根据角度进行轴线捕捉,如果存在捕捉,那么返回捕捉点 + private AxisSnap(basePoint: THREE.Vector3, fixAn: number): THREE.Vector3 + { + let wcs = app.m_Editor.m_MouseCtrl.m_CurMousePointWCS.clone(); + + //相差向量 + let subVec = wcs.clone().sub(basePoint); + let an = Math.atan2(subVec.y, subVec.x); + + if (an < 0) an += Math.PI * 2; + let newan = fixAngle(an, fixAn); + + if (!equaln(newan, an, 0.001)) + { + //dis. + let v0 = new THREE.Vector3(0, 0, 0); + polar(v0, newan, 1); + + let dis = v0.dot(wcs.sub(basePoint)); + + let retP = basePoint.clone(); + polar(retP, newan, dis); + + return retP; + } + return undefined; + } + + //返回合适的轴线交点,如果不存在交点,那么返回轴线上的点 + private GetAxisSnapPoint(wcsP: THREE.Vector3) + { + //轴线列表 + let snapAxisList: SnapAxis[] = this.snapPoints.map((p) => + { + let snapPoint = this.AxisSnap(p, Math.PI * 0.5); + return { BasePoint: p, SnapPoint: snapPoint }; + }).filter(a => a.SnapPoint); + + if (snapAxisList.length === 0) + return; + + //检测交点 + let axisIntersectList: SnapIntersect[] = []; + for (let i = 0; i < snapAxisList.length; i++) + { + let axis1 = snapAxisList[i]; + for (let j = i + 1; j < snapAxisList.length; j++) + { + let axis2 = snapAxisList[j]; + let insP = Intersect(axis1.BasePoint, axis1.SnapPoint, axis2.BasePoint, axis2.SnapPoint); + if (insP) + axisIntersectList.push({ + IntersectPoint: insP, + Axis1: axis1, + Axis2: axis2 + }); + } + } + + //如果存在交点 那么找到最近的交点 + if (axisIntersectList.length > 0) + { + axisIntersectList.sort((d1, d2) => + { + return d1.IntersectPoint.distanceTo(wcsP) < d2.IntersectPoint.distanceTo(wcsP) ? -1 : 1; + }); + let insData = axisIntersectList[0]; + if (insData.IntersectPoint.distanceToSquared(wcsP) < 100) + { + this.DrawLine(insData.Axis1.BasePoint, insData.IntersectPoint, app.m_Viewer.m_PreViewer.m_BlueDashMaterial); + this.DrawLine(insData.Axis2.BasePoint, insData.IntersectPoint, app.m_Viewer.m_PreViewer.m_BlueDashMaterial); + + this.DrawBaisCross(insData.IntersectPoint); + return insData.IntersectPoint.clone(); + } + } + else + { + snapAxisList.sort((d1, d2) => + { + return d1.SnapPoint.distanceTo(wcsP) < d2.SnapPoint.distanceTo(wcsP) ? -1 : 1; + }); + + let snapAxis = snapAxisList[0]; + this.DrawLine(snapAxis.BasePoint, snapAxis.SnapPoint, app.m_Viewer.m_PreViewer.m_BlueDashMaterial); + return snapAxis.SnapPoint; + } + } + + //计算图形的捕捉点 如果有 则返回. + private GetEntitySnapPoint() + { + let vcsP = app.m_Editor.m_MouseCtrl.m_CurMousePointVCS; + for (let obj of app.m_Viewer.m_Scene.children) + { + if (!this.notSnapEntity.has(obj) && obj.userData && obj.userData instanceof Entity) + { + for (let p of obj.userData.GetSnapPoints()) + { + let pv = p.clone(); + app.m_Viewer.WorldToScreen(pv); + if (pv.distanceToSquared(vcsP) < 100) + { + if (!this.snapPoints.some(sp => { return sp.distanceToSquared(p) < 1e-5 })) + { + this.snapPoints.push(p); + if (this.snapPoints.length > 7) this.snapPoints.shift(); + } + this.DrawSquare(p, 10); + return p.clone(); + } + } + } + } + } + + //-------------------绘制函数------------------- + + //绘制十字光标列表 + private DrawSnapCross(preView: PreViewer) + { + for (let snapP of this.snapPoints) + { + let lines = preView.DrawCross(5, preView.m_BlueDashMaterial); + let p = snapP.clone(); + app.m_Viewer.WorldToScreen(p); + preView.ScreenPointToViewerPoint(p); + let move = new THREE.Matrix4(); + move.setPosition(p); + for (let l of lines) + { + l.applyMatrix(move); + this.preLines.push(l); + preView.Scene.add(l); + } + } + } + + //绘制 x光标 轴线捕捉 + private DrawBaisCross(ptWcs: THREE.Vector3) + { + let lines = app.m_Viewer.m_PreViewer.DrawBiasCross(10, ColorMaterial.GetLineMaterial(3)); + let preView = app.m_Viewer.m_PreViewer; + let pv = this.WorldToViewPoint(ptWcs); + + let move = new THREE.Matrix4().makeTranslation(pv.x, pv.y, pv.z); + lines.forEach(l => { l.applyMatrix(move) }); + this.preLines.push(...lines); + preView.Scene.add(...lines); + } + + //绘制正方形 + private DrawSquare(ptWcs: THREE.Vector3, size: number) + { + let pv = this.WorldToViewPoint(ptWcs); + + let addV = new THREE.Vector3(size * 0.5, size * 0.5, 0); + let p1 = pv.clone().sub(addV); + let p3 = pv.clone().add(addV) + addV.x *= -1; + let p2 = pv.clone().add(addV); + let p4 = pv.clone().sub(addV); + + let preView = app.m_Viewer.m_PreViewer; + let material = app.m_Viewer.m_PreViewer.m_SnapMaterial; + + let lines = [ + preView.DrawLine(p1, p2, material), + preView.DrawLine(p2, p3, material), + preView.DrawLine(p3, p4, material), + preView.DrawLine(p1, p4, material) + ] + + this.preLines.push(...lines); + preView.Scene.add(...lines); + } + + private WorldToViewPoint(pWcs: THREE.Vector3) + { + let preView = app.m_Viewer.m_PreViewer; + let pv = pWcs.clone(); + app.m_Viewer.WorldToScreen(pv); + preView.ScreenPointToViewerPoint(pv); + return pv; + } + + //在preView中绘制 临时的线段 wcs + private DrawLine(p1: THREE.Vector3, p2: THREE.Vector3, material?: THREE.LineBasicMaterial | THREE.LineDashedMaterial) + { + if (p1.distanceToSquared(p2) > 0.001) + { + let preView = app.m_Viewer.m_PreViewer; + let l = preView.DrawLineFromWcs(p1.clone(), p2.clone(), material); + this.preLines.push(l); + preView.Scene.add(l); + } + } + //销毁绘制的函数 + private DestroySnapLine() + { + this.preLines.forEach(l => + { + l.parent.remove(l); + l.geometry.dispose(); + }) + this.preLines.length = 0; + } +} \ No newline at end of file diff --git a/src/GraphicsSystem/PreViewer.ts b/src/GraphicsSystem/PreViewer.ts index 73fc3f04d..60fd5f66d 100644 --- a/src/GraphicsSystem/PreViewer.ts +++ b/src/GraphicsSystem/PreViewer.ts @@ -12,8 +12,8 @@ export class PreViewer //相机控制 protected m_Camera: CameraControl; //线材质 - private m_LineMaterial: THREE.LineBasicMaterial = new THREE.LineBasicMaterial({ color: 0xffffff }); - private m_SnapMaterial: THREE.LineBasicMaterial = new THREE.LineBasicMaterial({ color: 0x008B00 }); + m_LineMaterial: THREE.LineBasicMaterial = new THREE.LineBasicMaterial({ color: 0xffffff }); + m_SnapMaterial: THREE.LineBasicMaterial = new THREE.LineBasicMaterial({ color: 0x008B00 }); //点划线 黄色 m_DashMaterial = new THREE.LineDashedMaterial({ @@ -30,8 +30,6 @@ export class PreViewer //光标对象 private m_CursorObject: THREE.Group; - private m_SnapObject: THREE.Group; - m_LastSnapPoint: THREE.Vector3; m_LastEntity: THREE.Object3D; m_LastIndex: number; @@ -50,10 +48,7 @@ export class PreViewer this.m_CursorObject = new THREE.Group(); this.Scene.add(this.m_CursorObject); - this.m_SnapObject = new THREE.Group(); - this.Scene.add(this.m_SnapObject); - - this.InitCursor(8, 1800); + this.InitCursor(12, 1800); } get Scene() { return this.m_Scene; } @@ -77,15 +72,18 @@ export class PreViewer } ToSelect() { - this.InitCursor(16, 1000); + this.InitCursor(12, 1000); this.m_Render.render(this.Scene, this.Camera); } ToEntsel() { - this.InitCursor(16, 0); + this.InitCursor(12, 0); } + + //-------------------------初始化代码------------------------- + private InitCamera() { this.m_Camera = new CameraControl(); @@ -112,16 +110,18 @@ export class PreViewer this.m_Render = new THREE.WebGLRenderer({ canvas: canvas, clearColor: 0, alpha: true }) } + //-------------------------初始化代码------------------------- private DispoeObj(obj: THREE.Object3D) { for (let l of obj.children) { - let line = l; + let line = l as THREE.Line; line.geometry.dispose(); } obj.children = [] } + //光标 private InitCursor(selectBoxSize: number, lineLength: number) { this.DispoeObj(this.m_CursorObject); @@ -146,6 +146,7 @@ export class PreViewer ) } } + //正方形 private InitSquare(length: number, center: THREE.Vector3, obj: THREE.Object3D) { if (length > 0) @@ -164,16 +165,17 @@ export class PreViewer } } + //绘制线 DrawLine(p1: THREE.Vector3, p2: THREE.Vector3, material?: THREE.LineBasicMaterial | THREE.LineDashedMaterial): THREE.Line { - material = material ? material : this.m_LineMaterial; + material = material || this.m_LineMaterial; let geometry = new THREE.Geometry(); geometry.vertices.push(p1, p2); geometry.computeLineDistances(); return new THREE.Line(geometry, material); } - //绘制×光标 + //绘制十字光标 DrawCross(size: number, material?: THREE.LineBasicMaterial | THREE.LineDashedMaterial): THREE.Line[] { return [ @@ -183,6 +185,14 @@ export class PreViewer this.DrawLine(new THREE.Vector3(0, 0, 0), new THREE.Vector3(-size, 0, 0), material) ] } + //绘制×光标 + DrawBiasCross(size: number, material?: THREE.LineBasicMaterial | THREE.LineDashedMaterial): THREE.Line[] + { + let lines = this.DrawCross(size, material); + let roMat = new THREE.Matrix4().makeRotationZ(Math.PI / 4); + lines.forEach(l => l.applyMatrix(roMat)); + return lines; + } DrawLineFromWcs(p1: THREE.Vector3, p2: THREE.Vector3, material?: THREE.LineBasicMaterial | THREE.LineDashedMaterial): THREE.Line { material = material ? material : this.m_LineMaterial; @@ -195,45 +205,26 @@ export class PreViewer return this.DrawLine(p1, p2, material); } - SerCursorPostion(pt: THREE.Vector3) + SerCursorPostion(ptVcs: THREE.Vector3) { - let p = this.m_CursorObject.position; - if (this.m_LastSnapPoint && this.m_LastSnapPoint.distanceToSquared(pt) < 100) - { - p.copy(this.m_LastSnapPoint); - - let sbp = p.clone(); - this.ScreenPointToViewerPoint(sbp); - - this.DispoeObj(this.m_SnapObject); - this.InitSquare(16, sbp, this.m_SnapObject); - } - else - { - this.m_LastSnapPoint = undefined; - this.DispoeObj(this.m_SnapObject); - p.copy(pt); - } + let p = this.m_CursorObject.position.copy(ptVcs); this.ScreenPointToViewerPoint(p); this.render(); } - /** - * 屏幕点转换 screen的点 - * - * @param {THREE.Vector3} p - * @memberof CursorViewer - */ + + // 屏幕点转视图点 ScreenPointToViewerPoint(p: THREE.Vector3) { p.x = p.x - this.m_Width * 0.5; p.y = this.m_Height * 0.5 - p.y; } + //视图点转屏幕点 ViewerPointToScreenPoint(p: THREE.Vector3) { p.x = p.x + this.m_Width * 0.5; p.y = this.m_Height * 0.5 - p.y; } - + // onSize(width: number, height: number) { this.m_Width = width; diff --git a/src/GraphicsSystem/SnapServices.ts b/src/GraphicsSystem/SnapServices.ts deleted file mode 100644 index cb0842280..000000000 --- a/src/GraphicsSystem/SnapServices.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { ApplicationService } from '../ApplicationServices/Application'; - - -/** - * 提供捕捉的服务. - * - * @class SnapServices - */ -export class SnapServices -{ - - constructor(app: ApplicationService) - { - - } -} \ No newline at end of file