Metadata-Version: 2.4
Name: cad-core
Version: 0.1.27
Summary: OpenCascade CAD core API
Home-page: https://github.com/gcmartins/cad-core
Author: Gustavo Martins
Author-email: gucmartins@gmail.com
License-Expression: LicenseRef-Proprietary
Project-URL: Homepage, https://github.com/gcmartins/cad-core
Project-URL: Repository, https://github.com/gcmartins/cad-core
Keywords: cad,opencascade,3d,geometry
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.12
Requires-Python: >=3.12
Description-Content-Type: text/markdown
License-File: LICENSE
License-File: NOTICE
Requires-Dist: cadquery-ocp-novtk>=7.9.3.1
Requires-Dist: numpy>=1.24.0
Dynamic: author
Dynamic: author-email
Dynamic: classifier
Dynamic: description
Dynamic: description-content-type
Dynamic: home-page
Dynamic: keywords
Dynamic: license-file
Dynamic: license-expression
Dynamic: project-url
Dynamic: requires-dist
Dynamic: requires-python
Dynamic: summary

# cad-core

A pure-Python, headless CAD kernel built on top of [OpenCASCADE](https://dev.opencascade.org/) (via [OCP](https://github.com/CadQuery/OCP)). It provides parametric shape creation, boolean operations, sketching, modifier stacks (fillet, array, extrude, ...), undo/redo, and project (de)serialization — with no GUI framework dependency.

`cad-core` is meant to be embedded in a UI layer (desktop, web, etc.) that supplies its own rendering and input handling; this package owns the geometry model and business logic only.

## Features

- **Parametric shapes** — box, sphere, cylinder, cone, torus, rectangle, circle, polyline, imported (STEP/IGES) geometry.
- **Boolean operations** — union, subtraction and intersection, with inputs kept as editable internal components.
- **Modifier stack** — fillet, corner round, polyline round, rectangular array (1D/2D/3D), extrude, sweep (a profile along a polyline path) — each wraps a source shape and recomputes automatically when the source or its parameters change.
- **Sketching** — 2D lines, arcs, rectangles, circles, and polylines scoped to a reference plane (standard or arbitrary), with grid/snap helpers.
- **Reference planes** — standard (XY/XZ/YZ), arbitrary (translation + rotation), or derived from a picked face.
- **Undo/redo** — snapshot-based command stack shared by both the 3D geometry manager and the 2D sketch manager.
- **Geometry revisions** — `ManagedShape.geometry_revision` changes whenever a shape's geometry is replaced and stays the same for cosmetic edits (rename, colour) and for an `update_shape` call whose properties equal the current ones (that call is a no-op: no rebuild, notification or undo step). Undo/redo restores it, so a UI or analysis layer can tell when it needs to re-mesh or recompute.
- **Project persistence** — save/load full project state to a `.cadw` ZIP archive (parametric properties + embedded STEP for non-parametric geometry).
- **Headless by design** — the core managers and services have no UI framework dependency and run in any Python 3.12 environment; only optional view/selection services depend on an OCP AIS viewer context.

See [ARCHITECTURE.md](https://github.com/gcmartins/cad_core/blob/main/ARCHITECTURE.md) for a detailed description of the module layout and design.

## Requirements

- Python >= 3.12
- [`cadquery-ocp-novtk`](https://github.com/CadQuery/OCP) (OpenCASCADE Python bindings)
- `numpy`

## Installation

This project uses [uv](https://docs.astral.sh/uv/) for dependency management.

```bash
uv sync
```

To include the dev toolchain (tests, linting, type checking):

```bash
uv sync --group dev
```

## Usage

```python
from cad_core.managers.geometry_manager import CoreGeometryManager
from cad_core.enums import ShapeType
from cad_core.models.shape_properties import BoxProperties, SphereProperties

manager = CoreGeometryManager()

box = manager.create_shape(ShapeType.BOX, BoxProperties(width=50, height=30, depth=20))
sphere = manager.create_shape(ShapeType.SPHERE, SphereProperties(radius=20))
print(box.shape_id, box.name)

# Boolean union of two top-level shapes
union_id = manager.union_shapes([box.shape_id, sphere.shape_id])

manager.undo()
manager.redo()
```

Sketching on a reference plane:

```python
from cad_core.managers.sketch_manager import CoreSketchManager
from cad_core.enums import StandardPlane

plane = manager.create_plane_from_standard(StandardPlane.XY)
sketch = CoreSketchManager(plane_id=plane.shape_id, plane_props=plane.properties)
rect = sketch.create_rectangle(u=0, v=0, width=40, height=20)
```

Saving and loading a project:

```python
from cad_core.services.project_service import ProjectSerializer

ProjectSerializer.save_project("my_part.cadw", manager)
```

## Development

Common tasks are wired up via [`invoke`](https://www.pyinvoke.org/) (see `tasks.py`):

```bash
uv run invoke lint         # ruff check
uv run invoke format       # ruff format
uv run invoke typecheck    # ty check
uv run invoke test         # pytest
uv run invoke test-cov     # pytest with coverage
uv run invoke check        # lint + typecheck + test
```

Or invoke the underlying tools directly:

```bash
uv run pytest
uv run ruff check src tests
uv run ty check src tests
```

### Building a release wheel

A Cython-compiled release build (no `.py` sources, `.pyi` stubs only) can be produced with:

```bash
uv sync --group release
uv run invoke build-release
```

## Project layout

```
src/cad_core/
├── enums.py           # ShapeType, ShapeCategory, view/selection/tool enums
├── models/            # Shape property dataclasses (BoxProperties, FilletProperties, ...)
├── services/          # Stateless/OCP-facing operations (geometry, sketch, selection, view, undo/redo, project I/O)
└── managers/          # Stateful orchestration (CoreGeometryManager, CoreSketchManager)
```

## License

Proprietary — see [LICENSE](https://github.com/gcmartins/cad_core/blob/main/LICENSE). Third-party notices are listed in
[NOTICE](https://github.com/gcmartins/cad_core/blob/main/NOTICE).
