UNPKG

@itwin/frontend-devtools

Version:

Debug menu and supporting UI widgets

115 lines 5.4 kB
"use strict"; /*--------------------------------------------------------------------------------------------- * Copyright (c) Bentley Systems, Incorporated. All rights reserved. * See LICENSE.md in the project root for license terms and full copyright notice. *--------------------------------------------------------------------------------------------*/ Object.defineProperty(exports, "__esModule", { value: true }); exports.ViewDefinitionDecorationTool = void 0; /** @packageDocumentation * @module Tools */ const core_common_1 = require("@itwin/core-common"); const core_frontend_1 = require("@itwin/core-frontend"); const parseToggle_1 = require("./parseToggle"); const core_bentley_1 = require("@itwin/core-bentley"); const core_geometry_1 = require("@itwin/core-geometry"); class ViewDefinitionDecoration { static _instance; _removeMe; constructor() { this._removeMe = core_frontend_1.IModelApp.viewManager.addDecorator(this); } stop() { if (this._removeMe) { this._removeMe(); this._removeMe = undefined; } } _viewId; _loading = false; _preloadedFrustum = []; async getFrustumForViewId(iModel, viewId, transform = core_geometry_1.Transform.createIdentity()) { return iModel.views.load(viewId).then((result) => { // Use the view extents/delta let vector; if (result.is2d()) vector = core_geometry_1.Vector2d.create(result.delta.x, result.delta.y); else vector = core_geometry_1.Vector2d.create(result.getExtents().x, result.getExtents().y); // create a viewport to the size of the extents to ensure the aspect is correct. const vp = core_frontend_1.OffScreenViewport.create({ view: result, viewRect: new core_frontend_1.ViewRect(0, 0, vector.x, vector.y) }); const frustum = vp.getFrustum(); this._preloadedFrustum.push(frustum.transformBy(transform)); }).catch((err) => { core_bentley_1.Logger.logException("FrontendDevTools", err); }); } preload(viewport) { this._loading = true; const iModel = viewport.iModel; const view = viewport.view; const viewId = view.id; const loadPromises = []; loadPromises.push(this.getFrustumForViewId(iModel, viewId)); if (view.isDrawingView() && view.sectionDrawingInfo.spatialView) loadPromises.push(this.getFrustumForViewId(iModel, view.sectionDrawingInfo.spatialView, view.sectionDrawingInfo.drawingToSpatialTransform.inverse())); void Promise.all(loadPromises).finally(() => { core_frontend_1.IModelApp.viewManager.invalidateCachedDecorationsAllViews(this); this._viewId = viewId; this._loading = false; }); } /** This will allow the render system to cache and reuse the decorations created by this decorator's decorate() method. */ useCachedDecorations = true; decorate(context) { if (context.viewport.view.id !== this._viewId && !this._loading) { this._preloadedFrustum.length = 0; // just clear the decoration if the view is not from a view definition if (core_bentley_1.Id64.isValidId64(context.viewport.view.id)) this.preload(context.viewport); } const builder = context.createGraphicBuilder(core_frontend_1.GraphicType.WorldDecoration); const purple = core_common_1.ColorDef.fromString("#800080"); // The current view's frustum is at the end of the array. builder.setSymbology(purple, purple, 5, core_common_1.LinePixels.Code0); const endIndex = this._preloadedFrustum.length - 1; const currentFrustum = this._preloadedFrustum[endIndex]; if (currentFrustum) builder.addFrustum(currentFrustum); // Add other frustums. builder.setSymbology(purple, purple, 4, core_common_1.LinePixels.Code2); const remainingFrustums = this._preloadedFrustum.slice(0, endIndex); remainingFrustums.forEach((frustum) => builder.addFrustum(frustum)); context.addDecorationFromBuilder(builder); } static toggle(enabled) { const instance = ViewDefinitionDecoration._instance; if (undefined !== enabled && (undefined !== instance) === enabled) return; if (undefined === instance) { ViewDefinitionDecoration._instance = new ViewDefinitionDecoration(); } else { instance.stop(); ViewDefinitionDecoration._instance = undefined; } } } /** Display in every viewport a green range graphic for each displayed tile tree, plus a red range graphic for each tile tree's content range if defined. * @beta */ class ViewDefinitionDecorationTool extends core_frontend_1.Tool { static toolId = "ViewDefinitionDecorationTool"; static get minArgs() { return 0; } static get maxArgs() { return 1; } async run(enable) { ViewDefinitionDecoration.toggle(enable); return true; } async parseAndRun(...args) { const enable = (0, parseToggle_1.parseToggle)(args[0]); if (typeof enable !== "string") await this.run(enable); return true; } } exports.ViewDefinitionDecorationTool = ViewDefinitionDecorationTool; //# sourceMappingURL=ViewDefinitionDecorator.js.map