<!-- TE10 Studio documentation: /docs/api/modeler -->
# Modeler

`prj.modeler` creates and finds objects. Each creating method returns an [`Object`](#object). Positions
and sizes follow the [value rules](/docs/api/values-units): plain numbers are project units, strings
carry units or are expressions. `material` is a material name (default `"vacuum"`); `priority` is the
mesh priority where objects overlap (higher wins).

## Solids

| Method | Shape |
|---|---|
| `box(name, size, origin=(0, 0, 0), material="vacuum", priority=0)` | `size = (dx, dy, dz)`; `origin` is the lowest corner |
| `cylinder(name, radius, height, origin=(0, 0, 0), axis="z", material="vacuum", priority=0)` | Base circle centred at `origin`, along `axis` (`"x"`, `"y"`, `"z"`) |
| `sphere(name, radius, center=(0, 0, 0), material="vacuum", priority=0)` | |
| `cone(name, bottom_radius, top_radius, height, origin=(0, 0, 0), axis="z", material="vacuum", priority=0)` | Base centred at `origin`; `top_radius=0` for a point |
| `torus(name, major_radius, minor_radius, center=(0, 0, 0), axis="z", material="vacuum", priority=0)` | A ring around `axis` |
| `loft(name, profiles, ruled=False, material="vacuum", priority=0)` | A solid through sheets or closed curves, in order; `ruled=True` for straight sections |
| `add(name, shape, material="vacuum", priority=0, kind=None)` | Any [build123d](https://build123d.readthedocs.io) shape, drawn in project units |

```python
sub = prj.modeler.box("Substrate", size=(30, 20, 1.6), origin=(-15, -10, 0), material="FR4")
via = prj.modeler.cylinder("Via", radius=0.15, height=1.6, origin=(5, 0, 0), material="copper", priority=1)
```

## Sheets and curves

| Method | Shape |
|---|---|
| `rectangle(name, size, origin=(0, 0, 0), plane="xy", material="vacuum")` | `size = (du, dv)` in `plane` (`"xy"`, `"yz"`, `"xz"`); `origin` is a corner |
| `circle(name, radius, center=(0, 0, 0), plane="xy", material="vacuum")` | |
| `polyline(name, points, closed=False, cover=False, material="vacuum")` | A curve through `points` (each `(x, y, z)`, may use variables); `cover=True` fills a closed one into a sheet |

Sheets have `kind == "sheet"`, curves `kind == "wire"`.

## Imports

| Method | Returns |
|---|---|
| `import_step(path, material=None, heal=True, unit="mm")` | `list[Object]`. STEP and IGES keep their own units; BREP and STL have none, so `unit` is used. Flat STL triangles are merged into faces |
| `import_cad(path, material=None, heal=True, unit="mm")` | The same (any of `.step .stp .iges .igs .brep .brp .stl`) |
| `import_gds(path, layers, cell=None, substrates=(("Silicon", "525um"),), vacuum_above="500um", vacuum_below=0, margin=0, min_edge="1um")` | `list[Object]` of a GDSII / OASIS chip (below) |
| `import_mesh(path, unit="mm")` | `list[Object]`: a solid per region of a volume mesh (`.msh`, `.vtu`, Exodus, Nastran...), sheets for inner tagged surfaces, a named selection per tagged surface |

### `import_gds` layers

`layers` maps a layer (`"L1/0"` or `(1, 0)`) to a dict:

| Key | Values | Default |
|---|---|---|
| `role` | `"metal"`, `"solid"`, `"outline"`, `"ignore"` | |
| `negative` | `True`: the polygons are gaps; metal = chip outline minus them | `False` |
| `z` | height of the layer's bottom | `0` |
| `thickness` | `0` makes a sheet | `0` |
| `material` | material name | `"PEC"` |
| `boundary` | for sheets: `"PEC"`, `"Conductivity"`, `"None"` | `"PEC"` |

```python
objs = prj.modeler.import_gds("qubit.gds", {
    "1/0": {"role": "metal", "negative": True},           # ground plane with the circuit cut out
    "10/0": {"role": "outline"},
}, substrates=[("Silicon", "525um")], vacuum_above="500um")
```

## Finding objects

| Call | Returns |
|---|---|
| `prj.modeler["Trace"]` | The `Object` (also `"folder/sub:Trace"`) |
| `"Trace" in prj.modeler` | `bool` |
| `for obj in prj.modeler: ...` | Every object |
| `prj.modeler.names()` | `list[str]` |

## Object

| Property | Type | |
|---|---|---|
| `name` | `str`, settable | Renaming updates ports, boundaries and everything else that refers to it |
| `kind` | `str` | `"solid"`, `"sheet"` or `"wire"` |
| `material` | `str`, settable | Setting it resets `solve_inside` to the default |
| `priority` | `int`, settable | Mesh priority where objects overlap |
| `model` | `bool`, settable | `False` = construction geometry the solver doesn't see (a non-model solid can be a mesh refinement region) |
| `solve_inside` | `bool` or `None`, settable | Whether the field is solved inside. Default (`None`): off for PEC and conductors (σ ≥ 1e5 S/m: a surface impedance on their faces instead), on for everything else |
| `folder` | `str`, settable | The model folder, e.g. `"feed/connector"` (created if needed) |
| `qualified_name` | `str` | `"feed/connector:pin"` |
| `volume`, `area` | `float` | In project units³ / units² |
| `history` | `list[str]` | The steps that built it, e.g. `['CreateBox [3 mmx2 mmx1.6 mm]']` |
| `shape` | build123d shape | In metres |

| Method | |
|---|---|
| `bounding_box()` | `((xmin, ymin, zmin), (xmax, ymax, zmax))` in project units |
| `face(selector=None, nearest=None, index=None)` | One face: `face("+z")` (the face on the +z side), `face(nearest=(x, y, z))`, `face(index=i)` |
| `faces(normal=None)` | All faces, or the planar faces whose normal points along an axis: `"x"` (both sides), `"+x"`, `"-y"`... |

### Edits

Every edit changes the object in place and returns it, so calls chain.

| Method | |
|---|---|
| `move(vector)` | Translate by `(dx, dy, dz)` |
| `rotate(axis, angle_deg)` | About the global `"x"`, `"y"` or `"z"` axis, in degrees |
| `mirror(origin, normal)` | Mirror in a plane |
| `subtract(*tools, keep_tools=False)` | Cut the tools out |
| `unite(*tools)` | Join the tools into this object |
| `intersect(*tools)` | Keep the common volume |
| `insert()` | Cut this solid out of every solid it overlaps; it stays whole. Returns the changed objects (a list) |
| `shell(thickness, inside=True, open_faces=())` | Hollow it; `open_faces=[obj.face("+z")]` leaves faces open |
| `delete()` | Remove it |

Sheets and curves also have:

| Method | |
|---|---|
| `sweep_along_vector(vector)` | Extrude |
| `sweep_around_axis(axis, angle_deg=360)` | Revolve |
| `sweep_along_path(curve)` | Sweep along a curve (mitred at corners) |
| `thicken(thickness)` | Sheets: a solid of that thickness |
| `cover()` | Closed planar curves: a sheet |
| `trace(name, width, thickness, material="copper")` | Curves: a new rectangular trace along the curve that follows later curve edits |

```python
box = prj.modeler.box("Block", size=(10, 10, 10))
hole = prj.modeler.cylinder("Hole", radius=2, height=10, origin=(5, 5, 0))
box.subtract(hole).move((0, 0, 1)).rotate("z", 45)
```

## Faces

Faces are `FaceRef` values, passed to ports, boundaries and mesh rules. A list of faces is accepted
wherever faces are. Assigned faces follow their object when it is rebuilt.

```python
end = air.face("-z")                       # the face on the -z side
sides = air.faces(normal="x")              # both x faces
top = sub.face(nearest=(0, 0, 1.6))        # the face nearest a point
```

## Folders

| Call | |
|---|---|
| `obj.folder = "feed/connector"` | Move an object (creates the folder) |
| `prj.modeler.folders()` | All folder paths |
| `prj.modeler.new_folder(path)` | Returns the path |
| `prj.modeler.rename_folder(old, new)` | |
| `prj.modeler.delete_folder(path, delete_objects=False)` | Objects move up unless deleted; returns their names |
| `prj.modeler.in_folder(path, recursive=True)` | `list[Object]` |

## Overlaps

Where objects overlap, the higher `priority` wins. At equal priority an object lying wholly inside
another wins (an air box drawn first never swallows the model). Only partial overlaps go further: a
conductor wins over a dielectric, then `prj.overlap_rule` decides: `"earlier"` (default, the object created
first) or `"later"`. `prj.validate()` doesn't fail on overlaps; the app's Check Model lists them.

## Picks

`prj.picks` collects points, edges and faces for commands that use them (as in the app).

| Call | Adds |
|---|---|
| `point((x, y, z))` | A point |
| `vertex(obj, i)`, `edge_center(obj, i)`, `circle_center(obj, i)` | A point on an object (by vertex or edge index) |
| `face_center(face)` | The centre of a face |
| `edge(obj, i)`, `face(face)` | An edge or face pick |
| `distance()` | Between the last two points, in project units |
| `clear()` | Empties the list |

## Working coordinate system

`prj.wcs` is the movable local coordinate system. **While it is on, positions given to the modeler are in
its axes.** Objects keep their place when it moves later.

| Call | |
|---|---|
| `on()`, `off()`, `active` | Switch it, or read whether it is on |
| `align_to_face(face)` | XY plane on the face, Z along its outward normal |
| `align_to_points(p0, p1=None, p2=None)` | One point: move the origin there (axes kept). Two: origin at `p0`, X towards `p1`. Three: also `p2` in the XY plane on the +Y side |
| `align()` | To the newest picks (a face, a straight edge, or 1–3 points); clears them |
| `move(u=0, v=0, w=0)`, `rotate(u=0, v=0, w=0)` | Along / about its own axes (degrees) |
| `store(name=None)` | A named fixed copy; returns its name |
| `reset()` | Back to the global axes |
| `origin`, `matrix` | Its placement |

```python
prj.wcs.align_to_face(sub.face("+z"))            # draw on top of the substrate
prj.modeler.box("Patch", size=(10, 12, 0.035), origin=(-5, -6, 0), material="copper")
prj.wcs.off()
```
