2
# ***************************************************************************
3
# * Copyright (c) 2022 sliptonic <shopinthewoods@gmail.com> *
4
# * Copyright (c) 2022 Larry Woestman <LarryWoestman2@gmail.com> *
6
# * This program is free software; you can redistribute it and/or modify *
7
# * it under the terms of the GNU Lesser General Public License (LGPL) *
8
# * as published by the Free Software Foundation; either version 2 of *
9
# * the License, or (at your option) any later version. *
10
# * for detail see the LICENCE text file. *
12
# * This program is distributed in the hope that it will be useful, *
13
# * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15
# * GNU Library General Public License for more details. *
17
# * You should have received a copy of the GNU Library General Public *
18
# * License along with this program; if not, write to the Free Software *
19
# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
22
# ***************************************************************************
24
from importlib import reload
29
import Tests.PathTestUtils as PathTestUtils
30
from Path.Post.scripts import centroid_post as postprocessor
33
Path.Log.setLevel(Path.Log.Level.DEBUG, Path.Log.thisModule())
34
Path.Log.trackModule(Path.Log.thisModule())
37
class TestCentroidPost(PathTestUtils.PathTestBase):
41
This method is called upon instantiation of this test class. Add code
42
and objects here that are needed for the duration of the test() methods
43
in this class. In other words, set up the 'global' test environment
44
here; use the `setUp()` method to set up a 'local' test environment.
45
This method does not have access to the class `self` reference, but it
46
is able to call static methods within this same class.
49
# Open existing FreeCAD document with test geometry
50
FreeCAD.newDocument("Unnamed")
53
def tearDownClass(cls):
55
This method is called prior to destruction of this test class. Add
56
code and objects here that cleanup the test environment after the
57
test() methods in this class have been executed. This method does
58
not have access to the class `self` reference. This method
59
is able to call static methods within this same class.
61
# Close geometry document without saving
62
FreeCAD.closeDocument(FreeCAD.ActiveDocument.Name)
64
# Setup and tear down methods called before and after each unit test
67
This method is called prior to each `test()` method. Add code and
68
objects here that are needed for multiple `test()` methods.
70
self.doc = FreeCAD.ActiveDocument
71
self.con = FreeCAD.Console
72
self.docobj = FreeCAD.ActiveDocument.addObject("Path::Feature", "testpath")
75
) # technical debt. This shouldn't be necessary but here to bypass a bug
79
This method is called after each test() method. Add cleanup instructions here.
80
Such cleanup instructions will likely undo those in the setUp() method.
82
FreeCAD.ActiveDocument.removeObject("testpath")
85
"""Test Output Generation.
86
Empty path. Produces only the preamble and postable.
89
self.docobj.Path = Path.Path([])
90
postables = [self.docobj]
92
# Test generating with header
93
# Header contains a time stamp that messes up unit testing.
94
# Only test length of result.
95
args = "--no-show-editor"
96
gcode = postprocessor.export(postables, "-", args)
97
self.assertTrue(len(gcode.splitlines()) == 16)
100
expected = """G90 G80 G40 G49
105
;end operation: testpath
114
self.docobj.Path = Path.Path([])
115
postables = [self.docobj]
117
args = "--no-header --no-show-editor"
118
gcode = postprocessor.export(postables, "-", args)
119
self.assertEqual(gcode, expected)
121
# test without comments
122
expected = """G90 G80 G40 G49
132
args = "--no-header --no-comments --no-show-editor"
133
# args = ("--no-header --no-comments --no-show-editor --precision=2")
134
gcode = postprocessor.export(postables, "-", args)
135
self.assertEqual(gcode, expected)
138
"""Test command Generation.
141
c = Path.Command("G0 X10 Y20 Z30")
143
self.docobj.Path = Path.Path([c])
144
postables = [self.docobj]
146
args = "--no-header --no-show-editor"
147
gcode = postprocessor.export(postables, "-", args)
148
result = gcode.splitlines()[5]
149
expected = "G0 X10.0000 Y20.0000 Z30.0000"
150
self.assertEqual(result, expected)
152
args = "--no-header --axis-precision=2 --no-show-editor"
153
gcode = postprocessor.export(postables, "-", args)
154
result = gcode.splitlines()[5]
155
expected = "G0 X10.00 Y20.00 Z30.00"
156
self.assertEqual(result, expected)
162
c = Path.Command("G0 X10 Y20 Z30")
164
self.docobj.Path = Path.Path([c])
165
postables = [self.docobj]
167
args = "--no-header --line-numbers --no-show-editor"
168
gcode = postprocessor.export(postables, "-", args)
169
result = gcode.splitlines()[5]
170
expected = "N150 G0 X10.0000 Y20.0000 Z30.0000"
171
self.assertEqual(result, expected)
178
self.docobj.Path = Path.Path([])
179
postables = [self.docobj]
182
# The original centroid postprocessor does not have a
183
# --preamble option. We end up with the default preamble.
185
args = "--no-header --no-comments --preamble='G18 G55' --no-show-editor"
186
gcode = postprocessor.export(postables, "-", args)
187
result = gcode.splitlines()[1]
188
self.assertEqual(result, "G53 G00 G17")
194
self.docobj.Path = Path.Path([])
195
postables = [self.docobj]
197
# The original centroid postprocessor does not have a
198
# --postamble option. We end up with the default postamble.
200
args = "--no-header --no-comments --postamble='G0 Z50\nM2' --no-show-editor"
201
gcode = postprocessor.export(postables, "-", args)
202
self.assertEqual(gcode.splitlines()[-1], "M99")
209
c = Path.Command("G0 X10 Y20 Z30")
210
self.docobj.Path = Path.Path([c])
211
postables = [self.docobj]
213
args = "--no-header --inches --no-show-editor"
214
gcode = postprocessor.export(postables, "-", args)
215
self.assertEqual(gcode.splitlines()[3], "G20")
217
result = gcode.splitlines()[5]
218
expected = "G0 X0.3937 Y0.7874 Z1.1811"
219
self.assertEqual(result, expected)
221
args = "--no-header --inches --axis-precision=2 --no-show-editor"
222
gcode = postprocessor.export(postables, "-", args)
223
result = gcode.splitlines()[5]
224
expected = "G0 X0.39 Y0.79 Z1.18"
225
self.assertEqual(result, expected)
230
Suppress the command name if the same as previous
232
c = Path.Command("G0 X10 Y20 Z30")
233
c1 = Path.Command("G0 X10 Y30 Z30")
235
self.docobj.Path = Path.Path([c, c1])
236
postables = [self.docobj]
239
# The original centroid postprocessor does not have a
240
# --modal option. We end up with the original gcode.
242
args = "--no-header --modal --no-show-editor"
243
gcode = postprocessor.export(postables, "-", args)
244
result = gcode.splitlines()[6]
245
expected = "G0 X10.0000 Y30.0000 Z30.0000"
246
self.assertEqual(result, expected)
251
Suppress the axis coordinate if the same as previous
253
c = Path.Command("G0 X10 Y20 Z30")
254
c1 = Path.Command("G0 X10 Y30 Z30")
256
self.docobj.Path = Path.Path([c, c1])
257
postables = [self.docobj]
260
# The original centroid postprocessor does not have an
261
# --axis-modal option. We end up with the original gcode.
263
args = "--no-header --axis-modal --no-show-editor"
264
gcode = postprocessor.export(postables, "-", args)
265
result = gcode.splitlines()[6]
266
expected = "G0 X10.0000 Y30.0000 Z30.0000"
267
self.assertEqual(result, expected)
273
c = Path.Command("M6 T2")
274
c2 = Path.Command("M3 S3000")
275
self.docobj.Path = Path.Path([c, c2])
276
postables = [self.docobj]
278
args = "--no-header --no-show-editor"
279
gcode = postprocessor.export(postables, "-", args)
280
self.assertEqual(gcode.splitlines()[5], "G43 H2")
281
self.assertEqual(gcode.splitlines()[6], "M6 T2")
282
self.assertEqual(gcode.splitlines()[7], "M3 S3000")
286
# The original centroid postprocessor does not have an
287
# --no-tlo option. We end up with the original gcode.
289
args = "--no-header --no-tlo --no-show-editor"
290
gcode = postprocessor.export(postables, "-", args)
291
self.assertEqual(gcode.splitlines()[6], "M3 S3000")
298
c = Path.Command("(comment)")
300
self.docobj.Path = Path.Path([c])
301
postables = [self.docobj]
303
args = "--no-header --no-show-editor"
304
gcode = postprocessor.export(postables, "-", args)
305
result = gcode.splitlines()[5]
306
expected = ";comment"
307
self.assertEqual(result, expected)