This article explains how to transform an entity from AutoCAD Model Space to the 2D view of LMV, assuming you know the entity’s coordinates in UCS (User Coordinate System).
Example
Let’s say you have a circle in AutoCAD Model Space with the following properties:
- Coordinates:
(535222.8838, 726697.4053, 0) - Radius:
800
Steps
1. Obtain Viewport ID
The code snippet retrieves information about viewports using NOP_VIEWER.model.getData().viewports.
It then extracts the ID of the most recently used viewport (vpId).
2. Get Transformation Matrix
The code uses NOP_VIEWER.model.getPageToModelTransform(vpId) to obtain a transformation matrix (xform). This matrix converts coordinates from the LMV 2D view (page space) to AutoCAD Model Space.
To transform from Model Space to the 2D view, we need the inverse of this matrix, which is calculated using xform.invert() and stored in modelToPage.
3. Decompose Matrix
The code snippet retrieves scale factor from decomposing transformation matrix.
modelToPage.decompose(pos, rot, scale);
4. Set Up Edit2D Tool
The code loads the Autodesk.Edit2D extension using NOP_VIEWER.loadExtension.
It then registers the default tools provided by the extension and makes them accessible through window.edit2d and window.tools.
Finally, it retrieves the current layer using window.layer.
5. Create and Transform the Circle
The code creates a new Autodesk.Edit2D.Circle object with the specified coordinates and radius.
The applyMatrix4(modelToPage) method is used to transform the circle center from Model Space to the 2D view using the previously calculated transformation matrix.
6. Draw the Circle
The transformed circle object is added to the current layer using layer.addShape(circle).
Code
/* run this one by one */
const edit2d = await NOP_VIEWER.loadExtension('Autodesk.Edit2D');
edit2d.registerDefaultTools();
window.edit2d = NOP_VIEWER.getExtension('Autodesk.Edit2D');
window.layer = edit2d.defaultContext.layer;
window.tools = edit2d.defaultTools;
function drawCircle(x, y, r) {
const vports = NOP_VIEWER.model.getData().viewports;
const vpId = vports && Object.keys(vports).length > 0
? Number(Object.keys(vports).pop())
: undefined;
const xform = NOP_VIEWER.model.getPageToModelTransform(vpId);
const modelToPage = xform.invert();
let pos = new THREE.Vector3(); // in world-coordinates
let rot = new THREE.Quaternion(); // ccw Euler angles in degrees
let scale = new THREE.Vector3();
modelToPage.decompose(pos, rot, scale);
let center = new THREE.Vector3(x, y, 0);
center = center.applyMatrix4(modelToPage);
r = r * scale.x; // get scale factor
let circle = new Autodesk.Edit2D.Circle(center.x, center.y, r);
layer.addShape(circle);
}
drawCircle(535222.8838, 726697.4053, 800.0);
AutoCAD
Exporting a yellow circle from AutoCAD model space to LMV 2D View

LMV


Leave a Reply