-
Notifications
You must be signed in to change notification settings - Fork 208
/
TransformElementsTool.ts
330 lines (269 loc) · 11.8 KB
/
TransformElementsTool.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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module Editing
*/
import { BentleyError, Id64, Id64Arg, Id64String } from "@itwin/core-bentley";
import { GeometricElementProps, IModelStatus, isPlacement2dProps, PersistentGraphicsRequestProps, Placement, Placement2d, Placement3d } from "@itwin/core-common";
import { AccuDrawHintBuilder, BeButtonEvent, DynamicsContext, ElementSetTool, GraphicBranch, IModelApp, IModelConnection, IpcApp, ModifyElementSource, NotifyMessageDetails, OutputMessagePriority, readElementGraphics, RenderGraphic, RenderGraphicOwner } from "@itwin/core-frontend";
import { Transform } from "@itwin/core-geometry";
import { editorBuiltInCmdIds } from "@itwin/editor-common";
import { EditTools } from "./EditTool";
import { basicManipulationIpc } from "./EditToolIpc";
/** Geometric element id, placement, and RenderGraphic managed by [[TransformGraphicsProvider]].
* @beta
*/
export interface TransformGraphicsData {
id: Id64String;
placement: Placement;
graphic: RenderGraphicOwner;
}
/** A class for creating and managing RenderGraphics representing geometric elements for the purpose of interactive tool dynamics.
* @beta
*/
export class TransformGraphicsProvider {
public readonly iModel: IModelConnection;
public readonly data: TransformGraphicsData[];
public readonly pending: Map<Id64String, string>;
public readonly prefix: string;
/** Chord tolerance to use to stroke the element's geometry in meters. */
public chordTolerance = 0.01;
constructor(iModel: IModelConnection, prefix: string) {
this.iModel = iModel;
this.prefix = prefix;
this.data = new Array<TransformGraphicsData>();
this.pending = new Map<Id64String, string>();
}
private getRequestId(id: Id64String): string { return `${this.prefix}-${id}`; }
private getToleranceLog10(): number { return Math.floor(Math.log10(this.chordTolerance)); }
private async createRequest(id: Id64String): Promise<TransformGraphicsData | undefined> {
const elementProps = (await this.iModel.elements.getProps(id)) as GeometricElementProps[];
if (0 === elementProps.length)
return;
const placementProps = elementProps[0].placement;
if (undefined === placementProps)
return;
const placement = isPlacement2dProps(placementProps) ? Placement2d.fromJSON(placementProps) : Placement3d.fromJSON(placementProps);
if (!placement.isValid)
return; // Ignore assembly parents w/o geometry, etc...
const requestProps: PersistentGraphicsRequestProps = {
id: this.getRequestId(id),
elementId: id,
toleranceLog10: this.getToleranceLog10(),
};
this.pending.set(id, requestProps.id); // keep track of requests so they can be cancelled...
const graphicData = await IModelApp.tileAdmin.requestElementGraphics(this.iModel, requestProps);
if (undefined === graphicData)
return;
const graphic = await readElementGraphics(graphicData, this.iModel, elementProps[0].model, placement.is3d, { noFlash: true, noHilite: true });
if (undefined === graphic)
return;
return { id, placement, graphic: IModelApp.renderSystem.createGraphicOwner(graphic) };
}
private disposeOfGraphics(): void {
this.data.forEach((data) => {
data.graphic.disposeGraphic();
});
this.data.length = 0;
}
private async cancelPendingRequests(): Promise<void> {
const requests = new Array<string>();
for (const [_key, id] of this.pending)
requests.push(id);
this.pending.clear();
if (0 === requests.length)
return;
return IpcApp.appFunctionIpc.cancelElementGraphicsRequests(this.iModel.key, requests);
}
/** Call to request a RenderGraphic for the supplied element id.
* @see [[cleanupGraphics]] Must be called when the tool exits.
*/
public async createSingleGraphic(id: Id64String): Promise<boolean> {
try {
const info = await this.createRequest(id);
if (undefined !== info?.id)
this.pending.delete(info.id);
if (undefined === info?.graphic)
return false;
this.data.push(info);
return true;
} catch {
return false;
}
}
/** Call to request RenderGraphics for the supplied element ids. Does not wait for results as
* generating graphics for a large number of elements can take time. Instead an array of [[RenderGraphicOwner]]
* is populated as requests are resolved and the current dynamics frame displays what is available.
* @see [[cleanupGraphics]] Must be called when the tool exits.
*/
public createGraphics(elements: Id64Arg): void {
if (0 === Id64.sizeOf(elements))
return;
try {
for (const id of Id64.iterable(elements)) {
const promise = this.createRequest(id);
// eslint-disable-next-line @typescript-eslint/no-floating-promises
promise.then((info) => {
if (undefined !== info?.id)
this.pending.delete(info.id);
if (undefined !== info?.graphic)
this.data.push(info);
});
}
} catch { }
}
/** Call to dispose of [[RenderGraphic]] held by [[RenderGraphicOwner]] and cancel requests that are still pending.
* @note Must be called when the tool exits to avoid leaks of graphics memory or other webgl resources.
*/
public async cleanupGraphics(): Promise<void> {
await this.cancelPendingRequests();
this.disposeOfGraphics();
}
public addSingleGraphic(graphic: RenderGraphic, transform: Transform, context: DynamicsContext): void {
const branch = new GraphicBranch(false);
branch.add(graphic);
const branchGraphic = context.createBranch(branch, transform);
context.addGraphic(branchGraphic);
}
public addGraphics(transform: Transform, context: DynamicsContext): void {
if (0 === this.data.length)
return;
const branch = new GraphicBranch(false);
for (const data of this.data)
branch.add(data.graphic);
const branchGraphic = context.createBranch(branch, transform);
context.addGraphic(branchGraphic);
}
}
/** Edit tool base class for applying a transform to element placements.
* Example of a tool that would translate elements:
* ```ts
* class SampleMoveElementsTool extends TransformElementsTool {
* public static override toolId = "SampleMoveElements";
* public static override iconSpec = "icon-move";
* protected calculateTransform(ev: BeButtonEvent): Transform | undefined {
* return (this.anchorPoint ? Transform.createTranslation(ev.point.minus(this.anchorPoint)) : undefined);
* }
* public async onRestartTool(): Promise<void> {
* const tool = new SampleMoveElementsTool();
* if (!await tool.run()) return this.exitTool();
* }
* }
* ```
* @beta
*/
export abstract class TransformElementsTool extends ElementSetTool {
protected override get allowSelectionSet(): boolean { return true; }
protected override get allowGroups(): boolean { return true; }
protected override get allowDragSelect(): boolean { return true; }
protected override get controlKeyContinuesSelection(): boolean { return true; }
protected override get wantAccuSnap(): boolean { return true; }
protected override get wantDynamics(): boolean { return true; }
protected get wantMakeCopy(): boolean { return false; }
protected get wantRepeatOperation(): boolean { return this.wantMakeCopy && !this.agenda.isEmpty; }
protected _graphicsProvider?: TransformGraphicsProvider;
protected _startedCmd?: string;
protected abstract calculateTransform(ev: BeButtonEvent): Transform | undefined;
protected async createAgendaGraphics(changed: boolean): Promise<void> {
if (changed) {
if (undefined === this._graphicsProvider)
return; // Not yet needed...
} else {
if (undefined !== this._graphicsProvider)
return; // Use existing graphics...
}
if (undefined === this._graphicsProvider)
this._graphicsProvider = new TransformGraphicsProvider(this.iModel, this.toolId);
else
await this._graphicsProvider.cleanupGraphics();
if (1 === this.agenda.length) {
await this._graphicsProvider.createSingleGraphic(this.agenda.elements[0]);
return;
}
this._graphicsProvider.createGraphics(this.agenda.elements);
}
protected async clearAgendaGraphics(): Promise<void> {
if (undefined === this._graphicsProvider)
return;
await this._graphicsProvider.cleanupGraphics();
this._graphicsProvider = undefined;
}
protected override async onAgendaModified(): Promise<void> {
await this.createAgendaGraphics(true);
}
protected override async initAgendaDynamics(): Promise<boolean> {
await this.createAgendaGraphics(false);
return super.initAgendaDynamics();
}
protected transformAgendaDynamics(transform: Transform, context: DynamicsContext): void {
if (undefined !== this._graphicsProvider)
this._graphicsProvider.addGraphics(transform, context);
}
public override onDynamicFrame(ev: BeButtonEvent, context: DynamicsContext): void {
const transform = this.calculateTransform(ev);
if (undefined === transform)
return;
this.transformAgendaDynamics(transform, context);
}
protected updateAnchorLocation(transform: Transform): void {
// Update anchor point to support creating additional copies (repeat vs. restart)...
if (undefined === this.anchorPoint)
return;
transform.multiplyPoint3d(this.anchorPoint, this.anchorPoint);
const hints = new AccuDrawHintBuilder();
hints.setOrigin(this.anchorPoint);
hints.sendHints();
}
protected async startCommand(): Promise<string> {
if (undefined !== this._startedCmd)
return this._startedCmd;
return EditTools.startCommand<string>({ commandId: editorBuiltInCmdIds.cmdBasicManipulation, iModelKey: this.iModel.key });
}
protected async replaceAgenda(newIds: Id64Arg | undefined): Promise<void> {
this.agenda.clear();
if (undefined !== newIds)
this.agenda.add(newIds);
if (this.isSelectionSetModify) {
if (this.agenda.isEmpty)
this.iModel.selectionSet.emptyAll();
else
this.iModel.selectionSet.replace(this.agenda.elements);
this.agenda.setSource(ModifyElementSource.SelectionSet);
this.setPreferredElementSource(); // Update "use selection set" flag...
}
return this.onAgendaModified();
}
protected async transformAndCopyAgenda(_transform: Transform): Promise<Id64Arg | undefined> {
return undefined;
}
protected async transformAgenda(transform: Transform): Promise<void> {
try {
this._startedCmd = await this.startCommand();
if (IModelStatus.Success === await basicManipulationIpc.transformPlacement(this.agenda.compressIds(), transform.toJSON()))
await this.saveChanges();
} catch (err) {
IModelApp.notifications.outputMessage(new NotifyMessageDetails(OutputMessagePriority.Error, BentleyError.getErrorMessage(err) || "An unknown error occurred."));
}
}
public override async processAgenda(ev: BeButtonEvent): Promise<void> {
const transform = this.calculateTransform(ev);
if (undefined === transform)
return;
if (this.wantMakeCopy)
await this.replaceAgenda(await this.transformAndCopyAgenda(transform));
else
await this.transformAgenda(transform);
this.updateAnchorLocation(transform);
}
public override async onProcessComplete(): Promise<void> {
if (this.wantRepeatOperation)
return; // Continue with current agenda instead of restarting (ex. create additional copies)
return super.onProcessComplete();
}
public override async onCleanup() {
await this.clearAgendaGraphics();
return super.onCleanup();
}
}