Python

Overview

Python library for reading, writing and manipulating .duc CAD files — backed by SQLite.

Installation

pip install ducpy

or

uv add ducpy

How it works

A .duc file is a standard SQLite database. ducpy provides two complementary APIs for working with these files:

APIBest for
BuildersConstructing elements programmatically with type-safe Python classes
DucSQLRaw SQL access — batch inserts, complex queries, schema introspection

Builders API

Use the fluent builder DSL to create elements, then serialize to bytes or a .duc file.

import ducpy as duc
 
# Build elements
rect = (
    duc.ElementBuilder()
    .at_position(0, 0)
    .with_size(100, 50)
    .with_label("My Rectangle")
    .with_styles(duc.create_fill_and_stroke_style(
        fill_content=duc.create_solid_content("#4ECDC4"),
        stroke_content=duc.create_solid_content("#2C3E50"),
        stroke_width=2.0,
    ))
    .build_rectangle()
    .build()
)
 
# Serialize to bytes, then write to disk
data = duc.serialize_duc(name="my-drawing", elements=[rect])
with open("output.duc", "wb") as f:
    f.write(data)

SQL Builder API

For direct, low-level access to the SQLite schema:

from ducpy.builders.sql_builder import DucSQL
 
# Create a brand-new .duc file with the full schema bootstrapped
with DucSQL.new() as db:
    db.sql(
        "INSERT INTO elements (id, element_type, x, y, width, height, label) "
        "VALUES (?,?,?,?,?,?,?)",
        "r1", "rectangle", 0, 0, 200, 100, "SQL Rectangle",
    )
    db.sql(
        "INSERT INTO backgrounds (owner_type, owner_id, src, opacity) "
        "VALUES (?,?,?,?)",
        "element", "r1", "#FF6B6B", 1.0,
    )
    db.save("output.duc")
 
# Open and query an existing .duc file
with DucSQL("output.duc") as db:
    rows = db.sql("SELECT id, label, element_type FROM elements")
    for row in rows:
        print(dict(row))

Parsing an existing file

import ducpy as duc
 
with open("drawing.duc", "rb") as f:
    data = duc.parse_duc(f)
 
print(f"Elements: {len(data.elements)}")
print(f"Source: {data.source}")

Package structure

ModulePurpose
buildersFluent builders for elements, states, styles
builders.sql_builderDucSQL — raw SQLite access
classesDataclass definitions for all duc entities
enumsInteger enum constants matching the schema
parseparse_duc(), parse_duc_lazy(), get_external_file()
serializeserialize_duc()
utilsHelper utilities
Edit on GitHub

Last updated on