/* * File: \src\utils\viewHelper.ts * Project: viewer_for_ruwei * File Created: Tuesday, 10th September 2024 1:34:19 pm * Author: Sun Jie (j.sun@supreium.com) * ----- * Last Modified: Wednesday, 16th October 2024 3:43:00 pm * Modified By: Sun Jie (j.sun@supreium.com) * ----- * Description: * ----- * Copyright (c) 2024 Supreium Co., Ltd , All Rights Reserved. */ import { CadView, RenderingEngine, NodeBasedMeshVO, SupVector, View, NodeBasedMesh, SupColor, MeshProp, } from "poseidon-engine-core"; export type MeshPropertyType = { name: string; data: number[]; size: 1; }; type PropertyType = { prop: string; index: number; range: { max: number; min: number }; }; export class ViewHelper { private _view_: View | null = null; private engine: RenderingEngine | null = null; inited = false; private activeDiv: HTMLDivElement | null = null; private parentDiv: HTMLDivElement | null = null; private _meshVO: NodeBasedMeshVO | null = null; private _propertyMap: Map; // private _activityProperty: string = ""; constructor() { this._propertyMap = new Map(); } async initView(div: HTMLDivElement, bgColor?: number) { div.addEventListener("contextmenu", (e) => e.preventDefault()); this.parentDiv = div; this.activeDiv = document.createElement("div"); this.activeDiv.style.width = "100%"; this.activeDiv.style.height = "100%"; this.parentDiv.appendChild(this.activeDiv); const engine = new RenderingEngine(); engine.init( this.activeDiv, this.activeDiv.clientWidth, this.activeDiv.clientHeight ); this.engine = engine; const cadView = new CadView(); cadView.bind(engine); await cadView.init().catch((e: any) => console.error(e)); const view = new View(); view.bindEngine(cadView); // 使用传入的背景色,默认为白色 view.setBgColor(new SupColor(bgColor || 0xffffff)); engine.startAnimate(); this._view_ = view; this._view_!.legendMgr.legend.color = "#000000"; this._view_!.legendMgr.legend.title = "No Property"; this._view_!.legendMgr.legend.setColorMap("rainbow", 9); this._view_?.activeLegend(); this.inited = true; } viewResize(width: number, height: number) { if (!this.inited) { throw new Error("ViewHelper not inited"); } this.engine?.resize(width, height); } get view() { return this._view_; } async loadMesh(meshData: any, properties: any[]) { if (!this.inited) { throw new Error("ViewHelper not inited"); } const mesh = NodeBasedMesh.createFromData(meshData); properties.forEach((property) => { mesh.attachProperty(property.name, property.size, true, property.data); }); this._meshVO = new NodeBasedMeshVO(mesh); this._view_?.display(this._meshVO); mesh.props.forEach((value: MeshProp) => { this._propertyMap.set(value.name, { prop: value.name, index: 0, range: { max: value.range.max[0], min: value.range.min[0] }, }); }); } activeProperty(property: string) { const prop = this._propertyMap.get(property); if (prop) { this._meshVO?.activeProperty(prop.prop, prop.index); this._view_!.legendMgr.legend.range = prop.range; this._view_!.legendMgr.legend.title = property; } } get properties() { return Array.from(this._propertyMap.keys()); } async clear() { if (!this.inited) { return; } this._view_?.removeAll(); } isometricView() { if (!this.inited) { throw new Error("ViewHelper not inited"); } this._view_?.lookAt( new SupVector(0, 0, 0), new SupVector(-1, -1, -1), new SupVector(1, 0, 0) ); } fitAll() { if (!this.inited) { throw new Error("ViewHelper not inited"); return; } this._view_?.fitAll(); } static instance = new ViewHelper(); }