有一个类似的问题: How to get location with dbid in 2D
但尚未获得批准。
所以我有同样的问题,有没有办法在2D视图中使用dbid获取对象位置信息?
我们的产品要求标记某些对象并将dbids保存在外部数据库中,当下次打开模型时,我们需要使用这些ID并找到位置,然后绘制一些自定义形状突出那些对象。
我尝试使用viewer.impl.highlightObjectNode,但它只能在选中时显示对象,它在自定义虚拟化方面非常有限。
答案 0 :(得分:0)
以下是一些访问Forge片段网格信息的代码段,它可能会帮助您找到某个Forge查看器dbId
的位置。有关更多信息,请参阅此扩展: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 );
答案 1 :(得分:0)
您还可以查看" viewer.impl.fitToView"在viewer3D.js中,这个函数计算选定的对象联合边界框并使它们适合查看,所以我从这个函数中提取代码以获得对象边界框中心坐标与它的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();