-
Notifications
You must be signed in to change notification settings - Fork 2
/
ElementCascadingDeleter.ts
72 lines (69 loc) · 2.35 KB
/
ElementCascadingDeleter.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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module iModels
*/
import {
ElementTreeDeleter,
ElementTreeWalkerScope,
IModelDb,
} from "@itwin/core-backend";
import { DbResult, Id64String } from "@itwin/core-bentley";
/** Deletes an element tree and code scope references starting with the specified top element. The top element is also deleted. Uses ElementCascadeDeleter.
* @param iModel The iModel
* @param topElement The parent of the sub-tree
*/
export function deleteElementTreeCascade(
iModel: IModelDb,
topElement: Id64String
): void {
const del = new ElementCascadingDeleter(iModel);
del.deleteNormalElements(topElement);
del.deleteSpecialElements();
}
/** Deletes an entire element tree, including sub-models, child elements and code scope references.
* Items are deleted in bottom-up order. Definitions and Subjects are deleted after normal elements.
* Call deleteNormalElements on each tree. Then call deleteSpecialElements.
*/
export class ElementCascadingDeleter extends ElementTreeDeleter {
protected shouldVisitCodeScopes(
_elementId: Id64String,
_scope: ElementTreeWalkerScope
) {
return true;
}
/** The main tree-walking function */
protected override processElementTree(
element: Id64String,
scope: ElementTreeWalkerScope
): void {
if (this.shouldVisitCodeScopes(element, scope)) {
this._processCodeScopes(element, scope);
}
super.processElementTree(element, scope);
}
/** Process code scope references */
private _processCodeScopes(
element: Id64String,
scope: ElementTreeWalkerScope
) {
const newScope = new ElementTreeWalkerScope(scope, element);
this._iModel.withPreparedStatement(
`
SELECT ECInstanceId
FROM bis.Element
WHERE CodeScope.id=?
AND Parent.id IS NULL
`,
(stmt) => {
stmt.bindId(1, element);
while (stmt.step() === DbResult.BE_SQLITE_ROW) {
const elementId = stmt.getValue(0).getId();
this.processElementTree(elementId, newScope);
}
}
);
}
}