要在Autodesk Forge Viewer中加载自定义特性,您需要遵循以下步骤:
<script src="https://developer.api.autodesk.com/modelderivative/v2/viewers/7.*/viewer3D.min.js"></script>
let viewer;
function initializeViewer() {
const options = {
env: 'AutodeskProduction',
getAccessToken: function(onTokenReady) {
const token = 'YOUR_ACCESS_TOKEN';
const timeInSeconds = 3600; // Use value provided by Forge Authentication API
onTokenReady(token, timeInSeconds);
}
};
Autodesk.Viewing.Initializer(options, () => {
viewer = new Autodesk.Viewing.GuiViewer3D(document.getElementById('forgeViewer'), { extensions: ['Autodesk.DocumentBrowser'] });
viewer.start();
const documentId = 'urn:YOUR_URN';
Autodesk.Viewing.Document.load(documentId, onDocumentLoadSuccess, onDocumentLoadFailure);
});
function onDocumentLoadSuccess(doc) {
const viewables = doc.getRoot().getDefaultGeometry();
viewer.loadDocumentNode(doc, viewables);
}
function onDocumentLoadFailure(viewerErrorCode) {
console.error('onDocumentLoadFailure() - errorCode:' + viewerErrorCode);
}
}
initializeViewer();
请将YOUR_ACCESS_TOKEN
替换为您的Forge应用程序的访问令牌,将YOUR_URN
替换为您要加载的模型的URN。
class CustomExtension extends Autodesk.Viewing.Extension {
constructor(viewer, options) {
super(viewer, options);
this.viewer = viewer;
}
load() {
console.log('CustomExtension loaded');
// 在这里添加您的自定义代码
return true;
}
unload() {
console.log('CustomOverride unloaded');
// 在这里添加您的自定义代码
return true;
}
}
Autodesk.Viewing.theExtensionManager.registerExtension('CustomExtension', CustomExtension);
// 在initializeViewer函数中添加以下代码来加载自定义扩展
viewer.loadExtension('CustomExtension');
领取专属 10元无门槛券
手把手带您无忧上云