0

I'm parsing 2D dwg based geometry. lines, polylines, easy stuff. Based on https://stackoverflow.com/a/50693640/2337681 I use a VertexBufferReader's enumGeomsForObject function to obtain line segments and vertexes. Those are individual callbacks for all segments of a path. The results come in order from what I can see - but how do I know when the enumeration is complete? With closed polylines one could determine endpoint of the latest segment is close to equal to the startpoint of the first segment. But that won't work for open polygons...

Gregor
  • 134
  • 8

1 Answers1

1

With my experience, VertexBufferReader#enumGeomsForObject is a synchronous function, and I don't see any async codes in the implementation of the VertexBufferReader. So, you can pass a data container to the callback, the 2nd of the enumGeomsForObject method, to store the data you want.

Here is my test code, and the GeometryCallback.data of the last enumeration (console.log( 'enuming node fragments', gc.data );) is the same as data of the result of 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.getInstanceTree();
var gc = new GeometryCallback(NOP_VIEWER);
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 );
Eason Kang
  • 6,155
  • 1
  • 7
  • 24
  • Great. Thank you - exactly what I was after. 1) adding the data container to the callback. 2) noting that vbr.enumGeomsForObject(dbId, gc); executes synchronously. – Gregor Feb 11 '19 at 15:04