APS_NodeJS_MultiModel/wwwroot/viewer.js

90 lines
2.6 KiB
JavaScript
Raw Permalink Normal View History

2024-11-24 08:52:39 +01:00
async function getAccessToken(callback) {
try {
const resp = await fetch("/api/auth/token");
if (!resp.ok) {
throw new Error(await resp.text());
}
const { access_token, expires_in } = await resp.json();
callback(access_token, expires_in);
} catch (err) {
alert("Could not obtain access token. See the console for more details.");
console.error(err);
}
}
export function initViewer(container, urns) {
const viewerOptions = {
extensions: [],
};
Autodesk.Viewing.Initializer(
{ env: "AutodeskProduction", getAccessToken },
() => {
var viewer = new Autodesk.Viewing.Private.GuiViewer3D(
container,
viewerOptions
);
viewer.start();
viewer.addEventListener(
Autodesk.Viewing.TOOLBAR_CREATED_EVENT,
onToolbarCreated
);
urns.map((m) => {
addViewable(viewer, m);
});
}
);
}
async function addViewable(viewer, urn, xform, offset) {
return new Promise(function (resolve, reject) {
function onDocumentLoadSuccess(doc) {
const viewable = doc.getRoot().getDefaultGeometry();
const options = {
//preserveView: true,
keepCurrentModels: true,
modelSpace: true, // 2D drawings
applyRefPoint: true, // 3D shared coordinates
applyScaling: 'm', // force all models to same scale
globalOffset: {x:0,y:0,z:0}, // force all models to origin
//placementTransform: (new THREE.Matrix4()).setPosition(m.xform)
};
if (xform) {
options.placementTransform = xform;
}
if (offset) {
options.globalOffset = offset;
}
viewer
.loadDocumentNode(doc, viewable, options)
.then(resolve)
.catch(reject)
// .then(onToolbarCreated(viewer));
}
function onDocumentLoadFailure(code) {
console.error(`Document load failed with error code: ${code}`);
console.error(`Failed URN: ${urn}`);
// console.error(`Access Token: ${accessToken}`); // Log the access token if possible
reject(`Could not load document (${code}).`);
}
console.log(urn);
Autodesk.Viewing.Document.load(
"urn:" + urn,
onDocumentLoadSuccess,
onDocumentLoadFailure
);
});
}
const onToolbarCreated = (e) => {
const group = viewer.toolbar.getControl("settingsTools");
group.removeControl("toolbar-modelStructureTool");
group.removeControl("toolbar-propertiesTool");
//group.removeControl('toolbar-settingsTool');
//group.removeControl('toolbar-fullscreenTool');
viewer.removeEventListener(
Autodesk.Viewing.TOOLBAR_CREATED_EVENT,
onToolbarCreated
);
};