- Notifications
You must be signed in to change notification settings - Fork 322
/
Copy pathtest_cad_objects.py
825 lines (687 loc) · 27 KB
/
test_cad_objects.py
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
# system modules
importmath
importpytest
importunittest
fromtestsimportBaseTest
fromOCP.gpimportgp_Vec, gp_Pnt, gp_Ax2, gp_Circ, gp_Elips, gp, gp_XYZ, gp_Trsf
fromOCP.BRepBuilderAPIimportBRepBuilderAPI_MakeEdge
fromcadqueryimport*
DEG2RAD=math.pi/180
RAD2DEG=180/math.pi
classTestCadObjects(BaseTest):
def_make_circle(self):
circle=gp_Circ(gp_Ax2(gp_Pnt(1, 2, 3), gp.DZ_s()), 2.0)
returnShape.cast(BRepBuilderAPI_MakeEdge(circle).Edge())
def_make_ellipse(self):
ellipse=gp_Elips(gp_Ax2(gp_Pnt(1, 2, 3), gp.DZ_s()), 4.0, 2.0)
returnShape.cast(BRepBuilderAPI_MakeEdge(ellipse).Edge())
deftestVectorConstructors(self):
v1=Vector(1, 2, 3)
v2=Vector((1, 2, 3))
v3=Vector(gp_Vec(1, 2, 3))
v4=Vector([1, 2, 3])
v5=Vector(gp_XYZ(1, 2, 3))
forvin [v1, v2, v3, v4, v5]:
self.assertTupleAlmostEquals((1, 2, 3), v.toTuple(), 4)
v6=Vector((1, 2))
v7=Vector([1, 2])
v8=Vector(1, 2)
forvin [v6, v7, v8]:
self.assertTupleAlmostEquals((1, 2, 0), v.toTuple(), 4)
v9=Vector()
self.assertTupleAlmostEquals((0, 0, 0), v9.toTuple(), 4)
v9.x=1.0
v9.y=2.0
v9.z=3.0
self.assertTupleAlmostEquals((1, 2, 3), (v9.x, v9.y, v9.z), 4)
withself.assertRaises(TypeError):
Vector("vector")
withself.assertRaises(TypeError):
Vector(1, 2, 3, 4)
deftestVertex(self):
"""
Tests basic vertex functions
"""
v=Vertex.makeVertex(1, 1, 1)
self.assertEqual(1, v.X)
self.assertEqual(Vector, type(v.Center()))
deftestBasicBoundingBox(self):
v=Vertex.makeVertex(1, 1, 1)
v2=Vertex.makeVertex(2, 2, 2)
self.assertEqual(BoundBox, type(v.BoundingBox()))
self.assertEqual(BoundBox, type(v2.BoundingBox()))
bb1=v.BoundingBox().add(v2.BoundingBox())
# OCC uses some approximations
self.assertAlmostEqual(bb1.xlen, 1.0, 1)
# Test adding to an existing bounding box
v0=Vertex.makeVertex(0, 0, 0)
bb2=v0.BoundingBox().add(v.BoundingBox())
bb3=bb1.add(bb2)
self.assertTupleAlmostEquals((2, 2, 2), (bb3.xlen, bb3.ylen, bb3.zlen), 7)
bb3=bb2.add((3, 3, 3))
self.assertTupleAlmostEquals((3, 3, 3), (bb3.xlen, bb3.ylen, bb3.zlen), 7)
bb3=bb2.add(Vector(3, 3, 3))
self.assertTupleAlmostEquals((3, 3, 3), (bb3.xlen, bb3.ylen, bb3.zlen), 7)
# Test 2D bounding boxes
bb1= (
Vertex.makeVertex(1, 1, 0)
.BoundingBox()
.add(Vertex.makeVertex(2, 2, 0).BoundingBox())
)
bb2= (
Vertex.makeVertex(0, 0, 0)
.BoundingBox()
.add(Vertex.makeVertex(3, 3, 0).BoundingBox())
)
bb3= (
Vertex.makeVertex(0, 0, 0)
.BoundingBox()
.add(Vertex.makeVertex(1.5, 1.5, 0).BoundingBox())
)
# Test that bb2 contains bb1
self.assertEqual(bb2, BoundBox.findOutsideBox2D(bb1, bb2))
self.assertEqual(bb2, BoundBox.findOutsideBox2D(bb2, bb1))
# Test that neither bounding box contains the other
self.assertIsNone(BoundBox.findOutsideBox2D(bb1, bb3))
# Test creation of a bounding box from a shape - note the low accuracy comparison
# as the box is a little larger than the shape
bb1=BoundBox._fromTopoDS(Solid.makeCylinder(1, 1).wrapped, optimal=False)
self.assertTupleAlmostEquals((2, 2, 1), (bb1.xlen, bb1.ylen, bb1.zlen), 1)
deftestEdgeWrapperCenter(self):
e=self._make_circle()
self.assertTupleAlmostEquals((1.0, 2.0, 3.0), e.Center().toTuple(), 3)
deftestEdgeWrapperEllipseCenter(self):
e=self._make_ellipse()
w=Wire.assembleEdges([e])
self.assertTupleAlmostEquals(
(1.0, 2.0, 3.0), Face.makeFromWires(w).Center().toTuple(), 3
)
deftestEdgeWrapperMakeCircle(self):
halfCircleEdge=Edge.makeCircle(
radius=10, pnt=(0, 0, 0), dir=(0, 0, 1), angle1=0, angle2=180
)
# self.assertTupleAlmostEquals((0.0, 5.0, 0.0), halfCircleEdge.CenterOfBoundBox(0.0001).toTuple(),3)
self.assertTupleAlmostEquals(
(10.0, 0.0, 0.0), halfCircleEdge.startPoint().toTuple(), 3
)
self.assertTupleAlmostEquals(
(-10.0, 0.0, 0.0), halfCircleEdge.endPoint().toTuple(), 3
)
deftestEdgeWrapperMakeTangentArc(self):
tangent_arc=Edge.makeTangentArc(
Vector(1, 1), # starts at 1, 1
Vector(0, 1), # tangent at start of arc is in the +y direction
Vector(2, 1), # arc curves 180 degrees and ends at 2, 1
)
self.assertTupleAlmostEquals((1, 1, 0), tangent_arc.startPoint().toTuple(), 3)
self.assertTupleAlmostEquals((2, 1, 0), tangent_arc.endPoint().toTuple(), 3)
self.assertTupleAlmostEquals(
(0, 1, 0), tangent_arc.tangentAt(locationParam=0).toTuple(), 3
)
self.assertTupleAlmostEquals(
(1, 0, 0), tangent_arc.tangentAt(locationParam=0.5).toTuple(), 3
)
self.assertTupleAlmostEquals(
(0, -1, 0), tangent_arc.tangentAt(locationParam=1).toTuple(), 3
)
deftestEdgeWrapperMakeEllipse1(self):
# Check x_radius > y_radius
x_radius, y_radius=20, 10
angle1, angle2=-75.0, 90.0
arcEllipseEdge=Edge.makeEllipse(
x_radius=x_radius,
y_radius=y_radius,
pnt=(0, 0, 0),
dir=(0, 0, 1),
angle1=angle1,
angle2=angle2,
)
start= (
x_radius*math.cos(angle1*DEG2RAD),
y_radius*math.sin(angle1*DEG2RAD),
0.0,
)
end= (
x_radius*math.cos(angle2*DEG2RAD),
y_radius*math.sin(angle2*DEG2RAD),
0.0,
)
self.assertTupleAlmostEquals(start, arcEllipseEdge.startPoint().toTuple(), 3)
self.assertTupleAlmostEquals(end, arcEllipseEdge.endPoint().toTuple(), 3)
deftestEdgeWrapperMakeEllipse2(self):
# Check x_radius < y_radius
x_radius, y_radius=10, 20
angle1, angle2=0.0, 45.0
arcEllipseEdge=Edge.makeEllipse(
x_radius=x_radius,
y_radius=y_radius,
pnt=(0, 0, 0),
dir=(0, 0, 1),
angle1=angle1,
angle2=angle2,
)
start= (
x_radius*math.cos(angle1*DEG2RAD),
y_radius*math.sin(angle1*DEG2RAD),
0.0,
)
end= (
x_radius*math.cos(angle2*DEG2RAD),
y_radius*math.sin(angle2*DEG2RAD),
0.0,
)
self.assertTupleAlmostEquals(start, arcEllipseEdge.startPoint().toTuple(), 3)
self.assertTupleAlmostEquals(end, arcEllipseEdge.endPoint().toTuple(), 3)
deftestEdgeWrapperMakeCircleWithEllipse(self):
# Check x_radius == y_radius
x_radius, y_radius=20, 20
angle1, angle2=15.0, 60.0
arcEllipseEdge=Edge.makeEllipse(
x_radius=x_radius,
y_radius=y_radius,
pnt=(0, 0, 0),
dir=(0, 0, 1),
angle1=angle1,
angle2=angle2,
)
start= (
x_radius*math.cos(angle1*DEG2RAD),
y_radius*math.sin(angle1*DEG2RAD),
0.0,
)
end= (
x_radius*math.cos(angle2*DEG2RAD),
y_radius*math.sin(angle2*DEG2RAD),
0.0,
)
self.assertTupleAlmostEquals(start, arcEllipseEdge.startPoint().toTuple(), 3)
self.assertTupleAlmostEquals(end, arcEllipseEdge.endPoint().toTuple(), 3)
deftestFaceWrapperMakePlane(self):
mplane=Face.makePlane(10, 10)
self.assertTupleAlmostEquals((0.0, 0.0, 1.0), mplane.normalAt().toTuple(), 3)
deftestMatrixOfInertia(self):
"""
Tests the calculation of the matrix of inertia for a solid
"""
radius=1.0
height=2.0
cylinder=Solid.makeCylinder(radius=radius, height=height)
moi=Shape.matrixOfInertia(cylinder)
two_pi=2*math.pi
true_moi= (
two_pi* (radius**2/4+height**2/12),
two_pi* (radius**2/4+height**2/12),
two_pi*radius**2/2,
)
self.assertTupleAlmostEquals((moi[0][0], moi[1][1], moi[2][2]), true_moi, 3)
deftestVertexMatrixOfInertiaNotImplemented(self):
withself.assertRaises(NotImplementedError):
vertex=Vertex.makeVertex(1, 1, 1)
Shape.matrixOfInertia(vertex)
deftestCenterOfBoundBox(self):
pass
deftestCombinedCenterOfBoundBox(self):
pass
deftestCompoundCenter(self):
"""
Tests whether or not a proper weighted center can be found for a compound
"""
defcylinders(self, radius, height):
c=Solid.makeCylinder(radius, height, Vector())
# Combine all the cylinders into a single compound
r=self.eachpoint(lambdaloc: c.located(loc), True).combineSolids()
returnr
Workplane.cyl=cylinders
# Now test. here we want weird workplane to see if the objects are transformed right
s= (
Workplane("XY")
.rect(2.0, 3.0, forConstruction=True)
.vertices()
.cyl(0.25, 0.5)
)
self.assertEqual(4, len(s.val().Solids()))
self.assertTupleAlmostEquals((0.0, 0.0, 0.25), s.val().Center().toTuple(), 3)
deftestDot(self):
v1=Vector(2, 2, 2)
v2=Vector(1, -1, 1)
self.assertEqual(2.0, v1.dot(v2))
deftestVectorAdd(self):
result=Vector(1, 2, 0) +Vector(0, 0, 3)
self.assertTupleAlmostEquals((1.0, 2.0, 3.0), result.toTuple(), 3)
deftestVectorOperators(self):
result=Vector(1, 1, 1) +Vector(2, 2, 2)
self.assertEqual(Vector(3, 3, 3), result)
result=Vector(1, 2, 3) -Vector(3, 2, 1)
self.assertEqual(Vector(-2, 0, 2), result)
result=Vector(1, 2, 3) *2
self.assertEqual(Vector(2, 4, 6), result)
result=3*Vector(1, 2, 3)
self.assertEqual(Vector(3, 6, 9), result)
result=Vector(2, 4, 6) /2
self.assertEqual(Vector(1, 2, 3), result)
self.assertEqual(Vector(-1, -1, -1), -Vector(1, 1, 1))
self.assertEqual(0, abs(Vector(0, 0, 0)))
self.assertEqual(1, abs(Vector(1, 0, 0)))
self.assertEqual((1+4+9) **0.5, abs(Vector(1, 2, 3)))
deftestVectorEquals(self):
a=Vector(1, 2, 3)
b=Vector(1, 2, 3)
c=Vector(1, 2, 3.000001)
self.assertEqual(a, b)
self.assertEqual(a, c)
deftestVectorProject(self):
"""
Test line projection and plane projection methods of cq.Vector
"""
decimal_places=9
normal=Vector(1, 2, 3)
base=Vector(5, 7, 9)
x_dir=Vector(1, 0, 0)
# test passing Plane object
point=Vector(10, 11, 12).projectToPlane(Plane(base, x_dir, normal))
self.assertTupleAlmostEquals(
point.toTuple(), (59/7, 55/7, 51/7), decimal_places
)
# test line projection
vec=Vector(10, 10, 10)
line=Vector(3, 4, 5)
angle=vec.getAngle(line)
vecLineProjection=vec.projectToLine(line)
self.assertTupleAlmostEquals(
vecLineProjection.normalized().toTuple(),
line.normalized().toTuple(),
decimal_places,
)
self.assertAlmostEqual(
vec.Length*math.cos(angle), vecLineProjection.Length, decimal_places
)
deftestVectorNotImplemented(self):
v=Vector(1, 2, 3)
withself.assertRaises(NotImplementedError):
v.distanceToLine()
withself.assertRaises(NotImplementedError):
v.distanceToPlane()
deftestVectorSpecialMethods(self):
v=Vector(1, 2, 3)
self.assertEqual(repr(v), "Vector: (1.0, 2.0, 3.0)")
self.assertEqual(str(v), "Vector: (1.0, 2.0, 3.0)")
deftestMatrixCreationAndAccess(self):
defmatrix_vals(m):
return [[m[r, c] forcinrange(4)] forrinrange(4)]
# default constructor creates a 4x4 identity matrix
m=Matrix()
identity= [
[1.0, 0.0, 0.0, 0.0],
[0.0, 1.0, 0.0, 0.0],
[0.0, 0.0, 1.0, 0.0],
[0.0, 0.0, 0.0, 1.0],
]
self.assertEqual(identity, matrix_vals(m))
vals4x4= [
[1.0, 0.0, 0.0, 1.0],
[0.0, 1.0, 0.0, 2.0],
[0.0, 0.0, 1.0, 3.0],
[0.0, 0.0, 0.0, 1.0],
]
vals4x4_tuple=tuple(tuple(r) forrinvals4x4)
# test constructor with 16-value input
m=Matrix(vals4x4)
self.assertEqual(vals4x4, matrix_vals(m))
m=Matrix(vals4x4_tuple)
self.assertEqual(vals4x4, matrix_vals(m))
# test constructor with 12-value input (the last 4 are an implied
# [0,0,0,1])
m=Matrix(vals4x4[:3])
self.assertEqual(vals4x4, matrix_vals(m))
m=Matrix(vals4x4_tuple[:3])
self.assertEqual(vals4x4, matrix_vals(m))
# Test 16-value input with invalid values for the last 4
invalid= [
[1.0, 0.0, 0.0, 1.0],
[0.0, 1.0, 0.0, 2.0],
[0.0, 0.0, 1.0, 3.0],
[1.0, 2.0, 3.0, 4.0],
]
withself.assertRaises(ValueError):
Matrix(invalid)
# Test input with invalid type
withself.assertRaises(TypeError):
Matrix("invalid")
# Test input with invalid size / nested types
withself.assertRaises(TypeError):
Matrix([[1, 2, 3, 4], [1, 2, 3], [1, 2, 3, 4]])
withself.assertRaises(TypeError):
Matrix([1, 2, 3])
# Invalid sub-type
withself.assertRaises(TypeError):
Matrix([[1, 2, 3, 4], "abc", [1, 2, 3, 4]])
# test out-of-bounds access
m=Matrix()
withself.assertRaises(IndexError):
m[0, 4]
withself.assertRaises(IndexError):
m[4, 0]
withself.assertRaises(IndexError):
m["ab"]
# test __repr__ methods
m=Matrix(vals4x4)
mRepr="Matrix([[1.0, 0.0, 0.0, 1.0],\n [0.0, 1.0, 0.0, 2.0],\n [0.0, 0.0, 1.0, 3.0],\n [0.0, 0.0, 0.0, 1.0]])"
self.assertEqual(repr(m), mRepr)
self.assertEqual(str(eval(repr(m))), mRepr)
deftestMatrixFunctionality(self):
# Test rotate methods
defmatrix_almost_equal(m, target_matrix):
forr, rowinenumerate(target_matrix):
forc, target_valueinenumerate(row):
self.assertAlmostEqual(m[r, c], target_value)
root_3_over_2=math.sqrt(3) /2
m_rotate_x_30= [
[1, 0, 0, 0],
[0, root_3_over_2, -1/2, 0],
[0, 1/2, root_3_over_2, 0],
[0, 0, 0, 1],
]
mx=Matrix()
mx.rotateX(30*DEG2RAD)
matrix_almost_equal(mx, m_rotate_x_30)
m_rotate_y_30= [
[root_3_over_2, 0, 1/2, 0],
[0, 1, 0, 0],
[-1/2, 0, root_3_over_2, 0],
[0, 0, 0, 1],
]
my=Matrix()
my.rotateY(30*DEG2RAD)
matrix_almost_equal(my, m_rotate_y_30)
m_rotate_z_30= [
[root_3_over_2, -1/2, 0, 0],
[1/2, root_3_over_2, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1],
]
mz=Matrix()
mz.rotateZ(30*DEG2RAD)
matrix_almost_equal(mz, m_rotate_z_30)
# Test matrix multipy vector
v=Vector(1, 0, 0)
self.assertTupleAlmostEquals(
mz.multiply(v).toTuple(), (root_3_over_2, 1/2, 0), 7
)
# Test matrix multipy matrix
m_rotate_xy_30= [
[root_3_over_2, 0, 1/2, 0],
[1/4, root_3_over_2, -root_3_over_2/2, 0],
[-root_3_over_2/2, 1/2, 3/4, 0],
[0, 0, 0, 1],
]
mxy=mx.multiply(my)
matrix_almost_equal(mxy, m_rotate_xy_30)
# Test matrix inverse
vals4x4= [[1, 2, 3, 4], [5, 1, 6, 7], [8, 9, 1, 10], [0, 0, 0, 1]]
vals4x4_invert= [
[-53/144, 25/144, 1/16, -53/144],
[43/144, -23/144, 1/16, -101/144],
[37/144, 7/144, -1/16, -107/144],
[0, 0, 0, 1],
]
m=Matrix(vals4x4).inverse()
matrix_almost_equal(m, vals4x4_invert)
deftestTranslate(self):
e=Edge.makeCircle(2, (1, 2, 3))
e2=e.translate(Vector(0, 0, 1))
self.assertTupleAlmostEquals((1.0, 2.0, 4.0), e2.Center().toTuple(), 3)
deftestVertices(self):
e=Shape.cast(BRepBuilderAPI_MakeEdge(gp_Pnt(0, 0, 0), gp_Pnt(1, 1, 0)).Edge())
self.assertEqual(2, len(e.Vertices()))
deftestPlaneEqual(self):
# default orientation
self.assertEqual(
Plane(origin=(0, 0, 0), xDir=(1, 0, 0), normal=(0, 0, 1)),
Plane(origin=(0, 0, 0), xDir=(1, 0, 0), normal=(0, 0, 1)),
)
# moved origin
self.assertEqual(
Plane(origin=(2, 1, -1), xDir=(1, 0, 0), normal=(0, 0, 1)),
Plane(origin=(2, 1, -1), xDir=(1, 0, 0), normal=(0, 0, 1)),
)
# moved x-axis
self.assertEqual(
Plane(origin=(0, 0, 0), xDir=(1, 1, 0), normal=(0, 0, 1)),
Plane(origin=(0, 0, 0), xDir=(1, 1, 0), normal=(0, 0, 1)),
)
# moved z-axis
self.assertEqual(
Plane(origin=(0, 0, 0), xDir=(1, 0, 0), normal=(0, 1, 1)),
Plane(origin=(0, 0, 0), xDir=(1, 0, 0), normal=(0, 1, 1)),
)
deftestPlaneNotEqual(self):
# type difference
forvaluein [None, 0, 1, "abc"]:
self.assertNotEqual(
Plane(origin=(0, 0, 0), xDir=(1, 0, 0), normal=(0, 0, 1)), value
)
# origin difference
self.assertNotEqual(
Plane(origin=(0, 0, 0), xDir=(1, 0, 0), normal=(0, 0, 1)),
Plane(origin=(0, 0, 1), xDir=(1, 0, 0), normal=(0, 0, 1)),
)
# x-axis difference
self.assertNotEqual(
Plane(origin=(0, 0, 0), xDir=(1, 0, 0), normal=(0, 0, 1)),
Plane(origin=(0, 0, 0), xDir=(1, 1, 0), normal=(0, 0, 1)),
)
# z-axis difference
self.assertNotEqual(
Plane(origin=(0, 0, 0), xDir=(1, 0, 0), normal=(0, 0, 1)),
Plane(origin=(0, 0, 0), xDir=(1, 0, 0), normal=(0, 1, 1)),
)
deftestInvalidPlane(self):
# Test plane creation error handling
withself.assertRaises(ValueError):
Plane.named("XX", (0, 0, 0))
withself.assertRaises(ValueError):
Plane(origin=(0, 0, 0), xDir=(0, 0, 0), normal=(0, 1, 1))
withself.assertRaises(ValueError):
Plane(origin=(0, 0, 0), xDir=(1, 0, 0), normal=(0, 0, 0))
deftestPlaneMethods(self):
# Test error checking
p=Plane(origin=(0, 0, 0), xDir=(1, 0, 0), normal=(0, 1, 0))
withself.assertRaises(ValueError):
p.toLocalCoords("box")
withself.assertRaises(NotImplementedError):
p.mirrorInPlane([Solid.makeBox(1, 1, 1)], "Z")
# Test translation to local coordinates
local_box=Workplane(p.toLocalCoords(Solid.makeBox(1, 1, 1)))
local_box_vertices= [(v.X, v.Y, v.Z) forvinlocal_box.vertices().vals()]
target_vertices= [
(0, -1, 0),
(0, 0, 0),
(0, -1, 1),
(0, 0, 1),
(1, -1, 0),
(1, 0, 0),
(1, -1, 1),
(1, 0, 1),
]
fori, target_pointinenumerate(target_vertices):
self.assertTupleAlmostEquals(target_point, local_box_vertices[i], 7)
# Test mirrorInPlane
mirror_box=Workplane(p.mirrorInPlane([Solid.makeBox(1, 1, 1)], "Y")[0])
mirror_box_vertices= [(v.X, v.Y, v.Z) forvinmirror_box.vertices().vals()]
target_vertices= [
(0, 0, 1),
(0, 0, 0),
(0, -1, 1),
(0, -1, 0),
(-1, 0, 1),
(-1, 0, 0),
(-1, -1, 1),
(-1, -1, 0),
]
fori, target_pointinenumerate(target_vertices):
self.assertTupleAlmostEquals(target_point, mirror_box_vertices[i], 7)
deftestLocation(self):
# empty
loc=Location()
T=loc.wrapped.Transformation().TranslationPart()
self.assertTupleAlmostEquals((T.X(), T.Y(), T.Z()), (0, 0, 0), 6)
# Tuple
loc0=Location((0, 0, 1))
T=loc0.wrapped.Transformation().TranslationPart()
self.assertTupleAlmostEquals((T.X(), T.Y(), T.Z()), (0, 0, 1), 6)
# Vector
loc1=Location(Vector(0, 0, 1))
T=loc1.wrapped.Transformation().TranslationPart()
self.assertTupleAlmostEquals((T.X(), T.Y(), T.Z()), (0, 0, 1), 6)
# rotation + translation
loc2=Location(Vector(0, 0, 1), Vector(0, 0, 1), 45)
angle=loc2.wrapped.Transformation().GetRotation().GetRotationAngle() *RAD2DEG
self.assertAlmostEqual(45, angle)
# gp_Trsf
T=gp_Trsf()
T.SetTranslation(gp_Vec(0, 0, 1))
loc3=Location(T)
assert (
loc1.wrapped.Transformation().TranslationPart().Z()
==loc3.wrapped.Transformation().TranslationPart().Z()
)
# Test creation from the OCP.gp.gp_Trsf object
loc4=Location(gp_Trsf())
self.assertTupleAlmostEquals(loc4.toTuple()[0], (0, 0, 0), 7)
self.assertTupleAlmostEquals(loc4.toTuple()[1], (0, 0, 0), 7)
# Test composition
loc4=Location((0, 0, 0), Vector(0, 0, 1), 15)
loc5=loc1*loc4
loc6=loc4*loc4
loc7=loc4**2
T=loc5.wrapped.Transformation().TranslationPart()
self.assertTupleAlmostEquals((T.X(), T.Y(), T.Z()), (0, 0, 1), 6)
angle5= (
loc5.wrapped.Transformation().GetRotation().GetRotationAngle() *RAD2DEG
)
self.assertAlmostEqual(15, angle5)
angle6= (
loc6.wrapped.Transformation().GetRotation().GetRotationAngle() *RAD2DEG
)
self.assertAlmostEqual(30, angle6)
angle7= (
loc7.wrapped.Transformation().GetRotation().GetRotationAngle() *RAD2DEG
)
self.assertAlmostEqual(30, angle7)
# Test error handling on creation
withself.assertRaises(TypeError):
Location([0, 0, 1])
withself.assertRaises(TypeError):
Location("xy_plane")
# test to tuple
loc8=Location(z=2, ry=15)
trans, rot=loc8.toTuple()
self.assertTupleAlmostEquals(trans, (0, 0, 2), 6)
self.assertTupleAlmostEquals(rot, (0, 15, 0), 6)
deftestEdgeWrapperRadius(self):
# get a radius from a simple circle
e0=Edge.makeCircle(2.4)
self.assertAlmostEqual(e0.radius(), 2.4)
# radius of an arc
e1=Edge.makeCircle(1.8, pnt=(5, 6, 7), dir=(1, 1, 1), angle1=20, angle2=30)
self.assertAlmostEqual(e1.radius(), 1.8)
# test value errors
e2=Edge.makeEllipse(10, 20)
withself.assertRaises(ValueError):
e2.radius()
# radius from a wire
w0=Wire.makeCircle(10, Vector(1, 2, 3), (-1, 0, 1))
self.assertAlmostEqual(w0.radius(), 10)
# radius from a wire with multiple edges
rad=2.3
pnt= (7, 8, 9)
direction= (1, 0.5, 0.1)
w1=Wire.assembleEdges(
[
Edge.makeCircle(rad, pnt, direction, 0, 10),
Edge.makeCircle(rad, pnt, direction, 10, 25),
Edge.makeCircle(rad, pnt, direction, 25, 230),
]
)
self.assertAlmostEqual(w1.radius(), rad)
# test value error from wire
w2=Wire.makePolygon([Vector(-1, 0, 0), Vector(0, 1, 0), Vector(1, -1, 0),])
withself.assertRaises(ValueError):
w2.radius()
# (I think) the radius of a wire is the radius of it's first edge.
# Since this is stated in the docstring better make sure.
no_rad=Wire.assembleEdges(
[
Edge.makeLine(Vector(0, 0, 0), Vector(0, 1, 0)),
Edge.makeCircle(1.0, angle1=90, angle2=270),
]
)
withself.assertRaises(ValueError):
no_rad.radius()
yes_rad=Wire.assembleEdges(
[
Edge.makeCircle(1.0, angle1=90, angle2=270),
Edge.makeLine(Vector(0, -1, 0), Vector(0, 1, 0)),
]
)
self.assertAlmostEqual(yes_rad.radius(), 1.0)
many_rad=Wire.assembleEdges(
[
Edge.makeCircle(1.0, angle1=0, angle2=180),
Edge.makeCircle(3.0, pnt=Vector(2, 0, 0), angle1=180, angle2=359),
]
)
self.assertAlmostEqual(many_rad.radius(), 1.0)
deftestWireFillet(self):
points= [
(0.000, 0.000, 0.000),
(-0.287, 1.183, -0.592),
(-1.404, 4.113, -2.787),
(-1.332, 1.522, 0.553),
(7.062, 0.433, -0.097),
(8.539, -0.000, -0.000),
]
wire=Wire.makePolygon(points, close=False)
# Fillet the wire
wfillet=wire.fillet(radius=0.560)
assertlen(wfillet.Edges()) ==2*len(points) -3
# Fillet a single vertex
wfillet=wire.fillet(radius=0.560, vertices=wire.Vertices()[1:2])
assertlen(wfillet.Edges()) ==len(points)
# Assert exception if trying to fillet with too big
# a radius
withself.assertRaises(ValueError):
wfillet=wire.fillet(radius=1.0)
# Test a closed fillet
points= [[0, 0, 0], [5, 4, 0], [8, 3, 1], [10, 0, 0]]
wire=Wire.makePolygon(points, close=True)
wfillet=wire.fillet(radius=0.5)
assertlen(wfillet.Edges()) ==2*len(points)
# Fillet a single vertex
wfillet=wire.fillet(radius=0.5, vertices=wire.Vertices()[0:1])
assertlen(wfillet.Edges()) ==len(points) +1
@pytest.mark.parametrize(
"points, close, expected_edges",
[
(((0, 0, 0), (0, 1, 0), (1, 0, 0)), False, 2),
(((0, 0, 0), (0, 1, 0), (1, 0, 0)), True, 3),
(((0, 0, 0), (0, 1, 0), (1, 0, 0), (0, 0, 0)), False, 3),
(((0, 0, 0), (0, 1, 0), (1, 0, 0), (0, 0, 0)), True, 3),
],
)
deftest_wire_makepolygon(points, close, expected_edges):
assertlen(Wire.makePolygon(points, False, close).Edges()) ==expected_edges
deftest_equality():
# do not raise error comparing with other type
assert (Vector(0, 0, 0) ==0) ==False
assert (Plane.XY() ==0) ==False
list1= [
Vector(0, 0, 0),
Plane.XY(),
Vertex.makeVertex(0, 0, 0),
"a string",
4,
]
assert [list1.index(item) foriteminlist1] == [0, 1, 2, 3, 4]
if__name__=="__main__":
unittest.main()