[英]Autodesk Forge. "model" is always undefined,
這是我在 Autodesk Forge 和 Javascript 中的第一個項目,所以我很迷茫。
這是我的代碼:
async function loadHeatmaps(model){
const dataVizExtn = await viewer.loadExtension("Autodesk.DataVisualization");
// Given a model loaded from Forge
const structureInfo = new Autodesk.DataVisualization.Core.ModelStructureInfo(model);
const devices = [
{
id: "Oficina 6",
name:"Oficina-",
position: { x: 22.475382737884104, y: 7.4884431474006163, z: 3.0 },
sensorTypes: ["temperature", "humidity"]
}
];
var offset = Autodesk.viewer.model.getGlobalOffset();
removeOffset(devices[0],offset)
// Generates `SurfaceShadingData` after assigning each device to a room.
const shadingData = await Autodesk.structureInfo.generateSurfaceShadingData(devices);
// Use the resulting shading data to generate heatmap from.
await dataVizExtn.setupSurfaceShading(model, shadingData);
// Register color stops for the heatmap. Along with the normalized sensor value
// in the range of [0.0, 1.0], `renderSurfaceShading` will interpolate the final
// heatmap color based on these specified colors.
const sensorColors = [0x0000ff, 0x00ff00, 0xffff00, 0xff0000];
// Set heatmap colors for temperature
const sensorType = "temperature";
dataVizExtn.registerSurfaceShadingColors(sensorType, sensorColors);
// Function that provides sensor value in the range of [0.0, 1.0]
function getSensorValue(surfaceShadingPoint, sensorType) {
// The `SurfaceShadingPoint.id` property matches one of the identifiers passed
// to `generateSurfaceShadingData` function. In our case above, this will either
// be "cafeteria-entrace-01" or "cafeteria-exit-01".
const deviceId = surfaceShadingPoint.id;
// Read the sensor data, along with its possible value range
let sensorValue = readSensorValue(deviceId, sensorType);
const maxSensorValue = getMaxSensorValue(sensorType);
const minSensorValue = getMinSensorValue(sensorType);
// Normalize sensor value to [0, 1.0]
sensorValue = (sensorValue - minSensorValue) / (maxSensorValue - minSensorValue);
return clamp(sensorValue, 0.0, 1.0);
}
// This value can also be a room instead of a floor
const floorName = "01 - Entry Level";
dataVizExtn.renderSurfaceShading(floorName, sensorType, getSensorValue);
}
var viewer;
function view_document(token_id, urns, derivative) {
var options = {
env: 'AutodeskProduction',
api: derivative,
getAccessToken: function(onTokenReady) {
var token = token_id; //token from authentification
var timeInSeconds = 3600;
onTokenReady(token, timeInSeconds);
}//input for viewer initializer function
};
Autodesk.Viewing.Initializer(options, function() {
//fetch the forgeViewer id in the html
var htmlDiv = document.getElementById('forgeViewer');
//create the view at the html div
viewer = new Autodesk.Viewing.GuiViewer3D(htmlDiv);
//start viewer
var startedCode = viewer.start();
if (startedCode > 0) {
console.error('Failed to create a Viewer: WebGL not supported.');
return;
}
console.log('Initialization complete, loading a model next...');
for (let i = 0; i < urns.length; i ++) {
Autodesk.Viewing.Document.load(urns[i]["urn"], (doc) => {
var viewables = doc.getRoot().getDefaultGeometry();
viewer.loadDocumentNode(doc, viewables,{
preserveView: false,
keepCurrentModels: true,
placementTransform: (new THREE.Matrix4()).setPosition(urns[i]["xform"]), keepCurrentModels: true,
globalOffset: {x:0,y:0,z:0}
})
});
}
loadHeatmaps(this.viewer.model);
});
}
它應該工作。 我從一個教程網站上復制了它,並且到處都看到了類似的代碼。 但是當我加載它時,這個錯誤出現在日志控制台上:控制台上的錯誤
DG_V2:162 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'model') at loadHeatmaps (DG_V2:162:35)
請你幫助我好嗎? 我有一種沒有任何作用的感覺。 Using model, this.model, autodesk.model, autodesk.viewer.model, or any other kind of combination is not working and this is driving me crazy...
謝謝:! :)
加載模式是異步任務,因此在完全加載之前您無法獲得 model。 這是您的代碼片段的修訂版。
function view_document(token_id, urns, derivative) {
let options = {
env: 'AutodeskProduction',
api: derivative,
getAccessToken: function(onTokenReady) {
let token = token_id; //token from authentification
let timeInSeconds = 3600;
onTokenReady(token, timeInSeconds);
} //input for viewer initializer function
};
Autodesk.Viewing.Initializer(options, function() {
//fetch the forgeViewer id in the html
let htmlDiv = document.getElementById('forgeViewer');
//create the view at the html div
viewer = new Autodesk.Viewing.GuiViewer3D(htmlDiv);
//start viewer
let startedCode = viewer.start();
if (startedCode > 0) {
console.error('Failed to create a Viewer: WebGL not supported.');
return;
}
console.log('Initialization complete, loading a model next...');
for (let i = 0; i < urns.length; i++) {
Autodesk.Viewing.Document.load(urns[i]["urn"], (doc) => {
let viewables = doc.getRoot().getDefaultGeometry();
let model = await viewer.loadDocumentNode(doc, viewables, {
preserveView: false,
keepCurrentModels: true,
placementTransform: (new THREE.Matrix4()).setPosition(urns[i]["xform"]),
keepCurrentModels: true,
globalOffset: {
x: 0,
y: 0,
z: 0
}
});
await viewer.waitForLoadDone(); //!<<< Wait for loading materials, properties and geometries for this model (URN)
});
}
loadHeatmaps( viewer.getAllModels()[0] ); //!<<< equals to viewer.model
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.