【问题标题】:How to get location with dbid in 2D view如何在 2D 视图中使用 dbid 获取位置
【发布时间】:2018-06-07 06:43:29
【问题描述】:

有一个类似的问题: How to get location with dbid in 2D

但尚未得到答复。

所以我有同样的问题,有没有办法在 2D 视图中使用 dbid 获取对象位置信息?

我们的产品要求标记某些对象并将 dbids 保存在外部数据库中,下次打开模型时,我们需要使用这些 ids 并找到位置,然后绘制一些自定义形状来突出显示这些对象。

我尝试使用viewer.impl.highlightObjectNode,但它只能显示被选中的对象,它对自定义虚拟化非常有限。

【问题讨论】:

    标签: javascript autodesk-forge autodesk-viewer


    【解决方案1】:

    这里有一些代码sn-ps来访问Forge碎片的网格信息,它可以帮助你找到某个Forge查看器dbId的位置。更多信息可以参考这个extension:https://github.com/Autodesk-Forge/library-javascript-viewer-extensions/blob/master/src/Autodesk.ADN.Viewing.Extension.MeshData/Autodesk.ADN.Viewing.Extension.MeshData.js

    此外,viewer.impl.highlightObjectNode 仅用于突出显示元素,根据我的经验,它不能用于其他用途。

      function getLeafFragIds( model, leafId ) {
        const instanceTree = model.getData().instanceTree;
        const fragIds = [];
    
        instanceTree.enumNodeFragments( leafId, function( fragId ) {
          fragIds.push( fragId );
        });
    
        return fragIds;
      }
    
      function getComponentGeometry( viewer, dbId ) {
    
        const fragIds = getLeafFragIds( viewer.model, dbId );
    
        let matrixWorld = null;
    
        const meshes = fragIds.map( function( fragId ) {
    
          const renderProxy = viewer.impl.getRenderProxy( viewer.model, fragId );
    
          const geometry = renderProxy.geometry;
          const attributes = geometry.attributes;
          const positions = geometry.vb ? geometry.vb : attributes.position.array;
    
          const indices = attributes.index.array || geometry.ib;
          const stride = geometry.vb ? geometry.vbstride : 3;
          const offsets = geometry.offsets;
    
          matrixWorld = matrixWorld || renderProxy.matrixWorld.elements;
    
          return {
            positions,
            indices,
            offsets,
            stride
          };
        });
    
        return {
          matrixWorld,
          meshes
        };
      }
    
      var meshInfo = getComponentGeometry( viewer, 1234 );
    

    【讨论】:

      【解决方案2】:

      你也可以在viewer3D.js中勾选“viewer.impl.fitToView”,这个函数计算选定的对象联合边界框并使其适合查看,所以我从这个函数中提取代码来获取对象边界框中心坐标及其dbid

      function getObjectBound2D(viewer, objectId) {
          var model = viewer.model;
          // This doesn't guarantee that an object tree will be created but it will be pretty likely
          var bounds, bc, i;
          if (model.is2d()) {
              bounds = new THREE.Box3();
              // move this next one up into the calling method
              bc = new avp.BoundsCallback(bounds);
      
              var dbId2fragId = model.getData().fragments.dbId2fragId;
      
              var fragIds = dbId2fragId[objectId];
              // fragId is either a single vertex buffer or an array of vertex buffers
              if (Array.isArray(fragIds)) {
                  for (var j = 0; j < fragIds.length; j++) {
                      // go through each vertex buffer, looking for the object id
                      find2DBounds(model, fragIds[j], objectId, bc);
                  }
              } else if (typeof fragIds === 'number') {
                  // go through the specific vertex buffer, looking for the object id
                  find2DBounds(model, fragIds, objectId, bc);
              }
      
              // should have some real box at this point; check
              if (!bounds.empty()) {
                  return bounds;
              }
          }
      
          function find2DBounds(model, fragId, dbId, bc) {
              var mesh = model.getFragmentList().getVizmesh(fragId);
              var vbr = new avp.VertexBufferReader(mesh.geometry);
              vbr.enumGeomsForObject(dbId, bc);
          }
      
          function find2DLayerBounds(model, fragId, bc) {
              var mesh = model.getFragmentList().getVizmesh(fragId);
              var vbr = new avp.VertexBufferReader(mesh.geometry);
              var visibleLayerIds = that.getVisibleLayerIds();
              vbr.enumGeomsForVisibleLayer(visibleLayerIds, bc);
          }
      };
      
      var objBoundingbox = getObjectBound2D(viewer,dbid);
      
      var objCenterCoordinates = objBoundingbox.center();
      

      【讨论】:

        猜你喜欢
        • 2017-06-20
        • 1970-01-01
        • 2018-02-25
        • 2020-03-13
        • 2011-09-15
        • 2018-03-13
        • 2021-10-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多