2024-11-08 07:51:28 +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) {
|
|
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
|
Autodesk.Viewing.Initializer(
|
|
|
|
|
{ env: "AutodeskProduction", getAccessToken },
|
|
|
|
|
function () {
|
|
|
|
|
const config = {
|
|
|
|
|
extensions: ["Autodesk.DocumentBrowser"],
|
|
|
|
|
};
|
|
|
|
|
const viewer = new Autodesk.Viewing.GuiViewer3D(container, config);
|
|
|
|
|
viewer.start();
|
2024-11-08 09:32:57 +01:00
|
|
|
|
viewer.addEventListener(Autodesk.Viewing.TOOLBAR_CREATED_EVENT, onToolbarCreated);
|
2024-11-08 07:51:28 +01:00
|
|
|
|
viewer.setTheme("light-theme");
|
|
|
|
|
resolve(viewer);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function loadModel(viewer, urn) {
|
|
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
|
function onDocumentLoadSuccess(doc) {
|
|
|
|
|
resolve(viewer.loadDocumentNode(doc, doc.getRoot().getDefaultGeometry()));
|
|
|
|
|
}
|
|
|
|
|
function onDocumentLoadFailure(code, message, errors) {
|
|
|
|
|
reject({ code, message, errors });
|
|
|
|
|
}
|
|
|
|
|
viewer.setLightPreset(0);
|
|
|
|
|
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
|
|
|
|
|
);
|
|
|
|
|
};
|