How Multimodal LLMs Convert Technical Diagrams into SVG Code
This article explains how multimodal large language models turn 2D technical drawings into SVG code. It covers inputs and outputs, why SVG is chosen over formats like Mermaid and TikZ, and the three core challenges: projection inference, occlusion detection, and draw order. A working SVG example, a rendering validation workflow with a fixed sample set, common failure modes, and a developer checklist are included.
From Drawings to Code: Three Things to Understand First
When a multimodal LLM converts a 2D technical diagram into vector code, the difficulty concentrates in three areas: determining projection relationships, inferring occlusion, and arranging draw order. An error in any one of these produces output that does not match the original drawing. Below we first clarify inputs, outputs, and format choice, then demonstrate the three points with an SVG example, and finally list common failure modes and deployment checks.
1. Inputs and Outputs
The input is typically a technical diagram, such as an orthographic projection of a part or an isometric illustration. The drawing may contain a background grid, solid outlines, dashed lines representing hidden edges, and text annotations.
The output is usually SVG. SVG is an XML-based vector graphics format published by the W3C. Graphics are described by coordinates and paths, scale without distortion, and can be embedded directly into web pages and business systems. The examples in this article use SVG 1.1 syntax.
Choosing SVG over other formats depends on the use case. Mermaid suits flowcharts and sequence diagrams, where structure is defined by nodes and edges, but offers weak coordinate control over arbitrary outlines. TikZ depends on a LaTeX compilation environment, suits mathematical figures in papers, and adds a compilation step when embedding into web pages or backend services. If the goal is to turn a diagram directly into a graphic usable on the web, SVG is usually the more direct path.
2. Three Core Capabilities
2.1 Projection and 3D Inference
The same line may be the intersection of two faces or the projection of an edge from another view. The model must cross-reference multiple views to decide which 3D structure a 2D figure corresponds to. This step assumes the views share a consistent scale and are aligned with each other. With only a single view, information is insufficient and errors are likely. The prompt should state the view type, such as front view, top view, or left view.
2.2 Occlusion Detection
In engineering drawings, visible edges are drawn as solid lines and hidden, occluded edges as dashed lines. Relevant national drafting standards define this convention; this article does not cite specific standard numbers or clauses. In practice, follow the standard your project adopts or your team’s conventions, and have engineering staff verify the results. The model needs to use line style and contour continuity to decide which lines should be dashed. Drawing hidden edges as solid lines is a common error.
2.3 Layering and Draw Order
SVG has no z-index property. Overlap between elements is determined by document order: elements drawn later cover elements drawn earlier. When generating code, the model should draw the background grid and hidden edges first, then the foreground solid. If the order is reversed, the foreground is covered by the background and the result no longer matches the original.
3. Example Code
The following example includes a background grid, a dashed hidden rectangle, and a filled foreground polygon.
<svg xmlns="http://www.w3.org/2000/svg" width="200" height="150" viewBox="0 0 200 150"><!-- 1. Background grid: drawn first, light color, thin stroke --><g stroke="#dddddd" stroke-width="0.5"><line x1="0" y1="50" x2="200" y2="50"/><line x1="0" y1="100" x2="200" y2="100"/></g><!-- 2. Hidden outline: dashed rectangle, beneath the foreground --><rect x="60" y="40" width="80" height="60" fill="none" stroke="#000000" stroke-width="1" stroke-dasharray="4 3"/><!-- 3. Foreground solid: a path with fill that covers the hidden outline --><path d="M 20 30 L 120 30 L 140 50 L 40 50 Z" fill="#f0f0f0" stroke="#000000" stroke-width="1.5"/></svg>
The values in the code mean the following.
viewBox="0 0 200 150" defines the canvas coordinate range. The origin (0,0) is at the top-left corner; the x-axis increases to the right and the y-axis increases downward. This is the opposite of the coordinate direction in many engineering tools. If the model assumes the origin is at the bottom-left, the output will be vertically flipped.
stroke-dasharray="4 3" draws 4 units of line followed by a 3-unit gap, producing a dashed line.
In the path, M moves to the starting point, L draws a straight line to the next point, and Z closes the path. For curves, the C command describes a cubic Bézier curve in the form C control1x control1y control2x control2y endx endy.
Draw order makes the foreground polygon cover part of the hidden rectangle, creating the occlusion effect.
4. Rendering Validation and Fixed-Sample Evaluation
- Save the code as example.svg.
- Open it in a browser — Chrome, Edge, or Firefox all work. Record the browser version. Rendering of thin lines and dashes may differ across versions, so always compare results using the same version.
- Export a PNG with librsvg, for example by running
rsvg-convert -w 400 example.svg -o example.png. Command parameters vary by version; runrsvg-convert --versionfirst to confirm the version, then check parameters withrsvg-convert --help. - Build a fixed sample set. Select several manually annotated diagrams, each paired with a reference SVG and a check record covering occlusion, line style, draw order, and dimensions.
- After every change to the model or the prompt, regenerate output for the same sample set and compare item by item against the reference results.
5. Common Failure Modes
- Coordinate system errors. The origin position or y-axis direction does not match SVG, and the figure is flipped vertically or horizontally. How to detect: mark the four corner coordinates on the canvas and check whether the corners in the generated figure land where expected.
- Scale distortion. The model did not read the original dimensions, path coordinates are scaled incorrectly, and the figure is deformed. How to detect: measure the ratio of two known lengths in the original and compare it with the corresponding path ratio in the SVG. If the error exceeds the tolerance, treat it as distortion.
- Hidden edges drawn as solid lines. Occluded lines are not dashed and look like visible edges. How to detect: check each hidden-edge element for the
stroke-dasharrayattribute. - Reversed layer order. The foreground solid appears before the hidden edges, exposing lines that should be occluded. How to detect: inspect element order in the document and confirm the foreground solid comes after the hidden edges.
- Text annotations not converted to paths. Text in SVG depends on local fonts and renders incorrectly or goes missing in environments without them. How to detect: render on another machine or in a font-free environment and check whether the text displays correctly. For stable output, convert text to paths or preinstall the fonts in the deployment environment.
- Bézier control point errors. The control points of a
Ccommand are misplaced, and the curve shape deviates from the original. How to detect: draw the connecting lines between control points and curve endpoints, and check whether the curve bends in the expected direction.
6. Developer Checklist
- Coordinate system: origin at the top-left, y-axis pointing down, verified with corner coordinates.
- Canvas ratio: the viewBox aspect ratio matches the original, and the original dimensions have been read.
- Line style: all hidden edges dashed, all visible outlines solid.
- Draw order: background, hidden edges, and foreground solids arranged in sequence.
- Fonts: text converted to paths, or required fonts installed in the deployment environment.
- Multi-view alignment: consistent scale across views, with view types labeled.
- Fixed-sample regression: after every change, regenerate output on the same sample set and compare results.
- Human review: outputs involving dimensions or standard clauses must be confirmed by engineering staff before use.
