@inweb/viewer-visualize 25.3.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (164) hide show
  1. package/LICENSE +20 -0
  2. package/README.md +63 -0
  3. package/dist/viewer-visualize.js +16982 -0
  4. package/dist/viewer-visualize.js.map +1 -0
  5. package/dist/viewer-visualize.min.js +1 -0
  6. package/dist/viewer-visualize.module.js +5267 -0
  7. package/dist/viewer-visualize.module.js.map +1 -0
  8. package/lib/Viewer/Commands/ApplyModelTransform.d.ts +1 -0
  9. package/lib/Viewer/Commands/ClearMarkup.d.ts +1 -0
  10. package/lib/Viewer/Commands/ClearSlices.d.ts +1 -0
  11. package/lib/Viewer/Commands/CreatePreview.d.ts +1 -0
  12. package/lib/Viewer/Commands/Explode.d.ts +1 -0
  13. package/lib/Viewer/Commands/GetDefaultViewPositions.d.ts +1 -0
  14. package/lib/Viewer/Commands/GetModels.d.ts +1 -0
  15. package/lib/Viewer/Commands/GetSelected.d.ts +1 -0
  16. package/lib/Viewer/Commands/HideSelected.d.ts +1 -0
  17. package/lib/Viewer/Commands/IsolateSelected.d.ts +1 -0
  18. package/lib/Viewer/Commands/RegenerateAll.d.ts +1 -0
  19. package/lib/Viewer/Commands/ResetView.d.ts +1 -0
  20. package/lib/Viewer/Commands/SelectModel.d.ts +1 -0
  21. package/lib/Viewer/Commands/SetActiveDragger.d.ts +1 -0
  22. package/lib/Viewer/Commands/SetDefaultViewPosition.d.ts +1 -0
  23. package/lib/Viewer/Commands/SetMarkupColor.d.ts +1 -0
  24. package/lib/Viewer/Commands/SetSelected.d.ts +1 -0
  25. package/lib/Viewer/Commands/ShowAll.d.ts +1 -0
  26. package/lib/Viewer/Commands/Unselect.d.ts +1 -0
  27. package/lib/Viewer/Commands/ZoomToExtents.d.ts +1 -0
  28. package/lib/Viewer/Commands/ZoomToObjects.d.ts +1 -0
  29. package/lib/Viewer/Commands/ZoomToSelected.d.ts +1 -0
  30. package/lib/Viewer/Commands/index.d.ts +22 -0
  31. package/lib/Viewer/Draggers/Actions/OrbitAction.d.ts +21 -0
  32. package/lib/Viewer/Draggers/Actions/PanAction.d.ts +17 -0
  33. package/lib/Viewer/Draggers/Actions/ZoomAction.d.ts +7 -0
  34. package/lib/Viewer/Draggers/Common/Geometry.d.ts +114 -0
  35. package/lib/Viewer/Draggers/Common/GestureManager.d.ts +40 -0
  36. package/lib/Viewer/Draggers/Common/OdBaseDragger.d.ts +53 -0
  37. package/lib/Viewer/Draggers/Common/OdaGeAction.d.ts +29 -0
  38. package/lib/Viewer/Draggers/MeasureLineDragger/MeasureLineItem.d.ts +30 -0
  39. package/lib/Viewer/Draggers/MeasureLineDragger/MeasureUtils.d.ts +19 -0
  40. package/lib/Viewer/Draggers/MeasureLineDragger/index.d.ts +23 -0
  41. package/lib/Viewer/Draggers/OdBaseCuttingPlaneDragger.d.ts +26 -0
  42. package/lib/Viewer/Draggers/OdCuttingPlaneXAxisDragger.d.ts +7 -0
  43. package/lib/Viewer/Draggers/OdCuttingPlaneYAxisDragger.d.ts +7 -0
  44. package/lib/Viewer/Draggers/OdCuttingPlaneZAxisDragger.d.ts +7 -0
  45. package/lib/Viewer/Draggers/OdOrbitDragger.d.ts +14 -0
  46. package/lib/Viewer/Draggers/OdPanDragger.d.ts +11 -0
  47. package/lib/Viewer/Draggers/OdZoomDragger.d.ts +11 -0
  48. package/lib/Viewer/Draggers/OdZoomWheelDragger.d.ts +10 -0
  49. package/lib/Viewer/Draggers/OdZoomWindowDragger/OdSelectionFrame.d.ts +18 -0
  50. package/lib/Viewer/Draggers/OdZoomWindowDragger/index.d.ts +13 -0
  51. package/lib/Viewer/Draggers/OdaLineDragger.d.ts +14 -0
  52. package/lib/Viewer/Draggers/OdaTextDragger.d.ts +15 -0
  53. package/lib/Viewer/Draggers/OdaWalkDragger.d.ts +29 -0
  54. package/lib/Viewer/Draggers/OrbitAroundBuildingDragger.d.ts +18 -0
  55. package/lib/Viewer/Loaders/BaseLoader.d.ts +10 -0
  56. package/lib/Viewer/Loaders/LoaderFactory.d.ts +10 -0
  57. package/lib/Viewer/Loaders/TCSLoader.d.ts +4 -0
  58. package/lib/Viewer/Loaders/UpdaterController.d.ts +14 -0
  59. package/lib/Viewer/Loaders/VsfXLoader.d.ts +4 -0
  60. package/lib/Viewer/Loaders/VsfXPartialLoader.d.ts +4 -0
  61. package/lib/Viewer/Loaders/VsfXStreamingLoader.d.ts +4 -0
  62. package/lib/Viewer/Markup/Api/IMarkupArrow.d.ts +21 -0
  63. package/lib/Viewer/Markup/Api/IMarkupCloud.d.ts +14 -0
  64. package/lib/Viewer/Markup/Api/IMarkupColorable.d.ts +4 -0
  65. package/lib/Viewer/Markup/Api/IMarkupEllipse.d.ts +14 -0
  66. package/lib/Viewer/Markup/Api/IMarkupImage.d.ts +14 -0
  67. package/lib/Viewer/Markup/Api/IMarkupLine.d.ts +12 -0
  68. package/lib/Viewer/Markup/Api/IMarkupObject.d.ts +11 -0
  69. package/lib/Viewer/Markup/Api/IMarkupRectangle.d.ts +14 -0
  70. package/lib/Viewer/Markup/Api/IMarkupText.d.ts +12 -0
  71. package/lib/Viewer/Markup/Api/Impl/Konva/KonvaArrow.d.ts +46 -0
  72. package/lib/Viewer/Markup/Api/Impl/Konva/KonvaCloud.d.ts +35 -0
  73. package/lib/Viewer/Markup/Api/Impl/Konva/KonvaEllipse.d.ts +40 -0
  74. package/lib/Viewer/Markup/Api/Impl/Konva/KonvaImage.d.ts +36 -0
  75. package/lib/Viewer/Markup/Api/Impl/Konva/KonvaLine.d.ts +36 -0
  76. package/lib/Viewer/Markup/Api/Impl/Konva/KonvaRectangle.d.ts +38 -0
  77. package/lib/Viewer/Markup/Api/Impl/Konva/KonvaText.d.ts +37 -0
  78. package/lib/Viewer/Markup/IMarkup.d.ts +39 -0
  79. package/lib/Viewer/Markup/Impl/Konva/KonvaMarkup.d.ts +70 -0
  80. package/lib/Viewer/Markup/Impl/Konva/MarkupColor.d.ts +18 -0
  81. package/lib/Viewer/Markup/Impl/Visualize/VisualizeMarkup.d.ts +33 -0
  82. package/lib/Viewer/Markup/MarkupFactory.d.ts +6 -0
  83. package/lib/Viewer/Viewer.d.ts +346 -0
  84. package/lib/Viewer/utils.d.ts +3 -0
  85. package/lib/index.d.ts +3 -0
  86. package/package.json +44 -0
  87. package/src/Viewer/Commands/ApplyModelTransform.ts +69 -0
  88. package/src/Viewer/Commands/ClearMarkup.ts +28 -0
  89. package/src/Viewer/Commands/ClearSlices.ts +27 -0
  90. package/src/Viewer/Commands/CreatePreview.ts +33 -0
  91. package/src/Viewer/Commands/Explode.ts +38 -0
  92. package/src/Viewer/Commands/GetDefaultViewPositions.ts +36 -0
  93. package/src/Viewer/Commands/GetModels.ts +43 -0
  94. package/src/Viewer/Commands/GetSelected.ts +58 -0
  95. package/src/Viewer/Commands/HideSelected.ts +37 -0
  96. package/src/Viewer/Commands/IsolateSelected.ts +37 -0
  97. package/src/Viewer/Commands/RegenerateAll.ts +37 -0
  98. package/src/Viewer/Commands/ResetView.ts +43 -0
  99. package/src/Viewer/Commands/SelectModel.ts +52 -0
  100. package/src/Viewer/Commands/SetActiveDragger.ts +29 -0
  101. package/src/Viewer/Commands/SetDefaultViewPosition.ts +50 -0
  102. package/src/Viewer/Commands/SetMarkupColor.ts +29 -0
  103. package/src/Viewer/Commands/SetSelected.ts +47 -0
  104. package/src/Viewer/Commands/ShowAll.ts +37 -0
  105. package/src/Viewer/Commands/Unselect.ts +37 -0
  106. package/src/Viewer/Commands/ZoomToExtents.ts +43 -0
  107. package/src/Viewer/Commands/ZoomToObjects.ts +47 -0
  108. package/src/Viewer/Commands/ZoomToSelected.ts +39 -0
  109. package/src/Viewer/Commands/index.ts +45 -0
  110. package/src/Viewer/Draggers/Actions/OrbitAction.ts +250 -0
  111. package/src/Viewer/Draggers/Actions/PanAction.ts +102 -0
  112. package/src/Viewer/Draggers/Actions/ZoomAction.ts +45 -0
  113. package/src/Viewer/Draggers/Common/Geometry.ts +152 -0
  114. package/src/Viewer/Draggers/Common/GestureManager.ts +263 -0
  115. package/src/Viewer/Draggers/Common/OdBaseDragger.ts +270 -0
  116. package/src/Viewer/Draggers/Common/OdaGeAction.ts +146 -0
  117. package/src/Viewer/Draggers/MeasureLineDragger/MeasureLineItem.ts +248 -0
  118. package/src/Viewer/Draggers/MeasureLineDragger/MeasureUtils.ts +182 -0
  119. package/src/Viewer/Draggers/MeasureLineDragger/index.ts +166 -0
  120. package/src/Viewer/Draggers/OdBaseCuttingPlaneDragger.ts +182 -0
  121. package/src/Viewer/Draggers/OdCuttingPlaneXAxisDragger.ts +53 -0
  122. package/src/Viewer/Draggers/OdCuttingPlaneYAxisDragger.ts +53 -0
  123. package/src/Viewer/Draggers/OdCuttingPlaneZAxisDragger.ts +53 -0
  124. package/src/Viewer/Draggers/OdOrbitDragger.ts +70 -0
  125. package/src/Viewer/Draggers/OdPanDragger.ts +62 -0
  126. package/src/Viewer/Draggers/OdZoomDragger.ts +59 -0
  127. package/src/Viewer/Draggers/OdZoomWheelDragger.ts +103 -0
  128. package/src/Viewer/Draggers/OdZoomWindowDragger/OdSelectionFrame.ts +123 -0
  129. package/src/Viewer/Draggers/OdZoomWindowDragger/index.ts +75 -0
  130. package/src/Viewer/Draggers/OdaLineDragger.ts +80 -0
  131. package/src/Viewer/Draggers/OdaTextDragger.ts +118 -0
  132. package/src/Viewer/Draggers/OdaWalkDragger.ts +278 -0
  133. package/src/Viewer/Draggers/OrbitAroundBuildingDragger.ts +184 -0
  134. package/src/Viewer/Loaders/BaseLoader.ts +40 -0
  135. package/src/Viewer/Loaders/LoaderFactory.ts +48 -0
  136. package/src/Viewer/Loaders/TCSLoader.ts +82 -0
  137. package/src/Viewer/Loaders/UpdaterController.ts +36 -0
  138. package/src/Viewer/Loaders/VsfXLoader.ts +65 -0
  139. package/src/Viewer/Loaders/VsfXPartialLoader.ts +208 -0
  140. package/src/Viewer/Loaders/VsfXStreamingLoader.ts +87 -0
  141. package/src/Viewer/Markup/Api/IMarkupArrow.ts +12 -0
  142. package/src/Viewer/Markup/Api/IMarkupCloud.ts +15 -0
  143. package/src/Viewer/Markup/Api/IMarkupColorable.ts +4 -0
  144. package/src/Viewer/Markup/Api/IMarkupEllipse.ts +15 -0
  145. package/src/Viewer/Markup/Api/IMarkupImage.ts +15 -0
  146. package/src/Viewer/Markup/Api/IMarkupLine.ts +13 -0
  147. package/src/Viewer/Markup/Api/IMarkupObject.ts +15 -0
  148. package/src/Viewer/Markup/Api/IMarkupRectangle.ts +15 -0
  149. package/src/Viewer/Markup/Api/IMarkupText.ts +12 -0
  150. package/src/Viewer/Markup/Api/Impl/Konva/KonvaArrow.ts +117 -0
  151. package/src/Viewer/Markup/Api/Impl/Konva/KonvaCloud.ts +208 -0
  152. package/src/Viewer/Markup/Api/Impl/Konva/KonvaEllipse.ts +118 -0
  153. package/src/Viewer/Markup/Api/Impl/Konva/KonvaImage.ts +123 -0
  154. package/src/Viewer/Markup/Api/Impl/Konva/KonvaLine.ts +131 -0
  155. package/src/Viewer/Markup/Api/Impl/Konva/KonvaRectangle.ts +119 -0
  156. package/src/Viewer/Markup/Api/Impl/Konva/KonvaText.ts +110 -0
  157. package/src/Viewer/Markup/IMarkup.ts +42 -0
  158. package/src/Viewer/Markup/Impl/Konva/KonvaMarkup.ts +1144 -0
  159. package/src/Viewer/Markup/Impl/Konva/MarkupColor.ts +39 -0
  160. package/src/Viewer/Markup/Impl/Visualize/VisualizeMarkup.ts +229 -0
  161. package/src/Viewer/Markup/MarkupFactory.ts +32 -0
  162. package/src/Viewer/Viewer.ts +1183 -0
  163. package/src/Viewer/utils.ts +74 -0
  164. package/src/index.ts +26 -0
@@ -0,0 +1,1183 @@
1
+ ///////////////////////////////////////////////////////////////////////////////
2
+ // Copyright (C) 2002-2023, Open Design Alliance (the "Alliance").
3
+ // All rights reserved.
4
+ //
5
+ // This software and its documentation and related materials are owned by
6
+ // the Alliance. The software may only be incorporated into application
7
+ // programs owned by members of the Alliance, subject to a signed
8
+ // Membership Agreement and Supplemental Software License Agreement with the
9
+ // Alliance. The structure and organization of this software are the valuable
10
+ // trade secrets of the Alliance and its suppliers. The software is also
11
+ // protected by copyright law and international treaty provisions. Application
12
+ // programs incorporating this software must include the following statement
13
+ // with their copyright notices:
14
+ //
15
+ // This application incorporates Open Design Alliance software pursuant to a
16
+ // license agreement with Open Design Alliance.
17
+ // Open Design Alliance Copyright (C) 2002-2021 by Open Design Alliance.
18
+ // All rights reserved.
19
+ //
20
+ // By use of this software, its documentation or related materials, you
21
+ // acknowledge and accept the above terms.
22
+ ///////////////////////////////////////////////////////////////////////////////
23
+
24
+ import { EventEmitter2 } from "@inweb/eventemitter2";
25
+ import { Assembly, Client, File, Model } from "@inweb/client";
26
+ import {
27
+ CANVAS_EVENTS,
28
+ commands,
29
+ IClippingPlane,
30
+ IOrthogonalCamera,
31
+ IViewpoint,
32
+ IViewer,
33
+ Options,
34
+ OptionsData,
35
+ OptionsEventMap,
36
+ ViewerEventMap,
37
+ } from "@inweb/viewer-core";
38
+
39
+ import { OdBaseDragger } from "./Draggers/Common/OdBaseDragger";
40
+ import { MeasureLineDragger } from "./Draggers/MeasureLineDragger/index";
41
+ import { OdaWalkDragger } from "./Draggers/OdaWalkDragger";
42
+ import { OdCuttingPlaneXAxisDragger } from "./Draggers/OdCuttingPlaneXAxisDragger";
43
+ import { OdCuttingPlaneYAxisDragger } from "./Draggers/OdCuttingPlaneYAxisDragger";
44
+ import { OdCuttingPlaneZAxisDragger } from "./Draggers/OdCuttingPlaneZAxisDragger";
45
+ import { OdOrbitDragger } from "./Draggers/OdOrbitDragger";
46
+ import { OdPanDragger } from "./Draggers/OdPanDragger";
47
+ import { OdZoomDragger } from "./Draggers/OdZoomDragger";
48
+ import { OdZoomWheelDragger } from "./Draggers/OdZoomWheelDragger";
49
+ import { OdZoomWindowDragger } from "./Draggers/OdZoomWindowDragger/index";
50
+ import { OrbitAroundBuildingDragger } from "./Draggers/OrbitAroundBuildingDragger";
51
+ import { GestureManager } from "./Draggers/Common/GestureManager";
52
+
53
+ import { loadVisualizeJs } from "./utils";
54
+ import { LoaderFactory } from "./Loaders/LoaderFactory";
55
+ import { MarkupFactory } from "./Markup/MarkupFactory";
56
+ import { IMarkup, MarkupType } from "./Markup/IMarkup";
57
+
58
+ const OVERLAY_VIEW_NAME = "$OVERLAY_VIEW_NAME";
59
+
60
+ const isExist = (value) => value !== undefined && value !== null;
61
+
62
+ /**
63
+ * The `Client.js` library class that provides methods to integrate with the
64
+ * [VisualizeJS](https://cloud.opendesign.com/docs/index.html#/visualizejs) library.
65
+ */
66
+
67
+ export class Viewer extends EventEmitter2<ViewerEventMap & OptionsEventMap> implements IViewer {
68
+ private _activeDragger: OdBaseDragger | null;
69
+ private _zoomWheelDragger: OdZoomWheelDragger | null;
70
+ private _gestureManager: GestureManager | null;
71
+ private _enableAutoUpdate: boolean;
72
+ private _isNeedRender: boolean;
73
+ private _isRunAsyncUpdate: boolean;
74
+ private _renderTime: DOMHighResTimeStamp;
75
+
76
+ protected _options: Options;
77
+ protected _visualizeJsUrl: string;
78
+ protected _abortControllerForReferences: AbortController | undefined;
79
+
80
+ private canvaseventlistener: (event: Event) => void;
81
+
82
+ public draggerFactory: Map<string, typeof OdBaseDragger>;
83
+ public canvasEvents: string[];
84
+ private frameId: number;
85
+ private _resizeObserver: ResizeObserver | undefined;
86
+ public canvas: HTMLCanvasElement | undefined;
87
+ public markup: IMarkup;
88
+
89
+ public visualizeJs: any;
90
+ public _abortController: AbortController | undefined;
91
+ public _abortControllerForRequestMap: Map<string, AbortController> | undefined;
92
+ public client: Client;
93
+
94
+ /**
95
+ * @param client - The `Client` instance that provides access to a server. Do not specify
96
+ * `Client` if you need a standalone viewer instance without access to server models.
97
+ * @param params - An object containing viewer configuration parameters.
98
+ * @param params.visualizeJsUrl - `VisualizeJS` library URL. Set this URL to use your own
99
+ * library instance, or leave it undefined or blank to use the default URL defined by
100
+ * `Client.js` you are using.
101
+ *
102
+ * _Note: Your own `VisualizeJS` library version must match the version of the `Client.js`
103
+ * you are using._
104
+ * @param params.enableAutoUpdate - Enable auto-update of the viewer after any changes. If
105
+ * the auto-update is disabled, you need to update the `VisualizeJS` viewer and the active
106
+ * dragger manually using the `update` event. Default is `true`.
107
+ */
108
+ constructor(
109
+ client?: Client,
110
+ params: { visualizeJsUrl?: string; enableAutoUpdate?: boolean; markupType?: MarkupType } = {
111
+ markupType: MarkupType.Konva,
112
+ }
113
+ ) {
114
+ super();
115
+ this.configure(params);
116
+
117
+ this._options = new Options(this);
118
+
119
+ this.client = client;
120
+
121
+ this._activeDragger = null;
122
+ this._renderTime = 0;
123
+
124
+ this.markup = MarkupFactory.createMarkup(params.markupType ?? MarkupType.Konva);
125
+
126
+ this.draggerFactory = new Map<string, typeof OdBaseDragger>();
127
+ this.registerDragger("Pan", OdPanDragger);
128
+ this.registerDragger("Orbit", OdOrbitDragger);
129
+ this.registerDragger("Zoom", OdZoomDragger);
130
+ this.registerDragger("ZoomWindow", OdZoomWindowDragger);
131
+ this.registerDragger("OrbitAroundBuilding", OrbitAroundBuildingDragger);
132
+ this.registerDragger("MeasureLine", MeasureLineDragger);
133
+ this.registerDragger("CuttingPlaneXAxis", OdCuttingPlaneXAxisDragger);
134
+ this.registerDragger("CuttingPlaneYAxis", OdCuttingPlaneYAxisDragger);
135
+ this.registerDragger("CuttingPlaneZAxis", OdCuttingPlaneZAxisDragger);
136
+ this.registerDragger("Walk", OdaWalkDragger);
137
+
138
+ const markupDraggers = this.markup.getDraggers();
139
+ markupDraggers?.forEach((value, key) => {
140
+ this.registerDragger(key, value);
141
+ });
142
+
143
+ this.canvasEvents = CANVAS_EVENTS;
144
+ this.canvaseventlistener = (event: Event) => this.emit(event);
145
+
146
+ this._enableAutoUpdate = params.enableAutoUpdate ?? true;
147
+ this._isNeedRender = false;
148
+ this._isRunAsyncUpdate = false;
149
+
150
+ this.render = this.render.bind(this);
151
+ this.resize = this.resize.bind(this);
152
+ }
153
+
154
+ /**
155
+ * `VisualizeJS` parameters.
156
+ */
157
+ get options(): Options {
158
+ return this._options;
159
+ }
160
+
161
+ /**
162
+ * `VisualizeJS` library URL. Use {@link Viewer#configure | configure()} to change library URL.
163
+ *
164
+ * @readonly
165
+ */
166
+ get visualizeJsUrl(): string {
167
+ return this._visualizeJsUrl;
168
+ }
169
+
170
+ /**
171
+ * Change the viewer configuration parameters.
172
+ *
173
+ * @param params - An object containing new configuration parameters.
174
+ * @param params.visualizeJsUrl - `VisualizeJS` library URL. Set this URL to use your own
175
+ * library instance or leave it blank to use the default URL defined by `Client.js`.
176
+ */
177
+ configure(params: { visualizeJsUrl?: string }): this {
178
+ this._visualizeJsUrl = params.visualizeJsUrl || "VISUALIZE_JS_URL";
179
+ return this;
180
+ }
181
+
182
+ /**
183
+ * A [Viewer]{@link Viewer} event measuring progress of loading a `VisualizeJS` library.
184
+ *
185
+ * @property {string} type - `visualizeprogress`
186
+ * @property {number} loaded - A 64-bit unsigned integer value indicating the amount of work
187
+ * already performed by the underlying process. The ratio of work done can be calculated by
188
+ * dividing total by the value of this property.
189
+ * @property {number} total - A 64-bit unsigned integer representing the total amount of work
190
+ * that the underlying process is in the progress of performing.
191
+ * @event visualizeprogress
192
+ */
193
+
194
+ /**
195
+ * Load `VisualizeJS` module and initialize it with the specified canvas. Call
196
+ * {@link Viewer#dispose | dispose()} to release allocated resources.
197
+ *
198
+ * @async
199
+ * @param canvas - HTML `<canvas>` element for `VisualizeJS`.
200
+ * @param onProgress - A callback function that handles events measuring progress of loading
201
+ * of the `VisualizeJS` library. Retrieves {@link event:visualizeprogress | visualizeprogress} event.
202
+ */
203
+ async initialize(canvas: HTMLCanvasElement, onProgress?: (event: ProgressEvent) => void): Promise<this> {
204
+ this.addEventListener("optionschange", (event) => this.syncOptions(event.data));
205
+
206
+ if (canvas.style.width === "" && canvas.style.height === "") {
207
+ canvas.style.width = "100%";
208
+ canvas.style.height = "100%";
209
+ }
210
+ canvas.parentElement.style.touchAction = "none";
211
+ canvas.style.touchAction = "none";
212
+
213
+ canvas.width = canvas.clientWidth * window.devicePixelRatio;
214
+ canvas.height = canvas.clientHeight * window.devicePixelRatio;
215
+
216
+ const visualizeJs: any = await loadVisualizeJs(this.visualizeJsUrl, (ev) => {
217
+ const { loaded, timeStamp, total, lengthComputable } = ev;
218
+ const event = { loaded, timeStamp, total, lengthComputable, type: "visualizeprogress" } as ProgressEvent;
219
+ onProgress?.(event);
220
+ });
221
+
222
+ this.visualizeJs = visualizeJs;
223
+ this.visualizeJs.canvas = canvas;
224
+ this.visualizeJs.Viewer.create();
225
+
226
+ this.canvas = canvas;
227
+ this.canvasEvents.forEach((x) => canvas.addEventListener(x, this.canvaseventlistener));
228
+
229
+ this.markup.initialize(this, canvas, this.canvasEvents);
230
+
231
+ this._resizeObserver = new ResizeObserver(this.resize);
232
+ this._resizeObserver.observe(canvas.parentElement);
233
+ this.resize();
234
+
235
+ this._zoomWheelDragger = new OdZoomWheelDragger(this);
236
+ this._zoomWheelDragger.name = "ZoomWheel";
237
+ this._zoomWheelDragger.initialize();
238
+
239
+ this._gestureManager = new GestureManager(this);
240
+ this._gestureManager.initialize();
241
+
242
+ this.syncOpenCloudVisualStyle(true);
243
+ this.syncOptions();
244
+
245
+ this._renderTime = performance.now();
246
+ this.render(this._renderTime);
247
+
248
+ return this;
249
+ }
250
+
251
+ /**
252
+ * Releases all resources allocated by this `Viewer` instance. Call this method before
253
+ * release the `Viewer` instance.
254
+ */
255
+ dispose(): this {
256
+ this.cancel();
257
+ this.emitEvent({ type: "dispose" });
258
+
259
+ if (this.frameId) cancelAnimationFrame(this.frameId);
260
+ this.frameId = 0;
261
+
262
+ this.setActiveDragger("");
263
+
264
+ this._zoomWheelDragger?.dispose();
265
+ this._gestureManager?.dispose();
266
+
267
+ this.removeAllListeners();
268
+
269
+ this._resizeObserver?.disconnect();
270
+ this._resizeObserver = undefined;
271
+
272
+ this.markup.dispose();
273
+
274
+ this.canvasEvents.forEach((x) => this.canvas?.removeEventListener(x, this.canvaseventlistener));
275
+ this.canvas = undefined;
276
+
277
+ this.visualizeJs?.getViewer().clear();
278
+ this.visualizeJs = undefined;
279
+
280
+ return this;
281
+ }
282
+
283
+ /**
284
+ * Returns `true` if `VisualizeJS` module has been loaded andinitialized.
285
+ */
286
+ isInitialized(): boolean {
287
+ return !!this.visualizeJs;
288
+ }
289
+
290
+ // internal render/resize routines
291
+
292
+ private render(time: DOMHighResTimeStamp) {
293
+ this.frameId = requestAnimationFrame(this.render);
294
+
295
+ if (!this.visualizeJs) return;
296
+
297
+ if (this._isRunAsyncUpdate) return;
298
+
299
+ const visViewer = this.visualizeJs.getViewer();
300
+ if (visViewer.isRunningAnimation() || this._isNeedRender) {
301
+ visViewer.update();
302
+ this._activeDragger?.updatePreview();
303
+ this._isNeedRender = !visViewer.getActiveDevice().isValid();
304
+
305
+ const deltaTime = (time - this._renderTime) / 1000;
306
+ this._renderTime = time;
307
+ this.emitEvent({ type: "render", time, deltaTime });
308
+ }
309
+ }
310
+
311
+ public resize(): this {
312
+ if (!this.visualizeJs) return this;
313
+
314
+ const { clientWidth, clientHeight } = this.canvas;
315
+ this.canvas.width = clientWidth * window.devicePixelRatio;
316
+ this.canvas.height = clientHeight * window.devicePixelRatio;
317
+
318
+ const visViewer = this.visualizeJs.getViewer();
319
+ visViewer.resize(0, this.canvas.width, this.canvas.height, 0);
320
+
321
+ this.update(true);
322
+ this.emitEvent({ type: "resize", width: clientWidth, height: clientHeight });
323
+
324
+ return this;
325
+ }
326
+
327
+ /**
328
+ * Updates the viewer. Do nothing if the auto-update mode is disabled in the constructor (use
329
+ * the `update` event to update viewer manually).
330
+ *
331
+ * Fires:
332
+ *
333
+ * - {@link UpdateEvent | update}
334
+ *
335
+ * @param force - If `true` updates the viewer immidietly. Otherwise updates on next
336
+ * animation frame. Default is `false`.
337
+ */
338
+ update(force?: boolean) {
339
+ if (this._enableAutoUpdate) {
340
+ if (force) {
341
+ this.visViewer()?.update();
342
+ this.activeDragger()?.updatePreview();
343
+ } else {
344
+ this._isNeedRender = true;
345
+ }
346
+ }
347
+ this.emitEvent({ type: "update", data: force });
348
+ }
349
+
350
+ /**
351
+ * Update with internal schedule, need after change operation when have long update for
352
+ * update without lock UI
353
+ *
354
+ * @param maxScheduleUpdateTimeInMs - Maximum time for one update, by default 30 ms
355
+ * @param maxScheduleUpdateCount - Maximum count of schedule update
356
+ * @returns return void Promise
357
+ */
358
+
359
+ private scheduleUpdateAsync(maxScheduleUpdateTimeInMs = 50): Promise<void> {
360
+ return new Promise<void>((resolve, reject) => {
361
+ setTimeout(() => {
362
+ try {
363
+ this.visViewer()?.update(maxScheduleUpdateTimeInMs);
364
+ this.activeDragger()?.updatePreview();
365
+ resolve();
366
+ } catch (e) {
367
+ console.error(e);
368
+ reject();
369
+ }
370
+ }, 0);
371
+ });
372
+ }
373
+
374
+ async updateAsync(maxScheduleUpdateTimeInMs = 50, maxScheduleUpdateCount = 50): Promise<void> {
375
+ this._isRunAsyncUpdate = true;
376
+ const device = this.visViewer().getActiveDevice();
377
+ try {
378
+ for (let iterationCount = 0; !device.isValid() && iterationCount < maxScheduleUpdateCount; iterationCount++) {
379
+ await this.scheduleUpdateAsync(maxScheduleUpdateTimeInMs);
380
+ }
381
+ await this.scheduleUpdateAsync(maxScheduleUpdateTimeInMs);
382
+ } catch (e) {
383
+ console.error(e);
384
+ } finally {
385
+ this._isRunAsyncUpdate = false;
386
+ }
387
+ }
388
+
389
+ /**
390
+ * Returns `VisualizeJS`
391
+ * {@link https://cloud.opendesign.com/docs/index.html#/visualizejs_api | module} instance.
392
+ */
393
+ visLib(): any {
394
+ return this.visualizeJs;
395
+ }
396
+
397
+ /**
398
+ * Returns `VisualizeJS`
399
+ * {@link https://cloud.opendesign.com/docs/index.html#/vis/Viewer | Viewer} instance.
400
+ */
401
+ visViewer(): any {
402
+ return this.visualizeJs?.getViewer();
403
+ }
404
+
405
+ // update the VisualizeJS options
406
+
407
+ syncOpenCloudVisualStyle(isInitializing: boolean): this {
408
+ if (!this.visualizeJs) return this;
409
+
410
+ const visLib = this.visLib();
411
+ const visViewer = visLib.getViewer();
412
+
413
+ const device = visViewer.getActiveDevice();
414
+ if (device.isNull()) return this;
415
+
416
+ const view = device.getActiveView();
417
+
418
+ // setup light
419
+ view.enableDefaultLighting(true, visLib.DefaultLightingType.kTwoLights);
420
+ view.setDefaultLightingIntensity(1.25);
421
+
422
+ let visualStyleId;
423
+ try {
424
+ visualStyleId = visViewer.findVisualStyle("OpenCloud");
425
+ } catch (e) {
426
+ if (!isInitializing) {
427
+ console.log("OpenCloud visual style not found, creating it on client side");
428
+ }
429
+ visualStyleId = visViewer.createVisualStyle("OpenCloud");
430
+
431
+ const colorDef = new visLib.OdTvColorDef(66, 66, 66);
432
+ const shadedVsId = visViewer.findVisualStyle("Realistic");
433
+
434
+ const visualStylePtr = visualStyleId.openObject();
435
+ visualStylePtr.copyFrom(shadedVsId);
436
+ visualStylePtr.setOptionInt32(visLib.VisualStyleOptions.kFaceModifiers, 0, visLib.VisualStyleOperations.kSet);
437
+ //visualStylePtr.setOptionInt32(visLib.VisualStyleOptions.kEdgeModel, 1, visLib.VisualStyleOperations.kSet);
438
+ visualStylePtr.setOptionInt32(visLib.VisualStyleOptions.kEdgeModel, 2, visLib.VisualStyleOperations.kSet);
439
+ visualStylePtr.setOptionDouble(visLib.VisualStyleOptions.kEdgeCreaseAngle, 60, visLib.VisualStyleOperations.kSet);
440
+ visualStylePtr.setOptionInt32(visLib.VisualStyleOptions.kEdgeStyles, 0, visLib.VisualStyleOperations.kSet);
441
+ visualStylePtr.setOptionInt32(visLib.VisualStyleOptions.kEdgeModifiers, 8, visLib.VisualStyleOperations.kSet);
442
+ visualStylePtr.setOptionColor(
443
+ visLib.VisualStyleOptions.kEdgeColorValue,
444
+ colorDef,
445
+ visLib.VisualStyleOperations.kSet
446
+ );
447
+ visualStylePtr.delete();
448
+ }
449
+
450
+ view.visualStyle = visualStyleId;
451
+
452
+ view.delete();
453
+ device.delete();
454
+
455
+ return this;
456
+ }
457
+
458
+ syncOptions(options: OptionsData = this.options.data): this {
459
+ if (!this.visualizeJs) return this;
460
+
461
+ const visLib = this.visLib();
462
+ const visViewer = visLib.getViewer();
463
+
464
+ const device = visViewer.getActiveDevice();
465
+ if (device.isNull()) return this;
466
+
467
+ if (options.showWCS !== visViewer.getEnableWCS()) {
468
+ visViewer.setEnableWCS(options.showWCS);
469
+ }
470
+ if (options.cameraAnimation !== visViewer.getEnableAnimation()) {
471
+ visViewer.setEnableAnimation(options.cameraAnimation);
472
+ }
473
+ if (options.antialiasing !== visViewer.fxaaAntiAliasing3d) {
474
+ visViewer.fxaaAntiAliasing3d = options.antialiasing;
475
+ visViewer.fxaaQuality = 5;
476
+ }
477
+
478
+ if (options.shadows !== visViewer.shadows) {
479
+ visViewer.shadows = options.shadows;
480
+
481
+ const canvas = visLib.canvas;
482
+ device.invalidate([0, canvas.clientWidth, canvas.clientHeight, 0]);
483
+ }
484
+
485
+ if (options.groundShadow !== visViewer.groundShadow) {
486
+ visViewer.groundShadow = options.groundShadow;
487
+ }
488
+
489
+ if (options.ambientOcclusion !== device.getOptionBool(visLib.DeviceOptions.kSSAOEnable)) {
490
+ device.setOptionBool(visLib.DeviceOptions.kSSAOEnable, options.ambientOcclusion);
491
+ device.setOptionBool(visLib.DeviceOptions.kSSAODynamicRadius, true);
492
+ device.setOptionDouble(visLib.DeviceOptions.kSSAORadius, 1);
493
+ device.setOptionInt32(visLib.DeviceOptions.kSSAOLoops, 32);
494
+ device.setOptionDouble(visLib.DeviceOptions.kSSAOPower, 2);
495
+ device.setOptionInt32(visLib.DeviceOptions.kSSAOBlurRadius, 2);
496
+
497
+ const activeView = visViewer.activeView;
498
+ activeView.setSSAOEnabled(options.ambientOcclusion);
499
+ activeView.delete();
500
+ }
501
+
502
+ if (isExist(options.edgeModel)) {
503
+ const activeView = device.getActiveView();
504
+
505
+ const visualStyleId = visViewer.findVisualStyle("OpenCloud");
506
+ const visualStylePtr = visualStyleId.openObject();
507
+
508
+ visualStylePtr.setOptionInt32(
509
+ visLib.VisualStyleOptions.kEdgeModel,
510
+ options.edgeModel ? 2 : 0,
511
+ visLib.VisualStyleOperations.kSet
512
+ );
513
+
514
+ activeView.visualStyle = visualStyleId;
515
+
516
+ visualStylePtr.delete();
517
+ visualStyleId.delete();
518
+ activeView.delete();
519
+ }
520
+
521
+ device.delete();
522
+
523
+ this.syncHighlightingOptions(options);
524
+ this.update();
525
+
526
+ return this;
527
+ }
528
+
529
+ syncHighlightingOptions(options: OptionsData = this.options.data): this {
530
+ if (!this.visualizeJs) return this;
531
+
532
+ const params = options.enableCustomHighlight ? options : Options.defaults();
533
+
534
+ const visLib = this.visLib();
535
+ const visViewer = visLib.getViewer();
536
+ const { Entry, OdTvRGBColorDef } = visLib;
537
+
538
+ const highlightStyleId = visViewer.findHighlightStyle("Web_Default");
539
+ const highlightStylePtr = highlightStyleId.openObject();
540
+
541
+ if (isExist(params.facesColor)) {
542
+ const color = new OdTvRGBColorDef(params.facesColor.r, params.facesColor.g, params.facesColor.b);
543
+ highlightStylePtr.setFacesColor(Entry.k3D.value | Entry.k3DTop.value, color);
544
+ color.delete();
545
+ }
546
+
547
+ if (isExist(params.facesOverlap)) {
548
+ highlightStylePtr.setFacesVisibility(Entry.k3DTop.value, params.facesOverlap);
549
+ }
550
+ if (isExist(params.facesTransparancy)) {
551
+ highlightStylePtr.setFacesTransparency(Entry.k3D.value | Entry.k3DTop.value, params.facesTransparancy);
552
+ }
553
+
554
+ if (isExist(params.edgesColor)) {
555
+ const color = new OdTvRGBColorDef(params.edgesColor.r, params.edgesColor.g, params.edgesColor.b);
556
+ highlightStylePtr.setEdgesColor(
557
+ Entry.k3DTop.value | Entry.k3D.value | Entry.k2D.value | Entry.k2DTop.value,
558
+ color
559
+ );
560
+ color.delete();
561
+ }
562
+
563
+ if (isExist(params.edgesVisibility)) {
564
+ highlightStylePtr.setEdgesVisibility(
565
+ Entry.k2D.value | Entry.k2DTop.value | Entry.k3DTop.value | Entry.k3D.value,
566
+ params.edgesVisibility
567
+ );
568
+ }
569
+ if (isExist(params.edgesOverlap)) {
570
+ const visibility = !isExist(params.edgesVisibility) ? true : params.edgesVisibility;
571
+ highlightStylePtr.setEdgesVisibility(Entry.k2DTop.value | Entry.k3DTop.value, params.edgesOverlap && visibility);
572
+ }
573
+
574
+ const device = visViewer.getActiveDevice();
575
+ if (!device.isNull()) {
576
+ const canvas = visLib.canvas;
577
+
578
+ device.invalidate([0, canvas.clientWidth, canvas.clientHeight, 0]);
579
+ device.delete();
580
+ }
581
+
582
+ return this;
583
+ }
584
+
585
+ /**
586
+ * List of names of available draggers:
587
+ *
588
+ * - `Line`
589
+ * - `Text`
590
+ * - `Pan`
591
+ * - `Orbit`
592
+ * - `Zoom`
593
+ * - `ZoomWindow`
594
+ * - `OrbitAroundBuilding`
595
+ * - `MeasureLine`
596
+ * - `CuttingPlaneXAxis`
597
+ * - `CuttingPlaneYAxis`
598
+ * - `CuttingPlaneZAxis`
599
+ * - `Walk`
600
+ *
601
+ * @readonly
602
+ */
603
+ get draggers(): string[] {
604
+ return [...this.draggerFactory.keys()];
605
+ }
606
+
607
+ /**
608
+ * Register dragger on draggerFactory.
609
+ *
610
+ * @param name - Dragger name.
611
+ * @param dragger - Dragger class.
612
+ */
613
+ public registerDragger(name: string, dragger: typeof OdBaseDragger): void {
614
+ this.draggerFactory.set(name, dragger);
615
+ }
616
+
617
+ /**
618
+ * Returns active dragger instance or `null` if there is no active dragger.
619
+ */
620
+ activeDragger(): any | null {
621
+ return this._activeDragger;
622
+ }
623
+
624
+ /**
625
+ * Set active dragger. `Viewer` must be {@link Viewer#initialize | initialized} before enable
626
+ * dragger or exception is thrown.
627
+ *
628
+ * Fires:
629
+ *
630
+ * - {@link ChangeActiveDragger | changeactivedragger}
631
+ *
632
+ * @param name - Dragger name. Can be one of the {@link Viewer#draggers | draggers} list.
633
+ * @returns Returns active dragger instance or `null` if there is no dragger with the given name.
634
+ */
635
+ setActiveDragger(name: string): OdBaseDragger | null {
636
+ if (this._activeDragger?.name !== name) {
637
+ if (this._activeDragger) {
638
+ this._activeDragger.dispose();
639
+ this._activeDragger = null;
640
+ }
641
+ const Constructor = this.draggerFactory.get(name);
642
+ if (Constructor) {
643
+ this._activeDragger = new Constructor(this);
644
+ this._activeDragger.name = name;
645
+ this._activeDragger.initialize();
646
+ }
647
+ const canvas = this.canvas;
648
+ if (canvas) {
649
+ canvas.className = canvas.className
650
+ .split(" ")
651
+ .filter((x) => !x.startsWith("oda-cursor-"))
652
+ .filter((x) => x)
653
+ .concat(`oda-cursor-${name.toLowerCase()}`)
654
+ .join(" ");
655
+ }
656
+ this.emitEvent({ type: "changeactivedragger", data: name });
657
+ }
658
+ return this._activeDragger;
659
+ }
660
+
661
+ /**
662
+ * Reset the state of the active dragger.
663
+ */
664
+ resetActiveDragger(): void {
665
+ const dragger = this._activeDragger;
666
+ if (dragger) {
667
+ this.setActiveDragger("");
668
+ this.setActiveDragger(dragger.name);
669
+ }
670
+ }
671
+
672
+ /**
673
+ * Remove all cutting planes.
674
+ */
675
+ clearSlices(): void {
676
+ if (!this.visualizeJs) return;
677
+
678
+ const visViewer = this.visViewer();
679
+ const activeView = visViewer.activeView;
680
+ activeView.removeCuttingPlanes();
681
+ activeView.delete();
682
+
683
+ this.update();
684
+ }
685
+
686
+ /**
687
+ * Remove markup overlay.
688
+ */
689
+ clearOverlay(): void {
690
+ this.markup.clearOverlay();
691
+ }
692
+
693
+ /**
694
+ * Synchronize markup overlay.
695
+ */
696
+ syncOverlay(): any {
697
+ if (!this.visualizeJs) return;
698
+
699
+ const visViewer = this.visViewer();
700
+ const activeView = visViewer.activeView;
701
+
702
+ let overlayView = visViewer.getViewByName(OVERLAY_VIEW_NAME);
703
+ if (!overlayView) {
704
+ const overlayModel = visViewer.getMarkupModel();
705
+ const pDevice = visViewer.getActiveDevice();
706
+
707
+ overlayView = pDevice.createView(OVERLAY_VIEW_NAME, false);
708
+ overlayView.addModel(overlayModel);
709
+
710
+ activeView.addSibling(overlayView);
711
+ pDevice.addView(overlayView);
712
+ }
713
+
714
+ overlayView.viewPosition = activeView.viewPosition;
715
+ overlayView.viewTarget = activeView.viewTarget;
716
+ overlayView.upVector = activeView.upVector;
717
+ overlayView.viewFieldWidth = activeView.viewFieldWidth;
718
+ overlayView.viewFieldHeight = activeView.viewFieldHeight;
719
+
720
+ const viewPort = overlayView.getViewport();
721
+ overlayView.setViewport(viewPort.lowerLeft, viewPort.upperRight);
722
+ overlayView.vportRect = activeView.vportRect;
723
+
724
+ this.update();
725
+
726
+ return overlayView;
727
+ }
728
+
729
+ /**
730
+ * Returns `true` if current drawing is 3D drawing.
731
+ */
732
+ is3D(): boolean {
733
+ if (!this.visualizeJs) return false;
734
+
735
+ const visViewer = this.visViewer();
736
+ const ext = visViewer.getActiveExtents();
737
+ const min = ext.min();
738
+ const max = ext.max();
739
+ const extHeight = max[2] - min[2];
740
+ return extHeight !== 0;
741
+
742
+ //return visViewer.activeView.upVector[1] >= 0.95;
743
+ }
744
+
745
+ /**
746
+ * Returns a list of original handles for the selected entities.
747
+ */
748
+ getSelected(): string[] {
749
+ return this.executeCommand("getSelected");
750
+ }
751
+
752
+ /**
753
+ * Select model entities by original handles that are obtained using
754
+ * {@link File.getProperties | File.getProperties()} or
755
+ * {@link File.searchProperties | File.searchProperties()} methods.
756
+ *
757
+ * Fires:
758
+ *
759
+ * - {@link SelectEvent | select}
760
+ *
761
+ * @param handles - The list of original handles.
762
+ */
763
+ setSelected(handles?: string[]): void {
764
+ this.executeCommand("setSelected", handles);
765
+ }
766
+
767
+ /**
768
+ * Load model references into the viewer. References are images, fonts, or any other files to
769
+ * correct rendering of the model.
770
+ *
771
+ * @async
772
+ * @param model - Instance of model with references. If a `File` instance is specified
773
+ * instead of a model, the file references will be loaded.
774
+ */
775
+ async loadReferences(model: Model | File | Assembly): Promise<this> {
776
+ if (!this.visualizeJs) return this;
777
+
778
+ const abortController = new AbortController();
779
+ this._abortControllerForReferences?.abort();
780
+ this._abortControllerForReferences = abortController;
781
+
782
+ let references = [];
783
+ await model
784
+ .getReferences(abortController.signal)
785
+ .then((data) => (references = data.references))
786
+ .catch((e) => console.error("Cannot load model references.", e));
787
+
788
+ for (const file of references) {
789
+ await this.client
790
+ .downloadFile(file.id, undefined, abortController.signal)
791
+ .then((arrayBuffer) => this.visualizeJs?.getViewer().addEmbeddedFile(file.name, new Uint8Array(arrayBuffer)))
792
+ .catch((e) => console.error(`Cannot load reference file ${file.name}.`, e));
793
+ }
794
+
795
+ return this;
796
+ }
797
+
798
+ // Internal loading routines
799
+
800
+ applyModelTransformMatrix(model: Model | Assembly) {
801
+ this.executeCommand("applyModelTransform", model);
802
+ }
803
+
804
+ applySceneGraphSettings(options = this.options) {
805
+ if (!this.visualizeJs) return;
806
+
807
+ const visLib = this.visLib();
808
+ const visViewer = visLib.getViewer();
809
+
810
+ const device = visViewer.getActiveDevice();
811
+ if (isExist(options.sceneGraph)) {
812
+ device.setOptionBool(visLib.DeviceOptions.kDelaySceneGraphProc, !options.sceneGraph);
813
+ }
814
+ // if (options.enablePartialMode && visLib.HpTrc.Usd >= visViewer.memoryLimit) {
815
+ // device.setOptionBool(visLib.DeviceOptions.kDelaySceneGraphProc, true);
816
+ // }
817
+ device.delete();
818
+
819
+ this.update();
820
+ }
821
+
822
+ /**
823
+ * Loads a model of a file or assembly into the viewer.
824
+ *
825
+ * This method requires a [Client]{@link Client | Client} instance to work. For standalone
826
+ * viewer instance use {@link Viewer#openVsfFile | openVsfFile()} or
827
+ * {@link Viewer#openVsfxFile | openVsfxFile()}.
828
+ *
829
+ * Fires:
830
+ *
831
+ * - {@link OpenEvent | open}
832
+ * - {@link GeometryStartEvent | geometrystart}
833
+ * - {@link GeometryProgressEvent | geometryprogress}
834
+ * - {@link DatabaseChunkEvent | databasechunk}
835
+ * - {@link GeometryChunkEvent | geometrychunk}
836
+ * - {@link GeometryEndEvent | geometryend}
837
+ * - {@link GeometryErrorEvent | geometryerror}
838
+ *
839
+ * @async
840
+ * @param file - File or Assembly or Model instance to load. If a `File` instance with
841
+ * multiple models is specified, the default model will be loaded. If there is no default
842
+ * model, first availiable model will be loaded.
843
+ */
844
+ async open(file: File | Assembly | Model): Promise<this> {
845
+ if (!this.visualizeJs) return this;
846
+
847
+ this.cancel();
848
+ this.clear();
849
+
850
+ this.emitEvent({ type: "open", file, model: file });
851
+
852
+ let model: Model;
853
+
854
+ if (file) {
855
+ const models = (await file.getModels()) || [];
856
+ model = models.find((model: Model) => model.default) || models[0];
857
+ }
858
+ if (!model) throw new Error("No default model found");
859
+
860
+ const overrideOptions = new Options();
861
+ overrideOptions.data = this.options.data;
862
+ if (file.type === ".rcs") {
863
+ console.log("Partial load mode is forced for RCS file");
864
+ overrideOptions.enablePartialMode = true;
865
+ }
866
+
867
+ const loaderFactory = new LoaderFactory();
868
+ const loader = loaderFactory.create(this, model, overrideOptions.data);
869
+
870
+ await this.loadReferences(model);
871
+ await loader.load();
872
+
873
+ if (this.visualizeJs) {
874
+ this.applyModelTransformMatrix(model);
875
+ this.applySceneGraphSettings();
876
+ }
877
+
878
+ return this;
879
+ }
880
+
881
+ /**
882
+ * Loads a VSF file into the viewer.
883
+ *
884
+ * Fires:
885
+ *
886
+ * - {@link OpenEvent | open}
887
+ * - {@link GeometryStartEvent | geometrystart}
888
+ * - {@link GeometryProgressEvent | geometryprogress}
889
+ * - {@link DatabaseChunkEvent | databasechunk}
890
+ * - {@link GeometryEndEvent | geometryend}
891
+ * - {@link GeometryErrorEvent | geometryerror}
892
+ *
893
+ * @param buffer - Binary data buffer to load.
894
+ */
895
+ openVsfFile(buffer: Uint8Array | ArrayBuffer): this {
896
+ if (!this.visualizeJs) return this;
897
+
898
+ this.cancel();
899
+ this.clear();
900
+
901
+ this.emitEvent({ type: "open", buffer });
902
+
903
+ try {
904
+ this.emitEvent({ type: "geometrystart", buffer });
905
+
906
+ const visLib = this.visLib();
907
+ const visViewer = visLib.getViewer();
908
+
909
+ const data = buffer instanceof Uint8Array ? buffer : new Uint8Array(buffer);
910
+ visViewer.parseFile(data);
911
+
912
+ this.syncOpenCloudVisualStyle(false);
913
+ this.syncOptions();
914
+ this.resize();
915
+
916
+ this.emitEvent({ type: "geometryprogress", data: 1, buffer });
917
+ this.emitEvent({ type: "databasechunk", data, buffer });
918
+ this.emitEvent({ type: "geometryend", buffer });
919
+ } catch (error) {
920
+ this.emitEvent({ type: "geometryerror", data: error, buffer });
921
+ throw error;
922
+ }
923
+
924
+ return this;
925
+ }
926
+
927
+ /**
928
+ * Loads a VSFX file into the viewer.
929
+ *
930
+ * Fires:
931
+ *
932
+ * - {@link OpenEvent | open}
933
+ * - {@link GeometryStartEvent | geometrystart}
934
+ * - {@link GeometryProgressEvent | geometryprogress}
935
+ * - {@link DatabaseChunkEvent | databasechunk}
936
+ * - {@link GeometryEndEvent | geometryend}
937
+ * - {@link GeometryErrorEvent | geometryerror}
938
+ *
939
+ * @param buffer - Binary data buffer to load.
940
+ */
941
+ openVsfxFile(buffer: Uint8Array | ArrayBuffer): this {
942
+ if (!this.visualizeJs) return this;
943
+
944
+ this.cancel();
945
+ this.clear();
946
+
947
+ this.emitEvent({ type: "open", buffer });
948
+
949
+ try {
950
+ this.emitEvent({ type: "geometrystart", buffer });
951
+
952
+ const visLib = this.visLib();
953
+ const visViewer = visLib.getViewer();
954
+
955
+ const data = buffer instanceof Uint8Array ? buffer : new Uint8Array(buffer);
956
+ visViewer.parseVsfx(data);
957
+
958
+ this.syncOpenCloudVisualStyle(false);
959
+ this.syncOptions();
960
+ this.resize();
961
+
962
+ this.emitEvent({ type: "geometryprogress", data: 1, buffer });
963
+ this.emitEvent({ type: "databasechunk", data, buffer });
964
+ this.emitEvent({ type: "geometryend", buffer });
965
+ } catch (error) {
966
+ this.emitEvent({ type: "geometryerror", data: error, buffer });
967
+ throw error;
968
+ }
969
+
970
+ return this;
971
+ }
972
+
973
+ /**
974
+ * Cancels asynchronous model loading started by {@link Viewer#open | open()}.
975
+ */
976
+ cancel(): this {
977
+ this._abortControllerForReferences?.abort();
978
+ this._abortControllerForReferences = undefined;
979
+
980
+ this._abortController?.abort();
981
+ this._abortController = undefined;
982
+
983
+ this._abortControllerForRequestMap?.forEach((controller) => controller.abort());
984
+ this._abortControllerForRequestMap = undefined;
985
+
986
+ this.emitEvent({ type: "cancel" });
987
+
988
+ return this;
989
+ }
990
+
991
+ /**
992
+ * Unloads the model and clears the viewer.
993
+ */
994
+ clear(): this {
995
+ if (!this.visualizeJs) return this;
996
+
997
+ const visLib = this.visLib();
998
+ const visViewer = visLib.getViewer();
999
+
1000
+ this.clearOverlay();
1001
+
1002
+ visViewer.clear();
1003
+ visViewer.createLocalDatabase();
1004
+
1005
+ this.syncOpenCloudVisualStyle(true);
1006
+ this.syncOptions();
1007
+ this.resize();
1008
+
1009
+ this.emitEvent({ type: "clear" });
1010
+
1011
+ return this;
1012
+ }
1013
+
1014
+ /**
1015
+ * Get markup color.
1016
+ *
1017
+ * @returns Color with `RGB` values.
1018
+ */
1019
+ getMarkupColor(): { r: number; g: number; b: number } {
1020
+ return this.markup.getMarkupColor();
1021
+ }
1022
+
1023
+ /**
1024
+ * Set markup color.
1025
+ *
1026
+ * @param r - `Red` part of color.
1027
+ * @param g - `Green` part of color.
1028
+ * @param b - `Blue` part of color.
1029
+ */
1030
+ setMarkupColor(r = 255, g = 0, b = 0): void {
1031
+ this.markup.setMarkupColor(r, g, b);
1032
+ const color = { r, g, b };
1033
+ this.emitEvent({ type: "changemarkupcolor", data: color });
1034
+ }
1035
+
1036
+ /**
1037
+ * Colorize all markup entities with the specified color.
1038
+ *
1039
+ * @param r - `Red` part of color.
1040
+ * @param g - `Green` part of color.
1041
+ * @param b - `Blue` part of color.
1042
+ */
1043
+ colorizeAllMarkup(r = 255, g = 0, b = 0): void {
1044
+ this.markup.colorizeAllMarkup(r, g, b);
1045
+ }
1046
+
1047
+ /**
1048
+ * Add an empty markup entity to the overlay.
1049
+ */
1050
+ addMarkupEntity(entityName: string) {
1051
+ if (!this.visualizeJs) return null;
1052
+
1053
+ this.syncOverlay();
1054
+
1055
+ const visViewer = this.visViewer();
1056
+ const model = visViewer.getMarkupModel();
1057
+ const entityId = model.appendEntity(entityName);
1058
+ const entityPtr = entityId.openObject();
1059
+
1060
+ const color = this.getMarkupColor();
1061
+ entityPtr.setColor(color.r, color.g, color.b);
1062
+ entityPtr.setLineWeight(2);
1063
+ entityPtr.delete();
1064
+
1065
+ this.update();
1066
+
1067
+ return entityId;
1068
+ }
1069
+
1070
+ /**
1071
+ * Draw a viewpoint. To get a list of available model viewpoints, use the
1072
+ * {@link Model#getViewpoints | Model.getViewpoints()} or
1073
+ * {@link File#getViewpoints | File.getViewpoints()}.
1074
+ *
1075
+ * @param viewpoint - Viewpoint.
1076
+ */
1077
+ drawViewpoint(viewpoint: IViewpoint): void {
1078
+ this.setOrthogonalCameraSettings(viewpoint.orthogonal_camera);
1079
+ this.setClippingPlanes(viewpoint.clipping_planes);
1080
+ this.markup.setViewpoint(viewpoint);
1081
+ }
1082
+
1083
+ /**
1084
+ * Create a viewpoint. To add a viewpoint to the list of model viewpoints, use the
1085
+ * {@link Model#saveViewpoint | Model.saveViewpoint()} or
1086
+ * {@link File#saveViewpoint | File.saveViewpoint()}.
1087
+ */
1088
+ createViewpoint(): IViewpoint {
1089
+ const vp = this.markup.getViewpoint();
1090
+ vp.orthogonal_camera = this.getOrthogonalCameraSettings();
1091
+ vp.clipping_planes = this.getClippingPlanes();
1092
+
1093
+ return vp;
1094
+ }
1095
+
1096
+ private getPoint3dFromArray(array) {
1097
+ return { x: array[0], y: array[1], z: array[2] };
1098
+ }
1099
+
1100
+ private getLogicalPoint3dAsArray(point3d) {
1101
+ return [point3d.x, point3d.y, point3d.z];
1102
+ }
1103
+
1104
+ private getOrthogonalCameraSettings(): IOrthogonalCamera {
1105
+ const visViewer = this.visViewer();
1106
+ const activeView = visViewer.activeView;
1107
+
1108
+ return {
1109
+ view_point: this.getPoint3dFromArray(activeView.viewPosition),
1110
+ direction: this.getPoint3dFromArray(activeView.viewTarget),
1111
+ up_vector: this.getPoint3dFromArray(activeView.upVector),
1112
+ field_width: activeView.viewFieldWidth,
1113
+ field_height: activeView.viewFieldHeight,
1114
+ };
1115
+ }
1116
+
1117
+ private setOrthogonalCameraSettings(settings: IOrthogonalCamera) {
1118
+ const visViewer = this.visViewer();
1119
+ const activeView = visViewer.activeView;
1120
+
1121
+ this.resetActiveDragger();
1122
+ this.clearSlices();
1123
+ this.clearOverlay();
1124
+
1125
+ if (settings) {
1126
+ activeView.setView(
1127
+ this.getLogicalPoint3dAsArray(settings.view_point),
1128
+ this.getLogicalPoint3dAsArray(settings.direction),
1129
+ this.getLogicalPoint3dAsArray(settings.up_vector),
1130
+ settings.field_width,
1131
+ settings.field_height,
1132
+ true
1133
+ );
1134
+ }
1135
+
1136
+ this.syncOverlay();
1137
+ }
1138
+
1139
+ private getClippingPlanes(): IClippingPlane[] {
1140
+ const visViewer = this.visViewer();
1141
+ const activeView = visViewer.activeView;
1142
+
1143
+ const clipping_planes = [];
1144
+ for (let i = 0; i < activeView.numCuttingPlanes(); i++) {
1145
+ const cuttingPlane = activeView.getCuttingPlane(i);
1146
+
1147
+ const plane = {
1148
+ location: this.getPoint3dFromArray(cuttingPlane.getOrigin()),
1149
+ direction: this.getPoint3dFromArray(cuttingPlane.normal()),
1150
+ };
1151
+
1152
+ clipping_planes.push(plane);
1153
+ }
1154
+
1155
+ return clipping_planes;
1156
+ }
1157
+
1158
+ private setClippingPlanes(clippingPlanes: IClippingPlane[]) {
1159
+ if (clippingPlanes) {
1160
+ const visViewer = this.visViewer();
1161
+ const activeView = visViewer.activeView;
1162
+
1163
+ for (const plane of clippingPlanes) {
1164
+ const cuttingPlane = new (this.visLib().OdTvPlane)();
1165
+ cuttingPlane.set(this.getLogicalPoint3dAsArray(plane.location), this.getLogicalPoint3dAsArray(plane.direction));
1166
+
1167
+ activeView.addCuttingPlane(cuttingPlane);
1168
+ activeView.setEnableCuttingPlaneFill(true, 0x66, 0x66, 0x66);
1169
+ }
1170
+ }
1171
+ }
1172
+
1173
+ /**
1174
+ * Executes the command denoted by the given command identifier.
1175
+ *
1176
+ * @param id - Identifier of the command to execute.
1177
+ * @param args - Parameters passed to the command function.
1178
+ * @returns A returned value of the given command. Returns `undefined` when the command doesn't exists.
1179
+ */
1180
+ executeCommand(id: string, ...args: any[]): any {
1181
+ return commands("VisualizeJS").executeCommand(id, this, ...args);
1182
+ }
1183
+ }