Sam Curry
Sam Curry

Reputation: 475

How to convert viewer coordinates back to CAD coordinates?

I am trying to convert from the viewer (scene) coordinates back to the original CAD coordinates. I have seen people mentioning the global offset, but whenever I try to retrieve this, I get undefined. I assumed this means there is no offset. But when I compare two coordinates one in the viewer and one in CAD, they are definitely not equal. I came across the function NOP_VIEWER.model.getUnitScale(); however, this number did not seem to mean anything when applying it to the coordinates.

That being said, how can I convert back to the CAD coordinates from the viewer coordinates?

Upvotes: 0

Views: 629

Answers (1)

Eason Kang
Eason Kang

Reputation: 7070

The globalOffset is for 3D model commonly with my experience, you can apply the PageToModelTransform to your viewer points to transform them back to CAD. The following code snippet is extracted from a demo called viewer-dwgoffset written by our colleague.

Its key concept is using the VertexBufferReader to read vertices of a Forge Viewer 2D model, and get the corresponding transformation matrix to project points of the Viewer back to DWG coordinate system.

function GeometryCallback(viewer) {
    this.viewer = viewer;
}

GeometryCallback.prototype.onLineSegment = function(x1, y1, x2, y2, vpId) {
    var vpXform = this.viewer.model.getPageToModelTransform(vpId);

    var pt1 = new THREE.Vector3().set(x1, y1, 0).applyMatrix4(vpXform);
    var pt2 = new THREE.Vector3().set(x2, y2, 0).applyMatrix4(vpXform);

    console.log('Line segment vertices in CAD coordinate system', {
        pointX1: pt1.x,
        pointY1: pt1.y,
        pointX2: pt2.x,
        pointY2: pt2.y
    });
}

GeometryCallback.prototype.onCircularArc = function(cx, cy, start, end, radius, vpId) {
};

GeometryCallback.prototype.onEllipticalArc = function(cx, cy, start, end, major, minor, tilt, vpId) {

};

var it = viewer.model.getData().instanceTree;
it.enumNodeFragments( dbId, function( fragId ) {
    var m = viewer.impl.getRenderProxy(viewer.model, fragId);
    var vbr = new Autodesk.Viewing.Private.VertexBufferReader(m.geometry, viewer.impl.use2dInstancing);
    vbr.enumGeomsForObject(dbId, new GeometryCallback());
});

Hope it helps.

Upvotes: 1

Related Questions