import {DistanceMeasurementsPlugin} from '@xeokit/xeokit-sdk/src/plugins/DistanceMeasurementsPlugin/DistanceMeasurementsPlugin.js'
DistanceMeasurementsPlugin
Extends:
Viewer plugin for measuring point-to-point distances.
- [Example 1: Model with distance measurements]
- [Example 2: Create distance measurements with mouse]
- [Example 3: Configuring units and scale]
Overview
- A DistanceMeasurement represents a point-to-point measurement between two 3D points on one or two Entitys.
- As shown on the screen capture above, a DistanceMeasurement has one wire (light blue) that shows the direct point-to-point measurement, and three more wires (red, green and blue) that show the distance on each of the World-space X, Y and Z axis.
- Create DistanceMeasurements programmatically with DistanceMeasurementsPlugin#createMeasurement.
- Create DistanceMeasurements interactively using the DistanceMeasurementsControl, located at DistanceMeasurementsPlugin#control.
- Existing DistanceMeasurements are registered by ID in DistanceMeasurementsPlugin#measurements.
- Destroy DistanceMeasurements using DistanceMeasurementsPlugin#destroyMeasurement.
- Configure global measurement units and scale via Metrics, located at Scene#metrics.
Example 1: Creating DistanceMeasurements Programmatically
In our first example, we'll use an XKTLoaderPlugin to load a model, and then use a DistanceMeasurementsPlugin to programmatically create two DistanceMeasurements.
Note how each DistanceMeasurement has origin
and target
endpoints, which each indicate a 3D World-space
position on the surface of an Entity. The endpoints can be attached to the same Entity, or to different Entitys.
import {Viewer, XKTLoaderPlugin, DistanceMeasurementsPlugin} from "xeokit-sdk.es.js";
const viewer = new Viewer({
canvasId: "myCanvas",
transparent: true
});
viewer.scene.camera.eye = [-2.37, 18.97, -26.12];
viewer.scene.camera.look = [10.97, 5.82, -11.22];
viewer.scene.camera.up = [0.36, 0.83, 0.40];
const xktLoader = new XKTLoaderPlugin(viewer);
const distanceMeasurements = new DistanceMeasurementsPlugin(viewer);
const model = xktLoader.load({
src: "./models/xkt/duplex/duplex.xkt"
});
model.on("loaded", () => {
const myMeasurement1 = distanceMeasurements.createMeasurement({
id: "distanceMeasurement1",
origin: {
entity: viewer.scene.objects["2O2Fr$t4X7Zf8NOew3FLOH"],
worldPos: [0.044, 5.998, 17.767]
},
target: {
entity: viewer.scene.objects["2O2Fr$t4X7Zf8NOew3FLOH"],
worldPos: [4.738, 3.172, 17.768]
},
visible: true,
wireVisible: true
});
const myMeasurement2 = distanceMeasurements.createMeasurement({
id: "distanceMeasurement2",
origin: {
entity: viewer.scene.objects["2O2Fr$t4X7Zf8NOew3FNr2"],
worldPos: [0.457, 2.532, 17.766]
},
target: {
entity: viewer.scene.objects["1CZILmCaHETO8tf3SgGEXu"],
worldPos: [0.436, 0.001, 22.135]
},
visible: true,
wireVisible: true
});
});
Example 2: Creating DistanceMeasurements Interactively
In our second example, we'll use an XKTLoaderPlugin to load a model, then we'll use the DistanceMeasurementPlugin's DistanceMeasurementsControl to interactively create DistanceMeasurements with mouse or touch input.
After we've activated the DistanceMeasurementsControl, the first click on any Entity begins constructing a DistanceMeasurement, fixing its origin to that Entity. The next click on any Entity will complete the DistanceMeasurement, fixing its target to that second Entity.
The DistanceMeasurementControl will then wait for the next click on any Entity, to begin constructing another DistanceMeasurement, and so on, until deactivated again.
import {Viewer, XKTLoaderPlugin, DistanceMeasurementsPlugin} from "xeokit-sdk.es.js";
const viewer = new Viewer({
canvasId: "myCanvas",
transparent: true
});
viewer.scene.camera.eye = [-2.37, 18.97, -26.12];
viewer.scene.camera.look = [10.97, 5.82, -11.22];
viewer.scene.camera.up = [0.36, 0.83, 0.40];
const xktLoader = new XKTLoaderPlugin(viewer);
const distanceMeasurements = new DistanceMeasurementsPlugin(viewer);
const model = xktLoader.load({
src: "./models/xkt/duplex/duplex.xkt"
});
distanceMeasurements.control.activate(); // <------------ Activate the DistanceMeasurementsControl
Example 3: Configuring Measurement Units and Scale
In our third example, we'll use the Scene's Metrics to set the global unit of measurement to "meters"
. We'll also specify that a unit within the World-space coordinate system represents ten meters.
The wires belonging to our DistanceMeasurements show their lengths in Real-space coordinates, in the current unit of measurement. They will dynamically update as we set these configurations.
const metrics = viewer.scene.metrics;
metrics.units = "meters";
metrics.scale = 10.0;
Constructor Summary
Public Constructor | ||
public |
constructor(viewer: Viewer, cfg: Object) |
Member Summary
Public Members | ||
public get |
Gets the DistanceMeasurementsControl, which creates DistanceMeasurements from user input. |
|
public |
|
|
public |
defaultColor: * |
|
public |
|
|
public |
|
|
public |
|
|
public |
|
|
public set |
Sets the minimum length, in pixels, of an axis wire beyond which its label is shown. |
|
public get |
Gets the minimum length, in pixels, of an axis wire beyond which its label is shown. |
|
public get |
measurements: {String: DistanceMeasurement} Gets the existing DistanceMeasurements, each mapped to its DistanceMeasurement#id. |
|
public |
zIndex: * |
Method Summary
Public Methods | ||
public |
clear() Destroys all DistanceMeasurements. |
|
public |
createMeasurement(params: Object): DistanceMeasurement Creates a DistanceMeasurement. |
|
public |
destroy() Destroys this DistanceMeasurementsPlugin. |
|
public |
destroyMeasurement(id: String) Destroys a DistanceMeasurement. |
Inherited Summary
From class Plugin | ||
public |
ID for this Plugin, unique within its Viewer. |
|
public |
The Viewer that contains this Plugin. |
|
public |
destroy() Destroys this Plugin and removes it from its Viewer. |
|
public |
Logs an error message to the JavaScript developer console, prefixed with the ID of this Plugin. |
|
public |
Fires an event at this Plugin. |
|
public |
Logs a message to the JavaScript developer console, prefixed with the ID of this Plugin. |
|
public |
Subscribes to an event fired at this Plugin. |
|
public |
Logs a warning message to the JavaScript developer console, prefixed with the ID of this Plugin. |
Public Constructors
public constructor(viewer: Viewer, cfg: Object) source
Creates this Plugin and installs it into the given Viewer.
Override:
Plugin#constructorParams:
Name | Type | Attribute | Description |
viewer | Viewer | The Viewer. |
|
cfg | Object |
|
Plugin configuration. |
cfg.id | String |
|
Optional ID for this plugin, so that we can find it within Viewer#plugins. |
cfg.labelMinAxisLength | Number |
|
The minimum length, in pixels, of an axis wire beyond which its label is shown. |
cfg.container | HTMLElement |
|
Container DOM element for markers and labels. Defaults to |
cfg.defaultVisible | boolean |
|
The default value of the DistanceMeasurements |
cfg.defaultOriginVisible | boolean |
|
The default value of the DistanceMeasurements |
cfg.defaultTargetVisible | boolean |
|
The default value of the DistanceMeasurements |
cfg.defaultWireVisible | boolean |
|
The default value of the DistanceMeasurements |
cfg.defaultAxisVisible | boolean |
|
The default value of the DistanceMeasurements |
cfg.defaultColor | string |
|
The default color of the length dots, wire and label. |
cfg.zIndex | number |
|
If set, the wires, dots and labels will have this zIndex (+1 for dots and +2 for labels). |
Public Members
public get control: DistanceMeasurementsControl source
Gets the DistanceMeasurementsControl, which creates DistanceMeasurements from user input.
public defaultAxisVisible: * source
public defaultColor: * source
public defaultOriginVisible: * source
public defaultTargetVisible: * source
public defaultVisible: * source
public defaultWireVisible: * source
public set labelMinAxisLength: number source
Sets the minimum length, in pixels, of an axis wire beyond which its label is shown.
The axis wire's label is not shown when its length is less than this value.
This is 25
pixels by default.
Must not be less than 1
.
public get labelMinAxisLength: number: * source
Gets the minimum length, in pixels, of an axis wire beyond which its label is shown.
public get measurements: {String: DistanceMeasurement} source
Gets the existing DistanceMeasurements, each mapped to its DistanceMeasurement#id.
public zIndex: * source
Public Methods
public createMeasurement(params: Object): DistanceMeasurement source
Creates a DistanceMeasurement.
The DistanceMeasurement is then registered by DistanceMeasurement#id in DistanceMeasurementsPlugin#measurements.
Params:
Name | Type | Attribute | Description |
params | Object | DistanceMeasurement configuration. |
|
params.id | String | Unique ID to assign to DistanceMeasurement#id. The DistanceMeasurement will be registered by this in DistanceMeasurementsPlugin#measurements and Scene.components. Must be unique among all components in the Viewer. |
|
params.origin.worldPos | Number[] | Origin World-space 3D position. |
|
params.origin.entity | Entity | Origin Entity. |
|
params.target.worldPos | Number[] | Target World-space 3D position. |
|
params.target.entity | Entity | Target Entity. |
|
params.visible | Boolean |
|
Whether to initially show the DistanceMeasurement. |
params.originVisible | Boolean |
|
Whether to initially show the DistanceMeasurement origin. |
params.targetVisible | Boolean |
|
Whether to initially show the DistanceMeasurement target. |
params.wireVisible | Boolean |
|
Whether to initially show the direct point-to-point wire between DistanceMeasurement#origin and DistanceMeasurement#target. |
params.axisVisible | Boolean |
|
Whether to initially show the axis-aligned wires between DistanceMeasurement#origin and DistanceMeasurement#target. |
params.color | string |
|
The color of the length dot, wire and label. |
public destroy() source
Destroys this DistanceMeasurementsPlugin.
Destroys all DistanceMeasurements first.
Override:
Plugin#destroypublic destroyMeasurement(id: String) source
Destroys a DistanceMeasurement.
Params:
Name | Type | Attribute | Description |
id | String | ID of DistanceMeasurement to destroy. |