@jscad/dxf-deserializer 2.3.21 → 3.0.0-alpha.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.
@@ -1,38 +1,31 @@
1
- /*
2
- ## License
1
+ import { arc, circle, ellipse, geom2, geom3, mat4, line, path2, poly3, vec2, vec3 } from '@jscad/modeling'
3
2
 
4
- Copyright (c) 2017 Z3 Development https://github.com/z3dev
3
+ import { getColor, getColorNumber } from './helpers.js'
5
4
 
6
- All code released under MIT license
7
-
8
- */
9
- const { geometries, maths, primitives } = require('@jscad/modeling')
10
5
  const EPS = 1e-5 // FIXME
11
6
 
12
- const { getColor, getColorNumber } = require('./helpers')
13
-
14
7
  //
15
8
  // instantiate the given object (3dface) as a polygon
16
9
  //
17
- const instantiatePolygon = (obj, layers, options) => {
10
+ export const instantiatePolygon = (obj, layers, options) => {
18
11
  const vertices = []
19
12
  // FIXME: should check global variable to instantiate in the proper orientation
20
- vertices.push(maths.vec3.fromValues(obj.pptx, obj.ppty, obj.pptz))
21
- vertices.push(maths.vec3.fromValues(obj.sptx, obj.spty, obj.sptz))
22
- vertices.push(maths.vec3.fromValues(obj.tptx, obj.tpty, obj.tptz))
13
+ vertices.push(vec3.fromValues(obj.pptx, obj.ppty, obj.pptz))
14
+ vertices.push(vec3.fromValues(obj.sptx, obj.spty, obj.sptz))
15
+ vertices.push(vec3.fromValues(obj.tptx, obj.tpty, obj.tptz))
23
16
  if (obj.fptx) {
24
17
  let pushit = false
25
18
  if (obj.tptx !== obj.fptx) { pushit = true }
26
19
  if (obj.tpty !== obj.fpty) { pushit = true }
27
20
  if (obj.tptz !== obj.fptz) { pushit = true }
28
21
  if (pushit) {
29
- vertices.push(maths.vec3.fromValues(obj.fptx, obj.fpty, obj.fptz))
22
+ vertices.push(vec3.fromValues(obj.fptx, obj.fpty, obj.fptz))
30
23
  }
31
24
  }
32
25
  const cn = getColorNumber(obj, layers)
33
26
  const color = getColor(cn, options.colorindex)
34
27
 
35
- const polygon = geometries.poly3.create(vertices)
28
+ const polygon = poly3.create(vertices)
36
29
  if (color) polygon.color = color
37
30
  return polygon
38
31
  }
@@ -43,21 +36,21 @@ const instantiatePolygon = (obj, layers, options) => {
43
36
  const instantiateLine = (obj, layers, options) => {
44
37
  // console.log('***** instantiateLine',obj)
45
38
  if (obj.pptz === obj.sptz && obj.pptz === 0) {
46
- const p1 = maths.vec2.fromValues(obj.pptx, obj.ppty)
47
- const p2 = maths.vec2.fromValues(obj.sptx, obj.spty)
48
- return primitives.line([p1, p2])
39
+ const p1 = vec2.fromValues(obj.pptx, obj.ppty)
40
+ const p2 = vec2.fromValues(obj.sptx, obj.spty)
41
+ return line([p1, p2])
49
42
  }
50
43
 
51
- const p1 = maths.vec3.fromValues(obj.pptx, obj.ppty, obj.pptz)
52
- const p2 = maths.vec3.fromValues(obj.sptx, obj.spty, obj.sptz)
44
+ const p1 = vec3.fromValues(obj.pptx, obj.ppty, obj.pptz)
45
+ const p2 = vec3.fromValues(obj.sptx, obj.spty, obj.sptz)
53
46
  // FIXME what should this really create?
54
- return primitives.line([p1, p2])
47
+ return line([p1, p2])
55
48
  }
56
49
 
57
50
  //
58
51
  // instantiate the give object as 2D Vector or 3D Vector wrapped as an object
59
52
  //
60
- const instantiateVector = (obj) => {
53
+ export const instantiateVector = (obj) => {
61
54
  const d3line = parseInt('00000000000100000', 2)
62
55
  const d3mesh = parseInt('00000000001000000', 2)
63
56
  const d3face = parseInt('00000000010000000', 2)
@@ -65,20 +58,20 @@ const instantiateVector = (obj) => {
65
58
  const flags = obj.lflg
66
59
  const vtype = {}
67
60
  if ((flags & d3line) === d3line) {
68
- vtype.vec = maths.vec3.fromValues(obj.pptx, obj.ppty, obj.pptz)
61
+ vtype.vec = vec3.fromValues(obj.pptx, obj.ppty, obj.pptz)
69
62
  } else
70
63
  if ((flags & d3mesh) === d3mesh) {
71
- vtype.vec = maths.vec3.fromValues(obj.pptx, obj.ppty, obj.pptz)
64
+ vtype.vec = vec3.fromValues(obj.pptx, obj.ppty, obj.pptz)
72
65
  } else
73
66
  if ((flags & d3face) === d3face) {
74
- vtype.vec = maths.vec3.fromValues(obj.pptx, obj.ppty, obj.pptz)
67
+ vtype.vec = vec3.fromValues(obj.pptx, obj.ppty, obj.pptz)
75
68
  // pass on face indexes
76
69
  vtype.fvia = obj.fvia
77
70
  vtype.fvib = obj.fvib
78
71
  vtype.fvic = obj.fvic
79
72
  vtype.fvid = obj.fvid
80
73
  } else {
81
- vtype.vec = maths.vec2.fromValues(obj.pptx, obj.ppty)
74
+ vtype.vec = vec2.fromValues(obj.pptx, obj.ppty)
82
75
  vtype.bulg = obj.bulg // for rendering curved sections
83
76
  }
84
77
  return vtype
@@ -90,20 +83,20 @@ const instantiateVector = (obj) => {
90
83
  const addSection = (path, x1, y1, bulg) => {
91
84
  if (bulg === 0) {
92
85
  // add straight line to the end of the path
93
- path = geometries.path2.appendPoints([[x1, y1]], path)
86
+ path = path2.appendPoints([[x1, y1]], path)
94
87
  } else {
95
88
  // add arc to the end of the path
96
- const points = geometries.path2.toPoints(path)
89
+ const points = path2.toPoints(path)
97
90
  const prev = points[points.length - 1]
98
- const curr = maths.vec2.fromValues(x1, y1)
99
- const u = maths.vec2.distance(prev, curr)
91
+ const curr = vec2.fromValues(x1, y1)
92
+ const u = vec2.distance(prev, curr)
100
93
  const r = u * ((1 + Math.pow(bulg, 2)) / (4 * bulg))
101
94
  const clockwise = (bulg < 0)
102
95
  const large = false // FIXME how to determine?
103
96
  const d = Math.atan(bulg) * 4
104
97
  // FIXME; how to determine resolution
105
98
  const res = 16
106
- path = geometries.path2.appendArc({ endpoint: [x1, y1], radius: [r, r], xaxisrotation: d, clockwise: clockwise, large: large, segments: res }, path)
99
+ path = path2.appendArc({ endpoint: [x1, y1], radius: [r, r], xaxisrotation: d, clockwise: clockwise, large: large, segments: res }, path)
107
100
  }
108
101
  return path
109
102
  }
@@ -123,7 +116,7 @@ const instantiatePath2D = (obj, layers, options) => {
123
116
  const flags = obj.lflg
124
117
 
125
118
  // conversion
126
- let path = geometries.path2.create()
119
+ let path = path2.create()
127
120
  const isclosed = ((flags & closed) === closed)
128
121
  if (vlen === pptxs.length && vlen === pptys.length && vlen === bulgs.length) {
129
122
  pptxs.forEach((item, index, array) => {
@@ -140,7 +133,7 @@ const instantiatePath2D = (obj, layers, options) => {
140
133
  if (isclosed && (!path.isClosed)) {
141
134
  // apply the last section between last and first points
142
135
  path = addSection(path, pptxs[0], pptys[0], bulgs[vlen - 1])
143
- path = geometries.path2.close(path)
136
+ path = path2.close(path)
144
137
  // FIXME add optional to create 2D geometry from the path
145
138
  }
146
139
  return path
@@ -166,10 +159,10 @@ const instantiateArc = (obj, layers, options) => {
166
159
  // conversion
167
160
  if (lthk === 0.0) {
168
161
  // convert to 2D object
169
- return primitives.arc({ center: [pptx, ppty], radius: swid, startAngle: ang0, endAngle: ang1, segments: res })
162
+ return arc({ center: [pptx, ppty], radius: swid, startAngle: ang0, endAngle: ang1, segments: res })
170
163
  }
171
164
  // FIXME how to represent 3D arc?
172
- return primitives.arc({ center: [pptx, ppty], radius: swid, startAngle: ang0, endAngle: ang1, segments: res })
165
+ return arc({ center: [pptx, ppty], radius: swid, startAngle: ang0, endAngle: ang1, segments: res })
173
166
  }
174
167
 
175
168
  //
@@ -191,12 +184,12 @@ const instantiateCircle = (obj, layers, options) => {
191
184
 
192
185
  // convert to 2D object
193
186
  if (lthk === 0.0) {
194
- const cag = primitives.circle({ center: [pptx, ppty], radius: swid, segments: res })
187
+ const cag = circle({ center: [pptx, ppty], radius: swid, segments: res })
195
188
  if (color) cag.color = color
196
189
  return cag
197
190
  }
198
191
  // convert to 3D object
199
- const cag = primitives.circle({ center: [pptx, ppty], radius: swid, segments: res })
192
+ const cag = circle({ center: [pptx, ppty], radius: swid, segments: res })
200
193
  const csg = cag.extrude({ offset: [0, 0, lthk] })
201
194
  // FIXME need to use 210/220/230 for direction of extrusion
202
195
  if (color) csg.color = color
@@ -220,19 +213,19 @@ const instantiateEllipse = (obj, layers, options) => {
220
213
 
221
214
  // convert to 2D object
222
215
  if (pptz === 0.0 && sptz === 0.0) {
223
- const center = maths.vec2.fromValues(0, 0)
224
- const mjaxis = maths.vec2.fromValues(sptx, spty)
225
- const rx = maths.vec2.distance(center, mjaxis)
216
+ const center = vec2.fromValues(0, 0)
217
+ const mjaxis = vec2.fromValues(sptx, spty)
218
+ const rx = vec2.distance(center, mjaxis)
226
219
  const ry = rx * swid
227
220
  let angle = Math.atan2(spty, sptx) * 180 / Math.PI
228
221
  if (angle < EPS) angle = 0
229
222
  angle = angle * 0.017453292519943295 // radians
230
223
 
231
224
  // FIXME add start and end angle when supported
232
- const cag = primitives.ellipse({ center: [0, 0], radius: [rx, ry], segments: res })
233
- const matrix = maths.mat4.fromZRotation(maths.mat4.create(), angle)
234
- maths.mat4.multiply(matrix, matrix, maths.mat4.fromTranslation(maths.mat4.create(), [pptx, ppty, 0]))
235
- return geometries.geom2.transform(matrix, cag)
225
+ const cag = ellipse({ center: [0, 0], radius: [rx, ry], segments: res })
226
+ const matrix = mat4.fromZRotation(mat4.create(), angle)
227
+ mat4.multiply(matrix, matrix, mat4.fromTranslation(mat4.create(), [pptx, ppty, 0]))
228
+ return geom2.transform(matrix, cag)
236
229
  }
237
230
  // convert to 3D object
238
231
  }
@@ -297,7 +290,7 @@ const instantiateMesh = (obj, layers, options) => {
297
290
  let vi = 0
298
291
  while (vi < face.length) {
299
292
  const pi = face[vi]
300
- const vertex = maths.vec3.clone(points[pi])
293
+ const vertex = vec3.clone(points[pi])
301
294
  vertices.push(vertex)
302
295
  vi++
303
296
  }
@@ -306,7 +299,7 @@ const instantiateMesh = (obj, layers, options) => {
306
299
  }
307
300
  // FIXME how to correct bad normals?
308
301
 
309
- const poly = geometries.poly3.create(vertices)
302
+ const poly = poly3.create(vertices)
310
303
  if (color) poly.color = color
311
304
  polygons.push(poly)
312
305
 
@@ -318,7 +311,7 @@ const instantiateMesh = (obj, layers, options) => {
318
311
  } else {
319
312
  // invalid vlen
320
313
  }
321
- return geometries.geom3.create(polygons)
314
+ return geom3.create(polygons)
322
315
  }
323
316
 
324
317
  // works for both POLYLINE
@@ -336,16 +329,16 @@ const getPolyType = (obj) => {
336
329
  ptype = null // FIXME what to do?
337
330
  } else
338
331
  if ((flags & d3mesh) === d3mesh) {
339
- ptype = geometries.geom3.create()
332
+ ptype = geom3.create()
340
333
  ptype.closedM = ((flags & closedM) === closedM)
341
334
  ptype.closedN = ((flags & closedN) === closedN)
342
335
  } else
343
336
  if ((flags & d3face) === d3face) {
344
- ptype = geometries.geom3.create()
337
+ ptype = geom3.create()
345
338
  ptype.closedM = ((flags & closedM) === closedM)
346
339
  ptype.closedN = ((flags & closedN) === closedN)
347
340
  } else {
348
- ptype = geometries.path2.create()
341
+ ptype = path2.create()
349
342
  ptype.closedM = ((flags & closedM) === closedM)
350
343
  }
351
344
  if ('cnmb' in obj) { ptype.cnmb = obj.cnmb }
@@ -358,23 +351,23 @@ const getPolyType = (obj) => {
358
351
  // - a series of vertex => vectors => 2D geometry
359
352
  //
360
353
  const completeCurrent = (objects, baseobj, polygons, vectors, options) => {
361
- if (geometries.path2.isA(baseobj)) {
354
+ if (path2.isA(baseobj)) {
362
355
  // console.log('##### completing 2D geometry')
363
356
  const points = vectors.map((vector) => vector.vec)
364
357
  // FIXME add color support
365
- objects.push(geometries.path2.fromPoints({ closed: baseobj.closed }, points))
358
+ objects.push(path2.fromPoints({ closed: baseobj.closed }, points))
366
359
  }
367
- if (geometries.geom3.isA(baseobj)) {
360
+ if (geom3.isA(baseobj)) {
368
361
  // console.log('##### completing 3D geometry')
369
362
  // FIXME add color support
370
- objects.push(geometries.geom3.create(polygons))
363
+ objects.push(geom3.create(polygons))
371
364
  }
372
365
  return null
373
366
  }
374
367
 
375
- const instantiateAsciiDxf = (reader, options) => {
368
+ export const instantiateAsciiDxf = (reader, options) => {
376
369
  // console.log('**************************************************')
377
- // console.log(JSON.stringify(reader.objstack));
370
+ // console.log(JSON.stringify(reader.objstack))
378
371
  // console.log('**************************************************')
379
372
 
380
373
  const layers = [] // list of layers with various information like color
@@ -391,7 +384,7 @@ const instantiateAsciiDxf = (reader, options) => {
391
384
  // console.log('##### skip')
392
385
  continue
393
386
  }
394
- // console.log(JSON.stringify(obj));
387
+ // console.log(JSON.stringify(obj))
395
388
 
396
389
  switch (obj.type) {
397
390
  // control objects
@@ -412,7 +405,7 @@ const instantiateAsciiDxf = (reader, options) => {
412
405
  p = instantiatePolygon(obj, layers, options)
413
406
  if (current === null) {
414
407
  // console.log('##### start of 3dfaces')
415
- current = geometries.geom3.create()
408
+ current = geom3.create()
416
409
  }
417
410
  break
418
411
  case 'mesh':
@@ -470,7 +463,7 @@ const instantiateAsciiDxf = (reader, options) => {
470
463
  break
471
464
  }
472
465
  // accumlate polygons if necessary
473
- if (geometries.poly3.isA(p)) {
466
+ if (poly3.isA(p)) {
474
467
  polygons.push(p)
475
468
  }
476
469
  // accumlate vectors if necessary
@@ -488,15 +481,9 @@ const instantiateAsciiDxf = (reader, options) => {
488
481
  // console.log('**************************************************')
489
482
  // objects.forEach(
490
483
  // (e) => {
491
- // console.log(JSON.stringify(e));
484
+ // console.log(JSON.stringify(e))
492
485
  // }
493
- // );
486
+ // )
494
487
  // console.log('**************************************************')
495
488
  return objects
496
489
  }
497
-
498
- module.exports = {
499
- instantiatePolygon,
500
- instantiateVector,
501
- instantiateAsciiDxf
502
- }
@@ -1,16 +1,8 @@
1
- /*
2
- ## License
1
+ import { poly3, vec2, vec3 } from '@jscad/modeling'
3
2
 
4
- Copyright (c) 2017-2019 Z3 Development https://github.com/z3dev
3
+ import { instantiatePolygon, instantiateVector } from './instantiate.js'
5
4
 
6
- All code released under MIT license
7
-
8
- */
9
- const { maths, geometries } = require('@jscad/modeling')
10
-
11
- const { instantiatePolygon, instantiateVector } = require('./instantiate')
12
-
13
- const { findLayer, getColor, getColorNumber } = require('./helpers')
5
+ import { findLayer, getColor, getColorNumber } from './helpers.js'
14
6
 
15
7
  //
16
8
  // translate the give 2D vector to JSCAD script
@@ -32,7 +24,7 @@ const translateVector3D = (vector) => {
32
24
  // translate the given polygon into JSCAD script
33
25
  //
34
26
  const translatePolygon = (polygon) => {
35
- const vertices = geometries.poly3.toPoints(polygon)
27
+ const vertices = poly3.toVertices(polygon)
36
28
  let script = 'createPolygon(['
37
29
  vertices.forEach((vertice) => {
38
30
  script += `[${translateVector3D(vertice)}],`
@@ -64,16 +56,16 @@ const translateLine = (obj, layers, options) => {
64
56
 
65
57
  let script = ''
66
58
  if (!obj.pptz || (obj.pptz === obj.sptz && obj.pptz === 0)) {
67
- const p1 = maths.vec2.fromValues(obj.pptx, obj.ppty)
68
- const p2 = maths.vec2.fromValues(obj.sptx, obj.spty)
69
- script = ` let ${name} = primitives.line([[${translateVector2D(p1)}],[${translateVector2D(p2)}]])\n`
59
+ const p1 = vec2.fromValues(obj.pptx, obj.ppty)
60
+ const p2 = vec2.fromValues(obj.sptx, obj.spty)
61
+ script = ` let ${name} = line([[${translateVector2D(p1)}],[${translateVector2D(p2)}]])\n`
70
62
  } else {
71
- const p1 = maths.vec3.fromValues(obj.pptx, obj.ppty, obj.pptz)
72
- const p2 = maths.vec3.fromValues(obj.sptx, obj.spty, obj.sptz)
73
- script = ` let ${name} = primitives.line([[${translateVector3D(p1)}],[${translateVector3D(p2)}]])\n`
63
+ const p1 = vec3.fromValues(obj.pptx, obj.ppty, obj.pptz)
64
+ const p2 = vec3.fromValues(obj.sptx, obj.spty, obj.sptz)
65
+ script = ` let ${name} = line([[${translateVector3D(p1)}],[${translateVector3D(p2)}]])\n`
74
66
  }
75
67
  if (color) {
76
- script += ` ${name} = colors.colorize([${color[0]}, ${color[1]}, ${color[2]}, 1], ${name})\n`
68
+ script += ` ${name} = colorize([${color[0]}, ${color[1]}, ${color[2]}, 1], ${name})\n`
77
69
  }
78
70
  obj.script = script
79
71
  addToLayer(obj, layers)
@@ -86,21 +78,21 @@ const translateSection = (name, x1, y1, bulg, px, py) => {
86
78
  // console.log('translateSection',x1,y1,bulg,px,py)
87
79
  if (bulg === 0) {
88
80
  // add straight line to the end of the path
89
- return `geometries.path2.appendPoints([[${x1},${y1}]], ${name})
81
+ return `path2.appendPoints([[${x1},${y1}]], ${name})
90
82
  `
91
83
  }
92
84
 
93
85
  // add arc to the end of the path
94
- const prev = maths.vec2.fromValues(px, py)
95
- const curr = maths.vec2.fromValues(x1, y1)
96
- const u = maths.vec2.distance(prev, curr)
86
+ const prev = vec2.fromValues(px, py)
87
+ const curr = vec2.fromValues(x1, y1)
88
+ const u = vec2.distance(prev, curr)
97
89
  const r = u * ((1 + Math.pow(bulg, 2)) / (4 * bulg))
98
90
  const clockwise = (bulg < 0)
99
91
  const large = false // FIXME how to determine?
100
92
  const d = Math.atan(bulg) * 4
101
93
  // FIXME need to determine segments from object/layer/variables
102
94
  const res = 16
103
- return `geometries.path2.appendArc({endpoint: [${x1},${y1}],radius: [${r},${r}],xaxisrotation: ${d},clockwise: ${clockwise},large: ${large},segments: ${res}}, ${name})
95
+ return `path2.appendArc({endpoint: [${x1},${y1}],radius: [${r},${r}],xaxisrotation: ${d},clockwise: ${clockwise},large: ${large},segments: ${res}}, ${name})
104
96
  `
105
97
  }
106
98
 
@@ -122,11 +114,11 @@ const translatePath2D = (obj, layers, options) => {
122
114
  const color = getColor(cn, options.colorindex)
123
115
 
124
116
  // translation
125
- let script = ` let ${name} = geometries.path2.create()\n`
117
+ let script = ` let ${name} = path2.create()\n`
126
118
  const isclosed = ((flags & closed) === closed)
127
119
  if (vlen === pptxs.length && vlen === pptys.length && vlen === bulgs.length) {
128
120
  // add initial point
129
- script += ` ${name} = geometries.path2.appendPoints([[${pptxs[0]}, ${pptys[0]}]], ${name})\n`
121
+ script += ` ${name} = path2.appendPoints([[${pptxs[0]}, ${pptys[0]}]], ${name})\n`
130
122
  // add sections
131
123
  for (let i = 0; i < pptxs.length; i++) {
132
124
  const j = (i + 1) % pptxs.length
@@ -149,12 +141,12 @@ const translatePath2D = (obj, layers, options) => {
149
141
  return
150
142
  }
151
143
  if (isclosed) {
152
- script += ` ${name} = geometries.path2.close(${name})\n`
144
+ script += ` ${name} = path2.close(${name})\n`
153
145
  } else {
154
146
  script += '\n'
155
147
  }
156
148
  if (color) {
157
- script += ` ${name} = colors.colorize([${color[0]}, ${color[1]}, ${color[2]}, 1], ${name})\n`
149
+ script += ` ${name} = colorize([${color[0]}, ${color[1]}, ${color[2]}, 1], ${name})\n`
158
150
  }
159
151
  obj.script = script
160
152
  addToLayer(obj, layers)
@@ -186,18 +178,18 @@ const translateArc = (obj, layers, options) => {
186
178
 
187
179
  // convert to 2D object
188
180
  if (lthk === 0.0) {
189
- let script = ` let ${name} = primitives.arc({center: [${pptx}, ${ppty}], radius: ${swid}, startAngle: ${ang0}, endAngle: ${ang1}, segements: ${res}})\n`
181
+ let script = ` let ${name} = arc({center: [${pptx}, ${ppty}], radius: ${swid}, startAngle: ${ang0}, endAngle: ${ang1}, segements: ${res}})\n`
190
182
  if (color) {
191
- script += ` ${name} = colors.colorize([${color[0]}, ${color[1]}, ${color[2]}, 1], ${name})\n`
183
+ script += ` ${name} = colorize([${color[0]}, ${color[1]}, ${color[2]}, 1], ${name})\n`
192
184
  }
193
185
  obj.script = script
194
186
  addToLayer(obj, layers)
195
187
  return
196
188
  }
197
189
  // FIXME how to represent 3D arc?
198
- let script = ` let ${name} = primitives.arc({center: [${pptx}, ${ppty}], radius: ${swid}, startAngle: ${ang0}, endAngle: ${ang1}, segements: ${res}})\n`
190
+ let script = ` let ${name} = arc({center: [${pptx}, ${ppty}], radius: ${swid}, startAngle: ${ang0}, endAngle: ${ang1}, segements: ${res}})\n`
199
191
  if (color) {
200
- script += ` ${name} = colors.colorize([${color[0]}, ${color[1]}, ${color[2]}, 1], ${name})\n`
192
+ script += ` ${name} = colorize([${color[0]}, ${color[1]}, ${color[2]}, 1], ${name})\n`
201
193
  }
202
194
  obj.script = script
203
195
  addToLayer(obj, layers)
@@ -223,9 +215,9 @@ const translateCircle = (obj, layers, options) => {
223
215
 
224
216
  // convert to 2D object
225
217
  if (lthk === 0.0) {
226
- let script = ` let ${name} = primitives.circle({center: [${pptx}, ${ppty}], radius: ${swid}, segments: ${res}})\n`
218
+ let script = ` let ${name} = circle({center: [${pptx}, ${ppty}], radius: ${swid}, segments: ${res}})\n`
227
219
  if (color) {
228
- script += ` ${name} = colors.colorize([${color[0]}, ${color[1]}, ${color[2]}, 1], ${name})\n`
220
+ script += ` ${name} = colorize([${color[0]}, ${color[1]}, ${color[2]}, 1], ${name})\n`
229
221
  }
230
222
  obj.script = script
231
223
  addToLayer(obj, layers)
@@ -233,9 +225,9 @@ const translateCircle = (obj, layers, options) => {
233
225
  }
234
226
 
235
227
  // convert to 3D object
236
- let script = ` let ${name} = primitives.circle({center: [${pptx}, ${ppty}], radius: ${swid}, segments: ${res}}).extrude({offset: [0,0,${lthk}]}))\n`
228
+ let script = ` let ${name} = circle({center: [${pptx}, ${ppty}], radius: ${swid}, segments: ${res}}).extrude({offset: [0,0,${lthk}]}))\n`
237
229
  if (color) {
238
- script += ` ${name} = colors.colorize([${color[0]}, ${color[1]}, ${color[2]}, 1], ${name})\n`
230
+ script += ` ${name} = colorize([${color[0]}, ${color[1]}, ${color[2]}, 1], ${name})\n`
239
231
  }
240
232
 
241
233
  // FIXME need to use 210/220/230 for direction of rotation
@@ -265,19 +257,19 @@ const translateEllipse = (obj, layers, options) => {
265
257
 
266
258
  // convert to 2D object
267
259
  if (pptz === 0.0 && sptz === 0.0) {
268
- const center = maths.vec2.fromValues(0, 0)
269
- const mjaxis = maths.vec2.fromValues(sptx, spty)
270
- const rx = maths.vec2.distance(center, mjaxis)
260
+ const center = vec2.fromValues(0, 0)
261
+ const mjaxis = vec2.fromValues(sptx, spty)
262
+ const rx = vec2.distance(center, mjaxis)
271
263
  const ry = rx * swid
272
264
  const angle = Math.atan2(spty, sptx) // * 180 / Math.PI
273
265
  // FIXME add start and end angle when supported
274
266
 
275
- let script = ` let ${name} = primitives.ellipse({center: [0, 0, 0], radius: [${rx}, ${ry}], segments: ${res}})
276
- let ${name}matrix = maths.mat4.multiply(maths.mat4.create(), maths.mat4.fromTranslation(maths.mat4.create(), [${pptx}, ${ppty}, 0]), maths.mat4.fromZRotation(maths.mat4.create(), ${angle}))
277
- ${name} = geometries.geom2.transform(${name}matrix, ${name})
267
+ let script = ` let ${name} = ellipse({center: [0, 0, 0], radius: [${rx}, ${ry}], segments: ${res}})
268
+ let ${name}matrix = mat4.multiply(mat4.create(), mat4.fromTranslation(mat4.create(), [${pptx}, ${ppty}, 0]), mat4.fromZRotation(mat4.create(), ${angle}))
269
+ ${name} = geom2.transform(${name}matrix, ${name})
278
270
  `
279
271
  if (color) {
280
- script += ` ${name} = colors.colorize([${color[0]}, ${color[1]}, ${color[2]}, 1], ${name})\n`
272
+ script += ` ${name} = colorize([${color[0]}, ${color[1]}, ${color[2]}, 1], ${name})\n`
281
273
  }
282
274
  obj.script = script
283
275
  addToLayer(obj, layers)
@@ -345,7 +337,7 @@ const translateMesh = (obj, layers, options) => {
345
337
  let vi = 0
346
338
  while (vi < face.length) {
347
339
  const pi = face[vi]
348
- const vector = maths.vec3.clone(points[pi])
340
+ const vector = vec3.clone(points[pi])
349
341
  vertices.push(vector)
350
342
  vi++
351
343
  }
@@ -354,7 +346,7 @@ const translateMesh = (obj, layers, options) => {
354
346
  }
355
347
  // FIXME how to correct bad normals?
356
348
 
357
- const poly = geometries.poly3.create(vertices)
349
+ const poly = poly3.create(vertices)
358
350
  if (color) poly.color = color
359
351
  polygons.push(poly)
360
352
 
@@ -374,7 +366,7 @@ const translateMesh = (obj, layers, options) => {
374
366
  script += ' ' + translatePolygon(polygon) + ',\n'
375
367
  }
376
368
  script += ` ]
377
- let ${name} = geometries.geom3.create(${name}_polygons)
369
+ let ${name} = geom3.create(${name}_polygons)
378
370
  `
379
371
  obj.script = script
380
372
  addToLayer(obj, layers)
@@ -497,8 +489,8 @@ const instantiateFacets = (meshM, meshN, parts, color, options) => {
497
489
  if (options.dxf.angdir === 1) {
498
490
  facet = facet.reverse()
499
491
  }
500
- const polygon = geometries.poly3.create(facet)
501
- const plane = geometries.poly3.plane(polygon)
492
+ const polygon = poly3.create(facet)
493
+ const plane = poly3.plane(polygon)
502
494
  if (Number.isFinite(plane[3])) {
503
495
  if (color) polygon.color = color
504
496
  facets.push(polygon)
@@ -556,7 +548,7 @@ const instantiatePolyFaces = (meshM, meshN, parts, color, options) => {
556
548
  if (options.dxf.angdir === 1) {
557
549
  vertices = vertices.reverse()
558
550
  }
559
- const polygon = geometries.poly3.create(vertices)
551
+ const polygon = poly3.create(vertices)
560
552
  faces.push(polygon)
561
553
  }
562
554
  i++
@@ -640,7 +632,7 @@ const translateCurrent = (obj, layers, parts, options) => {
640
632
  script += ' ' + translatePolygon(polygon) + ',\n'
641
633
  }
642
634
  script += ` ]
643
- let ${name} = geometries.geom3.create(${name}_polygons)
635
+ let ${name} = geom3.create(${name}_polygons)
644
636
  `
645
637
  if (color) {
646
638
  script += ` ${name}.color = [${color}]
@@ -657,7 +649,7 @@ const translateCurrent = (obj, layers, parts, options) => {
657
649
  const translateLayer = (layer) => {
658
650
  const name = layer.lnam || 'Unknown'
659
651
 
660
- let script = `function ${name}() {
652
+ let script = `const ${name} = () => {
661
653
  `
662
654
  for (const object of layer.objects) {
663
655
  script += object.script
@@ -685,7 +677,7 @@ const saveVariable = (obj, options) => {
685
677
  }
686
678
  }
687
679
 
688
- const translateAsciiDxf = (reader, options) => {
680
+ export const translateAsciiDxf = (reader, options) => {
689
681
  // console.log('**************************************************')
690
682
  // console.log(JSON.stringify(reader.objstack))
691
683
  // console.log('**************************************************')
@@ -815,28 +807,23 @@ const translateAsciiDxf = (reader, options) => {
815
807
  break
816
808
  }
817
809
  // accumlate polygons if necessary
818
- if (geometries.poly3.isA(p)) {
819
- // console.log('##### push Polygon')
810
+ if (poly3.isA(p)) {
820
811
  parts.push(p)
821
812
  }
822
813
  // accumlate vectors if necessary
823
814
  if (p && 'vec' in p && p.vec.length === 3) {
824
- // console.log('##### push vec3')
825
815
  parts.push(p)
826
816
  }
827
817
  if (p && 'vec' in p && p.vec.length === 2) {
828
- // console.log('##### push vec2')
829
818
  parts.push(p)
830
819
  }
831
820
  }
832
821
  // translate the last object if necessary
833
822
  current = translateCurrent(current, layers, parts, options)
834
823
 
835
- // debug output
836
- // console.log('**************************************************')
837
- let script = `const {colors, geometries, maths, primitives, transforms} = require('@jscad/modeling')
824
+ let script = `import * from '@jscad/modeling'
838
825
 
839
- const main = () => {
826
+ export const main = () => {
840
827
  let layers = []
841
828
  return layers.concat(`
842
829
 
@@ -849,10 +836,9 @@ const main = () => {
849
836
  // add helper functions for polygons and lines
850
837
  script +=
851
838
  `
852
- function createPolygon(listofpoints, color) {
853
- let polygon = geometries.poly3.fromPoints(listofpoints)
854
- if (color) polygon.color = color
855
- return polygon
839
+ const createPolygon = (listofpoints, color) => {
840
+ let polygon = poly3.create(listofpoints)
841
+ return colorize(color, polygon)
856
842
  }
857
843
  `
858
844
 
@@ -860,11 +846,5 @@ function createPolygon(listofpoints, color) {
860
846
  script += translateLayer(layer)
861
847
  })
862
848
 
863
- script += 'module.exports = {main}\n'
864
-
865
- // console.log(script)
866
- // console.log('**************************************************')
867
849
  return script
868
850
  }
869
-
870
- module.exports = translateAsciiDxf