@itwin/frontend-devtools
Version:
Debug menu and supporting UI widgets
157 lines • 7.07 kB
JavaScript
;
/*---------------------------------------------------------------------------------------------
* 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.SetScheduleScriptTool = exports.ReverseScheduleScriptTool = exports.QueryScheduleScriptTool = void 0;
/** @packageDocumentation
* @module Tools
*/
const core_bentley_1 = require("@itwin/core-bentley");
const core_common_1 = require("@itwin/core-common");
const core_frontend_1 = require("@itwin/core-frontend");
const ClipboardUtilities_1 = require("../ClipboardUtilities");
const parseArgs_1 = require("./parseArgs");
const DisplayStyleTools_1 = require("./DisplayStyleTools");
/** Query the schedule script JSON from an element.
* @beta
*/
class QueryScheduleScriptTool extends DisplayStyleTools_1.DisplayStyleTool {
_sourceId;
_action = "copy";
_includeElementIds = false;
_countElementIds = false;
_expandElementIds = false;
static toolId = "QueryScheduleScript";
static get minArgs() { return 0; }
static get maxArgs() { return 3; }
async parse(input, vp) {
const args = (0, parseArgs_1.parseArgs)(input);
this._sourceId = args.get("i") ?? vp.displayStyle[core_frontend_1._scheduleScriptReference]?.sourceId;
if (!this._sourceId)
return false;
const action = args.get("a") ?? "";
this._action = action.length > 0 && "b" === action[0].toLowerCase() ? "break" : "copy";
this._includeElementIds = this._countElementIds = this._expandElementIds = false;
const ids = args.get("e");
if (ids && ids.length > 0) {
switch (ids[0].toLowerCase()) {
case "i":
this._includeElementIds = true;
break;
case "c":
this._includeElementIds = this._countElementIds = true;
break;
case "e":
this._includeElementIds = this._expandElementIds = true;
break;
}
}
return true;
}
async execute(vp) {
if (!this._sourceId || !this._action)
return false;
const opts = {
displayStyle: { omitScheduleScriptElementIds: !this._includeElementIds },
renderTimeline: { omitScriptElementIds: !this._includeElementIds },
};
let script;
const props = await vp.iModel.elements.loadProps(this._sourceId, opts);
if (props.script)
script = JSON.parse(props.script.script);
else if (props.jsonProperties?.styles?.scheduleScript)
script = props.jsonProperties.styles.scheduleScript;
if (!script)
return false;
if (this._countElementIds || this._expandElementIds) {
for (const model of script) {
for (const elem of model.elementTimelines) {
const elemIds = typeof elem.elementIds === "string" ? core_bentley_1.CompressedId64Set.decompressArray(elem.elementIds) : elem.elementIds;
if (this._countElementIds)
elem.elementIds = elemIds.length;
else
elem.elementIds = elemIds;
}
}
}
if (this._action === "break")
debugger; // eslint-disable-line no-debugger
else
(0, ClipboardUtilities_1.copyStringToClipboard)(JSON.stringify(script, null, 2));
return true;
}
}
exports.QueryScheduleScriptTool = QueryScheduleScriptTool;
function reverseTimeline(timeline, accept) {
if (!timeline)
return;
const len = timeline.length;
for (let i = 0; i < len; i++) {
const timeEntry = timeline.getEntry(i);
const valueEntry = timeline.getEntry(len - i - 1);
(0, core_bentley_1.assert)(undefined !== timeEntry);
(0, core_bentley_1.assert)(undefined !== valueEntry);
accept(timeEntry.time, valueEntry);
}
}
/** A tool that modifies the [RenderSchedule.Script]($common), if any, associated with the selected [Viewport]($frontend) such that the entries in each
* of its [RenderSchedule.ElementTimeline]($common)s are reversed.
* @beta
*/
class ReverseScheduleScriptTool extends DisplayStyleTools_1.DisplayStyleTool {
static toolId = "ReverseScheduleScript";
async execute(vp) {
const script = vp?.displayStyle.scheduleScript;
if (!script || script.modelTimelines.some((x) => x.omitsElementIds))
return false;
const builder = new core_common_1.RenderSchedule.ScriptBuilder();
for (const modelTimeline of script.modelTimelines) {
const modelBuilder = builder.addModelTimeline(modelTimeline.modelId);
for (const elemTimeline of modelTimeline.elementTimelines) {
const elemBuilder = modelBuilder.addElementTimeline(elemTimeline.elementIds);
reverseTimeline(elemTimeline.visibility, (time, entry) => elemBuilder.addVisibility(time, entry.value, entry.interpolation));
reverseTimeline(elemTimeline.color, (time, entry) => elemBuilder.addColor(time, entry.value, entry.interpolation));
reverseTimeline(elemTimeline.transform, (time, entry) => elemBuilder.addTransform(time, entry.value, entry.components, entry.interpolation));
reverseTimeline(elemTimeline.cuttingPlane, (time, entry) => elemBuilder.addCuttingPlane(time, entry.value, entry.interpolation));
}
}
const scriptProps = builder.finish();
const newScript = core_common_1.RenderSchedule.Script.fromJSON(scriptProps);
(0, core_bentley_1.assert)(undefined !== newScript);
vp.displayStyle.scheduleScript = newScript;
return true;
}
async parse() {
return true;
}
}
exports.ReverseScheduleScriptTool = ReverseScheduleScriptTool;
/** A tool that changes or removes the [RenderSchedule.Script]($common) associated with the selected [Viewport]($frontend).
* @beta
*/
class SetScheduleScriptTool extends DisplayStyleTools_1.DisplayStyleTool {
static toolId = "SetScheduleScript";
static get minArgs() { return 0; }
static get maxArgs() { return 1; }
_script;
async parse(args) {
if (args.length === 0)
return true; // clear schedule script.
try {
this._script = core_common_1.RenderSchedule.Script.fromJSON(JSON.parse(args[0]));
}
catch (ex) {
if (ex instanceof Error)
alert(ex.toString());
}
return undefined !== this._script;
}
async execute(vp) {
vp.displayStyle.scheduleScript = this._script;
return true;
}
}
exports.SetScheduleScriptTool = SetScheduleScriptTool;
//# sourceMappingURL=ScheduleScriptTools.js.map