-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakeSimpleClamp.cpp
More file actions
1816 lines (1661 loc) · 53.9 KB
/
MakeSimpleClamp.cpp
File metadata and controls
1816 lines (1661 loc) · 53.9 KB
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
#pragma once
#include "MakeSimpleClamp.h"
#include <algorithm>
#include <array>
#include <Bnd_Box.hxx>
#include <BRep_Builder.hxx>//test
#include <BRepAdaptor_Curve.hxx>
#include <BRepAlgoAPI_Cut.hxx>
#include <BRepAlgoAPI_Section.hxx>
#include <BRepBndLib.hxx>
#include <BRepTools_WireExplorer.hxx>
#include <BRepBuilderAPI_MakeEdge.hxx>
#include <BRepFilletAPI_MakeFillet2d.hxx>
#include <BRepBuilderAPI_MakeFace.hxx>
#include <BRepBuilderAPI_MakeWire.hxx>
#include <BRepBuilderAPI_Transform.hxx>
#include <BRepLib.hxx>
#include <cmath>
#include <GeomAPI_ProjectPointOnCurve.hxx>
#include <gp_EulerSequence.hxx>
#include <gp_Quaternion.hxx>
#include <map>
#include <TCollection_AsciiString.hxx>
#include <TopExp_Explorer.hxx>
#include <TopLoc_Location.hxx>
#include <TopoDS.hxx>
#include <TopoDS_Compound.hxx>
#include <utility>
#include <vector>
#include <BRepPrimAPI_MakePrism.hxx>
#include <BRepPrimAPI_MakeCylinder.hxx>
#include <BRepBuilderAPI_Sewing.hxx>
#include <BRepBuilderAPI_MakeVertex.hxx>
#include <ShapeFix_Shape.hxx>
#include <BRepFeat_SplitShape.hxx>
#include <BRepAlgoAPI_Splitter.hxx>
static const double LINEAR_TOLERANCE = 0.2;
namespace OCCTK {
namespace SimpleClamp {
#pragma region 静态方法函数
// 计算2维两直线交点
//static bool Get2DLineIntersection(double x1, double y1, double x2, double y2,
// double x3, double y3, double x4, double y4, double& outX, double& outY) {
// // 计算两条线段的方向向量
// double d1x = x2 - x1;
// double d1y = y2 - y1;
// double d2x = x4 - x3;
// double d2y = y4 - y3;
//
// Handle(Geom2d_Line) hLine1 = new Geom2d_Line(gp_Pnt2d(x1, y1), gp_Dir2d(x2, y2));
// Handle(Geom2d_Line) hLine2 = new Geom2d_Line(gp_Pnt2d(x3, y3), gp_Dir2d(x4, y4));
//
// Geom2dAPI_InterCurveCurve ICC(hLine1, hLine2, 1e-2);
// if (ICC.NbPoints() > 0) {
// gp_Pnt2d P = ICC.Point(1);
// outX = P.X();
// outY = P.Y();
// return true;
// }
// return false;
//}
static bool Get2DLineIntersection(double x1, double y1, double x2, double y2,
double x3, double y3, double x4, double y4, double& outX, double& outY) {
// 计算两条线段的方向向量
double d1x = x2 - x1;
double d1y = y2 - y1;
double d2x = x4 - x3;
double d2y = y4 - y3;
// 计算叉积
double cross = d1x * d2y - d1y * d2x;
if (std::abs(cross) > 1e-6) {
// 计算向量 (x3 - x1, y3 - y1) 和 d2 的点积
double v1x = x3 - x1;
double v1y = y3 - y1;
double t = (v1x * d2y - v1y * d2x) / cross;
// 计算交点
outX = x1 + t * d1x;
outY = y1 + t * d1y;
return true;
}
// 如果两直线为 X 和 Y 方向,此时叉积也为 0
double tx1 = 0.0;
double ty1 = 0.0;
double tx2 = 0.0;
double ty2 = 0.0;
if (std::abs(d1x) < 1e-6) {
outX = (x1 * y2 - x2 * y1) / (y2 - y1);
if (std::abs(d2y) < 1e-6) {
outY = (x3 * y4 - x4 * y3) / (x3 - x4);
return true;
}
}
if (std::abs(d1y) < 1e-6) {
outY = (x1 * y2 - x2 * y1) / (x1 - x2);
if (std::abs(d2x) < 1e-6) {
outX = (x3 * y4 - x4 * y3) / (y4 - y3);
return true;
}
}
return false;
}
// 判断是否可以继续向线段中添加线
static bool AddEdge(Ring& theWire, std::vector<myEdge>& Edges, gp_Pnt& start, gp_Pnt& end, bool originDir) {
for (size_t i = 0; i < Edges.size(); ++i) {
myEdge oneEdge = Edges[i];
if (end.Distance(oneEdge.start) < LINEAR_TOLERANCE) {
if (oneEdge.dir == originDir) {
end = oneEdge.end;
theWire.push_back(oneEdge);
Edges.erase(Edges.begin() + i);
return true;
}
}
if (start.Distance(oneEdge.end) < LINEAR_TOLERANCE) {
if (oneEdge.dir == originDir) {
start = oneEdge.start;
theWire.insert(theWire.begin(), oneEdge);
Edges.erase(Edges.begin() + i);
return true;
}
}
}
return false;
}
//对Pieces排序
static std::vector<VerticalPiece> SortingPieces(const PlatePose& thePose, std::vector<VerticalPiece>& thePieces) {
// 设置start为左边点
gp_Trsf tempswapT;
tempswapT.SetRotation(gp_Ax1(thePose.point, gp_Dir(0, 0, 1)), thePose.dir.AngleWithRef(gp_Vec(0.0, 1.0, 0.0), gp_Vec(0.0, 0.0, 1.0)));
for (size_t i = 0; i < thePieces.size(); i++) {
auto thePiece = &thePieces[i];
gp_Pnt tempStart = thePiece->myEdge.start.Transformed(tempswapT);
gp_Pnt tempEnd = thePiece->myEdge.end.Transformed(tempswapT);
//对Edge的两端点排序
if (tempStart.X() > tempEnd.X()) {
std::swap(thePiece->myEdge.start, thePiece->myEdge.end);
std::swap(tempStart, tempEnd);
}
thePiece->order = tempStart.X();
}
//// 对Edges重新排序(start更小)
//std::sort(orderedPieces.begin(), orderedPieces.end(),
// [&](const myEdge& edge1, const myEdge& edge2) {
// return edge1.start.Transformed(tempswapT).X() < edge2.start.Transformed(tempswapT).X();
// });
// 对Edges重新排序(start更小)
std::sort(thePieces.begin(), thePieces.end(),
[&](const VerticalPiece& piece1, const VerticalPiece& piece2) {
return piece1.order < piece2.order;
});
return thePieces;
}
// 找到并返回一个环
static Ring FindARing(std::vector<myEdge>& Edges) {
Ring theRing;
theRing.push_back(Edges.front());
gp_Pnt start = Edges.front().start;
gp_Pnt end = Edges.front().end;
Edges.erase(Edges.begin());
//! debug
std::vector<double> debug_dis;
// 找到匹配的边,并删除该边
while (!Edges.empty()) {
bool endFlag = true;
for (size_t i = 0; i < Edges.size(); ++i) {
myEdge oneEdge = Edges[i];
if (end.Distance(oneEdge.start) < LINEAR_TOLERANCE) {
end = oneEdge.end;
theRing.push_back(oneEdge);
Edges.erase(Edges.begin() + i);
endFlag = false;
break;
}
if (end.Distance(oneEdge.end) < LINEAR_TOLERANCE) {
end = oneEdge.start;
theRing.push_back(oneEdge);
Edges.erase(Edges.begin() + i);
endFlag = false;
break;
}
if (start.Distance(oneEdge.end) < LINEAR_TOLERANCE) {
start = oneEdge.start;
theRing.insert(theRing.begin(), oneEdge);
Edges.erase(Edges.begin() + i);
endFlag = false;
break;
}
if (start.Distance(oneEdge.start) < LINEAR_TOLERANCE) {
start = oneEdge.end;
theRing.insert(theRing.begin(), oneEdge);
Edges.erase(Edges.begin() + i);
endFlag = false;
break;
}
}
if (endFlag) { break; }
}
////! debug
//std::vector<std::pair<gp_Pnt2d, gp_Pnt2d>>debug1;
//for (auto a : theRing) {
// debug1.push_back({ a.start2D(),a.end2D() });
//}
//std::vector<std::pair<gp_Pnt2d, gp_Pnt2d>>debug2;
//for (auto a : Edges) {
// debug2.push_back({ a.start2D(),a.end2D() });
//}
return theRing;
}
// 从截面中划分出环
static std::vector<Ring> GetRings(std::vector<myEdge>& edges) {
//std::vector<Ring>Rings;
//Ring tempEdges = Edges;
//while (!tempEdges.empty()) {
// Rings.push_back(FindARing(tempEdges));
//}
//return Rings;
std::vector<std::vector<myEdge>> rings;
bool continue_flag = true;
gp_Pnt start, end; // 将start和end的声明移到这里
while (!edges.empty()) {
if (continue_flag) {
myEdge an_edge = edges.front();
edges.erase(edges.begin());
start = an_edge.start; // 这里可以访问start和end
end = an_edge.end;
std::vector<myEdge> a_ring = { an_edge };
rings.push_back(a_ring);
continue_flag = false;
}
bool end_for = true;
for (auto it = edges.begin(); it != edges.end(); ++it) {
if (start.Distance(it->start) < LINEAR_TOLERANCE) {
rings.back().push_back(*it);
end = it->end;
edges.erase(it);
end_for = false;
break;
}
else if (start.Distance(it->end) < LINEAR_TOLERANCE) {
rings.back().push_back(*it);
start = it->start;
edges.erase(it);
end_for = false;
break;
}
else if (end.Distance(it->end) < LINEAR_TOLERANCE) {
rings.back().push_back(*it);
end = it->start;
edges.erase(it);
end_for = false;
break;
}
else if (end.Distance(it->start) < LINEAR_TOLERANCE) {
rings.back().push_back(*it);
end = it->end;
edges.erase(it);
end_for = false;
break;
}
}
if (end_for) {
continue_flag = true;
}
}
return rings;
}
// 把一个环划分为上下两边,取下端的边
static std::vector<myEdge> SplitRing(Ring theRing) {
// 错误处理,理论上 Ring 不应该少于4条边
if (theRing.size() < 4) {
return theRing;
}
std::vector<myEdge> originRing = theRing;
//对线排序
std::vector<myEdge> orderedEdges;
TopTools_ListOfShape edgeList;
BRepBuilderAPI_MakeWire wireMaker = BRepBuilderAPI_MakeWire();
for (size_t i = 0; i < originRing.size(); i++) { edgeList.Append(originRing[i].edge); }
gp_Vec2d baseVec2d;
for (size_t i = 0; i < originRing.size(); i++) {
baseVec2d = gp_Vec2d(originRing[i].start2D(), originRing[i].end2D());
if (baseVec2d.Magnitude() != 0.0) {
break;
}
if (i == originRing.size() - 1) {
return originRing;//没有错误处理
}
}
////! debug
//std::vector<gp_Vec2d> debug_vec;
//std::vector<double> debug_vec_mag;
//std::vector <std::pair< double, double >> debug_vec_start;
//std::vector <std::pair< gp_Pnt2d, gp_Pnt2d >> debug_vec_start2;
wireMaker.Add(edgeList);
if (!wireMaker.IsDone()) {
return theRing;
}
BRepTools_WireExplorer wireExplorer(wireMaker);
while (wireExplorer.More()) {
myEdge anEdge(wireExplorer.Current());
// 对Edge两端点重新排序
gp_Pnt theStart = BRep_Tool::Pnt(wireExplorer.CurrentVertex());
if (anEdge.start.Distance(theStart) > anEdge.end.Distance(theStart)) { std::swap(anEdge.start, anEdge.end); }
//debug_vec_start2.push_back({ anEdge.start2D() , anEdge.end2D() });
// 获取线的相对方向
gp_Vec2d edgeVec2d = gp_Vec2d(anEdge.start2D(), anEdge.end2D());
if (edgeVec2d.Magnitude() != 0.0) {
if (edgeVec2d.IsOpposite(baseVec2d, 1e-2)) { anEdge.dir = false; }
else { anEdge.dir = true; }
}
orderedEdges.push_back(anEdge);
wireExplorer.Next();
//debug_vec.push_back(edgeVec2d);
//debug_vec_mag.push_back(edgeVec2d.Magnitude());
}
int debug_trueCount = 0;
int debug_falseCount = 0;
for (auto d : orderedEdges) {
if (d.dir) {
debug_trueCount += 1;
}
else {
debug_falseCount += 1;
}
}
// 构建第一条线
std::vector<myEdge> firstWire, secondWire;
gp_Pnt startP = orderedEdges.front().start;
gp_Pnt endP = orderedEdges.front().end;
bool originDir = orderedEdges.front().dir;
firstWire.push_back(orderedEdges.front());
orderedEdges.erase(orderedEdges.begin());
while (AddEdge(firstWire, orderedEdges, startP, endP, originDir)) {}
// 直接取剩余线作为第二条
secondWire = orderedEdges;
// 去除垂直的边
gp_Vec Z(0.0, 0.0, 1.0);
Ring Temp1, Temp2;
for (myEdge theEdge : firstWire) {
if (!gp_Vec(theEdge.start, theEdge.end).IsParallel(Z, 0.1)) {
Temp1.push_back(theEdge);
}
}
firstWire = Temp1;
for (myEdge theEdge : secondWire) {
if (!gp_Vec(theEdge.start, theEdge.end).IsParallel(Z, 0.1)) {
Temp2.push_back(theEdge);
}
}
secondWire = Temp2;
if ((!firstWire.empty()) && (!secondWire.empty())) {
//! 使用两端的 Z 值比较比较高度(可能不准确,由于斜着的边缘)
for (size_t i = 0; i < firstWire.size(); ++i) {
const myEdge& firstEdge = firstWire[i];
bool isSame = false;
for (size_t j = 0; j < secondWire.size(); ++j) {
const myEdge& secondEdge = secondWire[j];
gp_Pnt2d a(firstEdge.middle.X(), firstEdge.middle.Y());
gp_Pnt2d b(secondEdge.middle.X(), secondEdge.middle.Y());
if (a.Distance(b) < 3.0) {
isSame = true;
}
if (isSame) {
if (firstEdge.middle.Z() < secondEdge.middle.Z()) {
return firstWire;
}
else {
return secondWire;
}
}
}
}
if (firstWire.front().middle.Z() < secondWire.front().middle.Z()) {
return firstWire;
}
else {
return secondWire;
}
}
else {
if (!firstWire.empty()) {
return firstWire;
}
if (!secondWire.empty()) {
return secondWire;
}
}
//不应该出现执行到此处的情况
return theRing;
}
// 拆分多位整数为单位数
static std::vector<int> GetDigitValues(int number) {
std::vector<int> digits;
if (number < 0) {
number = -number; // 处理负数
}
do {
digits.push_back(number % 10); // 获取当前最低位数字
number /= 10; // 去掉最低位数字
} while (number != 0);
std::reverse(digits.begin(), digits.end()); // 反转结果
return digits;
}
static std::vector<int> GetNumberString(TCollection_AsciiString numberString) {
std::vector<int> digits;
for (size_t i = 1; i < numberString.Length() + 1; ++i) {
char text = numberString.Value(i);
// 属于数字0-9
if (text >= '0' && text <= '9') {
int digit = text - '0'; // 将字符转换为对应的整数值
digits.push_back(digit);
}
// 属于字母 'X'
else if (text == 'X') {
digits.push_back(10);
}
// 属于字母 'Y'
else if (text == 'Y') {
digits.push_back(11);
}
else if (text == 'A') {
digits.push_back(44);
}
else {
digits.push_back(12);
}
}
return digits;
}
#pragma endregion
#pragma region 生成底板
// 根据工件的 AABB 包围盒得到两个角点(Z 值为 0)
void GetCorners(TopoDS_Shape theShape, BasePlate& theBasePlate) {
Bnd_Box bbox;
BRepBndLib::Add(theShape, bbox);
double xMin, yMin, zMin, xMax, yMax, zMax;
bbox.Get(xMin, yMin, zMin, xMax, yMax, zMax);
theBasePlate.X = xMin;
theBasePlate.Y = yMin;
theBasePlate.Z = zMin;
theBasePlate.dX = xMax - xMin;
theBasePlate.dY = yMax - yMin;
//theBasePlate.lowestZ = zMin;
theBasePlate.hight = zMax - zMin;
}
// 非交互的生成底板
BasePlate MakeBasePlate(TopoDS_Shape theWorkpiece, double theOffsetZ, double theOffsetX, double theOffsetY) {
ShapeFix_Shape shapeFixer(theWorkpiece);
shapeFixer.Perform();
BasePlate theBaseplate;
theBaseplate.offsetX = theOffsetX;
theBaseplate.offsetY = theOffsetY;
theBaseplate.offsetZ = theOffsetZ;
GetCorners(shapeFixer.Shape(), theBaseplate);
theBaseplate.Z -= theOffsetZ;
return theBaseplate;
}
#pragma endregion
#pragma region 生成竖板
// 得到切割后的工件平面
static TopoDS_Shape MakeSection(PlatePose thePose, const TopoDS_Shape& theWorkpiece) {
BRepAlgoAPI_Splitter aSplitter;
TopTools_ListOfShape aListOfArguments, aListOfTools;
aListOfArguments.Append(theWorkpiece);
aListOfTools.Append(BRepBuilderAPI_MakeFace(thePose.Plane()));
aSplitter.SetArguments(aListOfArguments);
aSplitter.SetTools(aListOfTools);
aSplitter.SetFuzzyValue(0.1);
aSplitter.Build();
TopoDS_Compound test;
BRep_Builder bb;
bb.MakeCompound(test);
//bb.Add(test, BRepBuilderAPI_MakeFace(thePose.Plane(), -5, 5, -5, 5));
TopTools_ListOfShape edges = aSplitter.SectionEdges();
TopTools_ListIteratorOfListOfShape it(edges);
for (; it.More(); it.Next()) {
bb.Add(test, it.Value());
}
//TopoDS_Shape aSplitShape = aSplitter.Shape();
//return aSplitShape;
return test;
throw std::runtime_error("截面获取失败");
}
// 从截平面得到单块板
VerticalPlate MakeVerticalPieceWithSection(VerticalPlate& thePalte, TopoDS_Shape theSection) {
PlatePose thePose = thePalte.pose;
double theZ = thePalte.Z;
#pragma region 得到原始构造线
//! 首先判断有几个环,对环进行处理
std::vector<myEdge> TempEdges;
std::vector<Ring> Rings;
TopExp_Explorer aEdgeExp = TopExp_Explorer(theSection, TopAbs_EDGE);
while (aEdgeExp.More()) {
myEdge anEdge(TopoDS::Edge(aEdgeExp.Current()));// 遍历得到每一个边
aEdgeExp.Next();
TempEdges.push_back(anEdge);
}
Rings = GetRings(TempEdges);
//! 每个环中取最下边的线段作为原始构造线
std::vector<myEdge>allEdges;
std::vector<VerticalPiece>finalPieces;
for (Ring aRing : Rings) {
std::vector<myEdge> bottomEdges = SplitRing(aRing);
for (myEdge anEdge : bottomEdges) {
//VerticalPiece anPiece(thePose, anEdge, theZ);
allEdges.push_back(anEdge);
}
}
// 检查是否存在分离失败的情况
gp_Trsf theT;
theT.SetRotation(gp_Ax1(thePose.point, gp_Dir(0, 0, 1)), thePose.dir.AngleWithRef(gp_Vec(0.0, 1.0, 0.0), gp_Vec(0.0, 0.0, 1.0)));
for (size_t i = 0; i < allEdges.size(); i++) {
myEdge Edge_i = allEdges[i];
bool collision = false;
gp_Pnt Start_i = Edge_i.start.Transformed(theT);
gp_Pnt Middle_i = Edge_i.middle.Transformed(theT);
gp_Pnt End_i = Edge_i.end.Transformed(theT);
for (size_t j = 0; j < allEdges.size(); j++) {
if (i == j) { continue; }
myEdge Edge_j = allEdges[j];
gp_Pnt Start_j = Edge_j.start.Transformed(theT);
gp_Pnt Middle_j = Edge_j.middle.Transformed(theT);
gp_Pnt End_j = Edge_j.end.Transformed(theT);
double localTol = 3.0;
if (Start_i.X() - localTol <= Middle_j.X() && Middle_j.X() <= End_i.X() + localTol) {
if (Middle_i.Z() > Middle_j.Z()) {
collision = true;
}
}
if (Start_j.X() - localTol <= Middle_i.X() && Middle_i.X() <= End_j.X() + localTol) {
if (Middle_i.Z() > Middle_j.Z()) {
collision = true;
}
}
}
if (!collision) {
VerticalPiece aPiece(thePose, Edge_i, theZ);
finalPieces.push_back(aPiece);
}
}
thePalte.pieces = finalPieces;
#pragma endregion
return thePalte;
}
// 修剪边的两端
static TopoDS_Edge TrimEdge(const TopoDS_Edge theOriginEdge, gp_Pnt p1, gp_Pnt p2) {
//获取底层曲线
TopLoc_Location l = TopLoc_Location();
double first, last;
Handle(Geom_Curve) aCurve = BRep_Tool::Curve(theOriginEdge, l, first, last);
// 有时底层曲线没有创建,要手动创建
if (aCurve.IsNull()) {
BRepLib::BuildCurves3d(theOriginEdge, 1.0e-5, GeomAbs_C1);//创建曲线 (一阶导数的连续性)
aCurve = BRep_Tool::Curve(theOriginEdge, l, first, last);
}
if (!aCurve.IsNull()) {
//投影点到曲线上,并获取投影点处的参数
GeomAPI_ProjectPointOnCurve ppc1(p1, aCurve);
double param1 = ppc1.LowerDistanceParameter();
GeomAPI_ProjectPointOnCurve ppc2(p2, aCurve);
double param2 = ppc2.LowerDistanceParameter();
if (param1 > param2) {
std::swap(param1, param2);
}
//如果投影点参数小于起始参数或大于终止参数,则分割失败
if (first > param1 || param1 > last || first > param2 || param2 > last)
return TopoDS_Edge();
//处于起始和终止参数中间,则构建两个新边
else {
TopoDS_Edge newEdge = BRepBuilderAPI_MakeEdge(aCurve, param1, param2);
newEdge.Orientation(theOriginEdge.Orientation());//同向
return newEdge;
}
}
//如果失败了返回它本身
return theOriginEdge;
}
// 根据长度修剪线
static VerticalPlate TrimEdgeEnds(VerticalPlate& thePlate) {
std::vector<VerticalPiece> tempPieces;
for (size_t i = 0; i < thePlate.pieces.size(); ++i) {
auto aPiece = thePlate.pieces[i];
if (aPiece.Length() < thePlate.clearances * 2) {
// 去掉修剪后长度过小的边
continue;
}
gp_Pnt p1 = aPiece.myEdge.start;
gp_Pnt p2 = aPiece.myEdge.end;
double ratio = thePlate.clearances / aPiece.Length();
if (std::abs(ratio) > 1e-2) {
p1.SetX(p1.X() + (p2.X() - p1.X()) * ratio);
p1.SetY(p1.Y() + (p2.Y() - p1.Y()) * ratio);
p2.SetX(p2.X() + (p1.X() - p2.X()) * ratio);
p2.SetY(p2.Y() + (p1.Y() - p2.Y()) * ratio);
TopoDS_Edge outEdge = TrimEdge(aPiece.myEdge.edge, p1, p2);
if (!outEdge.IsNull()) {
tempPieces.push_back(VerticalPiece(aPiece.pose, myEdge(outEdge), aPiece.Z));
}
}
else {
//不做改变
tempPieces.push_back(aPiece);
}
}
thePlate.pieces = tempPieces;
return thePlate;
}
// 移除长度过短的边
static VerticalPlate RemoveShortEdge(VerticalPlate& ThePlate) {
std::vector<VerticalPiece> tempPieces;
for (auto aPiece : ThePlate.pieces) {
if (aPiece.Length() > ThePlate.minSupportLen) {
tempPieces.push_back(aPiece);
}
}
ThePlate.pieces = tempPieces;
return ThePlate;
}
//切开过长边
static std::vector<VerticalPiece> CutEdgeUniform(VerticalPiece thePiece, double supLen, double cutLen) {
std::vector<VerticalPiece> result;
// 如果最小支撑长度不足,则不做切割
if (supLen <= 1) {
result.push_back(thePiece);
return result;
}
double edgeLength = thePiece.Length();
// 获取底层曲线
double first, last, left, right;
Handle(Geom_Curve) aCurve = BRep_Tool::Curve(thePiece.myEdge.edge, first, last);
gp_Pnt startP = thePiece.myEdge.start, endP = thePiece.myEdge.end;
//以中点的Z值XOY平面作为投影平面
startP.SetZ(thePiece.myEdge.middle.Z());
endP.SetZ(thePiece.myEdge.middle.Z());
double param1, param2;
double currentNum = 0.0;
while (currentNum * (supLen + cutLen) < thePiece.Length()) {
gp_Pnt start, end;
gp_Trsf toStart = gp_Trsf();
toStart.SetTranslation(gp_Vec(startP, endP).Normalized().Multiplied(currentNum * (supLen + cutLen)));
start = startP.Transformed(toStart);
gp_Trsf toEnd = gp_Trsf();
toEnd.SetTranslation(gp_Vec(startP, endP).Normalized().Multiplied(supLen));
end = start.Transformed(toEnd);
//投影点到曲线上,并获取投影点处的参数
GeomAPI_ProjectPointOnCurve ppc1S(start, aCurve);
param1 = ppc1S.LowerDistanceParameter();
GeomAPI_ProjectPointOnCurve ppc2S(end, aCurve);
param2 = ppc2S.LowerDistanceParameter();
TopoDS_Edge newEdge = BRepBuilderAPI_MakeEdge(aCurve, param1, param2);
myEdge newmyEdge(newEdge);
//newEdge.Orientation(thePiece.myEdge.edge.Orientation());//设置线的方向为同向
VerticalPiece newPiece(thePiece.pose, newmyEdge, thePiece.Z);
result.push_back(newPiece);
currentNum += 1.0;
}
return result;
}
// 切开过长的边,与修剪两端类似的做法
static VerticalPlate CutLongEdge(VerticalPlate& thePlate) {
double theSupportLen = thePlate.minSupportLen;
double theCuttingDistance = thePlate.cuttingDistance;
//如果两个参数值均为 0,则不处理
if (theSupportLen + theCuttingDistance == 0) {
return thePlate;
}
std::vector<VerticalPiece> tempPieces;
for (auto aPiece : thePlate.pieces) {
//跳过不需要切割的长度
if (aPiece.Length() < theSupportLen * 2 + theCuttingDistance) {
tempPieces.push_back(aPiece);
continue;
}
// 以 (theSupportLen + theCuttingDistance)作为分割依据,调整实际的 supLen, cutLen 值
// 如果小于它,同时增加 supLen, cutLen
double supLen, cutLen;
double remainder = std::fmod(aPiece.Length() - theSupportLen, (theSupportLen + theCuttingDistance));
int quotient = std::trunc((aPiece.Length() - theSupportLen) / (theSupportLen + theCuttingDistance));
supLen = theSupportLen + remainder / (double)(quotient + 1) * ((double)(quotient + 1) / (double)(quotient * 2 + 1));
cutLen = theCuttingDistance + remainder / (double)quotient * ((double)quotient / (double)(quotient * 2 + 1));
for (auto aP : CutEdgeUniform(aPiece, supLen, cutLen)) {
tempPieces.push_back(aP);
}
}
thePlate.pieces = tempPieces;
return thePlate;
}
// 生成仅含 Pieces 的竖板
VerticalPlate MakeVerticalPlate(TopoDS_Shape theWorkpiece, BasePlate theBasePlate, PlatePose thePose, double theClearances, double theMinSupportLen, double theCutDistance) {
VerticalPlate result;
result.pose = thePose;
result.Z = theBasePlate.Z;
result.clearances = theClearances;
result.minSupportLen = theMinSupportLen;
result.cuttingDistance = theCutDistance;
//切割工件
TopoDS_Shape aSection = MakeSection(thePose, theWorkpiece);
//result.shape = aSection;
//return result;
result = MakeVerticalPieceWithSection(result, aSection);
result = TrimEdgeEnds(result);
result = RemoveShortEdge(result);
result = CutLongEdge(result);
// 把最后得到的片体排序
result.pieces = SortingPieces(thePose, result.pieces);
return result;
}
// 合并片体为一块板
VerticalPlate SuturePiece(VerticalPlate& thePlate, const BasePlate theBase, double theAvoidanceHeight, double theConnectionThickness) {
if (thePlate.pieces.empty()) { return thePlate; }
//! 板宽度不包含 Offset 的部分
thePlate.avoidanceHeight = theAvoidanceHeight;
double AH = theAvoidanceHeight;
thePlate.connectionThickness = theConnectionThickness;
const double Z = thePlate.Z;
Ring tempEdges;
for (auto anPiece : SortingPieces(thePlate.pose, thePlate.pieces)) {
tempEdges.push_back(anPiece.myEdge);
}
#pragma region 线段生成
gp_Pnt start_p0 = tempEdges.front().start;
start_p0.SetZ(Z);
gp_Pnt end_p0 = tempEdges.back().end;
end_p0.SetZ(Z);
//! 线段生成逻辑见飞书
BRepBuilderAPI_MakeWire wire;
gp_Pnt lastP = start_p0;
for (size_t i = 0; i < tempEdges.size(); i++) {
gp_Pnt p0 = tempEdges[i].start;
p0.SetZ(tempEdges[i].middle.Z() - AH);
gp_Pnt p1 = tempEdges[i].start;
gp_Pnt p2 = tempEdges[i].end;
gp_Pnt p3 = tempEdges[i].end;
p3.SetZ(tempEdges[i].middle.Z() - AH);
// 首段线
if (i == 0) {
wire.Add(BRepBuilderAPI_MakeEdge(lastP, p1));
}
else {
if (tempEdges[i].start.Distance(tempEdges[i - 1].end) > 0.5) {
wire.Add(BRepBuilderAPI_MakeEdge(lastP, p0));
wire.Add(BRepBuilderAPI_MakeEdge(p0, p1));
}
}
wire.Add(tempEdges[i].edge);
//末段线
if (i == tempEdges.size() - 1) {
wire.Add(BRepBuilderAPI_MakeEdge(p2, end_p0));
}
else {
if (tempEdges[i].end.Distance(tempEdges[i + 1].start) > 0.5) {
wire.Add(BRepBuilderAPI_MakeEdge(p2, p3));
}
}
lastP = p3;
}
#pragma endregion
//合并后更新Plate的首末端
thePlate.start = start_p0;
thePlate.end = end_p0;
thePlate._unclosedWire = wire;
//thePlate.shape = wire.Shape();// debug
return thePlate;
}
// 在竖板上切连接槽
VerticalPlate SlotVerticalPlate(VerticalPlate& thePlate, std::vector<VerticalPlate> otherPlates, double theFilletRadius, bool middleToDown) {
if (thePlate.pieces.empty()) { return thePlate; }
double TOL = 1e-2;
double offset = 0.9;
double Z = thePlate.Z;
// 找到板和其它板的交点
std::vector<gp_Pnt> cutPoints;
double x1 = thePlate.start.X();
double y1 = thePlate.start.Y();
double x2 = thePlate.end.X();
double y2 = thePlate.end.Y();
if (x1 > x2) { std::swap(x1, x2); }
if (y1 > y2) { std::swap(y1, y2); }
for (auto other : otherPlates) {
double x3 = other.start.X();
double y3 = other.start.Y();
double x4 = other.end.X();
double y4 = other.end.Y();
if (x3 > x4) { std::swap(x3, x4); }
if (y3 > y4) { std::swap(y3, y4); }
//避开边缘,防止切出去
double cx, cy;
if (Get2DLineIntersection(x1, y1, x2, y2, x3, y3, x4, y4, cx, cy)) {
if (x1 - TOL <= cx && cx <= x2 + TOL && y1 - TOL <= cy && cy <= y2 + TOL) {
if (x3 - TOL <= cx && cx <= x4 + TOL && y3 - TOL <= cy && cy <= y4 + TOL) {
gp_Pnt cutPoint = gp_Pnt(cx, cy, Z);
cutPoints.push_back(cutPoint);
}
}
}
}
//按距离end从小到大排序,从end开始连接回start
std::sort(cutPoints.begin(), cutPoints.end(), [&](const gp_Pnt& p1, const gp_Pnt& p2) {
return p1.Distance(thePlate.end) < p2.Distance(thePlate.end);
});
std::sort(thePlate.cutPoints.begin(), thePlate.cutPoints.end(), [&](const gp_Pnt& p1, const gp_Pnt& p2) {
return p1.Distance(thePlate.start) < p2.Distance(thePlate.start);
});
// 如果没有相交点,则添加辅助板
if (cutPoints.empty()) {
thePlate.singlePlate = true;
cutPoints.push_back(gp_Pnt((thePlate.start.XYZ() + thePlate.end.XYZ()) / 2));
}
//用于后续烙印数字
thePlate.cutPoints = cutPoints;
double slotL = thePlate.slotLength / 2;
double slotH = thePlate.slotHight;
double ct = thePlate.connectionThickness / 2;
gp_Pnt lastP = thePlate.end;
// 在每个位置切槽
gp_Vec plateDir(thePlate.start, thePlate.end);
plateDir.Normalize();
for (gp_Pnt aPnt : cutPoints) {
gp_Pnt r1 = aPnt;
gp_Trsf Tr1;
Tr1.SetTranslation(plateDir.Multiplied(slotL));
r1.Transform(Tr1);
gp_Pnt r2 = aPnt;
gp_Trsf Tr2;
Tr2.SetTranslation(plateDir.Multiplied(slotL).Added(gp_Vec(0, 0, -ct * 2)));
r2.Transform(Tr2);
gp_Pnt l1 = aPnt;
gp_Trsf Tl1;
Tl1.SetTranslation(plateDir.Multiplied(-slotL).Added(gp_Vec(0, 0, -ct * 2)));
l1.Transform(Tl1);
gp_Pnt l2 = aPnt;
gp_Trsf Tl2;
Tl2.SetTranslation(plateDir.Multiplied(-slotL));
l2.Transform(Tl2);
if (middleToDown) {
// 从中间往下
gp_Pnt p3 = aPnt;
gp_Trsf Tp3;
Tp3.SetTranslation(plateDir.Multiplied(ct).Added(gp_Vec(0, 0, -ct * 2)));
p3.Transform(Tp3);
gp_Pnt p4 = aPnt;
gp_Trsf Tp4;
Tp4.SetTranslation(plateDir.Multiplied(ct).Added(gp_Vec(0, 0, slotH)));
p4.Transform(Tp4);
gp_Pnt p5 = aPnt;
gp_Trsf Tp5;
Tp5.SetTranslation(plateDir.Multiplied(-ct).Added(gp_Vec(0, 0, slotH)));
p5.Transform(Tp5);
gp_Pnt p6 = aPnt;
gp_Trsf Tp6;
Tp6.SetTranslation(plateDir.Multiplied(-ct).Added(gp_Vec(0, 0, -ct * 2)));
p6.Transform(Tp6);
thePlate._unclosedWire.Add(BRepBuilderAPI_MakeEdge(lastP, r1));
thePlate._unclosedWire.Add(BRepBuilderAPI_MakeEdge(r1, r2));
thePlate._unclosedWire.Add(BRepBuilderAPI_MakeEdge(r2, p3));
thePlate._unclosedWire.Add(BRepBuilderAPI_MakeEdge(p3, p4));
thePlate._unclosedWire.Add(BRepBuilderAPI_MakeEdge(p4, p5));
thePlate._unclosedWire.Add(BRepBuilderAPI_MakeEdge(p5, p6));
thePlate._unclosedWire.Add(BRepBuilderAPI_MakeEdge(p6, l1));
thePlate._unclosedWire.Add(BRepBuilderAPI_MakeEdge(l1, l2));
}
else {
// 从中间往上
thePlate._unclosedWire.Add(BRepBuilderAPI_MakeEdge(lastP, r1));
thePlate._unclosedWire.Add(BRepBuilderAPI_MakeEdge(r1, r2));
thePlate._unclosedWire.Add(BRepBuilderAPI_MakeEdge(r2, l1));
thePlate._unclosedWire.Add(BRepBuilderAPI_MakeEdge(l1, l2));
}
lastP = l2;
}
// 如果没有其它竖板相交
thePlate._unclosedWire.Add(BRepBuilderAPI_MakeEdge(lastP, thePlate.start));
////! debug
//thePlate.shape = thePlate._unclosedWire.Shape();
//return thePlate;
//
////! debug
//TopoDS_Compound testShape;
//BRep_Builder b;
//b.MakeCompound(testShape);
//thePlate.shape = testShape;
if (thePlate._unclosedWire.IsDone()) {
TopoDS_Shape finalShape = BRepBuilderAPI_MakeFace(thePlate._unclosedWire).Shape();
//b.Add(testShape, finalShape);
// 还需要额外处理切除
if (!middleToDown) {
double ZC = thePlate.Z + thePlate.slotHight;
// 创建一个圆柱 (从中间往上)
for (gp_Pnt aPnt : cutPoints) {
gp_Pnt cutPnt = aPnt;
cutPnt.SetZ(ZC);
TopoDS_Shape theSlot = BRepPrimAPI_MakeCylinder(gp_Ax2(cutPnt, gp_Dir(0.0, 0.0, 1.0)), ct, 999.0).Shape();
finalShape = BRepAlgoAPI_Cut(finalShape, theSlot).Shape();
//b.Add(testShape, theSlot);
}
}
thePlate.shape = finalShape;
////! debug
//thePlate.shape = testShape;
}
//todo 倒圆角 目前构建会失败
//// 缝合组合体为一个面
//BRepBuilderAPI_Sewing aSewingTool;
//aSewingTool.Init();
//aSewingTool.Load(thePlate.shape);
//aSewingTool.Perform();
//TopoDS_Shape sewedShape = aSewingTool.SewedShape();