# topo

> Deterministic 2D geometry engine (`pip install g-topo[dxf]`, `import topo, topo_dxf`).
> It repairs vector geometry and reads its structure: closed contours, what lies
> inside what, what is connected. It knows no file format (`topo_dxf` is the DXF/DWG
> adapter), no process and no meaning: what a contour *is* is the caller's reading.

This file is the short entry point. The full reference is `LLM.md`, next to this file (~64 KB):
**do not read it whole**. Each name is one table row, written without `topo.`
(`heal`, `geometry.gap`, `topo_dxf.to_dxf`): ``grep -n '^| `heal`' LLM.md``
prints that one card. For prose, read one section by the line range in the map at the bottom.

## Pipeline

```
topo_dxf.load_dxf / topo.load_geometry / topo.load_segments -> Document  (edges + texts, no topology)
topo.heal(doc, max_gap=...)  -> Network   (the repair, once)
network.faces()              -> Result    (closed contours, outer/inner tree; flat figures)
network.outlines()           -> Result    (one cluster per outermost outline; projections side by side)
topo.split(result)           -> Split     (.pieces: one Result per cluster; .loose/.texts nobody owns)
topo_dxf.to_dxf / topo.save_json         (render the model; never rereads the source)
```

```python
import topo, topo_dxf
doc = topo_dxf.load_dxf("part.dxf")
result = topo.heal(doc, max_gap=0.05).faces()
if not result.is_valid:
    raise SystemExit(result.errors)
for cluster in result.clusters:
    shape = topo.geometry.contour_shape(cluster.outer)   # kind, center, length, width, ...
    print(cluster.label, cluster.area, shape.kind if shape else None, len(cluster.inners))
for i, part in enumerate(topo.split(result).pieces):
    topo_dxf.to_dxf(part).saveas(f"piece_{i + 1}.dxf")
```

## Traps

- Units are mm everywhere; the unit a file declares is not read: scale before loading.
- Data positional, options keyword-only: `topo.heal(doc, max_gap=0.5)`, never `topo.heal(doc, 0.5)`.
  A function that mutates its argument returns `None`.
- No gap is closed unless you pass `max_gap` (the caller's choice, never guessed).
  Two points within `topo.precision.SAME_POINT` = 0.001 mm are one point; no `tolerance` parameter.
- Check `result.is_valid` before handing output to a machine: `to_dxf` renders an invalid
  result anyway (`allow_invalid=False` raises instead); `split` always raises on it.
- Nothing is dropped: every curve that is no contour is in `result.loose`
  (`cluster_ref` = index of the cluster containing it, or `None`); read `warnings`, `notes`, `errors`
  (each a `topo.Diagnostic`, a `str` with `code`, `points`, `help`).
- `faces()` and `outlines()` are two readings of one `Network`: never feed one's output to the other.
- `faces()` puts holes in `cluster.inners`; `outlines()` has no inners (closed loops go to `loose`).
- Roles: topo knows `outer`, `inner`, `unknown`; any other slug is the caller's. `role_rules=`
  is a `load_dxf` parameter, not a `heal` one; a non-structural role is set aside
  unless `heal(is_structural=...)` accepts it. Your detections go in `cluster.overlay`.
- Full names always: `topo.geometry.gap(...)`, `topo.experimental.face_loops(...)`;
  `topo.core` is internal, `topo.experimental` usable but unstable.

## Signatures (generated from the code)

<!-- gen:signatures -->
topo_dxf.load_dxf(path, *, upgrade=False, explode_inserts=True, flatten_z=True, verbose=False, role_rules=(), skip_layers=()) -> Document
topo.load_geometry(entities, *, source_path='') -> Document
topo.load_segments(segments, *, role='unknown', source_path='') -> Document
topo.validate_document(doc) -> Result
topo.heal(doc, *, max_gap=None, is_structural=None, source_file='', label='') -> Network
Network.faces() -> Result
Network.outlines() -> Result
topo.split(result) -> Split
topo.anchor_texts(result, *, snap_distance=0.0) -> None
Result.promote(curves, cluster, name, *, item=None) -> None
topo_dxf.to_dxf(result, *, filter_cluster=None, include_texts=True, include_loose=True, text_layer='Annotation', role_styles=None, layers=None, allow_invalid=True, exclude_types=None) -> ezdxf.document.Drawing
topo.save_json(result, path, *, indent=2, extra=None) -> None
topo.name_rules(mapping) -> List[RoleRule]
topo.RoleRule(role, *, name=None, name_contains=None, dashed=None, dash=None, color=None, filled=None)
topo.non_contour_candidates(doc) -> List[Edge]
topo.geometry.contour_shape(item) -> Optional[ContourShape]
topo.geometry.gap(a, b) -> Gap
topo.to_shapely(item, *, tolerance=None)
<!-- /gen:signatures -->

## Types: fields, properties, methods (generated)

<!-- gen:types -->
Document: edges, texts, source_path, source_format, warnings, notes
Network: edges, set_aside, warnings, notes, errors, max_gap, source_file, label, source_format, crossings, free_ends, is_valid, faces(), outlines()
Result: clusters, source_file, source_format, reading, max_gap, warnings, notes, errors, loose, texts, is_valid, promote(), to_dict()
Cluster: outer, inners, label, overlay, area, bbox, overlay_voids, summary, features(), to_dict()
Contour: role, segments, styles, depth, parent, area, bbox, centroid, to_dict()
Edge: role, start, end, segment, style
Text: position, text, height, rotation, width_factor, role, cluster_ref, halign, valign, rendered, measures
<!-- /gen:types -->

## LLM.md by line range (generated)

<!-- gen:sections -->
- 11-33 (1.4 KB) What it is
- 34-45 (0.9 KB) Pipeline
  - 46-61 (1.7 KB) Which output gives you what
- 62-78 (0.9 KB) How to write a call
- 79-94 (0.6 KB) Golden path
- 95-150 (25.4 KB) Functions — `topo.<name>`
- 151-168 (5.4 KB) Adapters — `topo_dxf.<name>`, `topo_pdf.<name>`
  - 169-178 (0.5 KB) Debug/inspect (stdout only, 3 levels in order)
- 179-215 (9.0 KB) Domain types
- 216-226 (1.3 KB) Roles — open vocabulary
  - 227-288 (2.6 KB) Building your own role + palette (external-tool recipe)
- 289-310 (1.2 KB) Reading by outlines: `outlines()`
  - 311-343 (1.4 KB) Consumer recipe
- 344-373 (6.8 KB) Hard rules (violate these = broken output)
- 374-391 (3.8 KB) Experimental — `topo.experimental`, no stability guarantee
- 392-405 (1.1 KB) Module layout (only if you need to import something not re-exported)
<!-- /gen:sections -->
