Wesley Reed
Wesley Reed

Reputation: 89

How can I get the viewer coordinates of AutoCAD geometry?

I am using the 2D Autodesk Forge Viewer, and I'm looking for a way to determine the X,Y coordinate of a block reference object from AutoCAD.

I have the dbID for the geometry element, and. I can get some information through NOP_VIEWER.getProperties() and NOP_VIEWER.getDimensions(), but neither of those have the X,Y coordinate.

Upvotes: 1

Views: 889

Answers (2)

Wesley Reed
Wesley Reed

Reputation: 89

With help from Xiaodong below, I was able to devise the following solution to get the X,Y coordinate of an object using its dbId

const geoList = NOP_VIEWER.model.getGeometryList().geoms;
const readers = [];

for (const geom of geoList) {
  if (geom) {
    readers.push(new Autodesk.Viewing.Private.VertexBufferReader(geom, NOP_VIEWER.impl.use2dInstancing));
  }
}

const findObjectLocation = (objectId) => {
  for (const reader of readers) {
    let result;
    reader.enumGeomsForObject(objectId, {
      onLineSegment: (x, y) => {
        result = { x, y };
      },
    });

    if (result) {
      return result;
    }
  }

  throw new Error(`Unable to find requested object`);
};

Upvotes: 2

Xiaodong Liang
Xiaodong Liang

Reputation: 2206

As I remember, it is true the position data is not available with block entity. I will check with engineer team if there is any comment about the native position data of block. One alterative is to use Forge Design Automation of AutoCAD to extract the data yourself, while it would require additional more code.

After Forge translates the source DWG, the entities are converted to primitives. By API, it is feasible to get geometry info of the primitives, such as start point of line, center of circle. The two blogs tell in details:

https://forge.autodesk.com/blog/working-2d-and-3d-scenes-and-geometry-forge-viewer https://forge.autodesk.com/blog/working-2d-and-3d-scenes-and-geometry-forge-viewer

Essentially, it uses the callback function:

  VertexBufferReader.prototype.enumGeomsForObject = function(dbId, callback)

The callback object needs these optional functions:

• onLineSegment(x0, y0, x1, y1, viewport_id)

• onCircularArc(centerX, centerY, startAngle, endAngle, radius, viewport_id)

• onEllipticalArccenterX, centerY, startAngle, endAngle, major, minor, tilt, viewport_id)

• onTriangleVertex(x, y, viewport_id)

.

Upvotes: 1

Related Questions