From fba276362c79f7bd80ec41709cd7d9f99c218c89 Mon Sep 17 00:00:00 2001 From: xief <1789784602@qq.com> Date: Fri, 14 Jul 2023 10:41:41 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=87=AA=E5=AE=9A=E4=B9=89?= =?UTF-8?q?=E7=BC=96=E5=8F=B7=E4=B8=8D=E6=98=BE=E7=A4=BA=EF=BC=8C=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E5=8F=8C=E9=9D=A2=E6=98=BE=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/SimpleBoard.ts | 42 +++++++++++++++------------- src/Text.ts | 70 ++++++++++++++++++++++++++++------------------ src/Utils.ts | 11 ++++++-- 3 files changed, 74 insertions(+), 49 deletions(-) diff --git a/src/SimpleBoard.ts b/src/SimpleBoard.ts index ddca460..4e5420e 100644 --- a/src/SimpleBoard.ts +++ b/src/SimpleBoard.ts @@ -1,7 +1,8 @@ import { CylinderGeometry, LineSegments, Matrix4, Mesh, MeshBasicMaterial, Vector2, Vector3 } from "three"; -import { DbText, ExtrudeSolid, Polyline, RenderType, TextAligen, boardUVGenerator2 } from "webcad_ue4_api"; +import { ExtrudeSolid, Polyline, boardUVGenerator2 } from "webcad_ue4_api"; import { ColorMaterial } from "./ColorPalette"; import { edgeMaterial } from "./Material"; +import { DbText } from "./Text"; function getVec(data: object): Vector3 { @@ -32,7 +33,6 @@ export class SimpleBoard DataID: number; _BoardType: BoardType; _CustomNumber: string; - _CustomNumberTextEntity: any; constructor(boardData: any) { @@ -157,32 +157,34 @@ export class SimpleBoard mesh.updateWorldMatrix(false, true); // 自定义编号 - this.createCustomNo(); - let o = this._CustomNumberTextEntity.GetDrawObjectFromRenderType(RenderType.Conceptual); - if (o) - { - mesh.add(o); - } + // let o = this._CustomNumberTextEntity.GetDrawObjectFromRenderType(RenderType.Conceptual); + // if (o) + // { + // mesh.add(o); + // } + if (this._CustomNumber) + { + mesh.add(this.createCustomNo()); + } return { mesh, edges }; } private createCustomNo() { - if (!this._CustomNumberTextEntity) - { - this._CustomNumberTextEntity = new DbText(); - this._CustomNumberTextEntity.TextAligen = TextAligen.Mid; - this._CustomNumberTextEntity.IsDoubleMesh = true; - this._CustomNumberTextEntity.IsFsText = this._BoardType === BoardType.Layer; - - this._CustomNumberTextEntity.IsEmbedEntity = true; - } + let text = new DbText(this._CustomNumber?.toString(), 100); + let position = new Vector3(this.width * 0.5, this.height * 0.5, this.thickness * 0.5); if (this._BoardType === BoardType.Layer) - this._CustomNumberTextEntity.OCSNoClone.makeRotationZ(-Math.PI / 2).setPosition(this.width * 0.5, this.height * 0.5, this.thickness * 0.5); + { + text.applyMatrix4(new Matrix4().makeRotationZ(-Math.PI / 2).setPosition(position)); + } else - this._CustomNumberTextEntity.OCSNoClone.identity().setPosition(this.width * 0.5, this.height * 0.5, this.thickness * 0.5); + { + text.applyMatrix4(new Matrix4().setPosition(position)); + } - this._CustomNumberTextEntity.TextString = this._CustomNumber?.toString() ?? "未编号"; + text.CreateDoubleMesh(this._BoardType === BoardType.Layer); + + return text; } } diff --git a/src/Text.ts b/src/Text.ts index 80822fa..af4cc5f 100644 --- a/src/Text.ts +++ b/src/Text.ts @@ -1,43 +1,59 @@ -import { Font, FontLoader, Mesh, ShapeGeometry, Vector3, Shape } from "three"; +import { Font, FontLoader, Matrix4, Mesh, Shape, ShapeGeometry, Vector3 } from "three"; import { ColorMaterial } from "./ColorPalette"; import { MoveMatrix } from "./GeUtils"; +import { XAxis, XAxisN, YAxis, YAxisN, ZAxisN } from "./Utils"; class FontLoaderUtil { - private static defFont: Font; - static Load(): Font - { - if (!this.defFont) + private static defFont: Font; + static Load(): Font { - const f = require("../resources/fonts/helvetiker_regular.typeface.json"); - let loader = new FontLoader(); - this.defFont = loader.parse(f); + if (!this.defFont) + { + const f = require("../resources/fonts/helvetiker_regular.typeface.json"); + let loader = new FontLoader(); + this.defFont = loader.parse(f); + } + return this.defFont; } - return this.defFont; - } } export class DbText extends Mesh { - /** - * - * 构造一个文本对象,8位点(中心点下面)在0点 - * @param {string} str 数字字符串 - * @param {number} height 文本高度 - * @memberof DbText - */ - constructor(str: string, height: number = 5) - { - let font = FontLoaderUtil.Load(); + /** + * + * 构造一个文本对象,8位点(中心点下面)在0点 + * @param {string} str 数字字符串 + * @param {number} height 文本高度 + * @memberof DbText + */ + constructor(str: string, height: number = 5) + { + let font = FontLoaderUtil.Load(); - let shapes: Shape[] = font.generateShapes(str, height); - let geometry = new ShapeGeometry(shapes); + let shapes: Shape[] = font.generateShapes(str, height); + let geometry = new ShapeGeometry(shapes); - geometry.computeBoundingBox(); + geometry.computeBoundingBox(); - super(geometry, ColorMaterial.GetBasicMaterial(5)); + super(geometry, ColorMaterial.GetBasicMaterial(5)); - let center = geometry.boundingBox.getCenter(new Vector3()); - this.applyMatrix4(MoveMatrix(new Vector3(-center.x, 0, 0))); - } + let center = geometry.boundingBox.getCenter(new Vector3()); + this.applyMatrix4(MoveMatrix(new Vector3(-center.x, 0, 0))); + } + + CreateDoubleMesh(IsFsText: boolean) + { + let mesh2 = new Mesh(this.geometry, ColorMaterial.GetBasicMaterial(5)); + //左右视图时应该这样,俯视图是X,-Y,-Z(y+=(xxx) + if (IsFsText) + { + mesh2.applyMatrix4(new Matrix4().makeBasis(XAxis, YAxisN, ZAxisN)); + } + else + { + mesh2.applyMatrix4(new Matrix4().makeBasis(XAxisN, YAxis, ZAxisN)); + } + this.add(mesh2); + } } diff --git a/src/Utils.ts b/src/Utils.ts index b569639..326bfcf 100644 --- a/src/Utils.ts +++ b/src/Utils.ts @@ -1,5 +1,12 @@ -import { Object3D, Mesh } from "three"; -import { createTemplateBoard, DrawDimension, Viewer } from "."; +import { Object3D, Vector3 } from "three"; +import { DrawDimension, Viewer, createTemplateBoard } from "."; + +export const XAxis = new Vector3(1, 0, 0); +export const XAxisN = new Vector3(-1, 0, 0); +export const YAxis = new Vector3(0, 1, 0); +export const YAxisN = new Vector3(0, -1, 0); +export const ZAxis = new Vector3(0, 0, 1); +export const ZAxisN = new Vector3(0, 0, -1); function dispose(m: Object3D) {