如何使用Autodesk Forge API从2D视图获取模型的位置?

时间:2019-01-29 06:52:33

标签: autodesk-forge

我可以在3D视图中获取带有边界框的位置,但是在2D视图中无法获取正确的位置。

以下是获取边界框的代码。

得到边界框后,我计算并获得该框的中心点。 当我将此代码用于从Revit导入的2D视图时,会得到错误的值。

获取2D对象位置的正确方法是什么?

static getWorldBoundingBox(model, dbId) {
  return new Promise(async (resolve, reject) => {
    try {
      var fragIds = await ViewerToolkit.getFragIds(model, dbId);
      if (!fragIds.length) {
        return reject('No geometry, invalid dbId?');
      }
      var fragList = model.getFragmentList();
      var fragbBox = new THREE.Box3();
      var nodebBox = new THREE.Box3();
      fragIds.forEach(function(fragId) {
        fragList.getWorldBounds(fragId, fragbBox);
        nodebBox.union(fragbBox);
      });
      return resolve(nodebBox);
    } catch (ex) {
      return reject(ex);
    }
  });
}

1 个答案:

答案 0 :(得分:0)

如果您参考viewer.impl.fitToView()的源代码,则有几个函数可以用来计算边界框,下面是一些代码:

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);
}