开发JS应用:如何从Forge Viewer导出含MarkupsCore标记的.dwg文件?
Hey there, this is absolutely feasible! The key challenge here is that MarkupsCore stores its annotations as SVG elements tied to the viewer's screen space, not the underlying model's coordinate system. So you'll need to bridge that gap and convert those SVG markers into DWG-compatible entities before merging them with the exported model. Here's a step-by-step breakdown:
1. Extract Markup SVG Data
First, grab the raw SVG from the MarkupsCore extension. Make sure the extension is active and your markups are visible (you might need to enter edit mode temporarily to ensure all annotations are included):
const markupsExt = viewer.getExtension('Autodesk.Viewing.MarkupsCore'); // Ensure markups are rendered properly markupsExt.enterEditMode(); const svgMarkup = markupsExt.generateSVG(); markupsExt.leaveEditMode();
2. Convert Screen-Space SVG to Model-Space Coordinates
This is the critical step—your SVG uses pixel coordinates relative to the viewer, but DWG needs model-world coordinates. Use the viewer's clientToWorld() method to map each SVG element's position to the model:
const parser = new DOMParser(); const svgDoc = parser.parseFromString(svgMarkup, 'image/svg+xml'); // Example: Convert a path element to model space const paths = svgDoc.querySelectorAll('path'); paths.forEach(path => { // Parse SVG path data into pixel points const pointMatches = path.getAttribute('d').match(/([\d.-]+)/g).map(Number); const modelPoints = []; for (let i = 0; i < pointMatches.length; i += 2) { const screenX = pointMatches[i]; const screenY = pointMatches[i + 1]; // Convert screen pixel to model world coordinate const worldPoint = viewer.clientToWorld(screenX, screenY); if (worldPoint) { // For 2D floor plans, lock the Z-axis to a consistent value (e.g., 0) modelPoints.push({ x: worldPoint.x, y: worldPoint.y, z: 0 }); } } // modelPoints now holds the path's positions in the model's coordinate system });
Repeat this logic for other SVG elements: convert text to AutoCAD MText, rectangles to RECTANGLE entities, circles to CIRCLE entities, etc.
3. Convert Model-Space Entities to DWG/DXF Format
DWG is a binary format, so it's easier to work with its text-based counterpart DXF first. Use a library like svg-to-dxf (for basic conversions) or dxf-writer (for manual entity creation) to turn your converted model-space points into valid DXF entities.
For example, using dxf-writer to create a polyline from your modelPoints:
const { Drawing } = require('dxf-writer'); const dxf = new Drawing(); // Create a dedicated layer for markups dxf.addLayer('Viewer_Markups', 'BYCOLOR', 'CONTINUOUS'); dxf.setActiveLayer('Viewer_Markups'); dxf.addPolyline(modelPoints, true); // true = closed polyline
4. Merge with the Exported Model DWG
Since you already know how to export the base model to DWG, here's how to combine it with your markup DXF:
- Export the base model to DXF instead of DWG (most viewer export tools support this)
- Use AutoCAD's Design Automation API (or a local AutoCAD instance) to merge the markup DXF into the base model DXF
- Convert the merged DXF back to DWG if needed (Design Automation can handle this in one step)
Alternatively, if you're working entirely in the cloud, you can use Forge Design Automation to run an AutoCAD script that imports the markup entities directly into the exported DWG file.
Key Notes to Avoid Headaches
- Layer Organization: Put all markups on a dedicated layer (like "Viewer_Markups") so users can easily toggle visibility later
- Font Compatibility: When converting SVG text to MText, use AutoCAD-native fonts (like Arial or Tahoma) to avoid missing text issues
- Scale Consistency: Double-check that the coordinate conversion preserves the correct scale of your markups relative to the model
内容的提问来源于stack exchange,提问作者Andrew McEwen

