@inweb/viewer-visualize
Version:
JavaScript library for rendering CAD and BIM files in a browser using VisualizeJS
359 lines (358 loc) • 14.1 kB
TypeScript
import { EventEmitter2 } from "@inweb/eventemitter2";
import { Assembly, Client, File, Model } from "@inweb/client";
import { CanvasEventMap, Dragger, FileSource, IComponent, IDragger, ILoader, IOptions, IViewer, IViewpoint, Options, OptionsEventMap, ViewerEventMap } from "@inweb/viewer-core";
import { IMarkup, IWorldTransform } from "@inweb/markup";
import { MarkupType } from "./Markup/MarkupFactory";
/**
* 3D viewer powered by {@link https://cloud.opendesign.com/docs/index.html#/visualizejs | VisualizeJS}
* library.
*/
export declare class Viewer extends EventEmitter2<ViewerEventMap & CanvasEventMap & OptionsEventMap> implements IViewer, IWorldTransform {
private _activeDragger;
private _components;
private _enableAutoUpdate;
private _isNeedRender;
private _isRunAsyncUpdate;
private _renderTime;
protected _options: Options;
protected _visualizeJsUrl: string;
protected _visualizeJs: any;
protected _visualizeTimestamp: number;
protected _crossOrigin: any;
private canvaseventlistener;
canvasEvents: string[];
private _markup;
canvas: HTMLCanvasElement | undefined;
_abortControllerForReferences: AbortController | undefined;
client: Client | undefined;
loaders: Array<ILoader>;
/**
* @param client - The `Client` instance that is used to load model reference files from the Open Cloud
* Server. Do not specify `Client` if you need a standalone viewer instance to view `VSFX` files from
* the web or from local computer.
* @param params - An object containing viewer configuration parameters.
* @param params.visualizeJsUrl - `VisualizeJS` library URL. Set this URL to use your own library
* instance, or specify `undefined` or blank to use the default URL defined by `Viewer.visualize`
* library you are using.
* @param params.crossOrigin - The
* {@link https://developer.mozilla.org/docs/Web/HTML/Attributes/crossorigin | crossorigin} content
* attribute on `Visalize.js` script element. One of the following values: `""`, `anonymous` or
* `use-credentials`.
* @param params.enableAutoUpdate - Enable auto-update of the viewer after any changes. If the
* auto-update is disabled, you need to register an `update` event handler and update the
* `VisualizeJS` viewer and active dragger manually. Default is `true`.
* @param params.markupType - The type of the markup core: `Visualize` (deprecated) or `Konva`. Default
* is `Konva`.
*/
constructor(client?: Client, params?: {
visualizeJsUrl?: string;
crossOrigin?: string;
enableAutoUpdate?: boolean;
markupType?: MarkupType;
});
/**
* Viewer options.
*/
get options(): IOptions;
/**
* `VisualizeJS` library URL. Use {@link configure | configure()} to change library URL.
*
* @readonly
*/
get visualizeJsUrl(): string;
/**
* 2D markup core instance used to create markups.
*
* @readonly
*/
get markup(): IMarkup;
/**
* Changes the viewer parameters.
*
* @param params - An object containing new parameters.
* @param params.visualizeJsUrl - `VisualizeJS` library URL. Set this URL to use your own library
* instance or specify `undefined` or blank to use the default URL defined by `Viewer.visualize`
* library you are using.
* @param params.crossOrigin - The
* {@link https://developer.mozilla.org/docs/Web/HTML/Attributes/crossorigin | crossorigin} content
* attribute on `Visalize.js` script element. One of the following values: `""`, `anonymous` or
* `use-credentials`.
*/
configure(params: {
visualizeJsUrl?: string;
crossOrigin?: string;
}): this;
/**
* Loads the `VisualizeJS` module and initializes it with the specified canvas. Call
* {@link dispose | dispose()} to release allocated resources.
*
* Fires:
*
* - {@link InitializeEvent | initialize}
* - {@link InitializeProgressEvent | initializeprogress}
*
* @param canvas -
* {@link https://developer.mozilla.org/docs/Web/API/HTMLCanvasElement | HTMLCanvasElement} for
* `VisualizeJS`.
* @param onProgress - A callback function that handles events measuring progress of loading of the
* `VisualizeJS` library.
*/
initialize(canvas: HTMLCanvasElement, onProgress?: (event: ProgressEvent) => void): Promise<this>;
dispose(): this;
/**
* Returns `true` if `VisualizeJS` module has been loaded and initialized.
*/
isInitialized(): boolean;
render(time: DOMHighResTimeStamp): void;
resize(): this;
/**
* Updates the viewer.
*
* Do nothing if the auto-update mode is disabled in the constructor. In this case, register an
* `update` event handler and update the `Visualize` viewer and active dragger manually.
*
* Fires:
*
* - {@link UpdateEvent | update}
*
* @param force - If `true` updates the viewer immidietly. Otherwise updates on next animation frame.
* Default is `false`.
*/
update(force?: boolean): void;
private scheduleUpdateAsync;
/**
* Updates the viewer asynchronously without locking the user interface. Used to update the viewer
* after changes that require a long rendering time.
*
* Do nothing if the auto-update mode is disabled in the constructor. In this case, register an
* `update` event handler and update the `VisualizeJS` viewer and active dragger manually.
*
* Fires:
*
* - {@link UpdateEvent | update}
*
* @param maxScheduleUpdateTimeInMs - Maximum time for one update, default 30 ms.
* @param maxScheduleUpdateCount - Maximum count of scheduled updates.
*/
updateAsync(maxScheduleUpdateTimeInMs?: number, maxScheduleUpdateCount?: number): Promise<void>;
/**
* Returns `VisualizeJS` {@link https://cloud.opendesign.com/docs/index.html#/visualizejs_api | module}
* instance.
*/
get visualizeJs(): any;
/**
* Returns `VisualizeJS` {@link https://cloud.opendesign.com/docs/index.html#/visualizejs_api | module}
* instance.
*/
visLib(): any;
/**
* Returns `VisualizeJS` {@link https://cloud.opendesign.com/docs/index.html#/vis/Viewer | Viewer}
* instance.
*/
visViewer(): any;
syncOpenCloudVisualStyle(isInitializing: boolean): this;
syncOptions(options?: IOptions): this;
syncHighlightingOptions(options?: IOptions): this;
get draggers(): string[];
get components(): string[];
/**
* Deprecated since `25.12`. Use {@link draggers.registerDragger} instead.
*/
registerDragger(name: string, dragger: typeof Dragger): void;
activeDragger(): IDragger | null;
setActiveDragger(name?: string): IDragger | null;
resetActiveDragger(): void;
getComponent(name: string): IComponent;
clearSlices(): void;
clearOverlay(): void;
syncOverlay(): void;
is3D(): boolean;
screenToWorld(position: {
x: number;
y: number;
}): {
x: number;
y: number;
z: number;
};
worldToScreen(position: {
x: number;
y: number;
z: number;
}): {
x: number;
y: number;
};
getScale(): {
x: number;
y: number;
z: number;
};
getSelected(): string[];
setSelected(handles?: string[]): void;
clearSelected(): void;
hideSelected(): void;
isolateSelected(): void;
showAll(): void;
explode(index?: number): void;
collect(): void;
loadReferences(model: Model | File | Assembly): Promise<this>;
applyModelTransformMatrix(model: Model | Assembly): void;
applySceneGraphSettings(options?: IOptions): void;
/**
* Loads a file into the viewer.
*
* The viewer must be {@link initialize | initialized} before opening the file. Otherwise, `open()` does
* nothing.
*
* This method requires a `Client` instance to be specified to load file from the Open Cloud Server.
* The file geometry data on the Open Cloud Server must be converted into a `vsfx` format, otherwise an
* exception will be thrown.
*
* For files from Open Cloud Server, the default model will be loaded. If there is no default model,
* first availiable model will be loaded. If no models are found in the file, an exception will be
* thrown.
*
* For URLs, the file extension is used to determine the file format. For a `ArrayBuffer` and `Data
* URL`, a file format must be specified using `params.format` parameter (see below). If no appropriate
* loader is found for the specified format, an exception will be thrown.
*
* If there was an active dragger before opening the file, it will be deactivated. After opening the
* file, you must manually activate the required dragger.
*
* To open a large files, enable {@link IOptions.enablePartialMode | partial streaming} mode before
* opening. Partial streaming is only supported when opening files from an Open Cloud Server, but not
* local files and URLs. Example:
*
* ```javascript
* viewer.options.enableStreamingMode = true;
* viewer.options.enablePartialMode = true;
* await viewer.open(file);
* ```
*
* Fires:
*
* - {@link OpenEvent | open}
* - {@link GeometryStartEvent | geometrystart}
* - {@link GeometryProgressEvent | geometryprogress}
* - {@link DatabaseChunkEvent | databasechunk}
* - {@link GeometryChunkEvent | geometrychunk}
* - {@link GeometryEndEvent | geometryend}
* - {@link GeometryErrorEvent | geometryerror}
*
* @param file - File to load. Can be one of:
*
* - `File`, `Assembly` or `Model` instance from the Open Cloud Server
* - File `URL` string
* - {@link https://developer.mozilla.org/docs/Web/HTTP/Basics_of_HTTP/Data_URIs | Data URL} string
* - {@link https://developer.mozilla.org/docs/Web/API/File | Web API File} object
* - {@link https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer | ArrayBuffer}
* object
*
* @param params - Loading parameters.
* @param params.format - File format string. Required when loading a file as `ArrayBuffer` or `Data
* URL`.
* @param params.mode - Reserved for future use.
* @param params.requestHeader - The
* {@link https://developer.mozilla.org/docs/Glossary/Request_header | request header} used in HTTP
* request.
* @param params.withCredentials - Whether the HTTP request uses credentials such as cookies,
* authorization headers or TLS client certificates. See
* {@link https://developer.mozilla.org/docs/Web/API/XMLHttpRequest/withCredentials | XMLHttpRequest.withCredentials}
* for more details.
*/
open(file: FileSource, params?: {
format?: string;
mode?: string;
requestHeader?: HeadersInit;
withCredentials?: boolean;
}): Promise<this>;
/**
* Deprecated since `26.4`. Use {@link open | open()} instead.
*
* @deprecated
*/
openVsfFile(buffer: Uint8Array | ArrayBuffer): this;
/**
* Deprecated since `26.4`. Use {@link open | open()} instead.
*
* @deprecated
*/
openVsfxFile(buffer: Uint8Array | ArrayBuffer): this;
cancel(): this;
clear(): this;
/**
* Deprecated since `25.11`. Use {@link IMarkup.getMarkupColor | markup.getMarkupColor()} instead.
*/
getMarkupColor(): {
r: number;
g: number;
b: number;
};
/**
* Deprecated since `25.11`. Use {@link IMarkup.setMarkupColor | markup.setMarkupColor()} instead.
*/
setMarkupColor(r?: number, g?: number, b?: number): void;
/**
* Deprecated since `25.11`. Use {@link IMarkup.colorizeAllMarkup | markup.colorizeAllMarkup()} instead.
*/
colorizeAllMarkup(r?: number, g?: number, b?: number): void;
/**
* Deprecated since `25.11`. Use
* {@link IMarkup.colorizeSelectedMarkups | markup.colorizeSelectedMarkups()} instead.
*/
colorizeSelectedMarkups(r?: number, g?: number, b?: number): void;
/**
* Adds an empty `Visualize` markup entity to the overlay.
*/
addMarkupEntity(entityName: string): any;
drawViewpoint(viewpoint: IViewpoint): void;
createViewpoint(): IViewpoint;
private getPoint3dFromArray;
private getLogicalPoint3dAsArray;
private getOrthogonalCameraSettings;
private setOrthogonalCameraSettings;
private getClippingPlanes;
private setClippingPlanes;
private getSelection;
private setSelection;
/**
* Executes the command denoted by the given command. If the command is not found, tries to set active
* dragger with the specified name.
*
* The following commands are available by default:
*
* - `applyModelTransform`
* - `autoTransformAllModelsToCentralPoint`
* - `clearMarkup`
* - `clearSelected`
* - `clearSlices`
* - `createPreview`
* - `explode`
* - `getDefaultViewPositions`
* - `getModels`
* - `getSelected`
* - `hideSelected`
* - `isolateSelected`
* - `regenerateAll`
* - `resetView`
* - `selectModel`
* - `setActiveDragger`
* - `setDefaultViewPosition`
* - `setMarkupColor`
* - `setSelected`
* - `showAll`
* - `zoomToExtents`
* - `zoomToObjects`
* - `zoomToSelected`
*
* To register custom command use the {@link commands.registerCommand}.
*
* @param id - Command ID or dragger name.
* @param args - Parameters passed to the command handler function.
* @returns Returns the result of the command handler function or new active dragger instance. Returns
* `undefined` if neither the command nor the dragger exists.
*/
executeCommand(id: string, ...args: any[]): any;
deviceAutoRegeneration(): void;
}