Skip to main content

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

Project description

click-cadquery

PyPI version Python Versions 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.2.0.tar.gz (3.2 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.2.0-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for click_cadquery-0.2.0.tar.gz
Algorithm Hash digest
SHA256 4b39dc0bf4221cf3200cab08e48e1d0441f3d56ad805aeb0c6d52b0bb3d46926
MD5 05349ef1e6be54723dc7dd8cce3a0fbe
BLAKE2b-256 65ec77a6d645e21b515ade7f98860857196229a3e0f704306c60ac10f1ec19f7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for click_cadquery-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b2f2b8cb9a17db624f39002379414c402a0c08b93b64e54c0d6454bca04aa183
MD5 8a6081eb671e21c92759203c75d442a8
BLAKE2b-256 b06d49dfd13f9779dd29ad7a541bf3b04b396009dd124cf8303a67c71863ef70

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