forma-embedded-view-sdk
Version:
The Forma Embedded View SDK is a JavaScript library for creating custom extensions in Autodesk Forma (previously Spacemaker).
126 lines (125 loc) • 4.42 kB
JavaScript
/**
* Interact with the terrain in the 3D scene.
*
* @remarks
* Available via {@link auto.Forma | Forma}.{@link index.EmbeddedViewSdk.terrain | terrain}.
*/
export class TerrainApi {
#iframeMessenger;
groundTexture;
/** @hidden */
constructor(iframeMessenger) {
this.#iframeMessenger = iframeMessenger;
this.groundTexture = new GroundTextureApi(iframeMessenger);
}
/**
* Fetch the bounding box for the terrain.
*
* @returns Axis-aligned bounding box for the terrain.
* More specifically, the minimum and maximum (x,y,z) values, in the local coordinate system.
*/
async getBbox() {
return await this.#iframeMessenger.sendRequest("scene/terrain/bbox/get");
}
/**
* Retrieves the elevation of the terrain (in meters above sea level) at a specific point within the scene.
*
* If the coordinates are outside the terrain mesh, it returns the minimum elevation in the terrain.
*/
async getElevationAt(request) {
return await this.#iframeMessenger.sendRequest("scene/terrain/get-elevation-at", request);
}
}
/**
* Manage ground textures applied to the terrain object in the 3D scene.
*
* @remarks
* Available via {@link auto.Forma | Forma}.{@link index.EmbeddedViewSdk.terrain | terrain}.{@link terrain.TerrainApi.groundTexture | groundTexture}.
*/
export class GroundTextureApi {
#iframeMessenger;
/** @hidden */
constructor(iframeMessenger) {
this.#iframeMessenger = iframeMessenger;
}
/**
* Add a ground texture to the terrain.
*
* @example
* // Create canvas
* const canvas = document.createElement("canvas");
* canvas.width = 100;
* canvas.height = 100;
*
* // Fill canvas with blue color
* const ctx = canvas.getContext("2d");
* if (ctx) {
* ctx.fillStyle = "blue";
* ctx.fillRect(0, 0, 100, 100);
*
* // Add canvas as ground texture to position (0, 0) in the local coordinate system.
* // The texture will cover a 100x100 meter square area on the terrain,
* // with lower left corner in (x: -50, y: -50) and upper right corner in (x: 50, y: 50).
* await Forma.terrain.groundTexture.add({
* name: "myGroundTexture",
* canvas,
* position: { x: 0, y: 0, z: 1 },
* scale: { x: 1, y: 1 },
* });
* }
*/
async add(request) {
const canvasUrl = request.canvas.toDataURL();
await this.#iframeMessenger.sendRequest("scene/terrain/ground-texture/add", {
name: request.name,
canvasUrl,
position: request.position,
scale: request.scale,
});
}
/**
* Update the texture data for an existing ground texture object.
*
* @example
* // Create a new canvas filled with red and update the ground texture with this new texture
* const newCanvas = document.createElement("newCanvas");
* newCanvas.width = 100;
* newCanvas.height = 100;
* const ctx = newCanvas.getContext("2d");
* if (ctx) {
* ctx.fillStyle = "red";
* ctx.fillRect(0, 0, 100, 100);
* await Forma.terrain.groundTexture.updateTextureData({
* name: "myGroundTexture",
* canvas: newCanvas,
* });
* }
*/
async updateTextureData(request) {
const canvasUrl = request.canvas.toDataURL();
await this.#iframeMessenger.sendRequest("scene/terrain/ground-texture/update-texture-data", { name: request.name, canvasUrl });
}
/**
* Update the placement of an existing ground texture object.
*
* @example
* // Move "myGroundTexture" to (100, 100) in the local coordinate system.
* await Forma.terrain.groundTexture.updatePosition({
* name: "myGroundTexture",
* position: { x: 100, y: 100, z: 1 }
* })
*/
async updatePosition(request) {
await this.#iframeMessenger.sendRequest("scene/terrain/ground-texture/update-position", request);
}
/**
* Remove an existing ground texture object.
*
* @example
* // Remove "myGroundTexture".
* await Forma.terrain.groundTexture.remove({ name: "myGroundTexture" })
*/
async remove(request) {
await this.#iframeMessenger.sendRequest("scene/terrain/ground-texture/remove", request);
}
}