UNPKG

@itwin/frontend-devtools

Version:

Debug menu and supporting UI widgets

533 lines • 24.2 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.MapBaseVisibilityTool = exports.MapBaseTransparencyTool = exports.MapBaseColorTool = exports.ToggleTerrainTool = exports.MapLayerZoomTool = exports.MapLayerSubLayerVisibilityTool = exports.MapLayerTransparencyTool = exports.ReorderMapLayers = exports.MapLayerVisibilityTool = exports.DetachMapLayersTool = exports.SetMapBaseTool = exports.AttachMapOverlayTool = exports.AttachMapLayerTool = exports.AttachTileURLMapLayerByUrlTool = exports.AttachOgcApiFeaturesMapLayerTool = exports.AttachArcGISFeatureMapLayerByUrlTool = exports.AttachArcGISMapLayerByUrlTool = exports.AttachWmtsMapLayerByUrlTool = exports.AttachWmsMapLayerByUrlTool = exports.AttachModelMapLayerTool = void 0; const core_common_1 = require("@itwin/core-common"); const core_frontend_1 = require("@itwin/core-frontend"); const parseBoolean_1 = require("./parseBoolean"); const parseToggle_1 = require("./parseToggle"); /** Base class for attaching map layer tool. */ class AttachMapLayerBaseTool extends core_frontend_1.Tool { _isBackground; _isBase; constructor(_isBackground = true, _isBase = false) { super(); this._isBackground = _isBackground; this._isBase = _isBase; } doAttach(source) { const vp = core_frontend_1.IModelApp.viewManager.selectedView; if (vp === undefined || source === undefined) return; source.validateSource().then((validation) => { if (validation.status === core_frontend_1.MapLayerSourceStatus.Valid || validation.status === core_frontend_1.MapLayerSourceStatus.RequireAuth) { if (this._isBase) { vp.displayStyle.backgroundMapBase = core_common_1.BaseMapLayerSettings.fromJSON({ ...source, subLayers: validation.subLayers }); vp.invalidateRenderPlan(); } else { const settings = source.toLayerSettings(validation.subLayers); if (settings) { // Need to specify index in mapLayerIndex, so just use -1 to attach layer at the bottom // Previously, this was done within attachMapLayer if index was undefined. vp.displayStyle.attachMapLayer({ settings, mapLayerIndex: { isOverlay: !this._isBackground, index: -1 } }); } } if (validation.status === core_frontend_1.MapLayerSourceStatus.Valid) { vp.invalidateRenderPlan(); const msg = core_frontend_1.IModelApp.localization.getLocalizedString("FrontendDevTools:tools.AttachMapLayerTool.Messages.MapLayerAttached", { sourceName: source.name }); core_frontend_1.IModelApp.notifications.outputMessage(new core_frontend_1.NotifyMessageDetails(core_frontend_1.OutputMessagePriority.Info, msg)); } else if (validation.status === core_frontend_1.MapLayerSourceStatus.RequireAuth) { const msg = core_frontend_1.IModelApp.localization.getLocalizedString("FrontendDevTools:tools.AttachMapLayerTool.Messages.MapLayerAttachedRequiresAuth", { sourceName: source.name }); core_frontend_1.IModelApp.notifications.outputMessage(new core_frontend_1.NotifyMessageDetails(core_frontend_1.OutputMessagePriority.Warning, msg)); } } else if (validation.status === core_frontend_1.MapLayerSourceStatus.IncompatibleFormat) { const msg = core_frontend_1.IModelApp.localization.getLocalizedString("FrontendDevTools:tools.AttachMapLayerTool.Messages.MapLayersIncompatibleFormat", { sourceName: source.name }); core_frontend_1.IModelApp.notifications.outputMessage(new core_frontend_1.NotifyMessageDetails(core_frontend_1.OutputMessagePriority.Error, msg)); } else { const msg = core_frontend_1.IModelApp.localization.getLocalizedString("FrontendDevTools:tools.AttachMapLayerTool.Messages.MapLayerValidationFailed", { sourceName: source.name }); core_frontend_1.IModelApp.notifications.outputMessage(new core_frontend_1.NotifyMessageDetails(core_frontend_1.OutputMessagePriority.Error, msg)); } }).catch((error) => { const msg = core_frontend_1.IModelApp.localization.getLocalizedString("FrontendDevTools:tools.AttachMapLayerTool.Messages.MapLayerAttachError", { error, sourceName: source.name }); core_frontend_1.IModelApp.notifications.outputMessage(new core_frontend_1.NotifyMessageDetails(core_frontend_1.OutputMessagePriority.Error, msg)); }); } } /** Attach a map layer from URL base class. A layer is attached for each unique model in the selection * @beta */ class AttachModelMapLayerTool extends core_frontend_1.Tool { _formatId; static get minArgs() { return 1; } static get maxArgs() { return 2; } static toolId = "AttachModelMapLayerTool"; constructor(_formatId) { super(); this._formatId = _formatId; } async run(nameIn, drapeTarget) { const vp = core_frontend_1.IModelApp.viewManager.selectedView; if (!vp) return false; if ("0" === nameIn) nameIn = undefined; const iModel = vp.iModel; const elements = await iModel.elements.getProps(iModel.selectionSet.elements); const modelIds = new Set(); for (const element of elements) modelIds.add(element.model); for (const modelId of modelIds) { const modelProps = await iModel.models.getProps(modelId); const modelName = modelProps[0].name ? modelProps[0].name : modelId; const name = nameIn ? (modelIds.size > 1 ? `${nameIn}: ${modelName}` : nameIn) : modelName; let settingsDrapeTarget; if ("reality" === drapeTarget) settingsDrapeTarget = core_common_1.ModelMapLayerDrapeTarget.RealityData; else if ("imodel" === drapeTarget) settingsDrapeTarget = core_common_1.ModelMapLayerDrapeTarget.IModel; const settings = core_common_1.ModelMapLayerSettings.fromJSON({ name, modelId, drapeTarget: settingsDrapeTarget }); vp.displayStyle.attachMapLayer({ settings, mapLayerIndex: { isOverlay: false, index: -1 } }); } return true; } async parseAndRun(...args) { return this.run(args[0], args[1]); } async onRestartTool() { } } exports.AttachModelMapLayerTool = AttachModelMapLayerTool; /** Attach a map layer from URL base class. */ class AttachMapLayerByURLBaseTool extends AttachMapLayerBaseTool { _formatId; static get minArgs() { return 1; } static get maxArgs() { return 4; } constructor(_formatId) { super(); this._formatId = _formatId; } async run(url, name, userName, password) { const source = core_frontend_1.MapLayerSource.fromJSON({ url, name: (name ? name : url), formatId: this._formatId }); if (source) { source.userName = userName; source.password = password; } this.doAttach(source); return true; } async parseAndRun(...args) { return this.run(args[0], args[1], args[2], args[3]); } } /** This tool attaches a WMS map layer from a given URL. * @beta */ class AttachWmsMapLayerByUrlTool extends AttachMapLayerByURLBaseTool { static toolId = "AttachWmsMapLayerTool"; constructor() { super("WMS"); } /** This method runs the tool, attaching a WMS map layer from a given URL. * @param args contains url, name, userName, password in array order */ async parseAndRun(...args) { return this.run(core_frontend_1.WmsUtilities.getBaseUrl(args[0]), args[1], args[2], args[3]); } } exports.AttachWmsMapLayerByUrlTool = AttachWmsMapLayerByUrlTool; /** This tool attaches a WMTS map layer from a given URL. * @beta */ class AttachWmtsMapLayerByUrlTool extends AttachMapLayerByURLBaseTool { static toolId = "AttachWmtsMapLayerTool"; constructor() { super("WMTS"); } /** This method runs the tool, attaching a WMTS map layer from a given URL. * @param args contains url, name, userName, password in array order */ async parseAndRun(...args) { return this.run(core_frontend_1.WmsUtilities.getBaseUrl(args[0]), args[1], args[2], args[3]); } } exports.AttachWmtsMapLayerByUrlTool = AttachWmtsMapLayerByUrlTool; /** This tool attaches an ArcGIS map layer from a given URL. * @beta */ class AttachArcGISMapLayerByUrlTool extends AttachMapLayerByURLBaseTool { static toolId = "AttachArcGISMapLayerTool"; constructor() { super("ArcGIS"); } } exports.AttachArcGISMapLayerByUrlTool = AttachArcGISMapLayerByUrlTool; /** This tool attaches an ArcGIS map layer from a given URL. * @beta */ class AttachArcGISFeatureMapLayerByUrlTool extends AttachMapLayerByURLBaseTool { static toolId = "AttachArcGISFeatureMapLayerTool"; constructor() { super("ArcGISFeature"); } } exports.AttachArcGISFeatureMapLayerByUrlTool = AttachArcGISFeatureMapLayerByUrlTool; /** This tool attaches an ArcGIS map layer from a given URL. * @beta */ class AttachOgcApiFeaturesMapLayerTool extends AttachMapLayerByURLBaseTool { static toolId = "AttachOgcApiFeaturesMapLayerTool"; constructor() { super("OgcApiFeatures"); } } exports.AttachOgcApiFeaturesMapLayerTool = AttachOgcApiFeaturesMapLayerTool; /** This tool attaches a map layer from a given tile URL. * @beta */ class AttachTileURLMapLayerByUrlTool extends AttachMapLayerByURLBaseTool { static toolId = "AttachTileURLMapLayerTool"; constructor() { super("TileURL"); } } exports.AttachTileURLMapLayerByUrlTool = AttachTileURLMapLayerByUrlTool; /** This tool add a Map Layer from a specified name (look up in MapLayerSources.json). * @beta */ class AttachMapLayerTool extends AttachMapLayerBaseTool { static toolId = "AttachMapLayerTool"; static get minArgs() { return 1; } static get maxArgs() { return 1; } /** This method runs the tool, adding a map layer from a specified name in MayLayerSources.json. * @param name the name of the map layer to add */ async run(name) { const vp = core_frontend_1.IModelApp.viewManager.selectedView; if (vp === undefined) return false; core_frontend_1.MapLayerSources.create().then((sources) => { if (sources !== undefined) { const source = sources.findByName(name, this._isBase); if (source !== undefined) this.doAttach(source); } }).catch((_err) => { }); return true; } /** Executes this tool's run method with args[0] containing `name`. * @see [[run]] */ async parseAndRun(...args) { return this.run(args[0]); } } exports.AttachMapLayerTool = AttachMapLayerTool; /** This tool attaches a Overlay map layer. * @beta */ class AttachMapOverlayTool extends AttachMapLayerTool { static toolId = "AttachMapOverlayTool"; constructor() { super(); this._isBackground = false; } } exports.AttachMapOverlayTool = AttachMapOverlayTool; /** Sets map layer base tool. * @beta */ class SetMapBaseTool extends AttachMapLayerTool { static toolId = "SetMapBaseTool"; constructor() { super(); this._isBase = true; } } exports.SetMapBaseTool = SetMapBaseTool; /** Detach Map Layers Tool. * @beta */ class DetachMapLayersTool extends core_frontend_1.Tool { static toolId = "DetachMapLayersTool"; static get minArgs() { return 0; } static get maxArgs() { return 0; } async parseAndRun(..._args) { return this.run(); } async run() { const vp = core_frontend_1.IModelApp.viewManager.selectedView; if (vp === undefined) return false; vp.displayStyle.detachMapLayerByIndex({ index: -1, isOverlay: true }); vp.displayStyle.detachMapLayerByIndex({ index: -1, isOverlay: false }); vp.invalidateRenderPlan(); return true; } } exports.DetachMapLayersTool = DetachMapLayersTool; function parseLayerIndex(args) { const layerIndex = args.length > 1 ? parseInt(args[1], 10) : 0; return isNaN(layerIndex) ? 0 : layerIndex; } /** This tool sets the visibility of the map layer. * @beta */ class MapLayerVisibilityTool extends core_frontend_1.Tool { static toolId = "SetMapLayerVisibility"; static get minArgs() { return 1; } static get maxArgs() { return 2; } /** This method runs the tool, setting the visibility of a map layer. * @param layerIndex the index of the layer to change * @param visible a boolean that should be true if the layer should be visible */ async run(layerIndex, enable) { const vp = core_frontend_1.IModelApp.viewManager.selectedView; if (undefined === vp || !vp.view.isSpatialView()) return false; const mapLayer = vp.displayStyle.mapLayerAtIndex({ index: layerIndex, isOverlay: false }); if (undefined === mapLayer) return false; const visible = (enable === undefined) ? !mapLayer.visible : enable; vp.displayStyle.changeMapLayerProps({ visible }, { index: layerIndex, isOverlay: false }); vp.invalidateRenderPlan(); return true; } /** Executes this tool's run method with args[0] containing `enable` and args[1] containing `layerIndex`. * @see [[run]] */ async parseAndRun(...args) { const enable = (0, parseToggle_1.parseToggle)(args[0]); const layerIndex = parseLayerIndex(args); if (typeof enable !== "string") await this.run(layerIndex, enable); return true; } } exports.MapLayerVisibilityTool = MapLayerVisibilityTool; /** This tool reorders map layers. * @beta */ class ReorderMapLayers extends core_frontend_1.Tool { static toolId = "ReorderMapLayers"; static get minArgs() { return 0; } static get maxArgs() { return 2; } /** This method runs the tool, reordering the map layers. * @param from a numeric value specifying the layer index that is being moved * @param from a numeric value specifying the layer index to move that layer to */ async run(from, to) { const vp = core_frontend_1.IModelApp.viewManager.selectedView; if (undefined === vp || !vp.view.isSpatialView()) return false; vp.displayStyle.moveMapLayerToIndex(isNaN(from) ? 0 : from, isNaN(to) ? vp.displayStyle.settings.mapImagery.backgroundLayers.length : to, false); vp.invalidateRenderPlan(); return true; } /** Executes this tool's run method with args[0] containing `from` and args[1] containing `to`. * @see [[run]] */ async parseAndRun(...args) { const from = parseInt(args[0], 10); const to = parseInt(args[1], 10); await this.run(from, to); return true; } } exports.ReorderMapLayers = ReorderMapLayers; /** This tool sets the transparency of a map layer. * @beta */ class MapLayerTransparencyTool extends core_frontend_1.Tool { static toolId = "SetMapLayerTransparency"; static get minArgs() { return 1; } static get maxArgs() { return 2; } /** This method runs the tool, setting the transparency of a map layer. * @param layerIndex the index of the layer to change * @param transparency a numeric value in the range 0.0 (fully opaque) to 1.0 (fully transparent) */ async run(layerIndex, transparency) { const vp = core_frontend_1.IModelApp.viewManager.selectedView; if (undefined === vp || !vp.view.isSpatialView()) return false; vp.displayStyle.changeMapLayerProps({ transparency }, { index: layerIndex, isOverlay: false }); vp.invalidateRenderPlan(); return true; } /** Executes this tool's run method with args[0] containing `transparency` and args[1] containing `layerIndex`. * @see [[run]] */ async parseAndRun(...args) { const transparency = parseFloat(args[0]); const layerIndex = parseLayerIndex(args); if (transparency >= 0 && transparency <= 1) await this.run(layerIndex, transparency); return true; } } exports.MapLayerTransparencyTool = MapLayerTransparencyTool; /** This tool sets the visibility of the map sublayer. * @beta */ class MapLayerSubLayerVisibilityTool extends core_frontend_1.Tool { static toolId = "SetMapSubLayerVisibility"; static get minArgs() { return 1; } static get maxArgs() { return 2; } /** This method runs the tool, setting the visibility of a map sublayer. * @param layerIndex the index of the layer to change * @param visible a boolean that should be true if the sublayer should be visible */ async run(layerIndex, visible) { const vp = core_frontend_1.IModelApp.viewManager.selectedView; if (undefined === vp || !vp.view.isSpatialView()) return false; vp.displayStyle.changeMapSubLayerProps({ visible }, -1, { index: layerIndex, isOverlay: false }); vp.invalidateRenderPlan(); return true; } /** Executes this tool's run method with args[0] containing `transparency` and args[1] containing `layerIndex`. * @see [[run]] */ async parseAndRun(...args) { const on = args[0] !== "off"; const layerIndex = parseLayerIndex(args); await this.run(layerIndex, on); return true; } } exports.MapLayerSubLayerVisibilityTool = MapLayerSubLayerVisibilityTool; /** This tool changes the viewport so it is zoomed to the range of a map layer. * @beta */ class MapLayerZoomTool extends core_frontend_1.Tool { static toolId = "MapLayerZoom"; static get minArgs() { return 0; } static get maxArgs() { return 1; } /** This method runs the tool, changing the viewport so it is zoomed to the range of a map layer. * @param layerIndex the index of the layer whose range to zoom to */ async run(layerIndex) { const vp = core_frontend_1.IModelApp.viewManager.selectedView; if (undefined === vp || !vp.view.isSpatialView()) return false; vp.viewMapLayerRange({ index: layerIndex, isOverlay: false }, vp).then(() => { }).catch(() => { }); return true; } /** Executes this tool's run method with args[0] containing `layerIndex`. * @see [[run]] */ async parseAndRun(...args) { const layerIndex = parseLayerIndex(args); await this.run(layerIndex); return true; } } exports.MapLayerZoomTool = MapLayerZoomTool; /** This tool toggles whether to apply terrain heights to the map. * @beta */ class ToggleTerrainTool extends core_frontend_1.Tool { static toolId = "ToggleTerrain"; static get minArgs() { return 0; } static get maxArgs() { return 1; } /** This method runs the tool, changing whether to apply terrain heights to the map. * @param enable whether or not to enable terrain heights on the map */ async run(enable) { const vp = core_frontend_1.IModelApp.viewManager.selectedView; if (undefined === vp || !vp.view.isSpatialView()) return false; const applyTerrain = (enable === undefined) ? !vp.displayStyle.backgroundMapSettings.applyTerrain : enable; vp.displayStyle.changeBackgroundMapProps({ applyTerrain }); vp.invalidateRenderPlan(); return true; } /** Executes this tool's run method with args[0] containing `enable`. * @see [[run]] */ async parseAndRun(...args) { const enable = (0, parseToggle_1.parseToggle)(args[0]); if (typeof enable !== "string") await this.run(enable); return true; } } exports.ToggleTerrainTool = ToggleTerrainTool; /** This tool changes the color of the base map. * @beta */ class MapBaseColorTool extends core_frontend_1.Tool { static toolId = "SetMapBaseColorTool"; static get minArgs() { return 3; } static get maxArgs() { return 3; } /** This method runs the tool, changing the color of the base map. * @param color the color for the base map */ async run(color) { const vp = core_frontend_1.IModelApp.viewManager.selectedView; if (undefined === vp || !vp.view.isSpatialView()) return false; const curTransparency = vp.displayStyle.backgroundMapBase instanceof core_common_1.ColorDef ? vp.displayStyle.backgroundMapBase.getTransparency() : 0; vp.displayStyle.backgroundMapBase = color.withTransparency(curTransparency); vp.invalidateRenderPlan(); return true; } /** Executes this tool's run method with args[0] containing a 0 to 255 red component, args[1] containing a 0 to 255 green component, and args[2] containing a 0 to 255 blue component. * These rgb values will be used to construct the `color` parameter passed to this tool's run method. * @see [[run]] */ async parseAndRun(...args) { const red = parseFloat(args[0]), green = parseFloat(args[1]), blue = parseFloat(args[2]); return (isNaN(red) || red < 0 || red > 255 || isNaN(green) || green < 0 || green > 255 || isNaN(blue) || blue < 0 || blue > 255) ? false : this.run(core_common_1.ColorDef.from(red, green, blue)); } } exports.MapBaseColorTool = MapBaseColorTool; /** This tool changes the transparency of the base map. * @beta */ class MapBaseTransparencyTool extends core_frontend_1.Tool { static toolId = "SetMapBaseTransparencyTool"; static get minArgs() { return 1; } static get maxArgs() { return 1; } /** This method runs the tool, changing the transparency of the base map. * @param transparency a numeric value in range 0.0 to 1.0 whether 0.0 means fully opaque and 1.0 means fully transparent */ async run(transparency) { const vp = core_frontend_1.IModelApp.viewManager.selectedView; if (undefined === vp || !vp.view.isSpatialView()) return false; vp.displayStyle.changeBaseMapTransparency(transparency); vp.invalidateRenderPlan(); return true; } /** Executes this tool's run method with args[0] containing `transparency`. * @see [[run]] */ async parseAndRun(...args) { const transparency = parseFloat(args[0]); return (isNaN(transparency) || transparency < 0 || transparency > 1) ? false : this.run(transparency); } } exports.MapBaseTransparencyTool = MapBaseTransparencyTool; /** This tool changes the visibility of the base map. * @beta */ class MapBaseVisibilityTool extends core_frontend_1.Tool { static toolId = "SetMapBaseVisibilityTool"; static get minArgs() { return 1; } static get maxArgs() { return 1; } /** This method runs the tool, changing the visibility of the base map. * @param visible a boolean which specifies whether or not to make the base map visible */ async run(visible) { const vp = core_frontend_1.IModelApp.viewManager.selectedView; if (undefined === vp || !vp.view.isSpatialView() || vp.displayStyle.backgroundMapBase instanceof core_common_1.ColorDef) return false; vp.displayStyle.backgroundMapBase = vp.displayStyle.backgroundMapBase.clone({ visible }); vp.invalidateRenderPlan(); return true; } /** Executes this tool's run method with args[0] containing `visible`. * @see [[run]] */ async parseAndRun(...args) { const visible = (0, parseBoolean_1.parseBoolean)(args[0]); return (visible !== undefined ? this.run(visible) : false); } } exports.MapBaseVisibilityTool = MapBaseVisibilityTool; //# sourceMappingURL=MapLayerTool.js.map