Multimodal LLM Code Generation: Turning Diagrams into Vector Drawing Code

    This article breaks down how multimodal large language models convert 2D diagram images into compilable vector drawing code. It explains why the task is difficult, divides the capability into four stages from pixels to code, and shares practical guidance on prompt design, evaluation, and fixing common failure modes.

    1. Why Generating Drawing Code from an Image Is Hard

    In practice combining intelligent coding with multimodal capabilities, a typical task is converting a schematic diagram into compilable vector drawing code. The input is a 2D bitmap, and the output is code containing coordinates, paths, solid and dashed lines, filled regions, and drawing order. This looks like a simple format conversion, but it actually spans three layers of capability: visual understanding, spatial reasoning, and code generation.

    The difficulty concentrates in three areas. First, a 2D image must be reconstructed into a 3D structure, so the model has to determine which lines indicate occlusion in front of or behind others, and which faces belong to the same layer. Second, the output language has strict syntax constraints; a slight deviation in coordinates, path closure, line style, or fill parameters can make the code fail to compile. Third, drawing order determines the occlusion result: even with correct geometry, a wrong order still renders incorrectly.

    2. Capability Breakdown: Four Stages from Pixels to Code

    2.1 Visual Structure Parsing

    The model first identifies the basic elements in the image: points, line segments, polygons, arcs, and text labels, and distinguishes solid lines, dashed lines, and auxiliary lines. The key in this stage is to establish the topological relationships between elements rather than viewing each line in isolation.

    2.2 3D and Occlusion Reasoning

    Using cues such as perspective, overlap, and shadows, the model infers the front-to-back relationships of objects and their visible faces. In engineering, the result is often expressed as a “layer + depth” ordering, replacing implicit visual guessing with explicit hierarchy to reduce uncertainty in later generation.

    2.3 Intermediate Representation Design

    It is recommended to introduce a structured intermediate representation, such as an element list where each item contains type, control points, line style, fill, and layer. The intermediate representation serves both as the model’s reasoning draft and as the anchor for validation and repair.

    1. {
    2. "elements": [
    3. {"type": "polygon", "points": [[0,0],[2,0],[2,1],[0,1]], "layer": 0, "fill": "none"},
    4. {"type": "path", "d": "M0,0 L2,1", "style": "dashed", "layer": 1}
    5. ],
    6. "draw_order": ["layer0", "layer1"]
    7. }

    2.4 Code Rendering and Self-Check

    The final step maps the intermediate representation to the coordinates, paths, and drawing instructions of the target drawing language, then performs compile validation and render review to close the loop.

    3. Prompt and Context Design

    • Define the coordinate system and units clearly, including the origin position and direction, to avoid mirroring and scaling errors.
    • Explicitly require the model to output the intermediate representation first and the code second, reducing “one-shot” hallucinations.
    • Provide enumerated values for line styles, fills, and layers to limit free-form output.
    • Require a self-check list from the model: whether paths are closed, whether layers are monotonic, and whether occlusion matches the image.

    4. Evaluation and Quality Assurance

    Evaluation should cover four types of metrics: geometric accuracy, occlusion correctness, compilability rate, and rendering consistency. Object storage can be used to save samples and outputs, a message queue can decouple batch inference tasks, a container platform can host parallel compile validation, and logging and monitoring with alerts can track failure rates and latency. If you use the relevant general capabilities of Baidu AI Cloud, it is also recommended to integrate them in the same layered manner to keep evaluation criteria consistent.

    5. Common Failure Modes and Fixes

    1. Layer inversion: enforce ordering before drawing in the prompt, and apply rule-based validation on the order.
    2. Coordinate drift: introduce normalized coordinates and a scale write-back step.
    3. Line style confusion: treat solid and dashed lines as a separate attribute field rather than encoding them into the path.
    4. Compilation failure: set up automatic retries and feed error messages back so the model can iterate based on the errors.

    6. Conclusion

    Converting a 2D schematic into vector drawing code is essentially about enabling the model to combine visual parsing, spatial reasoning, and constrained generation. Engineering design based on intermediate representations, explicit hierarchy, and compile review can significantly improve stability and maintainability. This approach also applies to a broader range of graphics-to-code conversion scenarios.