嗨,我真的很想知道为什么当我尝试通过特定 dbId 加载元素时只能显示单个项目。我传入一个像这样的数组
[12345, 6789]
它只会显示一个元素。我已经检查了我传入的外部 id 是否正确返回了 objectId,并且我还确保 js 正在接收 dbId 数组的正确数据结构。同样奇怪且可能相关的是元素加载但不适合查看。
这是我的代码:
async function loadAndInitializeForgeViewer(urn, accessToken, dbIds) {
if (typeof Autodesk === 'undefined') {
console.log("Autodesk namespace not found, loading Forge Viewer scripts...");
try {
// Load Forge Viewer CSS
await new Promise((resolve, reject) => {
var link = document.createElement('link');
link.rel = 'stylesheet';
link.href = 'https://developer.api.autodesk.com/modelderivative/v2/viewers/style.min.css';
link.onload = resolve;
link.onerror = reject;
document.head.appendChild(link);
});
// Load Forge Viewer JS
await new Promise((resolve, reject) => {
var script = document.createElement('script');
script.src = 'https://developer.api.autodesk.com/modelderivative/v2/viewers/viewer3D.min.js';
script.onload = resolve;
script.onerror = reject;
document.head.appendChild(script);
});
console.log("Forge Viewer scripts loaded successfully.");
} catch (error) {
console.error("Error loading Forge Viewer scripts:", error);
return;
}
} else {
console.log("Forge Viewer scripts already loaded.");
}
await initializeForgeViewer(urn, accessToken, dbIds);
}
let globalViewer = null;
async function initializeForgeViewer(encodedUrn, accessToken, dbIds) {
let options = {
env: 'AutodeskProduction',
getAccessToken: (callback) => {
callback(accessToken, 3600); // Access token expires in 3600 seconds (1 hour)
}
};
Autodesk.Viewing.Initializer(options, async () => {
let viewerDiv = document.getElementById('forgeViewerContainer');
globalViewer = new Autodesk.Viewing.GuiViewer3D(viewerDiv);
globalViewer.start();
let documentId = 'urn:' + encodedUrn;
Autodesk.Viewing.Document.load(documentId, async (doc) => {
let viewables = doc.getRoot().getDefaultGeometry();
if (!viewables) {
console.error("No viewable geometry found in the document.");
return;
}
// Define options to load specific elements by their dbIds
const loadOptions = {
ids: dbIds // Use the dynamic dbIds array to load all specified elements
};
console.log("Loading model with specified element IDs:", dbIds);
try {
await globalViewer.loadDocumentNode(doc, viewables, loadOptions);
console.log("Model loaded with specified element IDs only.");
if (dbIds && dbIds.length > 0) {
globalViewer.fitToView(dbIds);
console.log("Fitting view to the specified elements.");
}
} catch (error) {
console.error("Error loading document node:", error);
}
}, (errorCode) => {
console.error("Could not load document. Error code:", errorCode);
});
});
}
看起来您是在运行时而不是加载时嵌入查看器。即。
// Load Forge Viewer CSS
await new Promise((resolve, reject) => {
var link = document.createElement('link');
link.rel = 'stylesheet';
link.href = 'https://developer.api.autodesk.com/modelderivative/v2/viewers/style.min.css';
link.onload = resolve;
link.onerror = reject;
document.head.appendChild(link);
});
// Load Forge Viewer JS
await new Promise((resolve, reject) => {
var script = document.createElement('script');
script.src = 'https://developer.api.autodesk.com/modelderivative/v2/viewers/viewer3D.min.js';
script.onload = resolve;
script.onerror = reject;
document.head.appendChild(script);
});
console.log("Forge Viewer scripts loaded successfully.");
} catch (error) {
console.error("Error loading Forge Viewer scripts:", error);
return;
}
您可以先删除它,然后将查看器加载为典型运行时,然后测试消息传递和 fitToView() 吗?
我认为这可能会妨碍...