-
Notifications
You must be signed in to change notification settings - Fork 208
/
ViewportTools.ts
609 lines (499 loc) · 19.2 KB
/
ViewportTools.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
/*---------------------------------------------------------------------------------------------
* 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 { BeDuration } from "@itwin/core-bentley";
import { Camera, ColorDef, Hilite } from "@itwin/core-common";
import {
DrawingViewState, FlashMode, FlashSettings, FlashSettingsOptions, IModelApp, TileBoundingBoxes, Tool, Viewport,
} from "@itwin/core-frontend";
import { parseArgs } from "./parseArgs";
import { parseToggle } from "./parseToggle";
/** Base class for a tool that toggles some aspect of a Viewport.
* @beta
*/
export abstract class ViewportToggleTool extends Tool {
public static override get minArgs() { return 0; }
public static override get maxArgs() { return 1; }
protected abstract toggle(vp: Viewport, enable?: boolean): Promise<void>;
public override async run(enable?: boolean): Promise<boolean> {
const vp = IModelApp.viewManager.selectedView;
if (undefined !== vp)
await this.toggle(vp, enable);
return true;
}
public override async parseAndRun(...args: string[]): Promise<boolean> {
const enable = parseToggle(args[0]);
if (typeof enable !== "string")
await this.run(enable);
return true;
}
}
/** Freeze or unfreeze the scene for the selected viewport. While the scene is frozen, no new tiles will be selected for drawing within the viewport.
* @beta
*/
export class FreezeSceneTool extends ViewportToggleTool {
public static override toolId = "FreezeScene";
protected override async toggle(vp: Viewport, enable?: boolean) {
if (undefined === enable || enable !== vp.freezeScene)
vp.freezeScene = !vp.freezeScene;
return Promise.resolve();
}
}
const boundingVolumeNames = [
"none",
"volume",
"content",
"both",
"children",
"sphere",
"solid",
];
/** Set the tile bounding volume decorations to display in the selected viewport.
* Omitting the argument turns on Volume bounding boxes if bounding boxes are currently off; otherwise, toggles them off.
* Allowed inputs are "none", "volume", "content", "both" (volume and content), "children", and "sphere".
* @beta
*/
export class ShowTileVolumesTool extends Tool {
public static override toolId = "ShowTileVolumes";
public static override get minArgs() { return 0; }
public static override get maxArgs() { return 1; }
public override async run(boxes?: TileBoundingBoxes): Promise<boolean> {
const vp = IModelApp.viewManager.selectedView;
if (undefined === vp)
return true;
if (undefined === boxes)
boxes = TileBoundingBoxes.None === vp.debugBoundingBoxes ? TileBoundingBoxes.Volume : TileBoundingBoxes.None;
vp.debugBoundingBoxes = boxes;
return true;
}
public override async parseAndRun(...args: string[]): Promise<boolean> {
let boxes: TileBoundingBoxes | undefined;
if (0 !== args.length) {
const arg = args[0].toLowerCase();
for (let i = 0; i < boundingVolumeNames.length; i++) {
if (arg === boundingVolumeNames[i]) {
boxes = i;
break;
}
}
if (undefined === boxes)
return true;
}
return this.run(boxes);
}
}
/** Sets or unsets or flips the deactivated state of one or more tile tree references within the selected viewport.
* Deactivated tile tree references are omitted from the scene.
* This is useful for isolating particular tile trees or tiles for debugging.
* @beta
*/
export class ToggleTileTreeReferencesTool extends Tool {
public static override toolId = "ToggleTileTreeReferences";
public static override get minArgs() { return 1; }
public static override get maxArgs() { return 3; }
private _modelIds?: string | string[];
private _which?: "all" | "animated" | "primary" | "section" | number[];
private _deactivate?: boolean;
public override async parseAndRun(...args: string[]) {
const which = args[0].toLowerCase();
switch (which) {
case "all":
case "animated":
case "primary":
case "section":
this._which = which;
break;
default:
this._which = which.split(",").map((x) => Number.parseInt(x, 10)).filter((x) => !Number.isNaN(x));
}
let modelIds = args[2];
let deactivate = parseToggle(args[1]);
if (typeof deactivate !== "string") {
if (typeof deactivate === "boolean")
deactivate = !deactivate;
this._deactivate = deactivate;
} else {
modelIds = args[1];
}
if (modelIds)
this._modelIds = modelIds.toLowerCase().split(",");
return this.run();
}
public override async run(): Promise<boolean> {
const vp = IModelApp.viewManager.selectedView;
if (!vp || !this._which || !vp.view.isSpatialView())
return false;
vp.view.setTileTreeReferencesDeactivated(this._modelIds, this._deactivate, this._which);
vp.invalidateScene();
return true;
}
}
/** This tool sets the aspect ratio skew for the selected viewport.
* @beta
*/
export class SetAspectRatioSkewTool extends Tool {
public static override toolId = "SetAspectRatioSkew";
public static override get minArgs() { return 0; }
public static override get maxArgs() { return 1; }
/** This method runs the tool, setting the aspect ratio skew for the selected viewport.
* @param skew the aspect ratio (x/y) skew value; 1.0 or undefined removes any skew
*/
public override async run(skew?: number): Promise<boolean> {
if (undefined === skew)
skew = 1.0;
const vp = IModelApp.viewManager.selectedView;
if (undefined !== vp) {
vp.view.setAspectRatioSkew(skew);
vp.synchWithView();
}
return true;
}
/** Executes this tool's run method.
* @param args the first entry of this array contains the `skew` argument
* @see [[run]]
*/
public override async parseAndRun(...args: string[]): Promise<boolean> {
const skew = args.length > 0 ? parseFloat(args[0]) : 1.0;
return !Number.isNaN(skew) && this.run(skew);
}
}
/** Changes the [ModelSubCategoryHiliteMode]($frontend) for the [HiliteSet]($frontend) associated with the selected Viewport.
* @beta
*/
export class ChangeHiliteModeTool extends Tool {
public static override get minArgs() { return 1; }
public static override get maxArgs() { return 1; }
public static override toolId = "ChangeHiliteMode";
public override async run(mode?: string) {
const hilites = IModelApp.viewManager.selectedView?.iModel.hilited;
if (!hilites)
return false;
if (mode === "union" || mode === "intersection")
hilites.modelSubCategoryMode = mode;
return true;
}
public override async parseAndRun(...args: string[]) {
return this.run(args[0]);
}
}
/** Changes the selected viewport's hilite or emphasis settings.
* @beta
*/
export abstract class ChangeHiliteTool extends Tool {
public static override get minArgs() { return 0; }
public static override get maxArgs() { return 6; }
public override async run(settings?: Hilite.Settings): Promise<boolean> {
const vp = IModelApp.viewManager.selectedView;
if (undefined !== vp)
this.apply(vp, settings);
return true;
}
protected abstract apply(vp: Viewport, settings: Hilite.Settings | undefined): void;
protected abstract getCurrentSettings(vp: Viewport): Hilite.Settings;
public override async parseAndRun(...inputArgs: string[]): Promise<boolean> {
if (0 === inputArgs.length)
return this.run();
const vp = IModelApp.viewManager.selectedView;
if (undefined === vp)
return true;
const cur = this.getCurrentSettings(vp);
const colors = cur.color.colors;
let visible = cur.visibleRatio;
let hidden = cur.hiddenRatio;
let silhouette = cur.silhouette;
const args = parseArgs(inputArgs);
const parseColorComponent = (c: "r" | "g" | "b") => {
const num = args.getInteger(c);
if (undefined !== num)
colors[c] = Math.floor(Math.max(0, Math.min(255, num)));
};
parseColorComponent("r");
parseColorComponent("g");
parseColorComponent("b");
const silhouetteArg = args.getInteger("s");
if (undefined !== silhouetteArg && silhouetteArg >= Hilite.Silhouette.None && silhouetteArg <= Hilite.Silhouette.Thick)
silhouette = silhouetteArg;
const v = args.getFloat("v");
if (undefined !== v && v >= 0 && v <= 1)
visible = v;
const h = args.getFloat("h");
if (undefined !== h && h >= 0 && h <= 1)
hidden = h;
if (undefined === silhouette)
silhouette = cur.silhouette;
if (undefined === visible)
visible = cur.visibleRatio;
if (undefined === hidden)
hidden = cur.hiddenRatio;
const settings: Hilite.Settings = {
color: ColorDef.from(colors.r, colors.g, colors.b),
silhouette,
visibleRatio: visible,
hiddenRatio: hidden,
};
return this.run(settings);
}
}
/** Changes the selected viewport's hilite settings, or resets to defaults.
* @beta
*/
export class ChangeHiliteSettingsTool extends ChangeHiliteTool {
public static override toolId = "ChangeHiliteSettings";
protected getCurrentSettings(vp: Viewport) { return vp.hilite; }
protected apply(vp: Viewport, settings?: Hilite.Settings): void {
vp.hilite = undefined !== settings ? settings : new Hilite.Settings();
}
}
/** Changes the selected viewport's emphasis settings.
* @beta
*/
export class ChangeEmphasisSettingsTool extends ChangeHiliteTool {
public static override toolId = "ChangeEmphasisSettings";
protected getCurrentSettings(vp: Viewport) { return vp.emphasisSettings; }
protected apply(vp: Viewport, settings?: Hilite.Settings): void {
if (undefined !== settings)
vp.emphasisSettings = settings;
}
}
/** Changes the [FlashSettings]($frontend) for the selected [Viewport]($frontend).
* @beta
*/
export class ChangeFlashSettingsTool extends Tool {
public static override toolId = "ChangeFlashSettings";
public static override get minArgs() { return 0; }
public static override get maxArgs() { return 3; }
public override async run(settings?: FlashSettings): Promise<boolean> {
const vp = IModelApp.viewManager.selectedView;
if (vp)
vp.flashSettings = settings ?? new FlashSettings();
return true;
}
public override async parseAndRun(...inputArgs: string[]): Promise<boolean> {
const vp = IModelApp.viewManager.selectedView;
if (!vp)
return true;
if (1 === inputArgs.length && "default" === inputArgs[0].toLowerCase())
return this.run();
const options: FlashSettingsOptions = {};
const args = parseArgs(inputArgs);
const intensity = args.getFloat("i");
if (undefined !== intensity)
options.maxIntensity = intensity;
const mode = args.get("m");
if (mode) {
switch (mode[0].toLowerCase()) {
case "b":
options.litMode = FlashMode.Brighten;
break;
case "h":
options.litMode = FlashMode.Hilite;
break;
default:
return false;
}
}
const duration = args.getFloat("d");
if (undefined !== duration)
options.duration = BeDuration.fromSeconds(duration);
return this.run(vp.flashSettings.clone(options));
}
}
/** Enables or disables fade-out transparency mode for the selected viewport.
* @beta
*/
export class FadeOutTool extends ViewportToggleTool {
public static override toolId = "FadeOut";
protected override async toggle(vp: Viewport, enable?: boolean): Promise<void> {
if (undefined === enable || enable !== vp.isFadeOutActive)
vp.isFadeOutActive = !vp.isFadeOutActive;
return Promise.resolve();
}
}
/** Sets the default tile size modifier used for all viewports that don't explicitly override it.
* @beta
*/
export class DefaultTileSizeModifierTool extends Tool {
public static override toolId = "DefaultTileSizeMod";
public static override get minArgs() { return 1; }
public static override get maxArgs() { return 1; }
/** This method runs the tool, setting the default tile size modifier used for all viewports that don't explicitly override it.
* @param modifier the tile size modifier to use; if undefined, do not set modifier
*/
public override async run(modifier?: number): Promise<boolean> {
if (undefined !== modifier)
IModelApp.tileAdmin.defaultTileSizeModifier = modifier;
return true;
}
/** Executes this tool's run method with args[0] containing `modifier`.
* @see [[run]]
*/
public override async parseAndRun(...args: string[]): Promise<boolean> {
return this.run(Number.parseFloat(args[0]));
}
}
/** Sets or clears the tile size modifier override for the selected viewport.
* @beta
*/
export class ViewportTileSizeModifierTool extends Tool {
public static override toolId = "ViewportTileSizeMod";
public static override get minArgs() { return 1; }
public static override get maxArgs() { return 1; }
/** This method runs the tool, setting the tile size modifier used for the selected viewport.
* @param modifier the tile size modifier to use; if undefined, reset the modifier
*/
public override async run(modifier?: number): Promise<boolean> {
const vp = IModelApp.viewManager.selectedView;
if (undefined !== vp)
vp.setTileSizeModifier(modifier);
return true;
}
/** Executes this tool's run method with args[0] containing the `modifier` argument or the string "reset" in order to reset the modifier.
* @see [[run]]
*/
public override async parseAndRun(...args: string[]): Promise<boolean> {
const arg = args[0].toLowerCase();
const modifier = "reset" === arg ? undefined : Number.parseFloat(args[0]);
return this.run(modifier);
}
}
/** This tool adds a reality model to the viewport.
* @beta
*/
export class ViewportAddRealityModel extends Tool {
public static override toolId = "ViewportAddRealityModel";
public static override get minArgs() { return 1; }
public static override get maxArgs() { return 1; }
/** This method runs the tool, adding a reality model to the viewport
* @param url the URL which points to the reality model tileset
*/
public override async run(url: string): Promise<boolean> {
const vp = IModelApp.viewManager.selectedView;
if (undefined !== vp)
vp.displayStyle.attachRealityModel({ tilesetUrl: url });
return true;
}
/** Executes this tool's run method with args[0] containing the `url` argument.
* @see [[run]]
*/
public override async parseAndRun(...args: string[]): Promise<boolean> {
return this.run(args[0]);
}
}
/** Changes the `allow3dManipulations` flag for the selected viewport if the viewport is displaying a `ViewState3d`.
* @beta
*/
export class Toggle3dManipulationsTool extends ViewportToggleTool {
public static override toolId = "Toggle3dManipulations";
protected override async toggle(vp: Viewport, allow?: boolean): Promise<void> {
if (!vp.view.is3d())
return Promise.resolve();
if (undefined === allow)
allow = !vp.view.allow3dManipulations();
if (allow !== vp.view.allow3dManipulations()) {
vp.view.setAllow3dManipulations(allow);
// eslint-disable-next-line @typescript-eslint/no-floating-promises
IModelApp.toolAdmin.startDefaultTool();
}
return Promise.resolve();
}
}
/** Toggles display of view attachments in sheet views.
* @beta
*/
export class ToggleViewAttachmentsTool extends ViewportToggleTool {
public static override toolId = "ToggleViewAttachments";
protected override async toggle(vp: Viewport, enable?: boolean): Promise<void> {
if (undefined === enable || enable !== vp.wantViewAttachments)
vp.wantViewAttachments = !vp.wantViewAttachments;
return Promise.resolve();
}
}
/** Toggle display of view attachment boundaries in sheet views.
* @beta
*/
export class ToggleViewAttachmentBoundariesTool extends ViewportToggleTool {
public static override toolId = "ToggleViewAttachmentBoundaries";
protected override async toggle(vp: Viewport, enable?: boolean): Promise<void> {
if (undefined === enable || enable !== vp.wantViewAttachmentBoundaries)
vp.wantViewAttachmentBoundaries = !vp.wantViewAttachmentBoundaries;
return Promise.resolve();
}
}
/** Toggle display of view attachment clip shapes in sheet views.
* @beta
*/
export class ToggleViewAttachmentClipShapesTool extends ViewportToggleTool {
public static override toolId = "ToggleViewAttachmentClipShapes";
protected override async toggle(vp: Viewport, enable?: boolean): Promise<void> {
if (undefined === enable || enable !== vp.wantViewAttachmentClipShapes)
vp.wantViewAttachmentClipShapes = !vp.wantViewAttachmentClipShapes;
return Promise.resolve();
}
}
/** Toggles display of 2d graphics in a [DrawingViewState]($frontend). This setting affects all drawing views until it is reset.
* @beta
*/
export class ToggleDrawingGraphicsTool extends ViewportToggleTool {
public static override toolId = "ToggleDrawingGraphics";
protected override async toggle(vp: Viewport, enable?: boolean): Promise<void> {
if (undefined === enable || enable !== DrawingViewState.hideDrawingGraphics) {
DrawingViewState.hideDrawingGraphics = !DrawingViewState.hideDrawingGraphics;
vp.invalidateScene();
}
return Promise.resolve();
}
}
/** Toggles whether a [SectionDrawing]($backend)'s spatial view is always displayed along with the 2d graphics by a [DrawingViewState]($frontend), even
* if it otherwise would not be. This setting affects all section drawing views until it is reset.
* @beta
*/
export class ToggleSectionDrawingSpatialViewTool extends ViewportToggleTool {
public static override toolId = "ToggleSectionDrawingSpatialView";
protected async toggle(vp: Viewport, enable?: boolean): Promise<void> {
if (undefined === enable || enable !== DrawingViewState.alwaysDisplaySpatialView) {
DrawingViewState.alwaysDisplaySpatialView = !DrawingViewState.alwaysDisplaySpatialView;
if (vp.view instanceof DrawingViewState) {
// Force the view to update its section drawing attachment.
const view = vp.view.clone();
await view.changeViewedModel(view.baseModelId);
await view.load();
vp.changeView(view);
}
}
}
}
/** Change the camera settings of the selected viewport.
* @beta
*/
export class ChangeCameraTool extends Tool {
public static override get minArgs() { return 1; }
public static override get maxArgs() { return 2; }
public static override toolId = "ChangeCamera";
public override async run(camera?: Camera): Promise<boolean> {
const vp = IModelApp.viewManager.selectedView;
if (camera && vp && vp.view.is3d()) {
const view = vp.view.clone();
view.camera.setFrom(camera);
vp.changeView(view);
}
return true;
}
public override async parseAndRun(...inArgs: string[]): Promise<boolean> {
const vp = IModelApp.viewManager.selectedView;
if (!vp || !vp.view.is3d())
return false;
const camera = vp.view.camera.clone();
const args = parseArgs(inArgs);
const lens = args.getFloat("l");
if (undefined !== lens)
camera.lens.setDegrees(lens);
const focusDist = args.getFloat("d");
if (undefined !== focusDist)
camera.focusDist = focusDist;
return this.run(camera);
}
}