27
import Tests.PathTestUtils as PathTestUtils
28
from Path.Post.scripts import refactored_test_post as postprocessor
31
Path.Log.setLevel(Path.Log.Level.DEBUG, Path.Log.thisModule())
32
Path.Log.trackModule(Path.Log.thisModule())
35
class TestRefactoredTestPost(PathTestUtils.PathTestBase):
40
This method is called upon instantiation of this test class. Add code
41
and objects here that are needed for the duration of the test() methods
42
in this class. In other words, set up the 'global' test environment
43
here; use the `setUp()` method to set up a 'local' test environment.
44
This method does not have access to the class `self` reference, but it
45
is able to call static methods within this same class.
48
FreeCAD.newDocument("Unnamed")
51
def tearDownClass(cls):
54
This method is called prior to destruction of this test class. Add
55
code and objects here that cleanup the test environment after the
56
test() methods in this class have been executed. This method does
57
not have access to the class `self` reference. This method is able
58
to call static methods within this same class.
61
FreeCAD.closeDocument(FreeCAD.ActiveDocument.Name)
68
This method is called prior to each `test()` method. Add code and
69
objects here that are needed for multiple `test()` methods.
72
self.doc = FreeCAD.ActiveDocument
73
self.con = FreeCAD.Console
74
self.docobj = FreeCAD.ActiveDocument.addObject("Path::Feature", "testpath")
78
postprocessor.UNITS = "G21"
79
postprocessor.init_values(postprocessor.global_values)
84
This method is called after each test() method. Add cleanup instructions here.
85
Such cleanup instructions will likely undo those in the setUp() method.
87
FreeCAD.ActiveDocument.removeObject("testpath")
89
def single_compare(self, path, expected, args, debug=False):
90
"""Perform a test with a single comparison."""
92
self.docobj.Path = Path.Path(path)
93
postables = [self.docobj]
94
gcode = postprocessor.export(postables, "-", args)
96
print(f"--------{nl}{gcode}--------{nl}")
97
self.assertEqual(gcode, expected)
99
def compare_third_line(self, path_string, expected, args, debug=False):
100
"""Perform a test with a single comparison to the third line of the output."""
103
self.docobj.Path = Path.Path([Path.Command(path_string)])
105
self.docobj.Path = Path.Path([])
106
postables = [self.docobj]
107
gcode = postprocessor.export(postables, "-", args)
109
print(f"--------{nl}{gcode}--------{nl}")
110
self.assertEqual(gcode.splitlines()[2], expected)
126
Suppress the axis coordinate if the same as previous
128
c = Path.Command("G0 X10 Y20 Z30")
129
c1 = Path.Command("G0 X10 Y30 Z30")
131
self.docobj.Path = Path.Path([c, c1])
132
postables = [self.docobj]
134
args = "--axis-modal"
135
gcode = postprocessor.export(postables, "-", args)
137
self.assertEqual(gcode.splitlines()[3], "G0 Y30.000")
139
args = "--no-axis-modal"
140
gcode = postprocessor.export(postables, "-", args)
142
self.assertEqual(gcode.splitlines()[3], "G0 X10.000 Y30.000 Z30.000")
147
"""Test axis-precision."""
148
self.compare_third_line("G0 X10 Y20 Z30", "G0 X10.00 Y20.00 Z30.00", "--axis-precision=2")
158
(Block-name: testpath)
161
(Block-name: post_amble)
179
c = Path.Command("(comment)")
180
self.docobj.Path = Path.Path([c])
181
postables = [self.docobj]
183
gcode = postprocessor.export(postables, "-", args)
185
self.assertEqual(gcode.splitlines()[4], "(comment)")
190
"""Test feed-precision."""
192
c = Path.Command("G1 X10 Y20 Z30 F123.123456")
194
self.docobj.Path = Path.Path([c])
195
postables = [self.docobj]
198
gcode = postprocessor.export(postables, "-", args)
202
self.assertEqual(gcode.splitlines()[2], "G1 X10.000 Y20.000 Z30.000 F7387.407")
204
args = "--feed-precision=2"
205
gcode = postprocessor.export(postables, "-", args)
209
self.assertEqual(gcode.splitlines()[2], "G1 X10.000 Y20.000 Z30.000 F7387.41")
214
"""Test output with an empty path.
216
Also tests the interactions between --comments and --header.
218
self.docobj.Path = Path.Path([])
219
postables = [self.docobj]
224
args = "--comments --header"
225
gcode = postprocessor.export(postables, "-", args)
227
self.assertEqual(gcode.splitlines()[0], "(Exported by FreeCAD)")
229
gcode.splitlines()[1],
230
"(Post Processor: Path.Post.scripts.refactored_test_post)",
232
self.assertEqual(gcode.splitlines()[2], "(Cam File: )")
233
self.assertIn("(Output Time: ", gcode.splitlines()[3])
234
self.assertTrue(len(gcode.splitlines()[3]) == 41)
235
self.assertEqual(gcode.splitlines()[4], "(Begin preamble)")
236
self.assertEqual(gcode.splitlines()[5], "G90")
237
self.assertEqual(gcode.splitlines()[6], "G21")
238
self.assertEqual(gcode.splitlines()[7], "(Begin operation)")
239
self.assertEqual(gcode.splitlines()[8], "(Finish operation: testpath)")
240
self.assertEqual(gcode.splitlines()[9], "(Begin postamble)")
243
expected = """(Begin preamble)
247
(Finish operation: testpath)
250
args = "--comments --no-header"
251
gcode = postprocessor.export(postables, "-", args)
253
self.assertEqual(gcode, expected)
256
args = "--no-comments --header"
257
gcode = postprocessor.export(postables, "-", args)
259
self.assertEqual(gcode.splitlines()[0], "(Exported by FreeCAD)")
261
gcode.splitlines()[1],
262
"(Post Processor: Path.Post.scripts.refactored_test_post)",
264
self.assertEqual(gcode.splitlines()[2], "(Cam File: )")
265
self.assertIn("(Output Time: ", gcode.splitlines()[3])
266
self.assertTrue(len(gcode.splitlines()[3]) == 41)
267
self.assertEqual(gcode.splitlines()[4], "G90")
268
self.assertEqual(gcode.splitlines()[5], "G21")
274
args = "--no-comments --no-header"
275
gcode = postprocessor.export(postables, "-", args)
277
self.assertEqual(gcode, expected)
282
"""Test Line Numbers."""
283
self.compare_third_line(
284
"G0 X10 Y20 Z30", "N120 G0 X10.000 Y20.000 Z30.000", "--line-numbers"
292
c = Path.Command("G0 X10 Y20 Z30 A10 B20 C30 U10 V20 W30")
294
self.docobj.Path = Path.Path([c])
295
postables = [self.docobj]
297
gcode = postprocessor.export(postables, "-", args)
299
self.assertEqual(gcode.splitlines()[1], "G20")
301
gcode.splitlines()[2],
302
"G0 X0.3937 Y0.7874 Z1.1811 A0.3937 B0.7874 C1.1811 U0.3937 V0.7874 W1.1811",
310
Suppress the command name if the same as previous
312
c = Path.Command("G0 X10 Y20 Z30")
313
c1 = Path.Command("G0 X10 Y30 Z30")
314
self.docobj.Path = Path.Path([c, c1])
315
postables = [self.docobj]
317
gcode = postprocessor.export(postables, "-", args)
319
self.assertEqual(gcode.splitlines()[3], "X10.000 Y30.000 Z30.000")
321
gcode = postprocessor.export(postables, "-", args)
323
self.assertEqual(gcode.splitlines()[3], "G0 X10.000 Y30.000 Z30.000")
328
"""Test Outputting all arguments.
330
Empty path. Outputs all arguments.
332
expected = """Arguments that are commonly used:
333
--metric Convert output for Metric mode (G21) (default)
334
--inches Convert output for US imperial mode (G20)
335
--axis-modal Don't output axis values if they are the same as the
337
--no-axis-modal Output axis values even if they are the same as the
338
previous line (default)
339
--axis-precision AXIS_PRECISION
340
Number of digits of precision for axis moves, default
342
--bcnc Add Job operations as bCNC block headers. Consider
343
suppressing comments by adding --no-comments
344
--no-bcnc Suppress bCNC block header output (default)
345
--comments Output comments (default)
346
--no-comments Suppress comment output
347
--feed-precision FEED_PRECISION
348
Number of digits of precision for feed rate, default
350
--header Output headers (default)
351
--no-header Suppress header output
352
--line-numbers Prefix with line numbers
353
--no-line-numbers Don't prefix with line numbers (default)
354
--modal Don't output the G-command name if it is the same as
356
--no-modal Output the G-command name even if it is the same as
357
the previous line (default)
358
--output_all_arguments
359
Output all of the available arguments
360
--no-output_all_arguments
361
Don't output all of the available arguments (default)
362
--output_visible_arguments
363
Output all of the visible arguments
364
--no-output_visible_arguments
365
Don't output the visible arguments (default)
366
--postamble POSTAMBLE
367
Set commands to be issued after the last command,
369
--preamble PREAMBLE Set commands to be issued before the first command,
371
--precision PRECISION
372
Number of digits of precision for both feed rate and
373
axis moves, default is 3 for metric or 4 for inches
374
--return-to RETURN_TO
375
Move to the specified x,y,z coordinates at the end,
376
e.g. --return-to=0,0,0 (default is do not move)
377
--show-editor Pop up editor before writing output (default)
378
--no-show-editor Don't pop up editor before writing output
379
--tlo Output tool length offset (G43) following tool changes
381
--no-tlo Suppress tool length offset (G43) following tool
383
--tool_change Insert M6 and any other tool change G-code for all
384
tool changes (default)
385
--no-tool_change Convert M6 to a comment for all tool changes
386
--translate_drill Translate drill cycles G73, G81, G82 & G83 into G0/G1
388
--no-translate_drill Don't translate drill cycles G73, G81, G82 & G83 into
389
G0/G1 movements (default)
390
--wait-for-spindle WAIT_FOR_SPINDLE
391
Time to wait (in seconds) after M3, M4 (default = 0.0)
393
self.docobj.Path = Path.Path([])
394
postables = [self.docobj]
395
gcode: str = postprocessor.export(postables, "-", "--output_all_arguments")
400
gcode = "".join(gcode.split())
401
expected = "".join(expected.split())
402
self.assertEqual(gcode, expected)
407
"""Test Outputting visible arguments.
409
Empty path. Outputs visible arguments.
411
self.single_compare([], "", "--output_visible_arguments")
416
"""Test Post-amble."""
417
self.docobj.Path = Path.Path([])
418
postables = [self.docobj]
419
args = "--postamble='G0 Z50\nM2'"
420
gcode = postprocessor.export(postables, "-", args)
422
self.assertEqual(gcode.splitlines()[-2], "G0 Z50")
423
self.assertEqual(gcode.splitlines()[-1], "M2")
428
"""Test Pre-amble."""
429
self.docobj.Path = Path.Path([])
430
postables = [self.docobj]
431
args = "--preamble='G18 G55'"
432
gcode = postprocessor.export(postables, "-", args)
434
self.assertEqual(gcode.splitlines()[0], "G18 G55")
439
"""Test precision."""
440
self.compare_third_line(
441
"G1 X10 Y20 Z30 F100",
442
"G1 X10.00 Y20.00 Z30.00 F6000.00",
445
self.compare_third_line(
446
"G1 X10 Y20 Z30 F100",
447
"G1 X0.39 Y0.79 Z1.18 F236.22",
448
"--inches --precision=2",
454
"""Test return-to."""
455
self.compare_third_line("", "G0 X12 Y34 Z56", "--return-to='12,34,56'")
461
c = Path.Command("M6 T2")
462
c2 = Path.Command("M3 S3000")
463
self.docobj.Path = Path.Path([c, c2])
464
postables = [self.docobj]
466
gcode = postprocessor.export(postables, "-", args)
468
self.assertEqual(gcode.splitlines()[2], "M6 T2")
469
self.assertEqual(gcode.splitlines()[3], "G43 H2")
470
self.assertEqual(gcode.splitlines()[4], "M3 S3000")
473
gcode = postprocessor.export(postables, "-", args)
475
self.assertEqual(gcode.splitlines()[2], "M6 T2")
476
self.assertEqual(gcode.splitlines()[3], "M3 S3000")
481
"""Test tool_change."""
482
c = Path.Command("M6 T2")
483
c2 = Path.Command("M3 S3000")
484
self.docobj.Path = Path.Path([c, c2])
485
postables = [self.docobj]
486
args = "--tool_change"
487
gcode = postprocessor.export(postables, "-", args)
489
self.assertEqual(gcode.splitlines()[2], "M6 T2")
490
self.assertEqual(gcode.splitlines()[3], "M3 S3000")
491
args = "--comments --no-tool_change"
492
gcode = postprocessor.export(postables, "-", args)
494
self.assertEqual(gcode.splitlines()[5], "( M6 T2 )")
495
self.assertEqual(gcode.splitlines()[6], "M3 S3000")
500
"""Test wait-for-spindle."""
501
c = Path.Command("M3 S3000")
502
self.docobj.Path = Path.Path([c])
503
postables = [self.docobj]
505
gcode = postprocessor.export(postables, "-", args)
507
self.assertEqual(gcode.splitlines()[2], "M3 S3000")
508
args = "--wait-for-spindle=1.23456"
509
gcode = postprocessor.export(postables, "-", args)
511
self.assertEqual(gcode.splitlines()[2], "M3 S3000")
512
self.assertEqual(gcode.splitlines()[3], "G4 P1.23456")
513
c = Path.Command("M4 S3000")
514
self.docobj.Path = Path.Path([c])
515
postables = [self.docobj]
519
gcode = postprocessor.export(postables, "-", args)
521
self.assertEqual(gcode.splitlines()[2], "M4 S3000")
522
args = "--wait-for-spindle=1.23456"
523
gcode = postprocessor.export(postables, "-", args)
525
self.assertEqual(gcode.splitlines()[2], "M4 S3000")
526
self.assertEqual(gcode.splitlines()[3], "G4 P1.23456")