yskang

threejs-viewer-27

May 12th, 2021 (edited)
329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //修改 js/Viwer.js
  2.  
  3. onMouseClicked(event) {
  4.     // ... Other code snippets
  5.     this.selection.push(mesh.expressID);
  6.  
  7.     // ... Other code snippets
  8.     const propResult = mesh.parent.loader.getIfcItemInformation(mesh.expressID);
  9.     if(!propResult) return;
  10.  
  11.     const data = propResult.arguments;
  12.     const ifcGUID = data[0];
  13.     const objectName = data[2];
  14.  
  15.     const propsData = {
  16.       dbId: data.ID,
  17.       externalId: ifcGUID,
  18.       name: objectName,
  19.       properties: {}
  20.     };
  21.  
  22.     propsData.properties['Name'] = data[2];
  23.     propsData.properties['Type'] = data[4];
  24.     propsData.properties['Material'] = null;
  25.     propsData.properties['Layer'] = null;
  26.     propsData.properties['IFC Element'] = null;
  27.     propsData.properties['Tag'] = data[7];
  28.  
  29.     this.propertyPanel.setTitle(propsData.name);
  30.     this.propertyPanel.setProperties(propsData.properties);
  31.     // ... Other code snippets
  32. }
  33.  
  34. // ... Other code snippets
  35. // 整個取代
  36. loadModel(url) {
  37.     if (typeof url != 'string')
  38.       throw new Error('Invalid model url');
  39.  
  40.     const ext = url.split('.').pop();
  41.     if (ext.toLowerCase() === 'ifc') {
  42.       const ifcLoader = new IfcLoader();
  43.       ifcLoader.load(url, (geometry) => {
  44.         geometry.isIFC = true;
  45.         geometry.loader = ifcLoader;
  46.  
  47.         this.scene.add(geometry);
  48.         this.models.push(geometry);
  49.  
  50.         this.render();
  51.         this.fitToView();
  52.       })
  53.     } else {
  54.       const objectLoader = new THREE.ObjectLoader();
  55.       objectLoader.load(url, (obj) => {
  56.         obj.loader = objectLoader;
  57.         this.scene.add(obj);
  58.         this.models.push(obj);
  59.         this.render();
  60.  
  61.         this.fitToView();
  62.       });
  63.     }
  64. }
  65.  
  66. // ... Other code snippets
  67. // 整個取代
  68. hitTestViewport(vpVec) {
  69.     const raycaster = new THREE.Raycaster();
  70.     raycaster.setFromCamera(vpVec, this.camera);
  71.  
  72.     const ifcObjects = [];
  73.     this.models.forEach(md => {
  74.       if (md.isIFC && md.children) {
  75.         ifcObjects.push(...md.children);
  76.       }
  77.     });
  78.  
  79.     const intersects = raycaster.intersectObjects(ifcObjects, true);
  80.     console.log(intersects);
  81.  
  82.     return intersects[0];
  83. }
  84.  
  85. // ... Other code snippets
  86.  
  87. buildContextMenu() {
  88.     // ... Other code snippets
  89.  
  90.     const callBack = (key) => {
  91.       let objects, dbId;
  92.  
  93.       switch (key) {
  94.         case 'hide-selected':
  95.          // ... Other code snippets
  96.  
  97.           objects = this.models.map(m => m.getObjectByProperty('expressID', dbId));
  98.          // ... Other code snippets
  99.  
  100.           break;
  101.         case 'isolate-selected':
  102.          // ... Other code snippets
  103.  
  104.           this.models.forEach(m => {
  105.             m.traverse(child => {
  106.               if (child.type !== 'Mesh') return;
  107.  
  108.               if (child.expressID === dbId) {
  109.                 child.visible = true;
  110.                 return;
  111.               }
  112.  
  113.               child.visible = false;
  114.             });
  115.           });
  116.           break;
  117.           // ... Other code snippets
  118.       }
  119.     };
  120.  
  121.     // ... Other code snippets
  122. }
  123.  
  124. // ... Other code snippets
  125. // 整個取代
  126. getFitBounds(dbIds) {
  127.     let targets = [];
  128.  
  129.     if (Array.isArray(dbIds) && dbIds.length > 0) {
  130.       this.models.forEach(m => {
  131.         m.traverse(child => {
  132.           if (child.type !== 'Mesh' || !dbIds.includes(child.expressID)) return;
  133.  
  134.           targets.push(child);
  135.         });
  136.       });
  137.     } else {
  138.       targets = this.models.concat();
  139.     }
  140.  
  141.     // get bounding box of object - this will be used to setup navigation and camera
  142.     const bounds = new THREE.Box3();
  143.  
  144.     targets.forEach((object) => {
  145.       bounds.expandByObject(object);
  146.     });
  147.  
  148.     return bounds;
  149.   }
  150.  
  151. // Put your three.js codes below
  152. (async function () {
  153.   const SimpleContextMenu = await require('simple-context-menu');
  154.   window.SimpleContextMenu = SimpleContextMenu.default;
  155.  
  156.   document.addEventListener(
  157.     'DOMContentLoaded',
  158.     () => {
  159.       const container = document.getElementById('viewer');
  160.       const viewer = new Viewer(container);
  161.       viewer.init();
  162.  
  163.       viewer.loadModel('../Models/Technical_school-current_m_rooms.ifc');
  164.       window.NOP_VIEWER = viewer;
  165.     });
  166. })();
  167.  
  168. // 修改 index.html
  169.  
  170. <script type="module" src="js/IfcLoader.js"></script>
  171.  
Add Comment
Please, Sign In to add comment