Skip to main content

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

Project description

click-cadquery

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.1.tar.gz (2.8 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.1-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for click_cadquery-0.1.1.tar.gz
Algorithm Hash digest
SHA256 d902fdbda9d5bca51b0484b225cd041cb24d94260df3b7ea53ec32519612b26f
MD5 9f391d888afc8e67891bd4560bf5c1f1
BLAKE2b-256 eb65fb7d40da7a3f5eac325bd694809466562fb9cfec3c01269032418c00e50a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for click_cadquery-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d40168ee65bdbbb43e5de3a4ae4da312e8f8e5628ee87d3ff303f3b0e0f269ad
MD5 f141e447b6cb0fcd432a205bff7dfb33
BLAKE2b-256 6bbab304262f59f342b997d1330280508946c012675cef4a31c0517c8173ecce

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