-
Notifications
You must be signed in to change notification settings - Fork 208
/
CloneCurvesContext.ts
66 lines (64 loc) · 2.68 KB
/
CloneCurvesContext.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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module Curve
*/
import { Transform } from "../../geometry3d/Transform";
import { BagOfCurves, CurveChain, CurveCollection } from "../CurveCollection";
import { CurvePrimitive } from "../CurvePrimitive";
import { RecursiveCurveProcessorWithStack } from "../CurveProcessor";
/**
* Algorithmic class for cloning curve collections.
* * recurse through collection nodes, building image nodes as needed and inserting clones of children.
* * for individual primitive, invoke doClone (protected) for direct clone; insert into parent
*/
export class CloneCurvesContext extends RecursiveCurveProcessorWithStack {
protected _result: CurveCollection | undefined;
private _transform: Transform | undefined;
protected constructor(transform?: Transform) {
super();
this._transform = transform;
this._result = undefined;
}
public static clone(target: CurveCollection, transform?: Transform): CurveCollection | undefined {
const context = new CloneCurvesContext(transform);
target.announceToCurveProcessor(context);
return context._result;
}
public override enter(c: CurveCollection) {
if (c instanceof CurveCollection)
super.enter(c.cloneEmptyPeer());
}
public override leave(): CurveCollection | undefined {
const result = super.leave();
if (result) {
if (this._stack.length === 0) // this should only happen once !!!
this._result = result;
else // push this result to top of stack.
this._stack[this._stack.length - 1].tryAddChild(result);
}
return result;
}
// specialized clone methods override this (and allow announceCurvePrimitive to insert to parent)
protected doClone(primitive: CurvePrimitive): CurvePrimitive | CurvePrimitive[] | undefined {
if (this._transform)
return primitive.cloneTransformed(this._transform);
return primitive.clone();
}
public override announceCurvePrimitive(primitive: CurvePrimitive, _indexInParent: number): void {
const c = this.doClone(primitive);
if (c !== undefined && this._stack.length > 0) {
const parent = this._stack[this._stack.length - 1];
if (parent instanceof CurveChain || parent instanceof BagOfCurves)
if (Array.isArray(c)) {
for (const c1 of c) {
parent.tryAddChild(c1);
}
} else {
parent.tryAddChild(c);
}
}
}
}