Metadata-Version: 2.4
Name: cad2scad
Version: 0.2.0
Summary: Convert CAD/mesh files to parameterized OpenSCAD .scad with physics-aware Customizer sliders
Author: Joel Ajitesh Varun
License: MIT
Project-URL: Repository, https://github.com/joelvarun/cad2scad
Keywords: openscad,cad,3mf,stl,mesh,converter,3d-printing,physics,customizer,parametric
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Manufacturing
Classifier: Intended Audience :: Developers
Classifier: Topic :: Scientific/Engineering :: Visualization
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: numpy>=1.24
Requires-Dist: trimesh>=4.0
Requires-Dist: lxml>=4.9
Provides-Extra: full
Requires-Dist: scipy>=1.10; extra == "full"
Requires-Dist: networkx>=3.0; extra == "full"
Requires-Dist: rich>=13.0; extra == "full"
Provides-Extra: dev
Requires-Dist: pytest>=7.0; extra == "dev"
Requires-Dist: pytest-cov; extra == "dev"
Dynamic: license-file

# cad2scad

**Convert CAD/mesh files to parameterized OpenSCAD `.scad` with physics-aware Customizer sliders.**

Unlike simple "dump polyhedron" converters, `cad2scad` provides a full pipeline with material-aware physics constraints.

## Features

| Feature | Description |
|---------|-------------|
| **Multi-format input** | STL, 3MF, OBJ, PLY, OFF, GLTF/GLB, DAE, STEP* |
| **Vertex welding** | Merge duplicate vertices -> smaller `.scad` files |
| **Mesh decimation** | Reduce triangles while preserving shape |
| **Multi-body splitting** | Each solid -> separate OpenSCAD `module` |
| **Auto-centering** | Move bounding box center to origin |
| **Color extraction** | Preserve materials from 3MF/OBJ as `color()` wrappers |
| **Printability analysis** | Wall thickness, overhang detection, watertight checks |
| **Physics engine** | Mass, center of mass, stress, stability, warp risk |
| **8 materials** | PLA, ABS, PETG, TPU, Nylon, ASA, PC, Resin properties |
| **Customizer output** | Auto-generated sliders with physics constraints |
| **Feature detection** | Holes, flat surfaces, symmetry axes |
| **Parameterized .scad** | Scale, rotate, hollow with material-safe limits |
| **CLI + Python API** | Use from command line or as a library |

*\*STEP support requires `cadquery` (`pip install cadquery`)*

## Install

```bash
pip install cad2scad

# With all optional features (decimation, splitting, pretty CLI)
pip install cad2scad[full]
```

## Quick Start

### Command Line

```bash
# Basic conversion
cad2scad model.3mf -o model.scad

# Parameterized with physics Customizer sliders
cad2scad model.3mf -o model.scad --parameterize --material PLA

# Decimate to 50% triangles, center on origin
cad2scad model.stl -o model.scad --decimate 0.5 --center

# Physics analysis
cad2scad model.3mf --analyze --physics --material PETG

# Split multi-body file into separate modules
cad2scad assembly.3mf -o assembly.scad --split

# Batch convert all STLs in a directory
cad2scad *.stl -o converted/

# Compact output (smaller file, less readable)
cad2scad model.3mf -o model.scad --compact --precision 4
```

### Python API

```python
# One-liner
from cad2scad import convert
convert("model.3mf", "model.scad")

# Parameterized with physics
convert("model.3mf", "model.scad", parameterize=True, material="PETG")

# Full control
from cad2scad import Converter
c = Converter(parameterize=True, material="PLA", decimate=0.5)
c.load("model.3mf")
print(c.summary())
c.save("model.scad")

# Physics engine directly
from cad2scad import PhysicsEngine
engine = PhysicsEngine("PLA")
report = engine.analyze(vertices, faces)
print(f"Mass: {report.mass_grams:.1f}g, Stability: {report.stability_score}/100")

# Feature detection
from cad2scad import Parameterizer
p = Parameterizer(material="PETG")
features = p.detect_features(vertices, faces)
print(f"Holes: {len(features.holes)}, Symmetry: {[s.axis for s in features.symmetry]}")
```

## Output Example

### Standard mode
Input: `box.3mf` (100x115x50mm box)

```openscad
// Generated by cad2scad v0.2.0
// Source: box.3mf
module box() {
  polyhedron(points = [...], faces = [...]);
}
box();
```

### Parameterized mode (`--parameterize --material PLA`)

```openscad
/* [Material] */
material = "PLA"; // ["PLA", "ABS", "PETG", "TPU", "Nylon", "ASA", "PC", "Resin"]

/* [Dimensions] */
scale_x = 1.0; // [0.1:0.01:5.0] X scale (100.0mm nominal)
// ^ constraint: Min 0.8mm wall for PLA

/* [Structure] */
hollow = 0; // [0:1:1] Hollow the part
shell_thickness = 1.6; // [0.8:0.1:10.0] Shell wall thickness

/* [Physics (read-only)] */
_mass_g = 140.19;       // Estimated mass (g)
_printability = 71;     // Printability score (/100)
_stability = 85;        // Stability score (/100)
_warp_risk = 30;        // Warp risk (/100)
_safety_factor = 999.0; // Structural safety factor (x)

module box_base() { polyhedron(...); }
module box() {
  translate([offset_x, offset_y, offset_z])
  rotate([rotate_x, rotate_y, rotate_z])
  scale([_x_eff, _y_eff, _z_eff])
  if (hollow == 1) {
    difference() { box_base(); scale([...]) box_base(); }
  } else { box_base(); }
}
box();
```

Open in OpenSCAD -> **Window > Customizer** to get interactive sliders.

## CLI Options

```
cad2scad [OPTIONS] INPUT [INPUT ...]

Optimization:
  --decimate RATIO    Reduce faces (0.0-1.0). 0.5 = halve triangles
  --decimate-target N Reduce to exactly N faces
  --center            Center mesh at origin
  --split             Split disconnected bodies into modules
  --weld TOL          Vertex weld tolerance (default: 1e-6 mm)
  --round N           Round coordinates to N decimal places

Formatting:
  --precision N       Decimal places in output (default: 6)
  --compact           Compact arrays (smaller files)
  --no-color          Strip color data
  --prefix STR        Module name prefix

Analysis:
  --analyze           Print analysis report (no conversion)
  --min-wall MM       Min wall threshold (default: 0.8mm)
  --overhang DEG      Overhang angle threshold (default: 45 deg)

Physics / Customizer:
  --parameterize      Generate OpenSCAD Customizer with physics constraints
  --material MAT      Material: PLA, ABS, PETG, TPU, Nylon, ASA, PC, Resin
  --physics           Include physics report in --analyze output

General:
  -o, --output PATH   Output file or directory
  -v, --verbose       Increase verbosity (-v info, -vv debug)
  --version           Show version
```

## Material Database

| Material | Density | Tensile | Min Wall | Max Overhang | Shrinkage |
|----------|---------|---------|----------|--------------|-----------|
| PLA      | 1.24    | 50 MPa  | 0.8 mm   | 45 deg       | 0.3%      |
| ABS      | 1.04    | 40 MPa  | 1.0 mm   | 40 deg       | 0.7%      |
| PETG     | 1.27    | 50 MPa  | 0.8 mm   | 40 deg       | 0.4%      |
| TPU      | 1.21    | 30 MPa  | 1.2 mm   | 35 deg       | 0.5%      |
| Nylon    | 1.14    | 70 MPa  | 1.0 mm   | 35 deg       | 1.5%      |
| ASA      | 1.07    | 45 MPa  | 1.0 mm   | 40 deg       | 0.6%      |
| PC       | 1.20    | 65 MPa  | 1.0 mm   | 35 deg       | 0.7%      |
| Resin    | 1.12    | 45 MPa  | 0.3 mm   | 80 deg       | 0.5%      |

## Supported Formats

| Format | Extension | Notes |
|--------|-----------|-------|
| STL | `.stl` | Binary & ASCII |
| 3MF | `.3mf` | Multi-body, colors |
| OBJ | `.obj` | With materials |
| PLY | `.ply` | Binary & ASCII |
| OFF | `.off` | — |
| GLTF/GLB | `.gltf`, `.glb` | Scenes → modules |
| COLLADA | `.dae` | — |
| AMF | `.amf` | — |
| STEP | `.step`, `.stp` | Requires `cadquery` |
| IGES | `.iges`, `.igs` | Requires `cadquery` |

## License

MIT
