Skip to main content

Click decorators for CadQuery CLI applications with automatic option generation from Pydantic models

Project description

click-cadquery

PyPI version License: MIT

Click decorators for CadQuery CLI applications with automatic option generation from Pydantic models.

Overview

This library provides utilities to build command-line interfaces for CadQuery applications using Click. It automatically generates CLI options from Pydantic model fields, making it easy to create parametric CAD scripts with type-safe command-line interfaces.

Features

  • Automatic CLI generation: Convert Pydantic models to Click options automatically
  • Type safety: Leverage Pydantic's type validation for CLI parameters
  • CadQuery integration: Built-in support for output files and viewer integration
  • Git utilities: Helper functions for version tracking

Installation

pip install click-cadquery

Quick Start

from pathlib import Path

import cadquery as cq
import cadquery.vis as vis
import click
from click_cadquery import define_options
from click_cadquery.git import version_number as ver
from pydantic import BaseModel

class Param(BaseModel):
    width: int = 100
    height: int = 100
    depth: int = 100
    thickness: float = 2.0

    @property
    def filename(self) -> str:
        return f"v{ver()}-{self.width}w{self.height}h{self.depth}d{self.thickness}t.stl"

@click.group(context_settings={"show_default": True})
@click.pass_context
def main(ctx: click.Context) -> None:
    pass

@main.command(name="build")
@define_options(Param)
def command_build(output: Path | None, param: Param, show: bool) -> None:
    print("Build with:", param)

    result = build(param)

    dist = Path("dist")
    dist.mkdir(exist_ok=True)
    result.export(str(output if output else dist / param.filename))
    if show:
        vis.show(result, axes=True, axes_length=10)

def build(param: Param) -> cq.Workplane:
    result = cq.Workplane("XY")

    result = result.box(
        length=param.depth,
        height=param.height,
        width=param.width,
    )

    result = result.faces(">Z").shell(param.thickness, kind="intersection")

    fillet = param.thickness / 2
    result = result.edges("|Z").fillet(fillet)

    return result

if __name__ == "__main__":
    main()

This automatically creates a CLI with the following options:

python main.py build --width 150 --height 80 --depth 50 --thickness 3.0 --show

API Reference

define_options(klass: type[BaseModel])

Decorator that automatically generates Click options from a Pydantic model.

Parameters:

  • klass: A Pydantic BaseModel class whose fields will be converted to CLI options

Generated CLI signature:

  • Each model field becomes a --field-name option
  • Field types are preserved for Click type validation
  • Field defaults and descriptions are used for CLI help
  • Automatically adds output argument for file output
  • Automatically adds --show flag for showing results

Function signature requirements: The decorated function must accept:

  • param: Instance of the Pydantic model with parsed CLI values
  • output: Optional Path for output file
  • show: Boolean flag for showing results

Git Utilities

version_number() -> int

Returns the number of commits in the current git repository.

from click_cadquery.git import version_number

version = version_number()
print(f"Build version: {version}")

Requirements

  • Python >= 3.11
  • Click >= 8.2.1
  • Pydantic >= 2.11.7

License

MIT License - see LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

click_cadquery-0.1.2.tar.gz (3.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

click_cadquery-0.1.2-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

Details for the file click_cadquery-0.1.2.tar.gz.

File metadata

  • Download URL: click_cadquery-0.1.2.tar.gz
  • Upload date:
  • Size: 3.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.11

File hashes

Hashes for click_cadquery-0.1.2.tar.gz
Algorithm Hash digest
SHA256 34d3e61f95463eed9da332ee79729179d4f38648f4f8046620d11b3f920a7be2
MD5 364cf8c011cccc7a1e12762f7707930f
BLAKE2b-256 113173c4414fc114805ab6cd06ecd1639dd213342bbc100bc580d65742999e42

See more details on using hashes here.

File details

Details for the file click_cadquery-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for click_cadquery-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 97b48c7a48f328b004e96a0d76858bf038406a7e23e74bcf177d945eac460b7e
MD5 7f66c9f0cf2acfe7f06f0ebc577adcd5
BLAKE2b-256 cb2388ada0c711db1a33621249cf6c3fc3d89e63dfa62a9e60bbb975ce11842c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page