@margoengine/loaders 0.0.0-stage → 1.0.0

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 (226) hide show
  1. package/LICENSE +201 -0
  2. package/NOTICE +4 -0
  3. package/README.md +1665 -2
  4. package/dist/3dm.d.ts +161 -0
  5. package/dist/3dm.js +289 -0
  6. package/dist/3ds.d.ts +124 -0
  7. package/dist/3ds.js +392 -0
  8. package/dist/acclaim.d.ts +90 -0
  9. package/dist/acclaim.js +312 -0
  10. package/dist/amf.d.ts +45 -0
  11. package/dist/amf.js +256 -0
  12. package/dist/babylon.d.ts +197 -0
  13. package/dist/babylon.js +701 -0
  14. package/dist/common.d.ts +7 -0
  15. package/dist/common.js +6 -0
  16. package/dist/crease.d.ts +7 -0
  17. package/dist/crease.js +67 -0
  18. package/dist/dae-animation.d.ts +66 -0
  19. package/dist/dae-animation.js +266 -0
  20. package/dist/dae-common.d.ts +95 -0
  21. package/dist/dae-common.js +226 -0
  22. package/dist/dae-controllers.d.ts +57 -0
  23. package/dist/dae-controllers.js +164 -0
  24. package/dist/dae-kinematics.d.ts +65 -0
  25. package/dist/dae-kinematics.js +461 -0
  26. package/dist/dae-pose.d.ts +131 -0
  27. package/dist/dae-pose.js +381 -0
  28. package/dist/dae-scene.d.ts +203 -0
  29. package/dist/dae-scene.js +279 -0
  30. package/dist/dae.d.ts +166 -0
  31. package/dist/dae.js +644 -0
  32. package/dist/detect.d.ts +20 -0
  33. package/dist/detect.js +72 -0
  34. package/dist/dxf-hatch.d.ts +8 -0
  35. package/dist/dxf-hatch.js +346 -0
  36. package/dist/dxf-leaders.d.ts +59 -0
  37. package/dist/dxf-leaders.js +258 -0
  38. package/dist/dxf.d.ts +122 -0
  39. package/dist/dxf.js +985 -0
  40. package/dist/engine.d.ts +41 -0
  41. package/dist/engine.js +120 -0
  42. package/dist/font-opentype.d.ts +14 -0
  43. package/dist/font-opentype.js +33 -0
  44. package/dist/font-woff.d.ts +23 -0
  45. package/dist/font-woff.js +293 -0
  46. package/dist/font.d.ts +41 -0
  47. package/dist/font.js +26 -0
  48. package/dist/gcode.d.ts +92 -0
  49. package/dist/gcode.js +369 -0
  50. package/dist/geo-model.d.ts +84 -0
  51. package/dist/geo-model.js +220 -0
  52. package/dist/geo-mvt.d.ts +15 -0
  53. package/dist/geo-mvt.js +302 -0
  54. package/dist/geo-shp.d.ts +15 -0
  55. package/dist/geo-shp.js +234 -0
  56. package/dist/geo-xml.d.ts +6 -0
  57. package/dist/geo-xml.js +165 -0
  58. package/dist/geo.d.ts +8 -0
  59. package/dist/geo.js +59 -0
  60. package/dist/geometry.d.ts +60 -0
  61. package/dist/geometry.js +273 -0
  62. package/dist/index.d.ts +54 -0
  63. package/dist/index.js +28 -0
  64. package/dist/kmz.d.ts +78 -0
  65. package/dist/kmz.js +173 -0
  66. package/dist/las.d.ts +92 -0
  67. package/dist/las.js +110 -0
  68. package/dist/ldraw.d.ts +118 -0
  69. package/dist/ldraw.js +474 -0
  70. package/dist/load.d.ts +72 -0
  71. package/dist/load.js +205 -0
  72. package/dist/lottie.d.ts +154 -0
  73. package/dist/lottie.js +893 -0
  74. package/dist/lwo.d.ts +81 -0
  75. package/dist/lwo.js +454 -0
  76. package/dist/lzf.d.ts +13 -0
  77. package/dist/lzf.js +56 -0
  78. package/dist/materialx.d.ts +120 -0
  79. package/dist/materialx.js +563 -0
  80. package/dist/md2.d.ts +52 -0
  81. package/dist/md2.js +112 -0
  82. package/dist/mmcif.d.ts +14 -0
  83. package/dist/mmcif.js +246 -0
  84. package/dist/mmd.d.ts +244 -0
  85. package/dist/mmd.js +958 -0
  86. package/dist/mocap.d.ts +71 -0
  87. package/dist/mocap.js +178 -0
  88. package/dist/molecule-common.d.ts +4 -0
  89. package/dist/molecule-common.js +54 -0
  90. package/dist/mtl.d.ts +125 -0
  91. package/dist/mtl.js +550 -0
  92. package/dist/normals.d.ts +7 -0
  93. package/dist/normals.js +31 -0
  94. package/dist/nrrd.d.ts +70 -0
  95. package/dist/nrrd.js +785 -0
  96. package/dist/nurbs.d.ts +118 -0
  97. package/dist/nurbs.js +265 -0
  98. package/dist/obj.d.ts +145 -0
  99. package/dist/obj.js +462 -0
  100. package/dist/off.d.ts +10 -0
  101. package/dist/off.js +155 -0
  102. package/dist/outline-font.d.ts +87 -0
  103. package/dist/outline-font.js +110 -0
  104. package/dist/outline.d.ts +20 -0
  105. package/dist/outline.js +215 -0
  106. package/dist/pcd.d.ts +34 -0
  107. package/dist/pcd.js +258 -0
  108. package/dist/pdb.d.ts +67 -0
  109. package/dist/pdb.js +126 -0
  110. package/dist/ply.d.ts +35 -0
  111. package/dist/ply.js +392 -0
  112. package/dist/polygon.d.ts +9 -0
  113. package/dist/polygon.js +49 -0
  114. package/dist/prwm.d.ts +24 -0
  115. package/dist/prwm.js +114 -0
  116. package/dist/render.d.ts +32 -0
  117. package/dist/render.js +181 -0
  118. package/dist/sfnt-cff.d.ts +37 -0
  119. package/dist/sfnt-cff.js +749 -0
  120. package/dist/sfnt-layout.d.ts +24 -0
  121. package/dist/sfnt-layout.js +266 -0
  122. package/dist/sfnt-reader.d.ts +28 -0
  123. package/dist/sfnt-reader.js +87 -0
  124. package/dist/sfnt-variation.d.ts +40 -0
  125. package/dist/sfnt-variation.js +318 -0
  126. package/dist/shared/asset-url.js +82 -0
  127. package/dist/shared/crc32.d.ts +2 -0
  128. package/dist/shared/crc32.js +47 -0
  129. package/dist/shared/errors.js +36 -0
  130. package/dist/shared/inflate.js +284 -0
  131. package/dist/shared/normals.js +26 -0
  132. package/dist/shared/scene-scope.js +56 -0
  133. package/dist/shared/trailing-data.js +45 -0
  134. package/dist/stl.d.ts +64 -0
  135. package/dist/stl.js +309 -0
  136. package/dist/svg.d.ts +90 -0
  137. package/dist/svg.js +1007 -0
  138. package/dist/tds.d.ts +2 -0
  139. package/dist/tds.js +2 -0
  140. package/dist/textures.d.ts +42 -0
  141. package/dist/textures.js +53 -0
  142. package/dist/threemf-beam-reader.d.ts +19 -0
  143. package/dist/threemf-beam-reader.js +165 -0
  144. package/dist/threemf-beams.d.ts +52 -0
  145. package/dist/threemf-beams.js +268 -0
  146. package/dist/threemf-boolean-read.d.ts +10 -0
  147. package/dist/threemf-boolean-read.js +113 -0
  148. package/dist/threemf-boolean.d.ts +59 -0
  149. package/dist/threemf-boolean.js +20 -0
  150. package/dist/threemf-csg.d.ts +14 -0
  151. package/dist/threemf-csg.js +193 -0
  152. package/dist/threemf-displacement-read.d.ts +8 -0
  153. package/dist/threemf-displacement-read.js +213 -0
  154. package/dist/threemf-displacement.d.ts +71 -0
  155. package/dist/threemf-displacement.js +18 -0
  156. package/dist/threemf-evaluate.d.ts +36 -0
  157. package/dist/threemf-evaluate.js +86 -0
  158. package/dist/threemf-functions.d.ts +33 -0
  159. package/dist/threemf-functions.js +359 -0
  160. package/dist/threemf-lattice-mesh.d.ts +9 -0
  161. package/dist/threemf-lattice-mesh.js +115 -0
  162. package/dist/threemf-lattice.d.ts +139 -0
  163. package/dist/threemf-lattice.js +379 -0
  164. package/dist/threemf-levelset.d.ts +23 -0
  165. package/dist/threemf-levelset.js +105 -0
  166. package/dist/threemf-materials.d.ts +162 -0
  167. package/dist/threemf-materials.js +497 -0
  168. package/dist/threemf-mesh.d.ts +75 -0
  169. package/dist/threemf-mesh.js +422 -0
  170. package/dist/threemf-secure.d.ts +50 -0
  171. package/dist/threemf-secure.js +153 -0
  172. package/dist/threemf-tessellate.d.ts +37 -0
  173. package/dist/threemf-tessellate.js +277 -0
  174. package/dist/threemf-volume-colour.d.ts +25 -0
  175. package/dist/threemf-volume-colour.js +233 -0
  176. package/dist/threemf-volume-sample.d.ts +8 -0
  177. package/dist/threemf-volume-sample.js +65 -0
  178. package/dist/threemf-volumetric-read.d.ts +15 -0
  179. package/dist/threemf-volumetric-read.js +539 -0
  180. package/dist/threemf-volumetric.d.ts +177 -0
  181. package/dist/threemf-volumetric.js +28 -0
  182. package/dist/threemf.d.ts +170 -0
  183. package/dist/threemf.js +609 -0
  184. package/dist/tilt.d.ts +62 -0
  185. package/dist/tilt.js +186 -0
  186. package/dist/tmx.d.ts +151 -0
  187. package/dist/tmx.js +394 -0
  188. package/dist/ttf.d.ts +78 -0
  189. package/dist/ttf.js +568 -0
  190. package/dist/types.d.ts +108 -0
  191. package/dist/types.js +35 -0
  192. package/dist/usd.d.ts +2 -0
  193. package/dist/usd.js +2 -0
  194. package/dist/usd.worker.d.ts +1 -0
  195. package/dist/usdz.d.ts +50 -0
  196. package/dist/usdz.js +646 -0
  197. package/dist/util.d.ts +75 -0
  198. package/dist/util.js +241 -0
  199. package/dist/vdb.d.ts +111 -0
  200. package/dist/vdb.js +663 -0
  201. package/dist/vrml.d.ts +53 -0
  202. package/dist/vrml.js +532 -0
  203. package/dist/vtk-cells.d.ts +29 -0
  204. package/dist/vtk-cells.js +211 -0
  205. package/dist/vtk-legacy.d.ts +57 -0
  206. package/dist/vtk-legacy.js +439 -0
  207. package/dist/vtk-reader.d.ts +19 -0
  208. package/dist/vtk-reader.js +134 -0
  209. package/dist/vtk-xml.d.ts +96 -0
  210. package/dist/vtk-xml.js +578 -0
  211. package/dist/vtk.d.ts +13 -0
  212. package/dist/vtk.js +234 -0
  213. package/dist/woff-glyf.d.ts +17 -0
  214. package/dist/woff-glyf.js +215 -0
  215. package/dist/worker.d.ts +10 -0
  216. package/dist/worker.js +93 -0
  217. package/dist/workers/usd.js +3 -0
  218. package/dist/xfile.d.ts +99 -0
  219. package/dist/xfile.js +706 -0
  220. package/dist/xml.d.ts +107 -0
  221. package/dist/xml.js +843 -0
  222. package/dist/xyz.d.ts +16 -0
  223. package/dist/xyz.js +110 -0
  224. package/dist/zip.d.ts +38 -0
  225. package/dist/zip.js +150 -0
  226. package/package.json +289 -4
@@ -0,0 +1,258 @@
1
+ // The DXF LEADER and MULTILEADER entities as plain geometry: leader paths, arrowheads and, for a MULTILEADER, its
2
+ // text or block content. Written from Autodesk's DXF reference and public descriptions of the format
3
+ // (docs/clean-room.md, "DXF leaders"). Coordinates are the entity's own (world coordinates of its space); parseDXF
4
+ // places them.
5
+ const number = (v, fallback = 0) => {
6
+ const n = typeof v === 'number' ? v : v === undefined ? Number.NaN : Number(v.trim());
7
+ return Number.isFinite(n) ? n : fallback;
8
+ };
9
+ const string = (v) => (v === undefined ? '' : String(v).trim());
10
+ const add = (a, b, s = 1) => [a[0] + s * b[0], a[1] + s * b[1], a[2] + s * b[2]];
11
+ const distance = (a, b) => Math.hypot(b[0] - a[0], b[1] - a[1], b[2] - a[2]);
12
+ const unit = (v) => {
13
+ const l = Math.hypot(v[0], v[1], v[2]);
14
+ return l > 0 && l < Infinity ? [v[0] / l, v[1] / l, v[2] / l] : undefined;
15
+ };
16
+ const cross = (a, b) => [
17
+ a[1] * b[2] - a[2] * b[1],
18
+ a[2] * b[0] - a[0] * b[2],
19
+ a[0] * b[1] - a[1] * b[0],
20
+ ];
21
+ /**
22
+ * The dimension variables an entity's xdata overrides: under application `ACAD`, `DSTYLE`, then between the braces
23
+ * pairs of a variable's code (1070) and its value.
24
+ */
25
+ export function dimensionOverrides(codes, values) {
26
+ const out = new Map();
27
+ let k = codes.findIndex((c, i) => c === 1001 && string(values[i]) === 'ACAD');
28
+ if (k < 0)
29
+ return out;
30
+ while (k < codes.length && !(codes[k] === 1000 && string(values[k]) === 'DSTYLE'))
31
+ k++;
32
+ if (codes[k + 1] !== 1002 || string(values[k + 1]) !== '{')
33
+ return out;
34
+ for (k += 2; codes[k] === 1070 && k + 1 < codes.length && codes[k + 1] !== 1002; k += 2)
35
+ out.set(number(values[k]), values[k + 1]);
36
+ return out;
37
+ }
38
+ /**
39
+ * The complete cubic spline through `points` (C. de Boor, *A Practical Guide to Splines*, Springer 1978, chapter IV):
40
+ * chord-length parameters, end tangents along the first and last chords, solved by the tridiagonal elimination.
41
+ */
42
+ function fitSpline(points, limits) {
43
+ const p = points.filter((q, i) => i === 0 || distance(q, points[i - 1]) > 0), n = p.length;
44
+ if (n < 3)
45
+ return p;
46
+ if ((n - 1) * limits.segments > limits.most)
47
+ limits.tooMany();
48
+ const h = p.slice(1).map((q, i) => distance(p[i], q)), tangent = p.map(() => [0, 0, 0]), upper = new Float64Array(n);
49
+ tangent[0] = unit(add(p[1], p[0], -1));
50
+ tangent[n - 1] = unit(add(p[n - 1], p[n - 2], -1));
51
+ // Row i: h[i] m[i-1] + 2 (h[i-1] + h[i]) m[i] + h[i-1] m[i+1] = 3 (h[i]/h[i-1] dP[i-1] + h[i-1]/h[i] dP[i]).
52
+ for (let i = 1; i < n - 1; i++) {
53
+ const pivot = 2 * (h[i - 1] + h[i]) - h[i] * upper[i - 1];
54
+ upper[i] = h[i - 1] / pivot;
55
+ for (let a = 0; a < 3; a++) {
56
+ let right = 3 * ((h[i] / h[i - 1]) * (p[i][a] - p[i - 1][a]) + (h[i - 1] / h[i]) * (p[i + 1][a] - p[i][a])) -
57
+ h[i] * tangent[i - 1][a];
58
+ if (i === n - 2)
59
+ right -= h[i - 1] * tangent[n - 1][a];
60
+ tangent[i][a] = right / pivot;
61
+ }
62
+ }
63
+ for (let i = n - 3; i > 0; i--)
64
+ for (let a = 0; a < 3; a++)
65
+ tangent[i][a] -= upper[i] * tangent[i + 1][a];
66
+ const out = [p[0]];
67
+ for (let i = 0; i < n - 1; i++)
68
+ for (let k = 1; k <= limits.segments; k++) {
69
+ const t = k / limits.segments, t2 = t * t, t3 = t2 * t, a = 2 * t3 - 3 * t2 + 1, b = (t3 - 2 * t2 + t) * h[i], c = 3 * t2 - 2 * t3, d = (t3 - t2) * h[i];
70
+ out.push([0, 1, 2].map((x) => a * p[i][x] + b * tangent[i][x] + c * p[i + 1][x] + d * tangent[i + 1][x]));
71
+ }
72
+ return out;
73
+ }
74
+ /** The segment from `a` to `b` without the parts between each break's two points. */
75
+ function broken(a, b, breaks) {
76
+ const along = add(b, a, -1), at = (q) => (q[0] - a[0]) * along[0] + (q[1] - a[1]) * along[1] + (q[2] - a[2]) * along[2], gaps = breaks.map(([s, e]) => (at(s) <= at(e) ? [s, e] : [e, s])).sort((x, y) => at(x[0]) - at(y[0]));
77
+ const pieces = [];
78
+ let from = a;
79
+ for (const [start, end] of gaps) {
80
+ if (at(start) > at(from))
81
+ pieces.push([from, start]);
82
+ if (at(end) > at(from))
83
+ from = end;
84
+ }
85
+ if (at(b) > at(from))
86
+ pieces.push([from, b]);
87
+ return pieces;
88
+ }
89
+ /** No arrowhead is drawn on a first segment shorter than twice its size, as AutoCAD does. */
90
+ const arrowOn = (path, normal, size) => size > 0 && path.length > 1 && distance(path[0], path[1]) >= 2 * size
91
+ ? { tip: path[0], from: path[1], normal, size }
92
+ : undefined;
93
+ /** A LEADER: its vertices, as segments or a spline, the arrowhead at the first and the hook line under its text. */
94
+ export function readLeader(codes, values, dimension, limits) {
95
+ const first = (code) => {
96
+ const k = codes.indexOf(code);
97
+ return k < 0 ? undefined : values[k];
98
+ }, vertices = [];
99
+ for (let k = 0; k < codes.length; k++)
100
+ if (codes[k] === 10)
101
+ vertices.push([number(values[k]), 0, 0]);
102
+ else if ((codes[k] === 20 || codes[k] === 30) && vertices.length)
103
+ vertices[vertices.length - 1][codes[k] / 10 - 1] = number(values[k]);
104
+ const scale = number(dimension(40), 1) || 1, normal = [number(first(210)), number(first(220)), number(first(230), 1)], width = number(first(41));
105
+ // With text above the line (DIMTAD), a leader that carries text runs on under it. The file does not store this part.
106
+ if (vertices.length > 1 && number(dimension(77)) !== 0 && number(first(73), 3) === 0 && width > 0) {
107
+ const horizontal = unit([number(first(211), 1), number(first(221)), number(first(231))]) ?? [1, 0, 0], length = Math.abs(number(dimension(147))) * scale + width;
108
+ vertices.push(add(vertices[vertices.length - 1], horizontal, number(first(74)) === 1 ? -length : length));
109
+ }
110
+ const path = number(first(72)) === 1 ? fitSpline(vertices, limits) : vertices, arrow = number(first(71), 1) !== 0 ? arrowOn(vertices, normal, number(dimension(41)) * scale) : undefined;
111
+ return { paths: path.length > 1 ? [path] : [], arrows: arrow ? [arrow] : [] };
112
+ }
113
+ const block = () => ({ values: new Map(), points: [] });
114
+ const put = (to, code, value) => {
115
+ // Points: x codes 10 to 18 and 110 to 112, y and z 10 and 20 above them.
116
+ const family = code >= 100 ? code - 100 : code, axis = Math.floor(family / 10) - 1;
117
+ if (family >= 10 && family < 40) {
118
+ const x = code - 10 * axis;
119
+ if (axis === 0)
120
+ to.points.push({ code: x, p: [number(value), 0, 0] });
121
+ else {
122
+ for (let k = to.points.length - 1; k >= 0; k--)
123
+ if (to.points[k].code === x) {
124
+ to.points[k].p[axis] = number(value);
125
+ break;
126
+ }
127
+ }
128
+ }
129
+ else if (!to.values.has(code))
130
+ to.values.set(code, value);
131
+ };
132
+ const point = (from, code) => from.points.find((q) => q.code === code)?.p;
133
+ /** Break pairs: each start point (`start`) with the end point (`end`) that follows it. */
134
+ const pairs = (points, start, end) => {
135
+ const out = [];
136
+ let open;
137
+ for (const q of points)
138
+ if (q.code === start)
139
+ open = q.p;
140
+ else if (q.code === end && open) {
141
+ out.push([open, q.p]);
142
+ open = undefined;
143
+ }
144
+ return out;
145
+ };
146
+ /**
147
+ * A MULTILEADER, or `undefined` without its context data. The context block holds the content and one block per
148
+ * leader (landing point, dogleg), which holds one block per leader line (vertices and breaks).
149
+ */
150
+ export function readMultiLeader(codes, values, dimension, limits) {
151
+ const common = block(), context = block(), leaders = [], attributes = [];
152
+ let level = 'common', found = false, attribute = '';
153
+ for (let k = 0; k < codes.length && codes[k] < 1000; k++) {
154
+ const code = codes[k], value = values[k], mark = code >= 300 && code <= 305 ? string(value) : '';
155
+ if (level === 'common' && code === 300 && mark === 'CONTEXT_DATA{') {
156
+ level = 'context';
157
+ found = true;
158
+ }
159
+ else if (level === 'context' && code === 302 && mark === 'LEADER{') {
160
+ level = 'leader';
161
+ leaders.push({ own: block(), lines: [] });
162
+ }
163
+ else if (level === 'leader' && code === 304 && mark === 'LEADER_LINE{') {
164
+ level = 'line';
165
+ leaders[leaders.length - 1].lines.push(block());
166
+ }
167
+ else if (level === 'line' && code === 305 && mark === '}')
168
+ level = 'leader';
169
+ else if (level === 'leader' && code === 303 && mark === '}')
170
+ level = 'context';
171
+ else if (level === 'context' && code === 301 && mark === '}')
172
+ level = 'common';
173
+ else if (level === 'context')
174
+ put(context, code, value);
175
+ else if (level === 'leader')
176
+ put(leaders[leaders.length - 1].own, code, value);
177
+ else if (level === 'line')
178
+ put(leaders[leaders.length - 1].lines.at(-1), code, value);
179
+ else if (code === 330)
180
+ attribute = string(value);
181
+ else if (code === 302)
182
+ attributes.push([attribute, String(value)]);
183
+ else if (!common.values.has(code))
184
+ common.values.set(code, value);
185
+ }
186
+ if (!found)
187
+ return undefined;
188
+ const type = number(common.values.get(170), 1), doglegs = number(common.values.get(291), 1) !== 0, xAxis = point(context, 111), yAxis = point(context, 112), normal = (xAxis && yAxis && unit(cross(xAxis, yAxis))) || point(context, 11) || [0, 0, 1], size = number(context.values.get(140), number(common.values.get(42), number(dimension(41)) * (number(dimension(40), 1) || 1))), handle = string(common.values.get(342)), paths = [], arrows = [];
189
+ // A leader type of 0 is invisible: only the content is drawn.
190
+ if (type !== 0)
191
+ for (const { own, lines } of leaders) {
192
+ const landing = number(own.values.get(290), 1) !== 0 ? point(own, 10) : undefined, direction = number(own.values.get(291), 1) !== 0 ? unit(point(own, 11) ?? [0, 0, 0]) : undefined, length = number(own.values.get(40)), end = landing && direction ? add(landing, direction, length) : landing,
193
+ // Without a drawn dogleg the lines run straight to where it would have ended.
194
+ dogleg = !!(doglegs && type === 1 && landing && direction && length !== 0), join = dogleg ? landing : end;
195
+ for (const line of lines) {
196
+ const vertices = line.points.filter((q) => q.code === 10).map((q) => q.p);
197
+ if (!vertices.length)
198
+ continue;
199
+ if (join)
200
+ vertices.push(join);
201
+ const arrow = arrowOn(vertices, normal, size);
202
+ if (arrow)
203
+ arrows.push(handle && !/^0*$/.test(handle) ? { ...arrow, handle } : arrow);
204
+ if (type === 2) {
205
+ const path = fitSpline(vertices, limits);
206
+ if (path.length > 1)
207
+ paths.push(path);
208
+ continue;
209
+ }
210
+ // Breaks follow the vertex their segment starts at.
211
+ let start = 0, run = [vertices[0]];
212
+ for (let i = 0; i + 1 < vertices.length; i++) {
213
+ while (start < line.points.length && line.points[start].code !== 10)
214
+ start++;
215
+ let stop = start + 1;
216
+ while (stop < line.points.length && line.points[stop].code !== 10)
217
+ stop++;
218
+ const breaks = pairs(line.points.slice(start + 1, stop), 11, 12);
219
+ start = stop;
220
+ if (!breaks.length) {
221
+ run.push(vertices[i + 1]);
222
+ continue;
223
+ }
224
+ if (run.length > 1)
225
+ paths.push(run);
226
+ const pieces = broken(vertices[i], vertices[i + 1], breaks), last = pieces.at(-1),
227
+ // The part that reaches the next vertex goes on into the next segment.
228
+ joins = last && distance(last[1], vertices[i + 1]) === 0;
229
+ paths.push(...(joins ? pieces.slice(0, -1) : pieces));
230
+ run = joins ? [...last] : [vertices[i + 1]];
231
+ }
232
+ if (run.length > 1)
233
+ paths.push(run);
234
+ }
235
+ if (dogleg)
236
+ paths.push(...broken(landing, end, pairs(own.points, 12, 13)));
237
+ }
238
+ const content = number(common.values.get(172), -1), hasText = number(context.values.get(290)) !== 0, hasBlock = number(context.values.get(296)) !== 0, raw = String(context.values.get(304) ?? ''), plan = { paths, arrows, tolerance: content === 3 };
239
+ if (raw && (content === 2 || (content < 0 && hasText)))
240
+ plan.text = {
241
+ raw,
242
+ position: point(context, 12) ?? point(context, 10) ?? [0, 0, 0],
243
+ height: number(context.values.get(41)),
244
+ direction: point(context, 13) ?? [1, 0, 0],
245
+ normal: point(context, 11) ?? [0, 0, 1],
246
+ };
247
+ if (content === 1 || (content < 0 && hasBlock))
248
+ plan.block = {
249
+ handle: string(context.values.get(341) ?? common.values.get(344)),
250
+ position: point(context, 15) ?? point(context, 10) ?? [0, 0, 0],
251
+ normal: point(context, 14) ?? [0, 0, 1],
252
+ // Radians, unlike the degrees of INSERT.
253
+ rotation: number(context.values.get(46)),
254
+ scale: point(context, 16) ?? [1, 1, 1],
255
+ attributes,
256
+ };
257
+ return plan;
258
+ }
package/dist/dxf.d.ts ADDED
@@ -0,0 +1,122 @@
1
+ import { type LoadedGeometry, type LoadedGroup, type ParseLimits } from './types.js';
2
+ import { type BinaryInput } from './util.js';
3
+ /** Options of {@link parseDXF}. */
4
+ export interface DXFOptions extends ParseLimits {
5
+ /** `'meters'` (default) scales the drawing by its `$INSUNITS`; `'file'` keeps drawing units. */
6
+ units?: 'meters' | 'file';
7
+ /** Line segments per full turn of an arc, circle, ellipse or bulge, 8 to 4,096 (default 64). */
8
+ arcSegments?: number;
9
+ /** Line segments per knot span of a spline, 1 to 1,024 (default 16). */
10
+ splineSegments?: number;
11
+ /** Most line segments in all (default 8,000,000). */
12
+ maxSegments?: number;
13
+ /** Most block references expanded, rows and columns counted one by one (default 1,000,000). */
14
+ maxInserts?: number;
15
+ /** Deepest nesting of block references (default 32). */
16
+ maxDepth?: number;
17
+ /** Draws HATCH entities: `readDXFHatch` from `@margoengine/loaders/dxf-hatch`. Without it they count in `skipped`. */
18
+ hatch?: DXFHatchReader;
19
+ }
20
+ /** One HATCH entity's group codes and parseDXF's curve samplers, as a {@link DXFHatchReader} gets them. */
21
+ export interface DXFHatchInput {
22
+ codes: readonly number[];
23
+ values: readonly (string | number)[];
24
+ /** The `arcSegments` option: line segments per full turn. */
25
+ arcSegments: number;
26
+ /** Points of an arc at height `z`, from angle `a0` through `sweep` (radians, counterclockwise positive). */
27
+ arc(cx: number, cy: number, z: number, radius: number, a0: number, sweep: number): number[][];
28
+ /** Polyline vertices with bulges as points at height `z`. */
29
+ bulged(vertices: {
30
+ x: number;
31
+ y: number;
32
+ bulge: number;
33
+ }[], closed: boolean, z: number): number[][];
34
+ /** A NURBS curve sampled per knot span; one its numbers cannot make stays its control polygon. */
35
+ spline(degree: number, knots: number[], control: number[][], weights: number[]): number[][];
36
+ /** Most boundary points to hold. */
37
+ maxVertices: number;
38
+ /** Triangles still allowed by `maxTriangles` and `maxVertices`. */
39
+ maxTriangles: number;
40
+ /** Line segments still allowed by `maxSegments`. */
41
+ maxSegments: number;
42
+ }
43
+ /** What a hatch draws, in its object coordinate system. */
44
+ export interface DXFHatchGeometry {
45
+ /** x, y of each triangle corner. */
46
+ triangles: number[];
47
+ /** x0, y0, x1, y1 of each line segment. */
48
+ segments: number[];
49
+ /** z of the hatch in its object coordinate system. */
50
+ elevation: number;
51
+ /** Replaces the entity's colour (a gradient's first colour). */
52
+ color?: DXFColor;
53
+ }
54
+ /** Turns one HATCH entity into geometry, or `undefined` when it cannot. */
55
+ export type DXFHatchReader = (input: DXFHatchInput) => DXFHatchGeometry | undefined;
56
+ /** sRGB in 0..1. */
57
+ export type DXFColor = [number, number, number];
58
+ /** A layer from the LAYER table. */
59
+ export interface DXFLayer {
60
+ name: string;
61
+ color: DXFColor;
62
+ /** False when the layer is off or frozen: its entities are left out. */
63
+ visible: boolean;
64
+ }
65
+ /** A TEXT, MTEXT, MULTILEADER text or block attribute: the string (formatting and control codes resolved) and where it stands. */
66
+ export interface DXFText {
67
+ text: string;
68
+ /** Insertion point, in the output's units. */
69
+ position: [number, number, number];
70
+ /** Character height, in the output's units. */
71
+ height: number;
72
+ /** Direction of the baseline, a unit vector. */
73
+ direction: [number, number, number];
74
+ /** The text's plane normal, a unit vector. */
75
+ normal: [number, number, number];
76
+ layer: string;
77
+ color: DXFColor;
78
+ /** The attribute tag, on texts of `ATTRIB` and `ATTDEF` entities only. */
79
+ tag?: string;
80
+ }
81
+ /** An ACIS solid, body or region: its SAT text (decoded) and the placement of the block reference that holds it. */
82
+ export interface DXFSolid {
83
+ type: '3DSOLID' | 'BODY' | 'REGION';
84
+ /** The SAT data, lines joined with '\n'. */
85
+ sat: string;
86
+ /** Column-major 4x4 from the SAT's coordinates to the output's (units and block placements). */
87
+ transform: number[];
88
+ layer: string;
89
+ color: DXFColor;
90
+ }
91
+ /** Line segments, two vertices each, grouped by layer and colour. */
92
+ export interface DXFLines {
93
+ positions: Float32Array;
94
+ /** sRGB per vertex in 0..1. */
95
+ colors: Float32Array;
96
+ /** Vertex ranges (`start`, `count` in vertices), one per layer and colour. */
97
+ groups: LoadedGroup[];
98
+ }
99
+ /** What {@link parseDXF} reads. */
100
+ export interface DXFResult {
101
+ /** Faces as triangles, grouped by layer (`name`) and colour; empty when the drawing has none. Not consistently wound. */
102
+ mesh: LoadedGeometry;
103
+ lines: DXFLines;
104
+ /** POINT entities, xyz each. */
105
+ points: Float32Array;
106
+ /** TEXT and MTEXT, the text of MULTILEADERs, and the visible attributes of block references. */
107
+ texts: DXFText[];
108
+ layers: DXFLayer[];
109
+ solids: DXFSolid[];
110
+ /** Meters per drawing unit, from `$INSUNITS` (1 when the drawing has none). */
111
+ unit: number;
112
+ /** `$ACADVER`, such as 'AC1032'. */
113
+ version: string;
114
+ /** Entities left out, by type: unsupported kinds, paper space, binary ACIS data. */
115
+ skipped: Record<string, number>;
116
+ }
117
+ /**
118
+ * Reads a DXF drawing (ASCII or binary) into faces, line segments, points, texts and ACIS solids, in
119
+ * meters by default. Throws a {@link LoaderError}: `'syntax'` or `'truncated'` for a malformed file, `'limit'` past a
120
+ * limit, `'range'` for a bad option.
121
+ */
122
+ export declare function parseDXF(input: BinaryInput, options?: DXFOptions): DXFResult;