From 66687a861557ce12166382a003bc545d20e4cd2c Mon Sep 17 00:00:00 2001 From: ChenX Date: Thu, 1 Apr 2021 14:30:24 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96:=E7=AE=80=E5=8C=96=E9=93=B0?= =?UTF-8?q?=E9=93=BE=E4=BD=8D=E7=BD=AE=E5=A4=87=E6=B3=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Add-on/DXFLoad.ts | 2 +- src/Add-on/DrawBoard/ParseHinge.ts | 76 ++++++++++++++++++++++++------ src/Geometry/GeUtils.ts | 4 +- 3 files changed, 65 insertions(+), 17 deletions(-) diff --git a/src/Add-on/DXFLoad.ts b/src/Add-on/DXFLoad.ts index 563024ee4..963a24df4 100644 --- a/src/Add-on/DXFLoad.ts +++ b/src/Add-on/DXFLoad.ts @@ -1,5 +1,5 @@ import { Intent } from "@blueprintjs/core"; -import DxfParser from "dxf-parser"; +const DxfParser = require("dxf-parser"); import { MathUtils, Matrix4, Vector3 } from "three"; import { app } from "../ApplicationServices/Application"; import { arrayLast } from "../Common/ArrayExt"; diff --git a/src/Add-on/DrawBoard/ParseHinge.ts b/src/Add-on/DrawBoard/ParseHinge.ts index b58a87ed7..baa3ddd85 100644 --- a/src/Add-on/DrawBoard/ParseHinge.ts +++ b/src/Add-on/DrawBoard/ParseHinge.ts @@ -1,19 +1,23 @@ -import { Vector3 } from "three"; +import { Vector2, Vector3 } from "three"; import { app } from "../../ApplicationServices/Application"; -import { arrayRemoveIf } from "../../Common/ArrayExt"; -import { FixedNotZero } from "../../Common/Utils"; +import { arrayPushArray } from "../../Common/ArrayExt"; +import { Log } from "../../Common/Log"; +import { Intent } from "../../Common/Toaster"; import { Board } from "../../DatabaseServices/Entity/Board"; import { HardwareCompositeEntity } from "../../DatabaseServices/Hardware/HardwareCompositeEntity"; import { Command } from "../../Editor/CommandMachine"; import { PromptStatus } from "../../Editor/PromptResult"; +import { comparePoint } from "../../Geometry/GeUtils"; +import { AppToaster } from "../../UI/Components/Toaster"; import { BoardOpenDir } from "../../UI/Store/BoardInterface"; export class ParseHinge implements Command { async exec() { + Log("注意:绘制的铰链五金名称必须包含\"铰链\""); let res = await app.Editor.GetSelection({ - Msg: "请选择需要分析铰链的门板", + Msg: "请选择需要分析铰链的门板:", Filter: { filterFunction: (o, e) => e instanceof Board && e.OpenDir !== BoardOpenDir.None } @@ -22,9 +26,12 @@ export class ParseHinge implements Command { let doors = res.SelectSet.SelectEntityList as Board[]; + let succeedCount = 0; + let emptyCount = 0; + for (let door of doors) { - let hingeMap = new Map(); + let hingeMap = new Map(); for (let hid of door.RelativeHardware) { let hw = hid.Object; @@ -33,18 +40,59 @@ export class ParseHinge implements Command if (hw.HardwareOption.name.includes("铰链")) { let pos = new Vector3().setFromMatrixPosition(hw.SpaceOCS).applyMatrix4(door.OCSInv); - let posStr = hingeMap.get("jl-" + hw.HardwareOption.name); - if (!posStr) - posStr = ""; - posStr += `${FixedNotZero(pos.x, 2)},${FixedNotZero(pos.y, 2)},${FixedNotZero(pos.z, 2)}|`; - hingeMap.set("jl-" + hw.HardwareOption.name, posStr); + let key = "jl-" + hw.HardwareOption.name; + let posArr = hingeMap.get(key); + if (!posArr) + { + posArr = []; + hingeMap.set(key, posArr); + } + posArr.push(new Vector2(Math.round(pos.x), Math.round(pos.y))); } } } - let oldRemarks = door.BoardProcessOption.remarks.slice(); - arrayRemoveIf(oldRemarks, (r) => hingeMap.has(r[0])); - oldRemarks.push(...hingeMap.entries()); - door.BoardProcessOption.remarks = oldRemarks; + + let remarks = door.BoardProcessOption.remarks.filter(r => !hingeMap.has(r[0])); + + let hingeMarks: [string, string][] = []; + for (let d of hingeMap) + { + let arr = d[1]; + arr.sort(comparePoint("xy")); + if (arr.every(p => p.x === arr[0].x))//所有的X都相等 + hingeMarks.push([d[0], `X:${arr[0].x} Y:${arr.map(p => p.y).join(",")}`]); + else if (arr.every(p => p.y === arr[0].y)) + hingeMarks.push([d[0], `Y:${arr[0].y} X:${arr.map(p => p.x).join(",")}`]); + else + hingeMarks.push([d[0], "位置(x,y): " + arr.map(p => `(${p.x},${p.y})`).join(", ")]); + } + hingeMarks.sort((h1, h2) => h1[0].localeCompare(h2[0])); + if (hingeMarks.length) + { + arrayPushArray(remarks, hingeMarks); + door.BoardProcessOption.remarks = remarks; + succeedCount++; + } + else + emptyCount++; + } + + if (succeedCount) + { + AppToaster.show({ + message: `成功分析${succeedCount}个门板的铰链位置!(已经写入到板件备注!)`, + timeout: 10000, + intent: Intent.SUCCESS, + }); + } + + if (emptyCount) + { + AppToaster.show({ + message: `有${emptyCount}个门板没有铰链!`, + timeout: 10000, + intent: Intent.WARNING, + }); } } } diff --git a/src/Geometry/GeUtils.ts b/src/Geometry/GeUtils.ts index 5d983930e..9e227b822 100644 --- a/src/Geometry/GeUtils.ts +++ b/src/Geometry/GeUtils.ts @@ -322,7 +322,7 @@ export function UpdateBoundingSphere(obj: Object3D) } -export type compareVectorFn = (v1: Vector, v2: Vector3) => number; +export type compareVectorFn = (v1: Vector, v2: Vector) => number; const comparePointCache: Map = new Map(); @@ -359,7 +359,7 @@ export function comparePoint(sortKey: string): compareVectorFn sortIndex.push([ci, i2 > ci ? 1 : -1]); } - let compareFunction = (v1: Vector, v2: Vector3): number => + let compareFunction = (v1: Vector, v2: Vector): number => { if (!v1) return -1; if (!v2) return 1;