@@ -21,6 +21,13 @@ export class DxfViewer {
21
21
* @param options Some options can be overridden if specified. See DxfViewer.DefaultOptions.
22
22
*/
23
23
constructor ( domContainer , options = null ) {
24
+ this . dimensions = { dxfWidth : 0 , dxfHeight : 0 } ;
25
+ this . totalCuts = 0 ;
26
+ this . cutsLength = 0 ;
27
+ this . area = 0 ;
28
+ this . shapes = [ ] ;
29
+ this . bends = [ ] ;
30
+
24
31
this . domContainer = domContainer
25
32
this . options = Object . create ( DxfViewer . DefaultOptions )
26
33
if ( options ) {
@@ -119,6 +126,15 @@ export class DxfViewer {
119
126
return this . parsedDxf
120
127
}
121
128
129
+ GetFileDetails ( ) {
130
+ return {
131
+ dimensions : this . dimensions ,
132
+ totalCuts : this . totalCuts ,
133
+ cutsLength : this . cutsLength ,
134
+ area : this . area ,
135
+ }
136
+ }
137
+
122
138
SetSize ( width , height ) {
123
139
this . _EnsureRenderer ( )
124
140
@@ -172,12 +188,17 @@ export class DxfViewer {
172
188
173
189
this . worker = new DxfWorker ( workerFactory ? workerFactory ( ) : null )
174
190
const { scene, dxf} = await this . worker . Load ( url , fonts , this . options , progressCbk )
191
+ this . SetFileDetails ( scene ?. entityVertices ) ;
175
192
await this . worker . Destroy ( )
176
193
this . worker = null
177
194
this . parsedDxf = dxf
178
195
179
196
this . origin = scene . origin
180
197
this . bounds = scene . bounds
198
+ this . dimensions = {
199
+ dxfWidth : scene . bounds . maxY - scene . bounds . minY ,
200
+ dxfHeight : scene . bounds . maxX - scene . bounds . minX
201
+ } ;
181
202
this . hasMissingChars = scene . hasMissingChars
182
203
183
204
for ( const layer of scene . layers ) {
@@ -234,6 +255,167 @@ export class DxfViewer {
234
255
this . _EnsureRenderer ( )
235
256
this . renderer . render ( this . scene , this . camera )
236
257
}
258
+
259
+ checkTangentLayer ( layer ) {
260
+ const bendKeywords = [ 'tangent' , 'bend_extent' ] ;
261
+ return bendKeywords . some ( substr => layer . toLowerCase ( ) . indexOf ( substr ) >= 0 ) ;
262
+ }
263
+
264
+ checkBendLayer ( layer ) {
265
+ const bendKeywords = [ 'bend' , 'reference' ] ;
266
+ return bendKeywords . some ( substr => layer . toLowerCase ( ) . indexOf ( substr ) >= 0 ) ;
267
+ }
268
+
269
+ _TransformVertex ( vs = [ ] ) {
270
+ return vs . map ( v => { return { x : v . x - this . origin . x , y : v . y - this . origin . y } } )
271
+ }
272
+
273
+ getAllLastPoints ( entities ) {
274
+ let points = [ ] ;
275
+ for ( let i = 0 ; i < entities . length ; i ++ ) {
276
+ const entity = entities [ i ] ;
277
+ if ( ! this . checkBendLayer ( entity . layer ) && ! entity . lineType && ! this . checkTangentLayer ( entity . layer ) ) {
278
+ points . push ( {
279
+ startPointx : entity . vertices [ 0 ] . x ,
280
+ startPointy : entity . vertices [ 0 ] . y ,
281
+ endPointx : entity . vertices [ entity . vertices . length - 1 ] . x ,
282
+ endPointy : entity . vertices [ entity . vertices . length - 1 ] . y ,
283
+ visited : false ,
284
+ entity,
285
+ points : entity . vertices ,
286
+ } ) ;
287
+ }
288
+ }
289
+ return points ;
290
+ }
291
+
292
+ SetFileDetails ( entityVertices ) {
293
+ let lastPoints = this . getAllLastPoints ( entityVertices ) ;
294
+ let cuts = [ ] ;
295
+ var maxArea = 0 ;
296
+ let cutLength = 0 ;
297
+
298
+ function round3 ( value ) {
299
+ return Math . round ( value * 1000 ) / 1000 ;
300
+ }
301
+
302
+ function markPointVisited ( { startPointx, startPointy, endPointx, endPointy} ) {
303
+ const index = lastPoints . findIndex ( el => el . startPointx === startPointx && el . startPointy === startPointy && el . endPointx === endPointx && el . endPointy === endPointy ) ;
304
+ if ( index >= 0 ) {
305
+ lastPoints [ index ] . visited = true ;
306
+ }
307
+ }
308
+
309
+ function visitAllPoints ( { startPointx, startPointy, endPointx, endPointy} , entity , points ) {
310
+ markPointVisited ( { startPointx, startPointy, endPointx, endPointy} ) ;
311
+ let allPoints = [ ...points ] ;
312
+ let ended = false ;
313
+ let connectedPoints = [ entity ] ;
314
+ while ( ! ended ) {
315
+ const index = lastPoints . findIndex (
316
+ el =>
317
+ ( round3 ( el . startPointx ) === round3 ( endPointx ) && round3 ( el . startPointy ) === round3 ( endPointy ) && ! el . visited ) ||
318
+ ( round3 ( el . endPointx ) === round3 ( startPointx ) && round3 ( el . endPointy ) === round3 ( startPointy ) && ! el . visited ) ||
319
+ ( round3 ( el . endPointx ) === round3 ( endPointx ) && round3 ( el . endPointy ) === round3 ( endPointy ) && ! el . visited ) ||
320
+ ( round3 ( el . startPointx ) === round3 ( startPointx ) && round3 ( el . startPointy ) === round3 ( startPointy ) && ! el . visited )
321
+ ) ;
322
+ if ( index < 0 ) {
323
+ ended = true ;
324
+ cuts . push ( connectedPoints ) ;
325
+ } else {
326
+
327
+ if ( round3 ( lastPoints [ index ] . startPointx ) === round3 ( endPointx ) && round3 ( lastPoints [ index ] . startPointy ) === round3 ( endPointy ) ) {
328
+ allPoints = [ ...allPoints , ...lastPoints [ index ] . points ] ;
329
+ }
330
+ else if ( round3 ( lastPoints [ index ] . endPointx ) === round3 ( startPointx ) && round3 ( lastPoints [ index ] . endPointy ) === round3 ( startPointy ) ) {
331
+ allPoints = [ ...lastPoints [ index ] . points , ...allPoints ] ;
332
+ }
333
+ else if ( round3 ( lastPoints [ index ] . endPointx ) === round3 ( endPointx ) && round3 ( lastPoints [ index ] . endPointy ) === round3 ( endPointy ) ) {
334
+ allPoints = [ ...allPoints , ...lastPoints [ index ] . points . reverse ( ) ] ;
335
+ }
336
+ else if ( round3 ( lastPoints [ index ] . startPointx ) === round3 ( startPointx ) && round3 ( lastPoints [ index ] . startPointy ) === round3 ( startPointy ) ) {
337
+ allPoints = [ ...lastPoints [ index ] . points . reverse ( ) , ...allPoints ] ;
338
+ }
339
+
340
+ startPointx = allPoints [ 0 ] . x ;
341
+ startPointy = allPoints [ 0 ] . y ;
342
+ endPointx = allPoints [ allPoints . length - 1 ] . x ;
343
+ endPointy = allPoints [ allPoints . length - 1 ] . y ;
344
+ entity = lastPoints [ index ] . entity ;
345
+ lastPoints [ index ] . visited = true ;
346
+ connectedPoints . push ( lastPoints [ index ] . entity ) ;
347
+ }
348
+ }
349
+ return allPoints . map ( el => new three . Vector2 ( el . x , el . y ) ) ;
350
+ }
351
+
352
+ function isPointVisited ( { startPointx, startPointy, endPointx, endPointy} ) {
353
+ const found = lastPoints . find ( el => el . startPointx === startPointx && el . startPointy === startPointy && el . endPointx === endPointx && el . endPointy === endPointy ) ;
354
+ if ( found ) {
355
+ return found . visited ;
356
+ }
357
+ else {
358
+ return true ;
359
+ }
360
+ }
361
+
362
+ for ( let i = 0 ; i < entityVertices . length ; i ++ ) {
363
+ const entity = entityVertices [ i ] ;
364
+ if ( ! this . checkBendLayer ( entity . layer ) && ! entity . lineType && ! this . checkTangentLayer ( entity . layer ) && ! isPointVisited ( {
365
+ startPointx : entity . vertices [ 0 ] . x ,
366
+ startPointy : entity . vertices [ 0 ] . y ,
367
+ endPointx : entity . vertices [ entity . vertices . length - 1 ] . x ,
368
+ endPointy : entity . vertices [ entity . vertices . length - 1 ] . y ,
369
+ } ) ) {
370
+ const points = visitAllPoints ( {
371
+ startPointx : entity . vertices [ 0 ] . x ,
372
+ startPointy : entity . vertices [ 0 ] . y ,
373
+ endPointx : entity . vertices [ entity . vertices . length - 1 ] . x ,
374
+ endPointy : entity . vertices [ entity . vertices . length - 1 ] . y ,
375
+ } , entity , entity . vertices ) ;
376
+ this . shapes . push ( points ) ;
377
+ cutLength += this . getVectorDistance ( points ) ;
378
+ const area = this . calcPolygonArea ( points ) ;
379
+ if ( area > maxArea ) {
380
+ maxArea = area ;
381
+ }
382
+ } else if ( this . checkBendLayer ( entity . layer ) ) {
383
+ this . bends . push ( entity . vertices . map ( el => new three . Vector2 ( el . x , el . y ) ) ) ;
384
+ }
385
+ }
386
+
387
+ console . log ( "TOTAL LENGTH : " + cutLength . toFixed ( 3 ) ) ;
388
+ console . log ( "TOTAL CUTS : " + cuts . length ) ;
389
+ console . log ( "TOTAL AREA : " + maxArea . toFixed ( 3 ) ) ;
390
+
391
+ this . totalCuts = cuts . length ;
392
+ this . cutsLength = cutLength ;
393
+ this . area = maxArea ;
394
+ }
395
+
396
+ getVectorDistance ( vectors ) {
397
+ let sum = 0 ;
398
+ for ( let i = 0 ; i < vectors . length ; i ++ ) {
399
+ sum += vectors [ i ] . distanceTo ( vectors [ i === vectors . length - 1 ? 0 : i + 1 ] ) ;
400
+ }
401
+ return sum ;
402
+ }
403
+
404
+ calcPolygonArea ( vertices ) {
405
+ var total = 0 ;
406
+
407
+ for ( var i = 0 , l = vertices . length ; i < l ; i ++ ) {
408
+ var addX = vertices [ i ] . x ;
409
+ var addY = vertices [ i === vertices . length - 1 ? 0 : i + 1 ] . y ;
410
+ var subX = vertices [ i === vertices . length - 1 ? 0 : i + 1 ] . x ;
411
+ var subY = vertices [ i ] . y ;
412
+
413
+ total += ( addX * addY * 0.5 ) ;
414
+ total -= ( subX * subY * 0.5 ) ;
415
+ }
416
+
417
+ return Math . abs ( total ) ;
418
+ }
237
419
238
420
/** @return {Iterable<{name:String, color:number}> } List of layer names. */
239
421
GetLayers ( ) {
0 commit comments