@itwin/frontend-devtools
Version:
Debug menu and supporting UI widgets
269 lines • 11.8 kB
JavaScript
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module Tools
*/
import { FeatureAppearance } from "@itwin/core-common";
import { getCesiumAssetUrl, IModelApp, NotifyMessageDetails, OutputMessagePriority, Tool } from "@itwin/core-frontend";
import { copyStringToClipboard } from "../ClipboardUtilities";
import { parseBoolean } from "./parseBoolean";
import { parseToggle } from "./parseToggle";
/** This tool attaches a specified reality model.
* @beta
*/
export class AttachRealityModelTool extends Tool {
static toolId = "AttachRealityModelTool";
static get minArgs() { return 1; }
static get maxArgs() { return 1; }
/** This method runs the tool, attaching a specified reality model.
* @param data a [[ContextRealityModelProps]] JSON representation
*/
async run(data) {
const props = JSON.parse(data);
const vp = IModelApp.viewManager.selectedView;
if (vp === undefined)
return false;
if (props === undefined || props.tilesetUrl === undefined) {
IModelApp.notifications.outputMessage(new NotifyMessageDetails(OutputMessagePriority.Error, `Properties ${props} are not valid`));
}
vp.displayStyle.attachRealityModel(props);
IModelApp.notifications.outputMessage(new NotifyMessageDetails(OutputMessagePriority.Info, `Reality Model ${props.tilesetUrl} attached`));
return true;
}
/** Executes this tool's run method with args[0] containing `data`.
* @see [[run]]
*/
async parseAndRun(...args) {
return this.run(args[0]);
}
}
/** This tool saves a reality model's JSON representation to the system clipboard.
* @beta */
export class SaveRealityModelTool extends Tool {
static toolId = "SaveRealityModelTool";
static get minArgs() { return 0; }
static get maxArgs() { return 1; }
/** This method runs the tool, saving a reality model's JSON representation to the system clipboard.
* @param name the name of the reality model to copy; if undefined, copy the last found reality model
*/
async run(name) {
const vp = IModelApp.viewManager.selectedView;
if (vp === undefined)
return false;
vp.displayStyle.forEachRealityModel((realityModel) => {
if (name === undefined || realityModel.name === name) {
copyStringToClipboard(JSON.stringify(realityModel.toJSON()));
IModelApp.notifications.outputMessage(new NotifyMessageDetails(OutputMessagePriority.Info, `Reality Model ${realityModel.name} copied to clipboard`));
}
});
return true;
}
/** Executes this tool's run method with args[0] containing `name`.
* @see [[run]]
*/
async parseAndRun(...args) {
return this.run(args.length > 0 ? args[0] : undefined);
}
}
function changeRealityModelAppearanceOverrides(vp, overrides, index) {
if (index < 0) {
for (const model of vp.displayStyle.settings.contextRealityModels.models)
model.appearanceOverrides = model.appearanceOverrides ? model.appearanceOverrides.clone(overrides) : FeatureAppearance.fromJSON(overrides);
return vp.displayStyle.settings.contextRealityModels.models.length > 0;
}
else {
const model = vp.displayStyle.settings.contextRealityModels.models[index];
if (!model)
return false;
model.appearanceOverrides = model.appearanceOverrides ? model.appearanceOverrides.clone(overrides) : FeatureAppearance.fromJSON(overrides);
return true;
}
}
function appearanceChangedString(index) {
return index < 0 ? `All Reality Models` : `Reality Model at Index: ${index}`;
}
/** Set reality model appearance override for transparency in display style.
* @beta
*/
export class SetRealityModelTransparencyTool extends Tool {
static toolId = "SetRealityModelTransparencyTool";
static get minArgs() { return 1; }
static get maxArgs() { return 2; }
async run(transparency, index) {
const vp = IModelApp.viewManager.selectedView;
if (vp === undefined)
return false;
const changed = changeRealityModelAppearanceOverrides(vp, { transparency }, index);
if (changed)
IModelApp.notifications.outputMessage(new NotifyMessageDetails(OutputMessagePriority.Info, `${appearanceChangedString(index)} set to transparency: ${transparency}`));
return true;
}
async parseAndRun(...args) {
return this.run(parseFloat(args[0]), args.length > 1 ? parseInt(args[1], 10) : -1);
}
}
/** Set reality model appearance override for locatable in display style.
* @beta
*/
export class SetRealityModelLocateTool extends Tool {
static toolId = "SetRealityModelLocateTool";
static get minArgs() { return 1; }
static get maxArgs() { return 2; }
async run(locate, index) {
const vp = IModelApp.viewManager.selectedView;
if (vp === undefined)
return false;
const nonLocatable = locate ? undefined : true;
const changed = changeRealityModelAppearanceOverrides(vp, { nonLocatable }, index);
if (changed)
IModelApp.notifications.outputMessage(new NotifyMessageDetails(OutputMessagePriority.Info, `${appearanceChangedString(index)} set to locate: ${locate}`));
return true;
}
async parseAndRun(...args) {
const locate = parseBoolean(args[0]);
return locate === undefined ? false : this.run(locate, args.length > 1 ? parseInt(args[1], 10) : -1);
}
}
/** Set reality model appearance override for emphasized in display style.
* @beta
*/
export class SetRealityModelEmphasizedTool extends Tool {
static toolId = "SetRealityModelEmphasizedTool";
static get minArgs() { return 1; }
static get maxArgs() { return 2; }
async run(emphasized, index) {
const vp = IModelApp.viewManager.selectedView;
if (vp === undefined)
return false;
const changed = changeRealityModelAppearanceOverrides(vp, { emphasized }, index);
if (changed)
IModelApp.notifications.outputMessage(new NotifyMessageDetails(OutputMessagePriority.Info, `${appearanceChangedString(index)} set to emphasized: ${emphasized}`));
return true;
}
async parseAndRun(...args) {
const emphasized = parseBoolean(args[0]);
return emphasized === undefined ? false : this.run(emphasized ? true : undefined, args.length > 1 ? parseInt(args[1], 10) : -1);
}
}
/** Detach reality model from display style.
* @beta
*/
export class DetachRealityModelTool extends Tool {
static toolId = "ViewportDetachRealityModel";
static get minArgs() { return 0; }
static get maxArgs() { return 1; }
async run(index) {
const vp = IModelApp.viewManager.selectedView;
if (vp === undefined)
return false;
if (index < 0) {
vp.displayStyle.settings.contextRealityModels.models.forEach((model) => vp.displayStyle.settings.contextRealityModels.delete(model));
return true;
}
else {
const model = vp.displayStyle.settings.contextRealityModels.models[index];
if (!model)
return false;
vp.displayStyle.settings.contextRealityModels.delete(model);
return true;
}
}
async parseAndRun(...args) {
return this.run(args.length > 0 ? parseInt(args[0], 10) : -1);
}
}
/** Set reality model appearance override for color in display style.
* @beta
*/
export class SetRealityModelColorTool extends Tool {
static toolId = "SetRealityModelColorTool";
static get minArgs() { return 3; }
static get maxArgs() { return 4; }
async run(rgb, index) {
const vp = IModelApp.viewManager.selectedView;
if (vp === undefined)
return false;
const changed = changeRealityModelAppearanceOverrides(vp, { rgb }, index);
if (changed)
IModelApp.notifications.outputMessage(new NotifyMessageDetails(OutputMessagePriority.Info, `${appearanceChangedString(index)} set to RGB color: (${rgb.r}, ${rgb.g}, ${rgb.b})`));
return true;
}
async parseAndRun(...args) {
return this.run({ r: parseFloat(args[0]), g: parseFloat(args[1]), b: parseFloat(args[2]) }, args.length > 3 ? parseInt(args[3], 10) : -1);
}
}
/** Clear reality model appearance override in display style.
* @beta
*/
export class ClearRealityModelAppearanceOverrides extends Tool {
static toolId = "ClearRealityModelAppearanceOverrides";
static get minArgs() { return 0; }
static get maxArgs() { return 1; }
async run(index) {
const vp = IModelApp.viewManager.selectedView;
if (!vp)
return false;
const model = vp.displayStyle.settings.contextRealityModels.models[index];
if (!model)
return false;
model.appearanceOverrides = undefined;
return true;
}
async parseAndRun(...args) {
return this.run(args[0] === undefined ? -1 : parseInt(args[0], 10));
}
}
/** Attach a cesium asset from the Ion ID and key.
* @beta
*/
export class AttachCesiumAssetTool extends Tool {
static toolId = "AttachCesiumAssetTool";
static get minArgs() { return 1; }
static get maxArgs() { return 2; }
async run(assetId, requestKey) {
const vp = IModelApp.viewManager.selectedView;
if (vp === undefined)
return false;
// attach using getCesiumAssetUrl
const accessKey = requestKey ? requestKey : IModelApp.tileAdmin.cesiumIonKey ? IModelApp.tileAdmin.cesiumIonKey : "";
const props = {
tilesetUrl: getCesiumAssetUrl(assetId, accessKey),
};
// Alternative new way to attach using rdSourceKey
// const rdSourceKey = RealityDataSource.createCesiumIonAssetKey(assetId, accessKey);
// const props: ContextRealityModelProps = { rdSourceKey, tilesetUrl: getCesiumAssetUrl(assetId, accessKey) };
vp.displayStyle.attachRealityModel(props);
IModelApp.notifications.outputMessage(new NotifyMessageDetails(OutputMessagePriority.Info, `Cesium Asset #${assetId} attached`));
return true;
}
async parseAndRun(...args) {
const assetId = parseInt(args[0], 10);
return Number.isNaN(assetId) ? false : this.run(assetId, args[1]);
}
}
/** Turn on/off display of OpenStreetMap buildings
* @beta
*/
export class ToggleOSMBuildingDisplay extends Tool {
static toolId = "SetBuildingDisplay";
static get minArgs() { return 0; }
static get maxArgs() { return 2; }
async run(onOff, transparency) {
const vp = IModelApp.viewManager.selectedView;
if (vp === undefined)
return false;
if (onOff === undefined)
onOff = undefined === vp.displayStyle.getOSMBuildingRealityModel(); // Toggle current state.
const appearanceOverrides = (transparency !== undefined && transparency > 0 && transparency < 1) ? FeatureAppearance.fromJSON({ transparency }) : undefined;
vp.displayStyle.setOSMBuildingDisplay({ onOff, appearanceOverrides });
return true;
}
async parseAndRun(...args) {
const toggle = parseToggle(args[0]);
const transparency = args.length > 0 ? parseFloat(args[1]) : undefined;
return typeof toggle === "string" ? false : this.run(toggle, transparency);
}
}
//# sourceMappingURL=RealityModelTools.js.map