[Frontend] 更換urn、新增電梯變色函式

This commit is contained in:
wanli 2022-12-04 01:06:51 +08:00
parent a443c06001
commit b9c2f76f11
2 changed files with 141 additions and 39 deletions

View File

@ -49,12 +49,15 @@
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.4.1/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.3.7/jstree.min.js"></script>
<script src="https://developer.api.autodesk.com/modelderivative/v2/viewers/7.*/viewer3D.js"></script>
<script src="js/forge/modeltest.js"></script>
<!--<script src="js/forge/AdnLevelSectionPanel.js"></script>-->
<script>
$(document).ready(function () {
launchViewer('dXJuOmFkc2sub2JqZWN0czpvcy5vYmplY3Q6dnNjb2RlX2ZvcmdlX3Rlc3QvJUUzJTgwJTkwJUU1JThGJUIwJUU1JThDJTk3JUU0JUI4JUFEJUU4JThGJUIxJUU1JUE0JUE3JUU2JUE4JTkzJUUzJTgwJTkxQVJDLm53ZA');
//launchViewer('dXJuOmFkc2sub2JqZWN0czpvcy5vYmplY3Q6dnNjb2RlX2ZvcmdlX3Rlc3QvJUUzJTgwJTkwJUU1JThGJUIwJUU1JThDJTk3JUU0JUI4JUFEJUU4JThGJUIxJUU1JUE0JUE3JUU2JUE4JTkzJUUzJTgwJTkxQVJDLm53ZA');
launchViewer('dXJuOmFkc2sub2JqZWN0czpvcy5vYmplY3Q6dGEzaHFzZmZ6cWJub3V4a3BsZGt1a3NldzRzajIxdzUtYmltc19tb2RlbHMvJUUzJTgwJTkwJUU1JThGJUIwJUU1JThDJTk3JUU0JUI4JUFEJUU4JThGJUIxJUU1JUE0JUE3JUU2JUE4JTkzJUUzJTgwJTkxQVJDMjAyMjEyMDEubndk');
});
function move1Floor() {
setElevatorSpeed(0.2);

View File

@ -1,15 +1,7 @@
var viewer;
// Initialize sensor values
//let sensorVals = [];
let fragProxy;
var targetFloorZ;
var elevatorSpeed;
// for (let i = 0; i < devices.length; i++) {
// sensorVals[i] = Math.random();
// }
function launchViewer(urn) {
var options = {
@ -19,37 +11,10 @@ function launchViewer(urn) {
Autodesk.Viewing.Initializer(options, () => {
viewer = new Autodesk.Viewing.GuiViewer3D(document.getElementById('forgeViewer'));
//viewer = new Autodesk.Viewing.Viewer3D(document.getElementById('forgeViewer'));
//viewer = new Autodesk.Viewing.Viewer3D(document.getElementById('forgeViewer'));//沒有工具列
viewer.start();
var documentId = 'urn:' + urn;
Autodesk.Viewing.Document.load(documentId, onDocumentLoadSuccess, onDocumentLoadFailure);
//test
// for (let i = 0; i < urn.length; i++) {
// Autodesk.Viewing.Document.load(urn[i]["urn"], async (doc) => {
// let viewables = doc.getRoot().getDefaultGeometry();
// let model = await viewer.loadDocumentNode(doc, viewables, {
// preserveView: false,
// keepCurrentModels: true,
// placementTransform: (new THREE.Matrix4()).setPosition(urn[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
});
}
@ -89,8 +54,65 @@ function onDocumentLoadSuccess(doc) {
var currSelection = viewer.getSelection();
var domElem = document.getElementById('id_printer');
domElem.innerText = currSelection[0];
//var color = new THREE.Vector4(255 / 255, 0, 0, 1);
//viewer.setThemingColor(751, color);
//var color = new THREE.Vector4(0, 1, 0, 1);
//viewer.setThemingColor(751, color);
//var dbid = currSelection[0]; //750;
//var it = NOP_VIEWER.model.getData().instanceTree;
//var indexinNames = it.nodeAccess.dbIdToIndex[dbid];
//var indexinStrings = it.nodeAccess.names[indexinNames];
//var nodeMainString = it.nodeAccess.strings[indexinStrings];
//var nodeSuffixString = it.nodeAccess.nameSuffixes[indexinStrings];
//var nodeFinalName = it.getNodeName(dbid);
//console.log("names: " + indexinNames + ", inStrings: " + indexinStrings + ", nodeMainString: " + nodeMainString + ", nodeSuffix: " + nodeSuffixString);
//console.log("nodeFinalName: " + nodeFinalName);
//var vp = viewer.impl.clientToViewport(event.canvasX, event.canvasY);
//var renderer = viewer.impl.renderer();
//var dbId = renderer.idAtPixel(vp.x, vp.y);
//if (dbId) {
// console.debug("Selected Id: " + dbId);
// //viewer.select(dbId);
// //viewer.impl.invalidate(true);
// viewer.model.getExternalIdMapping(data => console.log(data));
//}
//set color
//let nodeIdObj = 750;
//this.viewer.setThemingColor(nodeIdObj, red, this.model);
//const color = new THREE.Vector4(1.0, 0.0, 0.0, 0.5);
//viewer.setThemingColor(751, color, null, true);
var myDbid = currSelection;
viewer.getProperties(myDbid, function (e) {
//console.log('Entire object response ', e);
//console.log('Properties ', e.properties);
//foreach(item in e.properties) {
// if (item.displayName == "【tag_id】") {
// console.log(">> " + item.displayValue);
// }
//}
e.properties.forEach(function (item) {
if (item.displayName == "【tag_id】") {
console.log(">> " + item.displayValue);
}
});
});
});
viewer.addEventListener(Autodesk.Viewing.GEOMETRY_LOADED_EVENT, function () {
var instanceTree = viewer.model.getData().instanceTree;
var allDbIdsStr = Object.keys(instanceTree.nodeAccess.dbIdToIndex);
@ -143,9 +165,11 @@ function onDocumentLoadSuccess(doc) {
});
NOP_VIEWER.getProperties(NOP_VIEWER.getSelection(), data => console.log(data));
//NOP_VIEWER.getProperties(NOP_VIEWER.getSelection(), data => console.log(data));
}
function setElevatorFloor(floor) {
if (floor == 0)
targetFloorZ = 0;
@ -184,12 +208,13 @@ function setElevatorSpeed(speed) { //0.01 ~ 1
function movElevator() {
let tree = viewer.model.getData().instanceTree;
let tree = viewer.model.getData().instanceTree;//三組:(749,750,751),(755,756,757),(761,762,763)
let nodeId = 749; //10952;
let nodeId2 = 750;
let nodeId3 = 751;
let fragProxyZ = 0;
var movStatus = 0; // 0=no 1=up 2=down
changeColor(nodeId3);
if (fragProxy.position.z > targetFloorZ) {
@ -258,11 +283,17 @@ function movElevator() {
if (fragProxyZ >= targetFloorZ) {
requestAnimationFrame(movElevator);
}
else {
hideColor(nodeId3);
}
}
else if (movStatus == 1) {
if (fragProxyZ <= targetFloorZ) {
requestAnimationFrame(movElevator);
}
else {
hideColor(nodeId3);
}
}
@ -275,6 +306,16 @@ function movElevator() {
//viewer.impl.sceneUpdated(true);
}
function changeColor(nodeId) {
var color = new THREE.Vector4(0, 1, 0, 1);
viewer.setThemingColor(nodeId, color);
}
function hideColor(nodeId) {
var color = new THREE.Vector4(0, 1, 0, 0);
viewer.setThemingColor(nodeId, color);
}
//function movElevator() {
@ -341,6 +382,61 @@ function getAllDbIds(viewer) {
return allDbIdsStr.map(function (id) { return parseInt(id) });
}
//釋放viewer
function ReleaseViewer() {
viewer.finish();
viewer = null;
Autodesk.Viewing.shutdown();
}
//擴展類 刪除添加的所有DOM元素和事件
//ToolbarExtension.prototype.unload = function () {
// if (this.subToolbar) {
// this.viewer.toolbar.removeControl(this.subToolbar);
// this.subToolbar = null;
// }
//};
// ---- 模型剖面 ----
function getCutPlaneParam(idx, n) {
if (idx < 0 || !n) return;
const level = this.levels[idx];
if (!level) return;
//const precision = Autodesk.Viewing.Private.calculatePrecision( level.elevation );
const model = this.viewer.model;
const globalOffset = model.getData().globalOffset;
const units = model.getUnitString();
const elevRaw = Autodesk.Viewing.Private.convertUnits('ft', units, 1, level.elevation);
let d = elevRaw - globalOffset.z - 0.5;
if (n == 1)
d = -1 * d;
return new THREE.Vector4(0, 0, n, d);
}
function onButtonClicked() {
const upperSelector = document.getElementById('adn-upper-lvl-selector');
const lowerSelector = document.getElementById('adn-lower-lvl-selector');
if (!upperSelector || !lowerSelector)
return;
const upperIdx = upperSelector.selectedIndex;
const upperCutPlaneParam = this.getCutPlaneParam(upperIdx, 1);
const lowerIdx = lowerSelector.selectedIndex;
const lowerCutPlaneParam = this.getCutPlaneParam(lowerIdx, -1);
this.viewer.setCutPlanes([upperCutPlaneParam, lowerCutPlaneParam]);
}
function displayCuttedPlane() {
}
/**
* Autodesk.Viewing.Document.load() failuire callback.
*/
@ -413,4 +509,7 @@ async function loadHeatmaps(model) {
// This value can also be a room instead of a floor
const floorName = "01 - Entry Level";
dataVizExtn.renderSurfaceShading(floorName, sensorType, getSensorValue);
}