g-topo 0.40.23__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (112) hide show
  1. g_topo-0.40.23/LICENSE +10 -0
  2. g_topo-0.40.23/PKG-INFO +338 -0
  3. g_topo-0.40.23/README.md +306 -0
  4. g_topo-0.40.23/g_topo.egg-info/PKG-INFO +338 -0
  5. g_topo-0.40.23/g_topo.egg-info/SOURCES.txt +110 -0
  6. g_topo-0.40.23/g_topo.egg-info/dependency_links.txt +1 -0
  7. g_topo-0.40.23/g_topo.egg-info/requires.txt +21 -0
  8. g_topo-0.40.23/g_topo.egg-info/top_level.txt +3 -0
  9. g_topo-0.40.23/pyproject.toml +58 -0
  10. g_topo-0.40.23/setup.cfg +4 -0
  11. g_topo-0.40.23/topo/__init__.py +129 -0
  12. g_topo-0.40.23/topo/core/base/__init__.py +1 -0
  13. g_topo-0.40.23/topo/core/base/cleanup.py +468 -0
  14. g_topo-0.40.23/topo/core/base/curves.py +104 -0
  15. g_topo-0.40.23/topo/core/base/gaps.py +535 -0
  16. g_topo-0.40.23/topo/core/base/hierarchy.py +189 -0
  17. g_topo-0.40.23/topo/core/base/merge.py +451 -0
  18. g_topo-0.40.23/topo/core/base/prepare.py +100 -0
  19. g_topo-0.40.23/topo/core/base/refit.py +246 -0
  20. g_topo-0.40.23/topo/core/geometry/__init__.py +71 -0
  21. g_topo-0.40.23/topo/core/geometry/axis.py +239 -0
  22. g_topo-0.40.23/topo/core/geometry/build.py +205 -0
  23. g_topo-0.40.23/topo/core/geometry/concentric.py +90 -0
  24. g_topo-0.40.23/topo/core/geometry/corners.py +112 -0
  25. g_topo-0.40.23/topo/core/geometry/curve_tests.py +141 -0
  26. g_topo-0.40.23/topo/core/geometry/fitting.py +522 -0
  27. g_topo-0.40.23/topo/core/geometry/integrals.py +293 -0
  28. g_topo-0.40.23/topo/core/geometry/intersections.py +247 -0
  29. g_topo-0.40.23/topo/core/geometry/lines.py +180 -0
  30. g_topo-0.40.23/topo/core/geometry/measure.py +238 -0
  31. g_topo-0.40.23/topo/core/geometry/numeric.py +31 -0
  32. g_topo-0.40.23/topo/core/geometry/points.py +138 -0
  33. g_topo-0.40.23/topo/core/geometry/regions.py +569 -0
  34. g_topo-0.40.23/topo/core/geometry/rings.py +299 -0
  35. g_topo-0.40.23/topo/core/geometry/shape.py +332 -0
  36. g_topo-0.40.23/topo/core/geometry/shapely_polygons.py +85 -0
  37. g_topo-0.40.23/topo/core/geometry/spline_fit.py +140 -0
  38. g_topo-0.40.23/topo/core/geometry/strips.py +285 -0
  39. g_topo-0.40.23/topo/core/geometry_input.py +220 -0
  40. g_topo-0.40.23/topo/core/primitives/__init__.py +11 -0
  41. g_topo-0.40.23/topo/core/primitives/clothoid.py +195 -0
  42. g_topo-0.40.23/topo/core/primitives/conics.py +661 -0
  43. g_topo-0.40.23/topo/core/primitives/formula.py +443 -0
  44. g_topo-0.40.23/topo/core/primitives/line.py +70 -0
  45. g_topo-0.40.23/topo/core/primitives/mathseg.py +113 -0
  46. g_topo-0.40.23/topo/core/primitives/minimize.py +69 -0
  47. g_topo-0.40.23/topo/core/primitives/motion.py +48 -0
  48. g_topo-0.40.23/topo/core/primitives/polyline.py +24 -0
  49. g_topo-0.40.23/topo/core/primitives/spline.py +368 -0
  50. g_topo-0.40.23/topo/core/primitives/trim.py +251 -0
  51. g_topo-0.40.23/topo/core/readings/__init__.py +1 -0
  52. g_topo-0.40.23/topo/core/readings/faces.py +150 -0
  53. g_topo-0.40.23/topo/core/readings/heal.py +46 -0
  54. g_topo-0.40.23/topo/core/readings/heal_steps.py +137 -0
  55. g_topo-0.40.23/topo/core/readings/network.py +302 -0
  56. g_topo-0.40.23/topo/core/readings/outer_scan.py +207 -0
  57. g_topo-0.40.23/topo/core/readings/outline.py +197 -0
  58. g_topo-0.40.23/topo/core/readings/parity_faces.py +139 -0
  59. g_topo-0.40.23/topo/core/topology/edge.py +76 -0
  60. g_topo-0.40.23/topo/core/topology/graph.py +406 -0
  61. g_topo-0.40.23/topo/core/topology/loops.py +84 -0
  62. g_topo-0.40.23/topo/core/topology/matching.py +541 -0
  63. g_topo-0.40.23/topo/core/topology/noding.py +754 -0
  64. g_topo-0.40.23/topo/core/topology/non_contour_edges.py +96 -0
  65. g_topo-0.40.23/topo/core/topology/outer_face.py +344 -0
  66. g_topo-0.40.23/topo/core/topology/parity.py +173 -0
  67. g_topo-0.40.23/topo/core/topology/rim.py +241 -0
  68. g_topo-0.40.23/topo/core/topology/touching.py +100 -0
  69. g_topo-0.40.23/topo/experimental.py +71 -0
  70. g_topo-0.40.23/topo/inspect.py +219 -0
  71. g_topo-0.40.23/topo/io/json_export.py +54 -0
  72. g_topo-0.40.23/topo/io/shapes.py +77 -0
  73. g_topo-0.40.23/topo/io/svg.py +199 -0
  74. g_topo-0.40.23/topo/io/view_model.py +192 -0
  75. g_topo-0.40.23/topo/model/__init__.py +65 -0
  76. g_topo-0.40.23/topo/model/cluster.py +109 -0
  77. g_topo-0.40.23/topo/model/contour.py +68 -0
  78. g_topo-0.40.23/topo/model/curve.py +95 -0
  79. g_topo-0.40.23/topo/model/diagnostic.py +91 -0
  80. g_topo-0.40.23/topo/model/document.py +52 -0
  81. g_topo-0.40.23/topo/model/overlay.py +92 -0
  82. g_topo-0.40.23/topo/model/result.py +114 -0
  83. g_topo-0.40.23/topo/model/role.py +133 -0
  84. g_topo-0.40.23/topo/model/role_rule.py +137 -0
  85. g_topo-0.40.23/topo/model/style.py +126 -0
  86. g_topo-0.40.23/topo/model/text.py +183 -0
  87. g_topo-0.40.23/topo/precision.py +69 -0
  88. g_topo-0.40.23/topo/primitives.py +26 -0
  89. g_topo-0.40.23/topo/rules/validator.py +123 -0
  90. g_topo-0.40.23/topo/tools/__init__.py +16 -0
  91. g_topo-0.40.23/topo/tools/anchor.py +72 -0
  92. g_topo-0.40.23/topo/tools/non_contour.py +48 -0
  93. g_topo-0.40.23/topo/tools/rotate.py +236 -0
  94. g_topo-0.40.23/topo/tools/split.py +164 -0
  95. g_topo-0.40.23/topo/tools/strips.py +135 -0
  96. g_topo-0.40.23/topo_dxf/__init__.py +56 -0
  97. g_topo-0.40.23/topo_dxf/adapter.py +333 -0
  98. g_topo-0.40.23/topo_dxf/exporter.py +412 -0
  99. g_topo-0.40.23/topo_dxf/inspect.py +138 -0
  100. g_topo-0.40.23/topo_dxf/layers.py +95 -0
  101. g_topo-0.40.23/topo_dxf/loader.py +426 -0
  102. g_topo-0.40.23/topo_dxf/mtext.py +43 -0
  103. g_topo-0.40.23/topo_dxf/parser.py +162 -0
  104. g_topo-0.40.23/topo_dxf/sanitize.py +526 -0
  105. g_topo-0.40.23/topo_dxf/text_extractor.py +598 -0
  106. g_topo-0.40.23/topo_dxf/writer.py +416 -0
  107. g_topo-0.40.23/topo_pdf/__init__.py +13 -0
  108. g_topo-0.40.23/topo_pdf/extractor_adapter.py +75 -0
  109. g_topo-0.40.23/topo_pdf/geometry_adapter.py +46 -0
  110. g_topo-0.40.23/topo_pdf/graph_adapter.py +31 -0
  111. g_topo-0.40.23/topo_pdf/loader.py +78 -0
  112. g_topo-0.40.23/topo_pdf/sanitize.py +105 -0
g_topo-0.40.23/LICENSE ADDED
@@ -0,0 +1,10 @@
1
+ Copyright (c) 2026 Federico Sidraschi. All rights reserved.
2
+
3
+ This software and its associated documentation are the proprietary property
4
+ of Federico Sidraschi. No license, express or implied, is granted to use,
5
+ copy, modify, merge, publish, distribute, sublicense, or sell copies of it,
6
+ in whole or in part, without prior written permission from the copyright
7
+ holder.
8
+
9
+ If you would like to try or use this software, please get in touch:
10
+ smia4punto6@gmail.com
@@ -0,0 +1,338 @@
1
+ Metadata-Version: 2.4
2
+ Name: g-topo
3
+ Version: 0.40.23
4
+ Summary: Deterministic 2D geometry engine: it repairs vector geometry and reads its structure
5
+ Author: Federico Sidraschi
6
+ License: All rights reserved
7
+ Keywords: cad,geometry,technical-drawing,dxf
8
+ Classifier: Development Status :: 3 - Alpha
9
+ Classifier: Intended Audience :: Developers
10
+ Classifier: Topic :: Scientific/Engineering
11
+ Classifier: Programming Language :: Python :: 3
12
+ Requires-Python: >=3.10
13
+ Description-Content-Type: text/markdown
14
+ License-File: LICENSE
15
+ Requires-Dist: shapely>=2.0
16
+ Requires-Dist: numpy>=1.24
17
+ Provides-Extra: dxf
18
+ Requires-Dist: ezdxf>=1.1; extra == "dxf"
19
+ Provides-Extra: pdf
20
+ Requires-Dist: PyMuPDF; extra == "pdf"
21
+ Provides-Extra: raster
22
+ Requires-Dist: opencv-python; extra == "raster"
23
+ Requires-Dist: Pillow; extra == "raster"
24
+ Provides-Extra: dev
25
+ Requires-Dist: pytest; extra == "dev"
26
+ Requires-Dist: pytest-cov; extra == "dev"
27
+ Requires-Dist: pytest-timeout; extra == "dev"
28
+ Requires-Dist: ezdxf>=1.1; extra == "dev"
29
+ Provides-Extra: all
30
+ Requires-Dist: g-topo[dxf,pdf,raster]; extra == "all"
31
+ Dynamic: license-file
32
+
33
+ # topo
34
+
35
+ **Deterministic 2D geometry engine: it repairs vector geometry and reads its structure — closed contours, what lies inside what, what is connected.**
36
+
37
+ `topo` turns broken 2D geometry into a clean, lossless model — healed closed
38
+ contours, their containment hierarchy, the drawing's texts, and an open overlay
39
+ where a consumer attaches its own reading — and renders it back out. The model
40
+ is the product. **topo knows no file format**: it works on geometry and roles.
41
+ Files come in and go out through adapters, sibling packages in the same
42
+ distribution.
43
+
44
+ The distinctive part is the **healing**: curves that should meet but do not —
45
+ small gaps, doubles, overshoots, pieces drawn one by one — are reconnected into
46
+ closed contours, and nothing is silently dropped.
47
+
48
+ Today the geometry mostly comes from CAD files and technical drawings (DXF in;
49
+ DXF, JSON or SVG out); the engine itself sees only geometry, whatever its source.
50
+
51
+ > Status: **alpha**. Used in production, but the API still moves.
52
+
53
+ > **Writing code against topo with an AI assistant?** Give it
54
+ > [`docs/LLM.md`](docs/LLM.md) too: every public name with its signature, in few
55
+ > tokens. This README is the overview; names outside the stable API live in
56
+ > `topo.experimental` and are always written with that prefix.
57
+
58
+ ---
59
+
60
+ ## Install
61
+
62
+ ```bash
63
+ pip install -e . # topo alone: geometry in, SVG / JSON / text out
64
+ pip install -e ".[dxf]" # + topo_dxf: DXF and DWG files in, DXF out
65
+ pip install -e ".[pdf]" # + topo_pdf: PDF in (experimental)
66
+ ```
67
+
68
+ The commands above install from a clone of the repository. From a built wheel, name the file:
69
+ `pip install "g_topo-<version>-py3-none-any.whl[dxf]"`. g-topo is not on PyPI.
70
+
71
+ One distribution (`g-topo`), three importable packages:
72
+
73
+ | package | what |
74
+ |------------|---------------------------------------------------------------|
75
+ | `topo` | the engine: model, heal, the two readings, split, SVG / JSON / text |
76
+ | `topo_dxf` | file adapter: reads DXF and DWG, writes DXF. DWG needs the free [ODA File Converter](https://www.opendesign.com/guestfiles/oda_file_converter) installed (`ODA_PATH`) |
77
+ | `topo_pdf` | file adapter: reads PDF (experimental) |
78
+
79
+ `topo` never imports an adapter, and an adapter uses only topo's public names —
80
+ a boundary the index check enforces (`scripts/gen_index.py --check`). What an
81
+ adapter reads and writes, and its options, are in `docs/API.md` (§9, §10).
82
+ Python ≥ 3.10.
83
+
84
+ ---
85
+
86
+ ## Quick start — geometry in hand
87
+
88
+ ```python
89
+ import topo
90
+
91
+ fg = topo.geometry
92
+ doc = topo.load_segments(fg.rectangle(200, 100) + fg.circle(10)) # -> Document
93
+ net = topo.heal(doc) # repaired once -> Network
94
+ result = net.faces() # -> Result
95
+
96
+ if not result.is_valid: # no closed outer contour
97
+ print(result.errors)
98
+
99
+ cluster = result.clusters[0] # one part: outer 200×100, one inner circle
100
+ print(cluster.outer.area, len(cluster.inners))
101
+ topo.save_json(result, "part.json") # the model's facts
102
+ topo.experimental.save_svg(result, "part.svg") # a picture of it
103
+ ```
104
+
105
+ Builders return topo segments: `polygon(points)`, `rectangle`, `regular_polygon`,
106
+ `circle`, `ellipse`, `stadium` (with true arcs). Any other straight-sided shape is a `polygon`.
107
+
108
+ ## From a file
109
+
110
+ ```python
111
+ import topo, topo_dxf
112
+
113
+ doc = topo_dxf.load_dxf("part.dxf", tolerance=0.5) # .dxf or .dwg -> Document
114
+ result = topo.heal(doc).faces()
115
+ topo_dxf.to_dxf(result, doc).saveas("part_healed.dxf")
116
+ ```
117
+
118
+ After `load_dxf` everything works on the format-free `Document`; the adapter is
119
+ the only code that knows the file.
120
+
121
+ ## Many parts in one drawing
122
+
123
+ ```python
124
+ for part in topo.split(result): # one Result per part, no format
125
+ name = part.clusters[0].label # _P1, _P2, ... (heal(doc, label="x") gives x_P1; or namer=)
126
+ topo.experimental.save_svg(part, f"output/{name}.svg")
127
+ ```
128
+
129
+ `split` is a decision on the model: which parts (`min_area`), their names
130
+ (`namer`), which loose curves and texts travel with each (the part that
131
+ contains them, or the nearest). An adapter then writes each part in its format.
132
+
133
+ ## Two readings of one healed network
134
+
135
+ `heal()` repairs the geometry once (nodes, doubles, gaps up to `max_gap`) and
136
+ returns a `Network`; a reading of it gives a `Result`.
137
+
138
+ - `faces()` reads from the inside: which loops close, which lies inside which.
139
+ For separate flat outlines.
140
+ - `outlines()` reads from the outside: curves that touch form a group, the outer
141
+ contour of each group is the outer face of its planar network, and every outer
142
+ contour not inside another is a cluster with only its `outer` (what lies inside
143
+ goes to `loose`). For a drawing of several views.
144
+
145
+ ```python
146
+ result = topo.heal(doc, max_gap=0.5).outlines()
147
+ for cluster in result.clusters: # one view (or part) per outline
148
+ print(cluster.outer.area)
149
+ ```
150
+
151
+ `max_gap` is the widest gap closed; without it no gap is closed, and every gap closed is listed in `notes`. The readings are
152
+ recipes over public steps: `heal()`'s steps are exported one by one in
153
+ `topo.experimental` (`topo.experimental.split_labeled`,
154
+ `topo.experimental.close_free_gaps`, `topo.experimental.face_loops`,
155
+ `topo.experimental.build_hierarchy`, ...), so a consumer can compose its own
156
+ order — see `docs/API.md` (the steps of `heal()`).
157
+
158
+ ## Shapes: what a contour is, not what it is for
159
+
160
+ topo names geometry, never its use. A circle is a circle; what it is for is a
161
+ consumer's reading.
162
+
163
+ ```python
164
+ for cluster in result.clusters:
165
+ for inner in cluster.inners:
166
+ shape = topo.geometry.contour_shape(inner) # circle / ellipse / stadium / rectangle / polygon / other
167
+ print(shape.kind, shape.center, shape.length, shape.width)
168
+
169
+ # circles sharing a center, smallest first (singletons included)
170
+ for group in topo.geometry.concentric_groups(cluster.inners, tolerance=0.1):
171
+ if len(group.items) > 1:
172
+ print("concentric:", group.center, group.diameters)
173
+
174
+ # arcs concentric to a circle and larger: sweep, radius ratio
175
+ # (an open arc closes nothing, so it is in result.loose)
176
+ arcs = [s for c in result.loose for s in c.segments if isinstance(s, topo.primitives.CircleSeg) and not s.full]
177
+ for found in topo.geometry.arcs_around((10, 20), 2.5, arcs, tolerance=0.1):
178
+ print(found.sweep, found.radius_ratio)
179
+ ```
180
+
181
+ No thresholds of meaning inside: what an arc of a given sweep means is decided
182
+ by the consumer.
183
+
184
+ ---
185
+
186
+ ## The pipeline
187
+
188
+ ```
189
+ an adapter (a file) ──► Document (edges + texts)
190
+ topo.load_segments(segs) ──► Document geometry in hand
191
+ │
192
+ ▼
193
+ topo.heal(doc) ──► Network repaired once: nodes, doubles, gaps
194
+ │
195
+ ▼ .faces() / .outlines()
196
+ Result closed contours, outer / inner containment tree
197
+ │
198
+ ▼
199
+ (a consumer's reading) written to cluster.overlay
200
+ │
201
+ ▼
202
+ topo.split(result) ──► list[Result] one per part, no format
203
+ topo.save_json(result, path) the model's facts as JSON
204
+ topo.experimental.to_svg(result) ──► SVG string for a UI / report (outside the 1.0 stable API)
205
+ topo.experimental.to_view_model(result) ──► dict (full geometry) for an external renderer (outside the 1.0 stable API)
206
+ an adapter ──► a file
207
+ ```
208
+
209
+ Every renderer draws from the same model, never from the source file, so they
210
+ all show the same picture.
211
+
212
+ ## What a `Result` holds
213
+
214
+ ```
215
+ Result clusters: list[Cluster] loose: list (curves that are no cluster's contour)
216
+ texts is_valid errors warnings notes
217
+ reading ("faces" / "outlines") tolerance max_gap
218
+ source_file source_format cluster_count
219
+ Cluster outer: Contour inners: list[Contour] label overlay (a consumer's reading)
220
+ area bbox
221
+ Contour segments (LineSeg / CircleSeg / EllipseSeg / BSplineSeg) role
222
+ area bbox centroid points depth parent contains(...)
223
+ ```
224
+
225
+ Shapes are asked of the geometry, not stored: `topo.geometry.contour_shape(contour)`.
226
+ Full cards in `docs/API.md` (`Result`, `Cluster`, `Contour`).
227
+
228
+ ---
229
+
230
+ ## Inspecting
231
+
232
+ ```python
233
+ topo.inspect_document(doc) # what the input is: edges, primitives, node graph
234
+ topo.inspect_result(result) # what topo made: clusters, outer/inner, overlay, loose curves
235
+ ```
236
+
237
+ Everything prints to stdout. Use it when something does not come out right and
238
+ you need to see where in the chain it breaks. An adapter can add a level of its
239
+ own for the raw file (`docs/API.md`).
240
+
241
+ ---
242
+
243
+ ## What topo promises, and its limits
244
+
245
+ - **Millimetres**: every length, tolerance and area is in mm. topo does not read the unit a file
246
+ declares (it is often wrong): convert an inch drawing to mm before reading it.
247
+ - **Faithful**: every contour lies within `tolerance` of the source geometry, both ways.
248
+ - **Complete**: every curve is a cluster's contour or in `result.loose`; nothing is
249
+ silently dropped.
250
+ - **Deterministic**: the same input gives the same result, every run. The same
251
+ geometry moved or rotated is *not* promised to give the same result: a yes/no
252
+ rule on a measured number has an edge, and the rounding of a rotation can push
253
+ a value across it. Why, for any reader: [`docs/LIMITS.md`](docs/LIMITS.md).
254
+ - A gap wider than `max_gap` is not closed: the curves stay in `loose`.
255
+ - If no closed outer contour can be formed, `result.is_valid` is `False`.
256
+ Renderers still draw what is there, so you can see what topo understood; a
257
+ caller feeding a machine passes `allow_invalid=False` and gets `ValueError`
258
+ instead. `topo.split` always raises: one result per part means nothing without parts.
259
+ - **Advice is experimental.** A warning is a `topo.Diagnostic`: what happened
260
+ (`code`, `message`), where (`points`) and why (`notes`) are stable. The call it
261
+ suggests to fix it (`help`, `param`, `value`) is experimental: it can be wrong
262
+ on a case it was not measured on, and it can change in a minor. Read it, judge
263
+ it, never apply it blindly.
264
+
265
+ **Known defects of the healing** — a wrong result you can meet today, to be
266
+ fixed in 1.x. Each was reproduced on the current code.
267
+
268
+ - *Self-crossing curve.* One curve that crosses itself is not cut at its own
269
+ crossing: both readings return the whole ring as one contour (a square with a
270
+ curl gives the ring's signed area, not the square plus a lobe in `loose`).
271
+ The warning `self-crossing` says so.
272
+ - *A node on another closed curve.* Two simple closed curves where a vertex of
273
+ one lies exactly on the other's boundary: `outlines()` gives the first and puts
274
+ the second in `loose`; the warning `closed-touch` says so.
275
+ - *A circle drawn as many short lines with a tiny opening.* It is refitted into
276
+ one open curve whose two ends are never treated as a gap, so it stays in
277
+ `loose` instead of closing (about 6% of such rings in a random sample); the
278
+ note `open-spline` reports it.
279
+ - *A curve written as points loses details under 0.02 mm.* Ten or more short
280
+ lines in a row are refitted into lines, arcs, conics and splines within
281
+ 0.02 mm of their points; a corner or a hook smaller than that, even on
282
+ exact points, can come out smoothed.
283
+ - *Edges excluded from the graph.* With a custom `is_structural` that accepts
284
+ every role, `faces()` can deliver edges set aside as non-contour nowhere at
285
+ all (no cluster, nothing in `loose`) — an exception to *complete* above. The
286
+ warning `non-contour` counts them; `outlines()` delivers them.
287
+ - *Near-copy advice on real corners.* A short line with both ends within
288
+ `tolerance` of a longer one is reported as a near-copy even where the two meet
289
+ at a real corner (45–90°), and its advice suggests a merge that would open the
290
+ contour. The geometry is read right; only the advice is wrong.
291
+
292
+ **Behaviour by design**, worth knowing:
293
+
294
+ - *One threshold.* Two points closer than `topo.precision.SAME_POINT` (0.001 mm)
295
+ are one point, everywhere in topo. Below 0.001 the contacts on splines still
296
+ break contours: a drawing whose points coincide only more coarsely needs `max_gap`.
297
+ - *Tiny pieces are welded.* A group of strokes within `SAME_POINT` is welded onto one
298
+ node (note `welded`): a piece shorter than that cannot be told from a node.
299
+ - *A jump is closed, never moved.* Where one curve passes another by a few
300
+ thousandths, the reading adds a short straight segment between the two ends
301
+ (note `gap-closed`) instead of moving either curve: the drawing's own curves
302
+ stay as drawn. Trimming the end along itself onto the other curve, as at a
303
+ crossing, is planned for 1.x.
304
+ - *`outlines()` joins pieces that continue each other* (collinear or on one
305
+ circle, within 1e-3) into one curve; `faces()` keeps every piece as drawn.
306
+ Both stay within `tolerance` of the source.
307
+ - *Two closed curves that cross* (the same circle drawn twice, 0.05 apart) are
308
+ two clusters and the result stays valid, with the warning `crossing`.
309
+ Whether crossing contours should make a result invalid is not decided yet.
310
+
311
+ **Cost and experimental parts**
312
+
313
+ - Drawings made of long chains of very short strokes cost about twice as much
314
+ to heal; spline discretization is the largest single cost of `outlines()`;
315
+ two large sheets of views measured over 300 s.
316
+ - Texts linked to geometry (`topo.experimental`) miss some links a reader would draw.
317
+
318
+ ---
319
+
320
+ ## Documentation
321
+
322
+ - **[`docs/LLM.md`](docs/LLM.md)** — a dense, token-minimal reference for an AI
323
+ agent writing code against topo: same ground as `API.md`. Give this one to an LLM.
324
+ - **[`docs/API.md`](docs/API.md)** — every public function: signature, what it
325
+ takes, returns, mutates, when it raises. With copyable examples. The adapters too.
326
+ - **[`docs/ARCHITECTURE.md`](docs/ARCHITECTURE.md)** — how it is built and why:
327
+ the layers, the flow, "the model is the product".
328
+ - **[`docs/LIMITS.md`](docs/LIMITS.md)** — what topo cannot promise, and why.
329
+ - **[`tutorial/`](tutorial/README.md)** — numbered lessons, from a part drawn in code to a folder of your own drawings.
330
+ - **`SCRIPTS.md`** — the maintenance tools in `scripts/`.
331
+
332
+ ---
333
+
334
+ ## License
335
+
336
+ All rights reserved — see [`LICENSE`](LICENSE). Copyright (c) 2026 Federico
337
+ Sidraschi. This is not open source; if you'd like to try or use it, get in
338
+ touch: smia4punto6@gmail.com.
@@ -0,0 +1,306 @@
1
+ # topo
2
+
3
+ **Deterministic 2D geometry engine: it repairs vector geometry and reads its structure — closed contours, what lies inside what, what is connected.**
4
+
5
+ `topo` turns broken 2D geometry into a clean, lossless model — healed closed
6
+ contours, their containment hierarchy, the drawing's texts, and an open overlay
7
+ where a consumer attaches its own reading — and renders it back out. The model
8
+ is the product. **topo knows no file format**: it works on geometry and roles.
9
+ Files come in and go out through adapters, sibling packages in the same
10
+ distribution.
11
+
12
+ The distinctive part is the **healing**: curves that should meet but do not —
13
+ small gaps, doubles, overshoots, pieces drawn one by one — are reconnected into
14
+ closed contours, and nothing is silently dropped.
15
+
16
+ Today the geometry mostly comes from CAD files and technical drawings (DXF in;
17
+ DXF, JSON or SVG out); the engine itself sees only geometry, whatever its source.
18
+
19
+ > Status: **alpha**. Used in production, but the API still moves.
20
+
21
+ > **Writing code against topo with an AI assistant?** Give it
22
+ > [`docs/LLM.md`](docs/LLM.md) too: every public name with its signature, in few
23
+ > tokens. This README is the overview; names outside the stable API live in
24
+ > `topo.experimental` and are always written with that prefix.
25
+
26
+ ---
27
+
28
+ ## Install
29
+
30
+ ```bash
31
+ pip install -e . # topo alone: geometry in, SVG / JSON / text out
32
+ pip install -e ".[dxf]" # + topo_dxf: DXF and DWG files in, DXF out
33
+ pip install -e ".[pdf]" # + topo_pdf: PDF in (experimental)
34
+ ```
35
+
36
+ The commands above install from a clone of the repository. From a built wheel, name the file:
37
+ `pip install "g_topo-<version>-py3-none-any.whl[dxf]"`. g-topo is not on PyPI.
38
+
39
+ One distribution (`g-topo`), three importable packages:
40
+
41
+ | package | what |
42
+ |------------|---------------------------------------------------------------|
43
+ | `topo` | the engine: model, heal, the two readings, split, SVG / JSON / text |
44
+ | `topo_dxf` | file adapter: reads DXF and DWG, writes DXF. DWG needs the free [ODA File Converter](https://www.opendesign.com/guestfiles/oda_file_converter) installed (`ODA_PATH`) |
45
+ | `topo_pdf` | file adapter: reads PDF (experimental) |
46
+
47
+ `topo` never imports an adapter, and an adapter uses only topo's public names —
48
+ a boundary the index check enforces (`scripts/gen_index.py --check`). What an
49
+ adapter reads and writes, and its options, are in `docs/API.md` (§9, §10).
50
+ Python ≥ 3.10.
51
+
52
+ ---
53
+
54
+ ## Quick start — geometry in hand
55
+
56
+ ```python
57
+ import topo
58
+
59
+ fg = topo.geometry
60
+ doc = topo.load_segments(fg.rectangle(200, 100) + fg.circle(10)) # -> Document
61
+ net = topo.heal(doc) # repaired once -> Network
62
+ result = net.faces() # -> Result
63
+
64
+ if not result.is_valid: # no closed outer contour
65
+ print(result.errors)
66
+
67
+ cluster = result.clusters[0] # one part: outer 200×100, one inner circle
68
+ print(cluster.outer.area, len(cluster.inners))
69
+ topo.save_json(result, "part.json") # the model's facts
70
+ topo.experimental.save_svg(result, "part.svg") # a picture of it
71
+ ```
72
+
73
+ Builders return topo segments: `polygon(points)`, `rectangle`, `regular_polygon`,
74
+ `circle`, `ellipse`, `stadium` (with true arcs). Any other straight-sided shape is a `polygon`.
75
+
76
+ ## From a file
77
+
78
+ ```python
79
+ import topo, topo_dxf
80
+
81
+ doc = topo_dxf.load_dxf("part.dxf", tolerance=0.5) # .dxf or .dwg -> Document
82
+ result = topo.heal(doc).faces()
83
+ topo_dxf.to_dxf(result, doc).saveas("part_healed.dxf")
84
+ ```
85
+
86
+ After `load_dxf` everything works on the format-free `Document`; the adapter is
87
+ the only code that knows the file.
88
+
89
+ ## Many parts in one drawing
90
+
91
+ ```python
92
+ for part in topo.split(result): # one Result per part, no format
93
+ name = part.clusters[0].label # _P1, _P2, ... (heal(doc, label="x") gives x_P1; or namer=)
94
+ topo.experimental.save_svg(part, f"output/{name}.svg")
95
+ ```
96
+
97
+ `split` is a decision on the model: which parts (`min_area`), their names
98
+ (`namer`), which loose curves and texts travel with each (the part that
99
+ contains them, or the nearest). An adapter then writes each part in its format.
100
+
101
+ ## Two readings of one healed network
102
+
103
+ `heal()` repairs the geometry once (nodes, doubles, gaps up to `max_gap`) and
104
+ returns a `Network`; a reading of it gives a `Result`.
105
+
106
+ - `faces()` reads from the inside: which loops close, which lies inside which.
107
+ For separate flat outlines.
108
+ - `outlines()` reads from the outside: curves that touch form a group, the outer
109
+ contour of each group is the outer face of its planar network, and every outer
110
+ contour not inside another is a cluster with only its `outer` (what lies inside
111
+ goes to `loose`). For a drawing of several views.
112
+
113
+ ```python
114
+ result = topo.heal(doc, max_gap=0.5).outlines()
115
+ for cluster in result.clusters: # one view (or part) per outline
116
+ print(cluster.outer.area)
117
+ ```
118
+
119
+ `max_gap` is the widest gap closed; without it no gap is closed, and every gap closed is listed in `notes`. The readings are
120
+ recipes over public steps: `heal()`'s steps are exported one by one in
121
+ `topo.experimental` (`topo.experimental.split_labeled`,
122
+ `topo.experimental.close_free_gaps`, `topo.experimental.face_loops`,
123
+ `topo.experimental.build_hierarchy`, ...), so a consumer can compose its own
124
+ order — see `docs/API.md` (the steps of `heal()`).
125
+
126
+ ## Shapes: what a contour is, not what it is for
127
+
128
+ topo names geometry, never its use. A circle is a circle; what it is for is a
129
+ consumer's reading.
130
+
131
+ ```python
132
+ for cluster in result.clusters:
133
+ for inner in cluster.inners:
134
+ shape = topo.geometry.contour_shape(inner) # circle / ellipse / stadium / rectangle / polygon / other
135
+ print(shape.kind, shape.center, shape.length, shape.width)
136
+
137
+ # circles sharing a center, smallest first (singletons included)
138
+ for group in topo.geometry.concentric_groups(cluster.inners, tolerance=0.1):
139
+ if len(group.items) > 1:
140
+ print("concentric:", group.center, group.diameters)
141
+
142
+ # arcs concentric to a circle and larger: sweep, radius ratio
143
+ # (an open arc closes nothing, so it is in result.loose)
144
+ arcs = [s for c in result.loose for s in c.segments if isinstance(s, topo.primitives.CircleSeg) and not s.full]
145
+ for found in topo.geometry.arcs_around((10, 20), 2.5, arcs, tolerance=0.1):
146
+ print(found.sweep, found.radius_ratio)
147
+ ```
148
+
149
+ No thresholds of meaning inside: what an arc of a given sweep means is decided
150
+ by the consumer.
151
+
152
+ ---
153
+
154
+ ## The pipeline
155
+
156
+ ```
157
+ an adapter (a file) ──► Document (edges + texts)
158
+ topo.load_segments(segs) ──► Document geometry in hand
159
+ │
160
+ ▼
161
+ topo.heal(doc) ──► Network repaired once: nodes, doubles, gaps
162
+ │
163
+ ▼ .faces() / .outlines()
164
+ Result closed contours, outer / inner containment tree
165
+ │
166
+ ▼
167
+ (a consumer's reading) written to cluster.overlay
168
+ │
169
+ ▼
170
+ topo.split(result) ──► list[Result] one per part, no format
171
+ topo.save_json(result, path) the model's facts as JSON
172
+ topo.experimental.to_svg(result) ──► SVG string for a UI / report (outside the 1.0 stable API)
173
+ topo.experimental.to_view_model(result) ──► dict (full geometry) for an external renderer (outside the 1.0 stable API)
174
+ an adapter ──► a file
175
+ ```
176
+
177
+ Every renderer draws from the same model, never from the source file, so they
178
+ all show the same picture.
179
+
180
+ ## What a `Result` holds
181
+
182
+ ```
183
+ Result clusters: list[Cluster] loose: list (curves that are no cluster's contour)
184
+ texts is_valid errors warnings notes
185
+ reading ("faces" / "outlines") tolerance max_gap
186
+ source_file source_format cluster_count
187
+ Cluster outer: Contour inners: list[Contour] label overlay (a consumer's reading)
188
+ area bbox
189
+ Contour segments (LineSeg / CircleSeg / EllipseSeg / BSplineSeg) role
190
+ area bbox centroid points depth parent contains(...)
191
+ ```
192
+
193
+ Shapes are asked of the geometry, not stored: `topo.geometry.contour_shape(contour)`.
194
+ Full cards in `docs/API.md` (`Result`, `Cluster`, `Contour`).
195
+
196
+ ---
197
+
198
+ ## Inspecting
199
+
200
+ ```python
201
+ topo.inspect_document(doc) # what the input is: edges, primitives, node graph
202
+ topo.inspect_result(result) # what topo made: clusters, outer/inner, overlay, loose curves
203
+ ```
204
+
205
+ Everything prints to stdout. Use it when something does not come out right and
206
+ you need to see where in the chain it breaks. An adapter can add a level of its
207
+ own for the raw file (`docs/API.md`).
208
+
209
+ ---
210
+
211
+ ## What topo promises, and its limits
212
+
213
+ - **Millimetres**: every length, tolerance and area is in mm. topo does not read the unit a file
214
+ declares (it is often wrong): convert an inch drawing to mm before reading it.
215
+ - **Faithful**: every contour lies within `tolerance` of the source geometry, both ways.
216
+ - **Complete**: every curve is a cluster's contour or in `result.loose`; nothing is
217
+ silently dropped.
218
+ - **Deterministic**: the same input gives the same result, every run. The same
219
+ geometry moved or rotated is *not* promised to give the same result: a yes/no
220
+ rule on a measured number has an edge, and the rounding of a rotation can push
221
+ a value across it. Why, for any reader: [`docs/LIMITS.md`](docs/LIMITS.md).
222
+ - A gap wider than `max_gap` is not closed: the curves stay in `loose`.
223
+ - If no closed outer contour can be formed, `result.is_valid` is `False`.
224
+ Renderers still draw what is there, so you can see what topo understood; a
225
+ caller feeding a machine passes `allow_invalid=False` and gets `ValueError`
226
+ instead. `topo.split` always raises: one result per part means nothing without parts.
227
+ - **Advice is experimental.** A warning is a `topo.Diagnostic`: what happened
228
+ (`code`, `message`), where (`points`) and why (`notes`) are stable. The call it
229
+ suggests to fix it (`help`, `param`, `value`) is experimental: it can be wrong
230
+ on a case it was not measured on, and it can change in a minor. Read it, judge
231
+ it, never apply it blindly.
232
+
233
+ **Known defects of the healing** — a wrong result you can meet today, to be
234
+ fixed in 1.x. Each was reproduced on the current code.
235
+
236
+ - *Self-crossing curve.* One curve that crosses itself is not cut at its own
237
+ crossing: both readings return the whole ring as one contour (a square with a
238
+ curl gives the ring's signed area, not the square plus a lobe in `loose`).
239
+ The warning `self-crossing` says so.
240
+ - *A node on another closed curve.* Two simple closed curves where a vertex of
241
+ one lies exactly on the other's boundary: `outlines()` gives the first and puts
242
+ the second in `loose`; the warning `closed-touch` says so.
243
+ - *A circle drawn as many short lines with a tiny opening.* It is refitted into
244
+ one open curve whose two ends are never treated as a gap, so it stays in
245
+ `loose` instead of closing (about 6% of such rings in a random sample); the
246
+ note `open-spline` reports it.
247
+ - *A curve written as points loses details under 0.02 mm.* Ten or more short
248
+ lines in a row are refitted into lines, arcs, conics and splines within
249
+ 0.02 mm of their points; a corner or a hook smaller than that, even on
250
+ exact points, can come out smoothed.
251
+ - *Edges excluded from the graph.* With a custom `is_structural` that accepts
252
+ every role, `faces()` can deliver edges set aside as non-contour nowhere at
253
+ all (no cluster, nothing in `loose`) — an exception to *complete* above. The
254
+ warning `non-contour` counts them; `outlines()` delivers them.
255
+ - *Near-copy advice on real corners.* A short line with both ends within
256
+ `tolerance` of a longer one is reported as a near-copy even where the two meet
257
+ at a real corner (45–90°), and its advice suggests a merge that would open the
258
+ contour. The geometry is read right; only the advice is wrong.
259
+
260
+ **Behaviour by design**, worth knowing:
261
+
262
+ - *One threshold.* Two points closer than `topo.precision.SAME_POINT` (0.001 mm)
263
+ are one point, everywhere in topo. Below 0.001 the contacts on splines still
264
+ break contours: a drawing whose points coincide only more coarsely needs `max_gap`.
265
+ - *Tiny pieces are welded.* A group of strokes within `SAME_POINT` is welded onto one
266
+ node (note `welded`): a piece shorter than that cannot be told from a node.
267
+ - *A jump is closed, never moved.* Where one curve passes another by a few
268
+ thousandths, the reading adds a short straight segment between the two ends
269
+ (note `gap-closed`) instead of moving either curve: the drawing's own curves
270
+ stay as drawn. Trimming the end along itself onto the other curve, as at a
271
+ crossing, is planned for 1.x.
272
+ - *`outlines()` joins pieces that continue each other* (collinear or on one
273
+ circle, within 1e-3) into one curve; `faces()` keeps every piece as drawn.
274
+ Both stay within `tolerance` of the source.
275
+ - *Two closed curves that cross* (the same circle drawn twice, 0.05 apart) are
276
+ two clusters and the result stays valid, with the warning `crossing`.
277
+ Whether crossing contours should make a result invalid is not decided yet.
278
+
279
+ **Cost and experimental parts**
280
+
281
+ - Drawings made of long chains of very short strokes cost about twice as much
282
+ to heal; spline discretization is the largest single cost of `outlines()`;
283
+ two large sheets of views measured over 300 s.
284
+ - Texts linked to geometry (`topo.experimental`) miss some links a reader would draw.
285
+
286
+ ---
287
+
288
+ ## Documentation
289
+
290
+ - **[`docs/LLM.md`](docs/LLM.md)** — a dense, token-minimal reference for an AI
291
+ agent writing code against topo: same ground as `API.md`. Give this one to an LLM.
292
+ - **[`docs/API.md`](docs/API.md)** — every public function: signature, what it
293
+ takes, returns, mutates, when it raises. With copyable examples. The adapters too.
294
+ - **[`docs/ARCHITECTURE.md`](docs/ARCHITECTURE.md)** — how it is built and why:
295
+ the layers, the flow, "the model is the product".
296
+ - **[`docs/LIMITS.md`](docs/LIMITS.md)** — what topo cannot promise, and why.
297
+ - **[`tutorial/`](tutorial/README.md)** — numbered lessons, from a part drawn in code to a folder of your own drawings.
298
+ - **`SCRIPTS.md`** — the maintenance tools in `scripts/`.
299
+
300
+ ---
301
+
302
+ ## License
303
+
304
+ All rights reserved — see [`LICENSE`](LICENSE). Copyright (c) 2026 Federico
305
+ Sidraschi. This is not open source; if you'd like to try or use it, get in
306
+ touch: smia4punto6@gmail.com.