首页 > 解决方案 > 将 VertexBufferReader 与 GeometryCallback 一起使用时 - 我如何知道枚举何时完成?

问题描述

我正在解析基于 2D dwg 的几何图形。线,折线,简单的东西。基于 https://stackoverflow.com/a/50693640/2337681我使用 VertexBufferReader 的 enumGeomsForObject 函数来获取线段和顶点。这些是路径所有段的单独回调。结果按我所见的顺序排列 - 但我怎么知道枚举何时完成?使用闭合的折线,可以确定最新段的端点接近等于第一段的起点。但这不适用于开放的多边形......

标签: autodesk-forge

解决方案


以我的经验,VertexBufferReader#enumGeomsForObject是一个同步函数,我在VertexBufferReader. 因此,您可以将数据容器传递给回调方法的第二个enumGeomsForObject方法,以存储您想要的数据。

这是我的测试代码,GeometryCallback.data最后一个枚举(console.log( 'enuming node fragments', gc.data );)的数据与结果的数据相同console.log( 'enumed node fragments', gc.data );

function GeometryCallback(viewer) {
    this.viewer = viewer;
    this.data = [];
}

GeometryCallback.prototype.onLineSegment = function(x1, y1, x2, y2, vpId) {
   console.log( 'On Line segment', this.data );
   this.data.push({
        type: 'Line segment',
        x1, y1, x2, y2, vpId
  });
}

GeometryCallback.prototype.onCircularArc = function(cx, cy, start, end, radius, vpId) {
  console.log( 'On Circular arc', this.data );
  this.data.push({
        type: 'CircularArc',
        cx, cy, start, end, radius, vpId
  });
};

GeometryCallback.prototype.onEllipticalArc = function(cx, cy, start, end, major, minor, tilt, vpId) {
 console.log( 'On Elliptical arc', this.data );
 this.data.push({
        type: 'EllipticalArc',
        cx, cy, start, end, major, minor, tilt, vpId
 });
};

var it = NOP_VIEWER.model.getData().instanceTree;
var gc = new GeometryCallback();
var dbId = NOP_VIEWER.getSelection()[0];
it.enumNodeFragments( dbId, function( fragId ) {
    var m = NOP_VIEWER.impl.getRenderProxy(NOP_VIEWER.model, fragId);
    var vbr = new Autodesk.Viewing.Private.VertexBufferReader(m.geometry, NOP_VIEWER.impl.use2dInstancing);
    vbr.enumGeomsForObject(dbId, gc);

    console.log( 'enuming node fragments', gc.data );
});

console.log( 'enumed node fragments', gc.data );

推荐阅读