diff --git a/src/Add-on/Join.ts b/src/Add-on/Join.ts index b5fe901ce..57a7e69bc 100644 --- a/src/Add-on/Join.ts +++ b/src/Add-on/Join.ts @@ -3,6 +3,7 @@ import { app } from "../ApplicationServices/Application"; import { curveLinkGroup } from "../Common/CurveUtils"; import { Status } from "../Common/Status"; import { Arc } from "../DatabaseServices/Arc"; +import { Board, BoardType } from "../DatabaseServices/Board"; import { Circle } from "../DatabaseServices/Circle"; import { Curve } from '../DatabaseServices/Curve'; import { Line } from "../DatabaseServices/Line"; @@ -16,11 +17,23 @@ export class Command_Join implements Command { async exec() { - let exSsRes = await app.m_Editor.GetSelection({ Msg: "请选择对象<全部选择>:", UseSelect: true, Filter: { filterTypes: [Curve] } }); + let exSsRes = await app.m_Editor.GetSelection({ Msg: "请选择对象<全部选择>:", UseSelect: true, Filter: { filterTypes: [Curve, Board] } }); if (exSsRes.Status === PromptStatus.Cancel) return; - let curveCol = exSsRes.SelectSet.SelectEntityList as Curve[]; + let curveCol: Curve[] = []; + let brs: Board[] = []; + for (let en of exSsRes.SelectSet.SelectEntityList) + { + if (en instanceof Curve) + curveCol.push(en) + else if (en instanceof Board) + brs.push(en) + } + + if (brs.length > 1) + this.MergeBoards(brs); + let typeMap = new Map(); for (let cu of curveCol) { @@ -179,4 +192,50 @@ export class Command_Join implements Command } } } + private MergeBoards(brs: Board[]) + { + let brMap: Map = new Map(); + if (brs.length > 0) + { + for (let br of brs) + { + let brs = brMap.get(br.BoardType); + if (brs) + brs.push(br); + else + brMap.set(br.BoardType, [br]); + } + } + for (let [, bs] of brMap) + { + this.JoinSameTypeBrs(bs); + } + } + private JoinSameTypeBrs(brs: Board[]) + { + while (brs.length > 0) + { + let br = brs.shift();//取第一个 + + while (brs.length > 0) + { + //剩余的 无法合并的板件 + let remBrs = brs.filter(b => + { + let isSuccess = br.Join(b); + if (isSuccess) + { + b.Erase(); + return false; + } + return true; + }); + + if (remBrs.length === brs.length) + break;//退出循环.下一个 + else + brs = remBrs; //更新为剩下的板件列表 + } + } + } } diff --git a/src/DatabaseServices/Board.ts b/src/DatabaseServices/Board.ts index cf3808357..2403b717d 100644 --- a/src/DatabaseServices/Board.ts +++ b/src/DatabaseServices/Board.ts @@ -4,7 +4,7 @@ import { arrayRemoveIf } from '../Common/ArrayExt'; import { UpdateDraw } from '../Common/Status'; import { ObjectSnapMode } from '../Editor/ObjectSnapMode'; import { Box3Ext } from '../Geometry/Box'; -import { equaln, equalv3, MoveMatrix, rotatePoint, cZeroVec } from '../Geometry/GeUtils'; +import { equaln, equalv3, MoveMatrix, rotatePoint, cZeroVec, isParallelTo } from '../Geometry/GeUtils'; import ThreeBSP from '../Geometry/ThreeCSG'; import { RenderType } from '../GraphicsSystem/RenderType'; import { BoardProcessOption, ComposingType, DrillType, LinesType, PXLFaceType } from '../UI/Store/BoardInterface'; @@ -414,7 +414,33 @@ export class Board extends Entity { return this.m_Shape.Explode().map(cu => cu.ApplyMatrix(this.OCS)); } + Join(target: this): boolean + { + if (equaln(this.m_Thickness, target.m_Thickness) + && isParallelTo(this.Normal, target.Normal) + && this.IsCoplaneTo(target)) + { + this.WriteAllObjectRecord(); + let cloneTar = target.Clone(); + let mat = this.OCSInv.multiply(target.OCS) + cloneTar.m_Shape.ApplyMatrix(mat); + let shapes = this.m_Shape.UnionBoolOperation(cloneTar.m_Shape); + if (shapes.length === 1) + { + cloneTar.m_BoardModeling.forEach(m => + { + m.shape.ApplyMatrix(mat); + }); + + this.m_BoardModeling.push(...cloneTar.m_BoardModeling); + this.Shape = shapes[0]; + this.UpdateOutlineShape(); + return true; + } + } + return false; + } RotateBoard(rox: number, roy: number, roz: number) { this.WriteAllObjectRecord(); @@ -689,7 +715,6 @@ export class Board extends Entity private UpdateBoardShape() { let deleteModel: Set = new Set(); - for (let m of this.m_BoardModeling) { let shapeZ = m.shape.Outline.Curve.Position.z; @@ -733,6 +758,10 @@ export class Board extends Entity arrayRemoveIf(this.m_BoardModeling, m => deleteModel.has(m)); + this.UpdateOutlineShape(); + } + private UpdateOutlineShape() + { //源形状min应在0点 let min = this.m_Shape.BoundingBox.min; if (!equalv3(min, new Vector3())) @@ -746,8 +775,8 @@ export class Board extends Entity this.ApplyMatrix( MoveMatrix(min.applyMatrix4(new Matrix4().extractRotation(this.OCS)))); + this.Update(UpdateDraw.Geometry); } - } InitDrawObject(renderType: RenderType = RenderType.Wireframe): Object3D {