UNPKG

forma-embedded-view-sdk

Version:

The Forma Embedded View SDK is a JavaScript library for creating custom extensions in Autodesk Forma (previously Spacemaker).

141 lines (140 loc) 4.67 kB
import { ElementColorApi } from "./element-colors.js"; import { RenderGeojsonApi } from "./renderGeoJson.js"; import { RenderGlbApi } from "./renderGlb.js"; /** * Render or color objects and elements in the 3D scene. * * These APIs will only do visual changes to the proposal while extension is open. * None of the changes will be saved to the proposal. And when the extension is * closed we automatically clean up all changes made by these APIs. * * @remarks * Available via {@link auto.Forma | Forma}.{@link index.EmbeddedViewSdk.render | render}. */ export class RenderApi { #iframeMessenger; glb; geojson; elementColors; /** @hidden */ constructor(iframeMessenger) { this.#iframeMessenger = iframeMessenger; this.glb = new RenderGlbApi(iframeMessenger); this.geojson = new RenderGeojsonApi(iframeMessenger); this.elementColors = new ElementColorApi(iframeMessenger); } /** * Hide an element from the scene. * * @example * await Forma.render.hideElement({ path: "root/myElementPath" }) */ async hideElement(request) { await this.#iframeMessenger.sendRequest("scene/hidden-elements/set-visibility", { path: request.path, visible: false, }); } /** * Hide a set of elements from the scene. * * @example * await Forma.render.hideElementsBatch({ paths: ["root/myElement1", "root/myElement2"] }) */ async hideElementsBatch(request) { const pathsVisibilities = request.paths.map((p) => { return { path: p, visible: false, }; }); await this.#iframeMessenger.sendRequest("scene/hidden-elements/set-visibility-batch", pathsVisibilities); } /** * Unhide an element from the scene. * * @example * await Forma.render.unhideElement({ path: "root/myElementPath" }) */ async unhideElement(request) { await this.#iframeMessenger.sendRequest("scene/hidden-elements/set-visibility", { path: request.path, visible: true, }); } /** * Unhide a set of elements from the scene. * * @example * await Forma.render.unhideElementsBatch({ paths: ["root/myElement1", "root/myElement2"] }) */ async unhideElementsBatch(request) { const pathsVisibilities = request.paths.map((p) => { return { path: p, visible: true, }; }); await this.#iframeMessenger.sendRequest("scene/hidden-elements/set-visibility-batch", pathsVisibilities); } /** * Hides or unhides elements from the scene based on the `visible` parameter for each path. * * @example * await Forma.render.setElementsVisibility( { [ { "root/myElement1", true}, { "root/myElement2", false} ] } ) */ async setElementsVisibility(request) { await this.#iframeMessenger.sendRequest("scene/hidden-elements/set-visibility-batch", request.paths); } /** * Unhide all elements added by the hideElements function of this API from the scene. * * Called automatically when the extension is unloaded. * * @example * await Forma.render.unhideAllElements() */ async unhideAllElements() { await this.#iframeMessenger.sendRequest("scene/hidden-elements/unhide-all"); } /** * Add a mesh to the scene. * * @returns Unique identifier of the mesh object in the scene. * * @example * const {id} = await Forma.render.addMesh({ geometryData }), */ async addMesh(request) { return await this.#iframeMessenger.sendRequest("scene/render/add-mesh", request); } /** * Upsert an mesh in the scene. If the mesh does not exist, it will be added. * * @example * await Forma.render.updateMesh({ id: "myPreviouslyAddedGlbId", geometryData }) */ async updateMesh(request) { await this.#iframeMessenger.sendRequest("scene/render/update-mesh", request); } /** * Remove an existing mesh from the scene. * * @example * await Forma.render.remove({ id: "myPreviouslyAddedMeshId" }) */ async remove(request) { await this.#iframeMessenger.sendRequest("scene/render/remove", request); } /** * Remove all meshes added by this API from the scene. * * Called automatically when the extension is unloaded. * * @example * await Forma.render.cleanup() */ async cleanup() { await this.#iframeMessenger.sendRequest("scene/render/cleanup"); } }