/
TestDraft.py
1419 lines (1216 loc) · 50 KB
/
TestDraft.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
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
"""Unit test for the Draft module.
From the terminal, run the following:
FreeCAD -t TestDraft
From within FreeCAD, run the following:
import Test, TestDraft
Test.runTestsFromModule(TestDraft)
"""
# ***************************************************************************
# * (c) 2013 Yorik van Havre <yorik@uncreated.net> *
# * (c) 2019 Eliud Cabrera Castillo *
# * *
# * This file is part of the FreeCAD CAx development system. *
# * *
# * This program is free software; you can redistribute it and/or modify *
# * it under the terms of the GNU Lesser General Public License (LGPL) *
# * as published by the Free Software Foundation; either version 2 of *
# * the License, or (at your option) any later version. *
# * for detail see the LICENCE text file. *
# * *
# * FreeCAD is distributed in the hope that it will be useful, *
# * but WITHOUT ANY WARRANTY; without even the implied warranty of *
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
# * GNU Library General Public License for more details. *
# * *
# * You should have received a copy of the GNU Library General Public *
# * License along with FreeCAD; if not, write to the Free Software *
# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
# * USA *
# * *
# ***************************************************************************
# ===========================================================================
# First the command to run the test from the operating system terminal,
# followed by the command to run the test from the Python console
# within FreeCAD.
# ===========================================================================
# Run all Draft tests
# ----
# FreeCAD -t TestDraft
#
# import Test, TestDraft
# Test.runTestsFromModule(TestDraft)
#
# ===========================================================================
# Run tests from a module
# ----
# FreeCAD -t TestDraft
#
# import Test, TestDraft
# Test.runTestsFromModule(TestDraft)
#
# ===========================================================================
# Run tests from a class
# ----
# FreeCAD -t TestDraft.DraftCreation
#
# import Test, TestDraft
# Test.runTestsFromClass(TestDraft.DraftCreation)
#
# ===========================================================================
# Run a specific test
# ----
# FreeCAD -t TestDraft.DraftCreation.test_line
#
# import unittest
# one_test = "TestDraft.DraftCreation.test_line"
# all_tests = unittest.TestLoader().loadTestsFromName(one_test)
# unittest.TextTestRunner().run(all_tests)
# ===========================================================================
import os
import unittest
import FreeCAD as App
import FreeCADGui as Gui
import Draft
from FreeCAD import Vector
def _msg(text, end="\n"):
App.Console.PrintMessage(text + end)
def _wrn(text, end="\n"):
App.Console.PrintWarning(text + end)
def _log(text, end="\n"):
App.Console.PrintLog(text + end)
def _draw_header():
_msg("")
_msg(78*"-")
def _import_test(module):
_msg(" Try importing '{}'".format(module))
try:
imported = __import__("{}".format(module))
except ImportError as exc:
imported = False
_msg(" {}".format(exc))
return imported
def _no_gui(module):
_msg(" #-----------------------------------------------------#\n"
" # No GUI; cannot test for '{}'\n"
" #-----------------------------------------------------#\n"
" Automatic PASS".format(module))
def _no_test():
_msg(" #-----------------------------------------------------#\n"
" # This test is not implemented currently\n"
" #-----------------------------------------------------#\n"
" Automatic PASS")
def _fake_function(p1=None, p2=None, p3=None, p4=None, p5=None):
_msg(" Arguments to placeholder function")
_msg(" p1={0}; p2={1}".format(p1, p2))
_msg(" p3={0}; p4={1}".format(p3, p4))
_msg(" p5={}".format(p5))
_no_test()
return True
class DraftImport(unittest.TestCase):
"""Import the Draft modules."""
# No document is needed to test 'import Draft' or other modules
# thus 'setUp' just draws a line, and 'tearDown' isn't defined.
def setUp(self):
_draw_header()
def test_import_draft(self):
"""Import the Draft module."""
module = "Draft"
imported = _import_test(module)
self.assertTrue(imported, "Problem importing '{}'".format(module))
def test_import_draft_geomutils(self):
"""Import Draft geometrical utilities."""
module = "DraftGeomUtils"
imported = _import_test(module)
self.assertTrue(imported, "Problem importing '{}'".format(module))
def test_import_draft_vecutils(self):
"""Import Draft vector utilities."""
module = "DraftVecUtils"
imported = _import_test(module)
self.assertTrue(imported, "Problem importing '{}'".format(module))
def test_import_draft_svg(self):
"""Import Draft SVG utilities."""
module = "getSVG"
imported = _import_test(module)
self.assertTrue(imported, "Problem importing '{}'".format(module))
class DraftGuiImport(unittest.TestCase):
"""Import the Draft graphical modules."""
# No document is needed to test 'import DraftGui' or other modules
# thus 'setUp' just draws a line, and 'tearDown' isn't defined.
def setUp(self):
_draw_header()
def test_import_gui_draftgui(self):
"""Import Draft TaskView GUI tools."""
module = "DraftGui"
if not App.GuiUp:
_no_gui(module)
self.assertTrue(True)
return
imported = _import_test(module)
self.assertTrue(imported, "Problem importing '{}'".format(module))
def test_import_gui_draft_snap(self):
"""Import Draft snapping."""
module = "DraftSnap"
if not App.GuiUp:
_no_gui(module)
self.assertTrue(True)
return
imported = _import_test(module)
self.assertTrue(imported, "Problem importing '{}'".format(module))
def test_import_gui_draft_tools(self):
"""Import Draft graphical commands."""
module = "DraftTools"
if not App.GuiUp:
_no_gui(module)
self.assertTrue(True)
return
imported = _import_test(module)
self.assertTrue(imported, "Problem importing '{}'".format(module))
def test_import_gui_draft_trackers(self):
"""Import Draft tracker utilities."""
module = "DraftTrackers"
if not App.GuiUp:
_no_gui(module)
self.assertTrue(True)
return
imported = _import_test(module)
self.assertTrue(imported, "Problem importing '{}'".format(module))
class DraftImportTools(unittest.TestCase):
"""Test for each individual module that defines a tool."""
# No document is needed to test 'import' of other modules
# thus 'setUp' just draws a line, and 'tearDown' isn't defined.
def setUp(self):
_draw_header()
def test_import_gui_draftedit(self):
"""Import Draft Edit."""
module = "DraftEdit"
if not App.GuiUp:
_no_gui(module)
self.assertTrue(True)
return
imported = _import_test(module)
self.assertTrue(imported, "Problem importing '{}'".format(module))
def test_import_gui_draftfillet(self):
"""Import Draft Fillet."""
module = "DraftFillet"
if not App.GuiUp:
_no_gui(module)
self.assertTrue(True)
return
imported = _import_test(module)
self.assertTrue(imported, "Problem importing '{}'".format(module))
def test_import_gui_draftlayer(self):
"""Import Draft Layer."""
module = "DraftLayer"
if not App.GuiUp:
_no_gui(module)
self.assertTrue(True)
return
imported = _import_test(module)
self.assertTrue(imported, "Problem importing '{}'".format(module))
def test_import_gui_draftplane(self):
"""Import Draft SelectPlane."""
module = "DraftSelectPlane"
if not App.GuiUp:
_no_gui(module)
self.assertTrue(True)
return
imported = _import_test(module)
self.assertTrue(imported, "Problem importing '{}'".format(module))
def test_import_gui_workingplane(self):
"""Import Draft WorkingPlane."""
module = "WorkingPlane"
if not App.GuiUp:
_no_gui(module)
self.assertTrue(True)
return
imported = _import_test(module)
self.assertTrue(imported, "Problem importing '{}'".format(module))
class DraftPivy(unittest.TestCase):
"""Test for the presence of Pivy and that it works with Coin3D."""
def setUp(self):
"""Set up a new document to hold the tests.
This is executed before every test, so we create a document
to hold the objects.
"""
_draw_header()
self.doc_name = self.__class__.__name__
if App.ActiveDocument:
if App.ActiveDocument.Name != self.doc_name:
App.newDocument(self.doc_name)
else:
App.newDocument(self.doc_name)
App.setActiveDocument(self.doc_name)
self.doc = App.ActiveDocument
_msg(" Temporary document '{}'".format(self.doc_name))
def test_pivy_import(self):
"""Import Pivy Coin."""
module = "pivy.coin"
imported = _import_test(module)
self.assertTrue(imported, "Problem importing '{}'".format(module))
def test_pivy_draw(self):
"""Use Coin (pivy.coin) to draw a cube on the active view."""
module = "pivy.coin"
if not App.GuiUp:
_no_gui(module)
self.assertTrue(True)
return
import pivy.coin
cube = pivy.coin.SoCube()
_msg(" Draw cube")
Gui.ActiveDocument.ActiveView.getSceneGraph().addChild(cube)
_msg(" Adding cube to the active view scene")
self.assertTrue(cube, "Pivy is not working properly.")
def tearDown(self):
"""Finish the test.
This is executed after each test, so we close the document.
"""
App.closeDocument(self.doc_name)
class DraftCreation(unittest.TestCase):
"""Test Draft creation functions."""
def setUp(self):
"""Set up a new document to hold the tests.
This is executed before every test, so we create a document
to hold the objects.
"""
_draw_header()
self.doc_name = self.__class__.__name__
if App.ActiveDocument:
if App.ActiveDocument.Name != self.doc_name:
App.newDocument(self.doc_name)
else:
App.newDocument(self.doc_name)
App.setActiveDocument(self.doc_name)
self.doc = App.ActiveDocument
_msg(" Temporary document '{}'".format(self.doc_name))
def test_line(self):
"""Create a line."""
operation = "Draft Line"
_msg(" Test '{}'".format(operation))
a = Vector(0, 0, 0)
b = Vector(2, 0, 0)
_msg(" a={0}, b={1}".format(a, b))
obj = Draft.makeLine(a, b)
self.assertTrue(obj, "'{}' failed".format(operation))
def test_polyline(self):
"""Create a polyline."""
operation = "Draft Wire"
_msg(" Test '{}'".format(operation))
a = Vector(0, 0, 0)
b = Vector(2, 0, 0)
c = Vector(2, 2, 0)
_msg(" a={0}, b={1}".format(a, b))
_msg(" c={}".format(c))
obj = Draft.makeWire([a, b, c])
self.assertTrue(obj, "'{}' failed".format(operation))
def test_fillet(self):
"""Create two lines, and a fillet between them."""
operation = "Draft Fillet"
_msg(" Test '{}'".format(operation))
a = Vector(0, 0, 0)
b = Vector(8, 0, 0)
c = Vector(8, 8, 0)
_msg(" Lines")
_msg(" a={0}, b={1}".format(a, b))
_msg(" b={0}, c={1}".format(b, c))
L1 = Draft.makeLine(a, b)
L2 = Draft.makeLine(b, c)
App.ActiveDocument.recompute()
if not App.GuiUp:
_no_gui("DraftFillet")
self.assertTrue(True)
return
import DraftFillet
radius = 4
_msg(" Fillet")
_msg(" radius={}".format(radius))
obj = DraftFillet.makeFillet([L1, L2], radius)
self.assertTrue(obj, "'{}' failed".format(operation))
def test_circle(self):
"""Create a circle."""
operation = "Draft Circle"
_msg(" Test '{}'".format(operation))
radius = 3
_msg(" radius={}".format(radius))
obj = Draft.makeCircle(radius)
self.assertTrue(obj, "'{}' failed".format(operation))
def test_arc(self):
"""Create a circular arc."""
operation = "Draft Arc"
_msg(" Test '{}'".format(operation))
radius = 2
start_angle = 0
end_angle = 90
_msg(" radius={}".format(radius))
_msg(" startangle={0}, endangle={1}".format(start_angle,
end_angle))
obj = Draft.makeCircle(radius,
start_angle, end_angle)
self.assertTrue(obj, "'{}' failed".format(operation))
def test_arc_3points(self):
"""Create a circular arc from three points."""
operation = "Draft Arc 3Points"
_msg(" Test '{}'".format(operation))
a = Vector(5, 0, 0)
b = Vector(4, 3, 0)
c = Vector(0, 5, 0)
_msg(" a={0}, b={1}".format(a, b))
_msg(" c={}".format(c))
Draft.make_arc_3points = _fake_function
obj = Draft.make_arc_3points(a, b, c)
self.assertTrue(obj, "'{}' failed".format(operation))
def test_ellipse(self):
"""Create an ellipse."""
operation = "Draft Ellipse"
_msg(" Test '{}'".format(operation))
a = 5
b = 3
_msg(" major_axis={0}, minor_axis={1}".format(a, b))
obj = Draft.makeEllipse(a, b)
self.assertTrue(obj, "'{}' failed".format(operation))
def test_polygon(self):
"""Create a regular polygon."""
operation = "Draft Polygon"
_msg(" Test '{}'".format(operation))
n_faces = 6
radius = 5
_msg(" n_faces={0}, radius={1}".format(n_faces, radius))
obj = Draft.makePolygon(n_faces, radius)
self.assertTrue(obj, "'{}' failed".format(operation))
def test_rectangle(self):
"""Create a rectangle."""
operation = "Draft Rectangle"
_msg(" Test '{}'".format(operation))
length = 5
width = 2
_msg(" length={0}, width={1}".format(length, width))
obj = Draft.makeRectangle(length, width)
self.assertTrue(obj, "'{}' failed".format(operation))
def test_text(self):
"""Create a text object."""
operation = "Draft Text"
_msg(" Test '{}'".format(operation))
text = "Testing testing"
_msg(" text='{}'".format(text))
obj = Draft.makeText(text)
self.assertTrue(obj, "'{}' failed".format(operation))
def test_dimension_linear(self):
"""Create a linear dimension."""
operation = "Draft Dimension"
_msg(" Test '{}'".format(operation))
_msg(" Occasionally crashes")
a = Vector(0, 0, 0)
b = Vector(9, 0, 0)
c = Vector(4, -1, 0)
_msg(" a={0}, b={1}".format(a, b))
_msg(" c={}".format(c))
obj = Draft.makeDimension(a, b, c)
self.assertTrue(obj, "'{}' failed".format(operation))
def test_dimension_radial(self):
"""Create a radial dimension. NOT IMPLEMENTED CURRENTLY."""
operation = "Draft Dimension Radial"
_msg(" Test '{}'".format(operation))
a = Vector(5, 0, 0)
b = Vector(4, 3, 0)
c = Vector(0, 5, 0)
_msg(" a={0}, b={1}".format(a, b))
_msg(" c={}".format(c))
Draft.make_dimension_radial = _fake_function
obj = Draft.make_dimension_radial(a, b, c)
self.assertTrue(obj, "'{}' failed".format(operation))
def test_bspline(self):
"""Create a BSpline of three points."""
operation = "Draft BSpline"
_msg(" Test '{}'".format(operation))
a = Vector(0, 0, 0)
b = Vector(2, 0, 0)
c = Vector(2, 2, 0)
_msg(" a={0}, b={1}".format(a, b))
_msg(" c={}".format(c))
obj = Draft.makeBSpline([a, b, c])
self.assertTrue(obj, "'{}' failed".format(operation))
def test_point(self):
"""Create a point."""
operation = "Draft Point"
_msg(" Test '{}'".format(operation))
p = Vector(5, 3, 2)
_msg(" p.x={0}, p.y={1}, p.z={2}".format(p.x, p.y, p.z))
obj = Draft.makePoint(p.x, p.y, p.z)
self.assertTrue(obj, "'{}' failed".format(operation))
def test_shapestring(self):
"""Create a ShapeString. NOT IMPLEMENTED CURRENTLY."""
operation = "Draft ShapeString"
_msg(" Test '{}'".format(operation))
_msg(" In order to test this, a font file is needed.")
text = "Testing Shapestring "
font = None # TODO: get a font file here
_msg(" text='{0}', font='{1}'".format(text, font))
Draft.makeShapeString = _fake_function
obj = Draft.makeShapeString("Text", font)
# Draft.makeShapeString("Text", FontFile="")
self.assertTrue(obj, "'{}' failed".format(operation))
def test_facebinder(self):
"""Create a box, and then a facebinder from its faces."""
operation = "Draft Facebinder"
_msg(" Test '{}'".format(operation))
_msg(" In order to test this, a selection is needed")
_msg(" or an App::PropertyLinkSubList")
_msg(" Box")
box = App.ActiveDocument.addObject("Part::Box")
App.ActiveDocument.recompute()
# The facebinder function accepts a Gui selection set,
# or a 'PropertyLinkSubList'
# Gui selection set only works when the graphical interface is up
# Gui.Selection.addSelection(box, ('Face1', 'Face6'))
# selection_set = Gui.Selection.getSelectionEx()
# elements = selection_set[0].SubElementNames
# PropertyLinkSubList
selection_set = [(box, ("Face1", "Face6"))]
elements = selection_set[0][1]
_msg(" object='{0}' ({1})".format(box.Shape.ShapeType, box.TypeId))
_msg(" sub-elements={}".format(elements))
obj = Draft.makeFacebinder(selection_set)
self.assertTrue(obj, "'{}' failed".format(operation))
def test_cubicbezcurve(self):
"""Create a cubic bezier curve of four points."""
operation = "Draft CubBezCurve"
_msg(" Test '{}'".format(operation))
a = Vector(0, 0, 0)
b = Vector(2, 2, 0)
c = Vector(5, 3, 0)
d = Vector(9, 0, 0)
_msg(" a={0}, b={1}".format(a, b))
_msg(" c={0}, d={1}".format(c, d))
obj = Draft.makeBezCurve([a, b, c, d], degree=3)
self.assertTrue(obj, "'{}' failed".format(operation))
def test_bezcurve(self):
"""Create a bezier curve of six points, degree five."""
operation = "Draft BezCurve"
_msg(" Test '{}'".format(operation))
a = Vector(0, 0, 0)
b = Vector(2, 2, 0)
c = Vector(5, 3, 0)
d = Vector(9, 0, 0)
e = Vector(12, 5, 0)
f = Vector(12, 8, 0)
_msg(" a={0}, b={1}".format(a, b))
_msg(" c={0}, d={1}".format(c, d))
_msg(" e={0}, f={1}".format(e, f))
obj = Draft.makeBezCurve([a, b, c, d, e, f])
self.assertTrue(obj, "'{}' failed".format(operation))
def test_label(self):
"""Create a label."""
operation = "Draft Label"
_msg(" Test '{}'".format(operation))
_msg(" Occasionally crashes")
target_point = Vector(0, 0, 0)
distance = -25
placement = App.Placement(Vector(50, 50, 0), App.Rotation())
_msg(" target_point={0}, "
"distance={1}".format(target_point, distance))
_msg(" placement={}".format(placement))
obj = Draft.makeLabel(targetpoint=target_point,
distance=distance,
placement=placement)
App.ActiveDocument.recompute()
self.assertTrue(obj, "'{}' failed".format(operation))
def tearDown(self):
"""Finish the test.
This is executed after each test, so we close the document.
"""
App.closeDocument(self.doc_name)
class DraftModification(unittest.TestCase):
"""Test Draft modification tools."""
def setUp(self):
"""Set up a new document to hold the tests.
This is executed before every test, so we create a document
to hold the objects.
"""
_draw_header()
self.doc_name = self.__class__.__name__
if App.ActiveDocument:
if App.ActiveDocument.Name != self.doc_name:
App.newDocument(self.doc_name)
else:
App.newDocument(self.doc_name)
App.setActiveDocument(self.doc_name)
self.doc = App.ActiveDocument
_msg(" Temporary document '{}'".format(self.doc_name))
def test_move(self):
"""Create a line and move it."""
operation = "Draft Move"
_msg(" Test '{}'".format(operation))
a = Vector(0, 2, 0)
b = Vector(2, 2, 0)
_msg(" Line")
_msg(" a={0}, b={1}".format(a, b))
obj = Draft.makeLine(a, b)
c = Vector(3, 1, 0)
_msg(" Translation vector")
_msg(" c={}".format(c))
Draft.move(obj, c)
self.assertTrue(obj.Start == Vector(3, 3, 0),
"'{}' failed".format(operation))
def test_copy(self):
"""Create a line, then copy and move it."""
operation = "Draft Move with copy"
_msg(" Test '{}'".format(operation))
a = Vector(0, 3, 0)
b = Vector(2, 3, 0)
_msg(" Line")
_msg(" a={0}, b={1}".format(a, b))
line = Draft.makeLine(a, b)
c = Vector(2, 2, 0)
_msg(" Translation vector (copy)")
_msg(" c={}".format(c))
obj = Draft.move(line, c, copy=True)
self.assertTrue(obj, "'{}' failed".format(operation))
def test_rotate(self):
"""Create a line, then rotate it."""
operation = "Draft Rotate"
_msg(" Test '{}'".format(operation))
a = Vector(1, 1, 0)
b = Vector(3, 1, 0)
_msg(" Line")
_msg(" a={0}, b={1}".format(a, b))
obj = Draft.makeLine(a, b)
App.ActiveDocument.recompute()
c = Vector(-1, 1, 0)
rot = 90
_msg(" Rotation")
_msg(" angle={} degrees".format(rot))
Draft.rotate(obj, rot)
self.assertTrue(obj.Start.isEqual(c, 1e-12),
"'{}' failed".format(operation))
def test_offset(self):
"""Create a rectangle, then produce an offset copy."""
operation = "Draft Offset"
_msg(" Test '{}'".format(operation))
length = 4
width = 2
_msg(" Rectangle")
_msg(" length={0}, width={1}".format(length, width))
rect = Draft.makeRectangle(length, width)
App.ActiveDocument.recompute()
offset = Vector(-1, -1, 0)
_msg(" Offset")
_msg(" vector={}".format(offset))
obj = Draft.offset(rect, offset, copy=True)
self.assertTrue(obj, "'{}' failed".format(operation))
def test_trim(self):
"""Trim a line. NOT IMPLEMENTED."""
operation = "Draft Trimex trim"
_msg(" Test '{}'".format(operation))
a = Vector(0, 0, 0)
b = Vector(3, 3, 0)
_msg(" Line")
_msg(" a={0}, b={1}".format(a, b))
line = Draft.makeLine(a, b)
c = Vector(2, 2, 0)
d = Vector(4, 2, 0)
_msg(" Line 2")
_msg(" c={0}, d={1}".format(c, d))
line2 = Draft.makeLine(c, d)
App.ActiveDocument.recompute()
Draft.trim_objects = _fake_function
obj = Draft.trim_objects(line, line2)
self.assertTrue(obj, "'{}' failed".format(operation))
def test_extend(self):
"""Extend a line. NOT IMPLEMENTED."""
operation = "Draft Trimex extend"
_msg(" Test '{}'".format(operation))
a = Vector(0, 0, 0)
b = Vector(1, 1, 0)
_msg(" Line")
_msg(" a={0}, b={1}".format(a, b))
line = Draft.makeLine(a, b)
c = Vector(2, 2, 0)
d = Vector(4, 2, 0)
_msg(" Line 2")
_msg(" c={0}, d={1}".format(c, d))
line2 = Draft.makeLine(c, d)
App.ActiveDocument.recompute()
Draft.extrude = _fake_function
obj = Draft.extrude(line, line2)
self.assertTrue(obj, "'{}' failed".format(operation))
def test_join(self):
"""Join two lines into a single Draft Wire."""
operation = "Draft Join"
_msg(" Test '{}'".format(operation))
a = Vector(0, 0, 0)
b = Vector(2, 2, 0)
c = Vector(2, 4, 0)
_msg(" Line 1")
_msg(" a={0}, b={1}".format(a, b))
_msg(" Line 2")
_msg(" b={0}, c={1}".format(b, c))
line_1 = Draft.makeLine(a, b)
line_2 = Draft.makeLine(b, c)
# obj = Draft.joinWires([line_1, line_2]) # Multiple wires
obj = Draft.joinTwoWires(line_1, line_2)
self.assertTrue(obj, "'{}' failed".format(operation))
def test_split(self):
"""Split a Draft Wire into two Draft Wires."""
operation = "Draft_Split"
_msg(" Test '{}'".format(operation))
a = Vector(0, 0, 0)
b = Vector(2, 2, 0)
c = Vector(2, 4, 0)
d = Vector(6, 4, 0)
_msg(" Wire")
_msg(" a={0}, b={1}".format(a, b))
_msg(" c={0}, d={1}".format(c, d))
wire = Draft.makeWire([a, b, c, d])
index = 1
_msg(" Split at")
_msg(" p={0}, index={1}".format(b, index))
obj = Draft.split(wire, b, index)
# TODO: split needs to be modified so that it returns True or False.
# Then checking for Wire001 is not needed
if App.ActiveDocument.Wire001:
obj = True
self.assertTrue(obj, "'{}' failed".format(operation))
def test_upgrade(self):
"""Upgrade two Draft Lines into a closed Draft Wire."""
operation = "Draft Upgrade"
_msg(" Test '{}'".format(operation))
a = Vector(0, 0, 0)
b = Vector(2, 2, 0)
c = Vector(2, 4, 0)
_msg(" Line 1")
_msg(" a={0}, b={1}".format(a, b))
_msg(" Line 2")
_msg(" b={0}, c={1}".format(b, c))
line_1 = Draft.makeLine(a, b)
line_2 = Draft.makeLine(b, c)
App.ActiveDocument.recompute()
obj = Draft.upgrade([line_1, line_2], delete=True)
App.ActiveDocument.recompute()
s = obj[0][0]
_msg(" 1: Result '{0}' ({1})".format(s.Shape.ShapeType, s.TypeId))
self.assertTrue(bool(obj[0]), "'{}' failed".format(operation))
obj2 = Draft.upgrade(obj[0], delete=True)
App.ActiveDocument.recompute()
s2 = obj2[0][0]
_msg(" 2: Result '{0}' ({1})".format(s2.Shape.ShapeType,
s2.TypeId))
self.assertTrue(bool(obj2[0]), "'{}' failed".format(operation))
obj3 = Draft.upgrade(obj2[0], delete=True)
App.ActiveDocument.recompute()
s3 = obj3[0][0]
_msg(" 3: Result '{0}' ({1})".format(s3.Shape.ShapeType, s3.TypeId))
self.assertTrue(bool(obj3[0]), "'{}' failed".format(operation))
obj4 = Draft.upgrade(obj3[0], delete=True)
App.ActiveDocument.recompute()
wire = App.ActiveDocument.Wire
_msg(" 4: Result '{0}' ({1})".format(wire.Proxy.Type, wire.TypeId))
_msg(" The last object cannot be upgraded further")
self.assertFalse(bool(obj4[0]), "'{}' failed".format(operation))
def test_downgrade(self):
"""Downgrade a closed Draft Wire into three simple Part Edges."""
operation = "Draft Downgrade"
_msg(" Test '{}'".format(operation))
a = Vector(0, 0, 0)
b = Vector(2, 2, 0)
c = Vector(2, 4, 0)
_msg(" Closed wire")
_msg(" a={0}, b={1}".format(a, b))
_msg(" c={0}, a={1}".format(c, a))
wire = Draft.makeWire([a, b, c, a])
App.ActiveDocument.recompute()
obj = Draft.downgrade(wire, delete=True)
App.ActiveDocument.recompute()
s = obj[0][0]
_msg(" 1: Result '{0}' ({1})".format(s.Shape.ShapeType, s.TypeId))
self.assertTrue(bool(obj[0]), "'{}' failed".format(operation))
obj2 = Draft.downgrade(obj[0], delete=True)
App.ActiveDocument.recompute()
s2 = obj2[0][0]
_msg(" 2: Result '{0}' ({1})".format(s2.Shape.ShapeType, s2.TypeId))
self.assertTrue(bool(obj2[0]), "'{}' failed".format(operation))
obj3 = Draft.downgrade(obj2[0], delete=True)
App.ActiveDocument.recompute()
s3 = obj3[0][0]
_msg(" 3: Result 3 x '{0}' ({1})".format(s3.Shape.ShapeType,
s3.TypeId))
self.assertTrue(len(obj3[0]) == 3, "'{}' failed".format(operation))
obj4 = Draft.downgrade(obj3[0], delete=True)
App.ActiveDocument.recompute()
s4 = obj4[0]
_msg(" 4: Result '{}'".format(s4))
_msg(" The last objects cannot be downgraded further")
self.assertFalse(bool(obj4[0]), "'{}' failed".format(operation))
def test_wire_to_bspline(self):
"""Convert a polyline to BSpline and back."""
operation = "Draft WireToBSpline"
_msg(" Test '{}'".format(operation))
a = Vector(0, 0, 0)
b = Vector(2, 2, 0)
c = Vector(2, 4, 0)
_msg(" Wire")
_msg(" a={0}, b={1}".format(a, b))
_msg(" c={}".format(c))
wire = Draft.makeWire([a, b, c])
obj = Draft.makeBSpline(wire.Points)
App.ActiveDocument.recompute()
_msg(" 1: Result '{0}' ({1})".format(obj.Proxy.Type, obj.TypeId))
self.assertTrue(obj, "'{}' failed".format(operation))
obj2 = Draft.makeWire(obj.Points)
_msg(" 2: Result '{0}' ({1})".format(obj2.Proxy.Type, obj2.TypeId))
self.assertTrue(obj2, "'{}' failed".format(operation))
def test_shape_2d_view(self):
"""Create a prism and then a 2D projection of it."""
operation = "Draft Shape2DView"
_msg(" Test '{}'".format(operation))
prism = App.ActiveDocument.addObject("Part::Prism")
prism.Polygon = 5
# Rotate the prism 45 degrees around the Y axis
prism.Placement.Rotation.Axis = Vector(0, 1, 0)
prism.Placement.Rotation.Angle = 45 * (3.14159/180)
_msg(" Prism")
_msg(" n_sides={}".format(prism.Polygon))
_msg(" placement={}".format(prism.Placement))
direction = Vector(0, 0, 1)
_msg(" Projection 2D view")
_msg(" direction={}".format(direction))
obj = Draft.makeShape2DView(prism, direction)
self.assertTrue(obj, "'{}' failed".format(operation))
def test_draft_to_sketch(self):
"""Convert a Draft object to a Sketch and back."""
operation = "Draft Draft2Sketch"
_msg(" Test '{}'".format(operation))
a = Vector(0, 0, 0)
b = Vector(2, 2, 0)
c = Vector(2, 4, 0)
_msg(" Wire")
_msg(" a={0}, b={1}".format(a, b))
_msg(" c={}".format(c))
wire = Draft.makeWire([a, b, c])
App.ActiveDocument.recompute()
obj = Draft.makeSketch(wire, autoconstraints=True)
App.ActiveDocument.recompute()
_msg(" 1: Result '{0}' ({1})".format(obj.Shape.ShapeType,
obj.TypeId))
self.assertTrue(obj, "'{}' failed".format(operation))
obj2 = Draft.draftify(obj, delete=False)
App.ActiveDocument.recompute()
_msg(" 2: Result '{0}' ({1})".format(obj2.Proxy.Type,
obj2.TypeId))
self.assertTrue(obj2, "'{}' failed".format(operation))
def test_rectangular_array(self):
"""Create a rectangle, and a rectangular array."""
operation = "Draft Array"
_msg(" Test '{}'".format(operation))
length = 4
width = 2
_msg(" Rectangle")
_msg(" length={0}, width={1}".format(length, width))
rect = Draft.makeRectangle(length, width)
App.ActiveDocument.recompute()
dir_x = Vector(5, 0, 0)
dir_y = Vector(0, 4, 0)
number_x = 3
number_y = 4
_msg(" Array")
_msg(" direction_x={}".format(dir_x))
_msg(" direction_y={}".format(dir_y))
_msg(" number_x={0}, number_y={1}".format(number_x, number_y))
obj = Draft.makeArray(rect,
dir_x, dir_y,
number_x, number_y)
self.assertTrue(obj, "'{}' failed".format(operation))
def test_polar_array(self):
"""Create a rectangle, and a polar array."""
operation = "Draft PolarArray"
_msg(" Test '{}'".format(operation))
length = 4
width = 2
_msg(" Rectangle")
_msg(" length={0}, width={1}".format(length, width))
rect = Draft.makeRectangle(length, width)
App.ActiveDocument.recompute()
center = Vector(-4, 0, 0)
angle = 180
number = 5
_msg(" Array")
_msg(" center={}".format(center))
_msg(" polar_angle={0}, number={1}".format(angle, number))
obj = Draft.makeArray(rect,
center, angle, number)
self.assertTrue(obj, "'{}' failed".format(operation))
def test_circular_array(self):
"""Create a rectangle, and a circular array."""
operation = "Draft CircularArray"
_msg(" Test '{}'".format(operation))
length = 4
width = 2
_msg(" Rectangle")
_msg(" length={0}, width={1}".format(length, width))
rect = Draft.makeRectangle(length, width)
App.ActiveDocument.recompute()
rad_distance = 10
tan_distance = 8
axis = Vector(0, 0, 1)
center = Vector(0, 0, 0)
number = 3
symmetry = 1
_msg(" Array")
_msg(" radial_distance={0}, "
"tangential_distance={1}".format(rad_distance, tan_distance))
_msg(" axis={}".format(axis))
_msg(" center={}".format(center))
_msg(" number={0}, symmetry={1}".format(number, symmetry))
obj = Draft.makeArray(rect,
rad_distance, tan_distance,
axis, center,
number, symmetry)
self.assertTrue(obj, "'{}' failed".format(operation))
def test_path_array(self):
"""Create a wire, a polygon, and a path array."""
operation = "Draft PathArray"
_msg(" Test '{}'".format(operation))
a = Vector(0, 0, 0)
b = Vector(2, 2, 0)
c = Vector(2, 4, 0)
d = Vector(8, 4, 0)