-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
executable file
·1071 lines (728 loc) · 26.1 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (c) 2018-2023, Ronald Römer
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
*/
import 'core-js/features/array/includes';
import 'core-js/features/typed-array/float64-array';
import 'core-js/features/typed-array/uint16-array';
import 'core-js/features/typed-array/uint32-array';
import 'core-js/features/object/entries';
import 'core-js/features/object/values';
import 'core-js/features/array/flat';
import 'core-js/features/object/assign';
import KDBush from 'kdbush';
const group = 10,
_window = RMainWindowQt.getMainWindow();
try {
let di, doc, filePath;
const engravingLayerName = typeof argEngravingLayerName !== 'undefined' ? argEngravingLayerName : 'Gravur',
offset = typeof argOffset !== 'undefined' ? argOffset : 0.05,
skipInner = typeof argSkipInner !== 'undefined' ? argSkipInner : false;
if (_window !== null) {
di = _window.getDocumentInterface();
doc = di.getDocument();
} else {
doc = new RDocument(new RMemoryStorage(), new RSpatialIndexNavel());
di = new RDocumentInterface(doc);
include('scripts/library.js');
filePath = args[args.length-1];
if (filePath) {
di.importFile(filePath);
// RDocumentInterface.IoErrorNoError
}
}
const model = doc.getModelSpaceBlockId();
function addLayer(name, color) {
const lay = new RLayer(doc, name, false, false, new RColor(color), doc.getLinetypeId('CONTINUOUS'), RLineweight.Weight000, false);
const op = new RAddObjectOperation(lay, false);
op.setTransactionGroup(group);
di.applyOperation(op);
return lay;
}
function setStyle(ent) {
if (ent.getLayerName() !== engravingLayerName) {
ent.setLayerId(newLay.getId());
}
ent.setLinetypeId(0);
ent.setLineweight(RLineweight.WeightByLayer);
ent.setColor(new RColor(RColor.ByLayer));
}
let newLay = doc.queryLayer('New');
if (isNull(newLay)) {
newLay = addLayer('New', 'Cyan');
}
let engLay = doc.queryLayer(engravingLayerName);
if (isNull(engLay)) {
engLay = addLayer(engravingLayerName, 'Red');
} else {
engLay.setLineweight(RLineweight.Weight000);
engLay.setColor(new RColor('Red'));
engLay.setLinetypeId(doc.getLinetypeId('CONTINUOUS'));
const op = new RModifyObjectOperation(engLay);
op.setTransactionGroup(group);
di.applyOperation(op);
}
let offLay = doc.queryLayer('Offset');
if (isNull(offLay)) {
offLay = addLayer('Offset', 'Green');
}
const op = new RDeleteObjectsOperation(),
all = doc.queryAllEntities(false, true);
op.setTransactionGroup(group);
for (const _ent of all) {
const ent = doc.queryEntity(_ent);
if (isDimensionEntity(ent) || isHatchEntity(ent) || isPointEntity(ent) || isXLineEntity(ent) || isRayEntity(ent) || isTextEntity(ent)) {
op.deleteObject(ent);
}
}
di.applyOperation(op);
const refs = doc.queryAllBlockReferences();
const op1 = new RAddObjectsOperation();
op1.setTransactionGroup(group);
const blocks = {},
usedIds = [];
let t = 0;
for (const _ref of refs) {
const ref = doc.queryEntity(_ref),
blockId = ref.getReferencedBlockId(),
block = doc.queryBlock(blockId);
if (usedIds.includes(blockId)) {
const newBlock = new RBlock(doc, 'Cpy' + t++, block.getOrigin());
blocks[_ref] = newBlock;
op1.addObject(newBlock, false);
} else {
blocks[_ref] = block;
usedIds.push(blockId);
}
}
di.applyOperation(op1);
const op2 = new RModifyObjectsOperation();
op2.setTransactionGroup(group);
// verschiebt die block-referenzen auf die 0 und kopiert mehrfach verwendete blöcke, sodass jede referenz auf eine kopie des blocks verweist
for (const _ref of refs) {
const ref = doc.queryEntity(_ref),
blockId = ref.getReferencedBlockId(),
block = doc.queryBlock(blockId);
const pos = ref.getPosition(),
rot = ref.getRotation();
const itms = doc.queryBlockEntities(blockId);
const newId = blocks[_ref].getId();
for (const _itm of itms) {
const itm = doc.queryEntity(_itm),
sh = itm.castToShape();
if (blockId === newId) {
itm.rotate(rot);
itm.move(pos);
setStyle(itm);
op2.addObject(itm, false);
} else {
// kopiert
const newItm = shapeToEntity(doc, sh.clone());
newItm.setBlockId(newId);
newItm.copyAttributesFrom(itm.data(), false);
newItm.rotate(rot);
newItm.move(pos);
setStyle(newItm);
op2.addObject(newItm, false);
}
}
if (blockId !== newId) {
ref.setReferencedBlockId(newId);
}
ref.setPosition(new RVector(0, 0));
ref.setRotation(0);
ref.setLayerId(doc.getLayer0Id());
op2.addObject(ref, false);
}
di.applyOperation(op2);
// vereinheitlicht die attribute
const op3 = new RModifyObjectsOperation();
op3.setTransactionGroup(group);
const all2 = doc.queryAllEntities(false, false, [RS.EntityArc, RS.EntityLine, RS.EntityCircle, RS.EntityPolyline, RS.EntityEllipse]);
for (const _ent of all2) {
const ent = doc.queryEntity(_ent);
setStyle(ent);
op3.addObject(ent, false);
}
di.applyOperation(op3);
// wandelt die circles in arcs um und löst die polylines auf
const op4 = new RAddObjectsOperation();
op4.setTransactionGroup(group);
const other = doc.queryAllEntities(false, true, [RS.EntityCircle, RS.EntityPolyline, RS.EntityEllipse]);
for (const _ent of other) {
const ent = doc.queryEntity(_ent),
sh = ent.castToShape();
if (isCircleEntity(ent)) {
const rad = sh.getRadius(),
c = sh.getCenter();
const arcA = new RArc(c, rad, 0, Math.PI),
arcB = new RArc(c, rad, Math.PI, 2*Math.PI);
const entA = shapeToEntity(doc, arcA),
entB = shapeToEntity(doc, arcB);
entA.copyAttributesFrom(ent.data());
entB.copyAttributesFrom(ent.data());
op4.addObject(entA, false);
op4.addObject(entB, false);
op4.deleteObject(ent);
} else {
const expls = [];
if (isEllipseEntity(ent)) {
const splines = sh.approximateWithSplines();
for (const spline of splines) {
expls.push(...spline.approximateWithArcs(.5).getExploded());
}
} else {
expls.push(...sh.getExploded());
}
for (const expl of expls) {
if (isLineShape(expl)) {
const {startPoint, endPoint} = expl;
if (startPoint.equalsFuzzy2D(endPoint, 1e-5)) {
continue;
}
}
const newEnt = shapeToEntity(doc, expl.clone());
newEnt.copyAttributesFrom(ent.data());
op4.addObject(newEnt, false);
}
op4.deleteObject(ent);
}
}
di.applyOperation(op4);
// löscht leere oder nicht verwendete blöcke
const op5 = new RDeleteObjectsOperation();
op5.setTransactionGroup(group);
const usedIds2 = [];
for (const _ref of refs) {
const ref = doc.queryEntity(_ref);
usedIds2.push(ref.getReferencedBlockId());
}
const blocks2 = doc.queryAllBlocks();
for (const block of blocks2) {
if ((doc.queryBlockEntities(block).length === 0 || !usedIds2.includes(block))
&& block !== doc.getModelSpaceBlockId()) {
op5.deleteObject(doc.queryBlock(block));
}
}
di.applyOperation(op5);
// löscht leere layer
const op6 = new RDeleteObjectsOperation();
op6.setTransactionGroup(group);
const lays = doc.queryAllLayers();
for (const layId of lays) {
if (doc.queryLayerEntities(layId, true).length === 0
&& layId !== doc.getLayer0Id()
&& ![newLay, engLay, offLay].map(lay => lay.getId()).includes(layId)) {
op6.deleteObject(doc.queryLayer(layId));
}
}
di.applyOperation(op6);
// duplikate löschen
const entities = doc.queryAllEntities(false, true);
const pts = [];
for (const entId of entities) {
const ent = doc.queryEntity(entId);
if (isArcEntity(ent) || isLineEntity(ent)) {
if (ent.getLength() > 1e-5) {
const ptA = ent.getStartPoint(),
ptB = ent.getEndPoint();
const layId = ent.getLayerId();
pts.push({layId, entId, startPt: ptA, endPt: ptB, end: 0});
pts.push({layId, entId, startPt: ptB, endPt: ptA, end: 1});
}
}
}
const tree = new KDBush(pts, p => p.startPt.x, p => p.startPt.y);
const dupls = {};
for (const p of pts) {
if (typeof dupls[p.entId] === 'undefined') {
const {x, y} = p.startPt;
const nearest = tree.within(x, y, 1e-5);
for (const id of nearest) {
const near = pts[id];
if (near.layId === p.layId
&& near.entId !== p.entId
&& typeof dupls[near.entId] === 'undefined'
&& near.endPt.equalsFuzzy2D(p.endPt, 1e-5)) {
const shA = doc.queryEntity(near.entId).castToShape(),
shB = doc.queryEntity(p.entId).castToShape();
if (isArcShape(shA) && isArcShape(shB)) {
if (shA.equals(shB, 1e-5)) {
dupls[near.entId] = null;
}
} else {
dupls[near.entId] = null;
}
}
}
}
}
const op7 = new RDeleteObjectsOperation();
op7.setTransactionGroup(group);
for (const dupl of Object.keys(dupls)) {
op7.deleteObject(doc.queryEntity(parseInt(dupl)));
}
di.applyOperation(op7);
// legt ungenaue endpunkte zusammen
const pts2 = pts.filter(p => typeof dupls[p.entId] === 'undefined' && p.layId !== engLay.getId()),
tree2 = new KDBush(pts2, p => p.startPt.x, p => p.startPt.y);
const skips = {},
shapes = {};
for (const p of pts2) {
if (typeof skips[p.entId] === 'undefined') {
const {x, y} = p.startPt;
const nearest = tree2.within(x, y, 1e-5);
const found = nearest.filter(id => pts2[id].entId !== p.entId);
if (found.length > 1) {
di.selectEntities(nearest.map(id => pts2[id].entId));
throw new Error('Ambiguous connection found.');
} else if (found.length === 1) {
const q = pts2[found[0]],
ent = doc.queryEntity(q.entId),
sh = ent.castToShape();
if (typeof shapes[q.entId] === 'undefined') {
shapes[q.entId] = sh.clone();
}
const _sh = shapes[q.entId];
if (isLineShape(_sh)) {
if (q.end === 0) {
_sh.setStartPoint(new RVector(x, y));
} else {
_sh.setEndPoint(new RVector(x, y));
}
} else {
// Arc
let phi = _sh.getAngleLength()/2;
const pt = q.end === 1 ? _sh.getStartPoint() : _sh.getEndPoint();
const v = new RVector(pt.x-x, pt.y-y),
l = v.getMagnitude()/2;
v.normalize();
const d = l/Math.tan(phi);
const f = (q.end === 0 ^ _sh.isReversed()) ? 1 : -1;
const c = new RVector(x+l*v.x-f*d*v.y, y+l*v.y+f*d*v.x);
const vA = new RVector(x-c.x, y-c.y),
vB = new RVector(pt.x-c.x, pt.y-c.y);
const r = vA.getMagnitude();
vA.normalize();
vB.normalize();
let angA = Math.atan2(vA.y, vA.x);
if (angA < 0) {
angA += 2*Math.PI;
}
let angB = Math.atan2(vB.y, vB.x);
if (angB < 0) {
angB += 2*Math.PI;
}
_sh.setCenter(c);
_sh.setRadius(r);
if (q.end === 0) {
_sh.setStartAngle(angA);
_sh.setEndAngle(angB);
} else {
_sh.setEndAngle(angA);
_sh.setStartAngle(angB);
}
}
skips[q.entId] = null;
}
}
}
const op8 = new RModifyObjectsOperation();
op8.setTransactionGroup(group);
for (const id of Object.keys(shapes)) {
const ent = doc.queryEntity(parseInt(id));
ent.setShape(shapes[id]);
op8.addObject(ent, false);
}
di.applyOperation(op8);
// lines zusammensetzen
function search(shapes, side, layId) {
if (side === 'right') {
const last = shapes[shapes.length-1],
pt = last.shape.getEndPoint(),
nearest = tree2.within(pt.x, pt.y, 1e-5);
for (const id of nearest) {
const near = pts2[id];
if (near.entId !== last.entId
&& near.entId !== shapes[0].entId
&& layId === near.layId) {
const ent = doc.queryEntity(near.entId),
sh = ent.castToShape().clone();
if (near.end === 1) {
sh.reverse();
}
shapes.push({entId: near.entId, shape: sh});
return true;
}
}
} else {
const first = shapes[0],
pt = first.shape.getStartPoint(),
nearest = tree2.within(pt.x, pt.y, 1e-5);
for (const id of nearest) {
const near = pts2[id];
if (near.entId !== first.entId
&& near.entId !== shapes[shapes.length-1].entId
&& layId === near.layId) {
const ent = doc.queryEntity(near.entId),
sh = ent.castToShape().clone();
if (near.end === 0) {
sh.reverse();
}
shapes.unshift({entId: near.entId, shape: sh});
return true;
}
}
}
return false;
}
const entities2 = pts2.map(p => p.entId);
const visited = {};
const op9 = new RAddObjectsOperation();
op9.setTransactionGroup(group);
for (const entId of entities2) {
if (typeof visited[entId] === 'undefined') {
const ent = doc.queryEntity(entId);
const shapes = [{entId, shape: ent.castToShape().clone()}];
while (search(shapes, 'right', ent.getLayerId())) {}
while (search(shapes, 'left', ent.getLayerId())) {}
shapes.forEach(sh => visited[sh.entId] = null);
const newPl = new RPolyline(shapes.map(sh => sh.shape));
const pl = shapeToEntity(doc, newPl);
pl.copyAttributesFrom(ent.data());
op9.addObject(pl, false);
}
}
for (const id of Object.keys(visited)) {
op9.deleteObject(doc.queryEntity(parseInt(id)));
}
di.applyOperation(op9);
// löscht zu kurze
const op10 = new RDeleteObjectsOperation();
op10.setTransactionGroup(group);
doc.queryAllEntities(false, true, [RS.EntityPolyline, RS.EntityLine, RS.EntityArc]).forEach(id => {
const ent = doc.queryEntity(id);
if (ent.getLength() < .01) {
op10.deleteObject(ent);
}
});
di.applyOperation(op10);
// löscht interne punkte
const op11 = new RMixedOperation();
op11.setTransactionGroup(group);
doc.queryAllEntities(false, true, [RS.EntityPolyline]).forEach(id => {
// qDebug('id', id);
const ent = doc.queryEntity(id);
const expl = ent.getExploded();
const grps = [[]];
expl.forEach(sh => {
const last = grps[grps.length-1];
if (last.length) {
if (last[0].getShapeType() === sh.getShapeType()) {
last.push(sh);
} else {
grps.push([sh]);
}
} else {
last.push(sh);
}
});
if (grps.length > 2) {
if (isLineShape(expl[0]) && isLineShape(expl[expl.length-1]) && ent.isClosed()) {
grps[0].unshift(...grps.pop());
}
}
for (const grp of grps) {
if (isLineShape(grp[0]) && grp.length > 1) {
let pts = grp.map(line => line.getStartPoint());
pts.push(grp[grp.length-1].getEndPoint());
// qDebug(pts);
if (grps.length === 1 && ent.isClosed()) {
const vecs = pts.slice(1).map((k, i) => k.operator_subtract(pts[i]).normalize());
vecs.unshift(vecs[vecs.length-1]);
const angs = vecs.slice(1).map((k, i) => Math.acos(k.dot(vecs[i])));
// qDebug(angs.join(', '));
const ind = angs.indexOf(Math.max(...angs));
// qDebug(ind);
pts.pop();
if (ind > 0) {
pts = pts.slice(ind).concat(pts.slice(0, ind));
}
// qDebug(pts);
for (let i = pts.length-2; i > 0; i--) {
const j = i+1;
const v = pts[i].operator_subtract(pts[0]).normalize(),
n = new RVector(-v.y, v.x),
d = pts[0].dot(n);
if (Math.abs(pts[j].dot(n)-d) < 1e-5) {
pts.pop();
} else {
break;
}
}
// qDebug(pts);
}
const badInds = [];
const pairs = [[0, pts.length-1]];
while (pairs.length) {
const [i, j] = pairs.shift();
const pA = pts[i],
pB = pts[j];
const v = pB.operator_subtract(pA).normalize();
const n = new RVector(-v.y, v.x);
const d = pA.dot(n);
let maxDist = 0,
maxIndex = 0;
for (let k = i+1; k < j; k++) {
const dist = Math.abs(pts[k].dot(n)-d);
if (dist > maxDist) {
maxDist = dist;
maxIndex = k;
}
}
if (maxDist > 1e-5) {
pairs.push([i, maxIndex]);
pairs.push([maxIndex, j]);
} else {
for (let k = i+1; k < j; k++) {
badInds.push(k);
}
}
}
badInds.sort((a, b) => b-a);
// qDebug(badInds);
const newPts = pts.slice();
for (const ind of badInds) {
newPts.splice(ind, 1);
}
if (grps.length === 1 && ent.isClosed()) {
newPts.push(newPts[0]);
}
// qDebug('->', newPts);
grp.length = 0;
for (let i = 0; i < newPts.length-1; i++) {
grp.push(new RLine(newPts[i], newPts[i+1]));
}
}
}
const newSh = new RPolyline(grps.flat()),
newEnt = shapeToEntity(doc, newSh);
newEnt.copyAttributesFrom(ent.data());
op11.addObject(newEnt, false);
op11.deleteObject(ent);
});
di.applyOperation(op11);
// gruppieren
const rects = [];
doc.queryAllEntities(false, false, RS.EntityPolyline).forEach(id => {
const ent = doc.queryEntity(id);
if (ent.isClosed() && ent.getLayerName() !== engravingLayerName) {
rects.push(ent);
}
});
const data = {};
rects.forEach(rect => {
const rectId = rect.getId(),
bb = rect.getBoundingBox().growXY(1e-5);
const ids = doc.queryContainedEntitiesXY(bb);
let rectSh = rect.castToShape();
// workaround
if (rectSh.hasArcSegments()) {
rectSh = rectSh.convertArcToLineSegmentsLength(.5);
}
ids.forEach(id => {
if (id !== rectId) {
const ent = doc.queryEntityDirect(id),
sh = ent.castToShape();
if ((isPolylineShape(sh) && rectSh.containsShape(sh))
|| ((isLineShape(sh) || isArcShape(sh)) && sh.getEndPoints().every(pt => rectSh.contains(pt, true)))) {
if (typeof data[rectId] === 'undefined') {
data[rectId] = [];
}
data[rectId].push(ent.getId());
}
}
});
});
const parentIds = Object.keys(data).map(id => parseInt(id)),
childIds = Object.values(data).flat();
const invalidIds = parentIds.filter(id => childIds.includes(id));
if (invalidIds.length) {
di.selectEntities(invalidIds);
throw new Error('Nesting detected.');
}
const allIds = parentIds.concat(childIds);
rects.forEach(rect => {
const rectId = rect.getId();
if (!allIds.includes(rectId)) {
data[rectId] = [];
}
});
// qDebug(JSON.stringify(data));
let i = 0;
for (const [parent, childs] of Object.entries(data)) {
const parentId = parseInt(parent);
const parentEnt = doc.queryEntityDirect(parentId);
const position = parentEnt.getBoundingBox().getCorner1();
const op = new RAddObjectsOperation(),
block = new RBlock(doc, `Block${i++}`, new RVector(0, 0));
op.setTransactionGroup(group);
op.addObject(block, false);
di.applyOperation(op);
const blockId = block.getId();
const op2 = new RModifyObjectsOperation();
op2.setTransactionGroup(group);
childs.unshift(parentId);
for (const childId of childs) {
const child = doc.queryEntity(childId);
child.move(position.getNegated());
child.setBlockId(blockId);
op2.addObject(child, false);
}
di.applyOperation(op2);
const op3 = new RAddObjectsOperation();
op3.setTransactionGroup(group);
const ref = new RBlockReferenceEntity(doc, new RBlockReferenceData(blockId, position, new RVector(1, 1), 0));
op3.addObject(ref, false);
di.applyOperation(op3);
}
// offset
const outerEnts = []; // wird nicht benötigt
const refs2 = doc.queryAllBlockReferences();
const op12 = new RAddObjectsOperation();
op12.setTransactionGroup(group);
for (const refId of refs2) {
const ref = doc.queryEntity(refId);
// qDebug(ref.getReferencedBlockName());
const filtered = doc.queryBlockEntities(ref.getReferencedBlockId())
.map(id => doc.queryEntity(id))
.filter(ent => isPolylineEntity(ent) && ent.isClosed() && ent.getLayerName() !== engravingLayerName);
const innerIds = [];
const _op = new RModifyObjectsOperation();
_op.setTransactionGroup(group);
for (const ent of filtered) {
const sh = ent.castToShape();
let isInner = false;
if (allIds.includes(ent.getId())) {
isInner = childIds.includes(ent.getId());
} else {
for (const _ent of filtered) {
if (ent !== _ent) {
const _sh = _ent.castToShape();
if (_sh.containsShape(sh)) {
isInner = true;
break;
}
}
}
}
if (isInner) {
innerIds.push(ent.getId());
}
if (isInner ^ sh.getOrientation() === RS.CW) {
// richtung umkehren
ent.reverse();
_op.addObject(ent, false);
}
}
di.applyOperation(_op);
// * mit workaround
for (const ent of filtered) {
const expl = ent.getExploded();
for (let i = 0; i < expl.length; i++) {
const newPl = new RPolyline(expl); // *
newPl.convertToClosed();
if (!(skipInner && innerIds.includes(ent.getId())) && offset > 0) {
const worker = new RPolygonOffset(offset, 1, RVector.invalid, RS.JoinMiter, false);
worker.setForceSide(RS.RightHand);
worker.addPolyline(newPl);
const offs = worker.getOffsetShapes();
if (offs.length) {
for (const _off of offs) {
const off = shapeToEntity(doc, _off.data());
off.copyAttributesFrom(ent.data());
off.setLayerId(offLay.getId());
if (_off.getOrientation() === RS.CCW) {
outerEnts.push(off);
}
op12.addObject(off, false);
}
break;
} else {
expl.push(expl.shift()); // *
}
} else {
const off = shapeToEntity(doc, newPl);
off.copyAttributesFrom(ent.data());
off.setLayerId(offLay.getId());
if (newPl.getOrientation() === RS.CCW) {
outerEnts.push(off);
}
op12.addObject(off, false);
break;
}
}
}
}
di.applyOperation(op12);
// outerEnts.forEach(ent => {
// qDebug(ent.getId());
// });
// verschiebt auf die 0
const op13 = new RModifyObjectsOperation();
op13.setTransactionGroup(group);
doc.queryLayerEntities(newLay.getId()).forEach(id => {
const ent = doc.queryEntityDirect(id);
ent.setLayerId(doc.getLayer0Id());
op13.addObject(ent, false);
});
di.applyOperation(op13);
// löst die blöcke auf