FreeCAD

Форк
0
/
TestGrblPost.py 
295 строк · 10.3 Кб
1
# -*- coding: utf-8 -*-
2
# ***************************************************************************
3
# *   Copyright (c) 2022 sliptonic <shopinthewoods@gmail.com>               *
4
# *                                                                         *
5
# *   This program is free software; you can redistribute it and/or modify  *
6
# *   it under the terms of the GNU Lesser General Public License (LGPL)    *
7
# *   as published by the Free Software Foundation; either version 2 of     *
8
# *   the License, or (at your option) any later version.                   *
9
# *   for detail see the LICENCE text file.                                 *
10
# *                                                                         *
11
# *   This program is distributed in the hope that it will be useful,       *
12
# *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
13
# *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
14
# *   GNU Library General Public License for more details.                  *
15
# *                                                                         *
16
# *   You should have received a copy of the GNU Library General Public     *
17
# *   License along with this program; if not, write to the Free Software   *
18
# *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  *
19
# *   USA                                                                   *
20
# *                                                                         *
21
# ***************************************************************************
22

23
import FreeCAD
24

25
import Path
26
import Tests.PathTestUtils as PathTestUtils
27
from importlib import reload
28
from Path.Post.scripts import grbl_post as postprocessor
29

30

31
Path.Log.setLevel(Path.Log.Level.DEBUG, Path.Log.thisModule())
32
Path.Log.trackModule(Path.Log.thisModule())
33

34

35
class TestGrblPost(PathTestUtils.PathTestBase):
36
    @classmethod
37
    def setUpClass(cls):
38
        """setUpClass()...
39
        This method is called upon instantiation of this test class.  Add code
40
        and objects here that are needed for the duration of the test() methods
41
        in this class.  In other words, set up the 'global' test environment
42
        here; use the `setUp()` method to set up a 'local' test environment.
43
        This method does not have access to the class `self` reference, but it
44
        is able to call static methods within this same class.
45
        """
46

47
        # Open existing FreeCAD document with test geometry
48
        FreeCAD.newDocument("Unnamed")
49

50
    @classmethod
51
    def tearDownClass(cls):
52
        """tearDownClass()...
53
        This method is called prior to destruction of this test class.  Add
54
        code and objects here that cleanup the test environment after the
55
        test() methods in this class have been executed.  This method does
56
        not have access to the class `self` reference.  This method is able
57
        to call static methods within this same class.
58
        """
59
        # Close geometry document without saving
60
        FreeCAD.closeDocument(FreeCAD.ActiveDocument.Name)
61

62
    # Setup and tear down methods called before and after each unit test
63
    def setUp(self):
64
        """setUp()...
65
        This method is called prior to each `test()` method.  Add code and
66
        objects here that are needed for multiple `test()` methods.
67
        """
68
        self.doc = FreeCAD.ActiveDocument
69
        self.con = FreeCAD.Console
70
        self.docobj = FreeCAD.ActiveDocument.addObject("Path::Feature", "testpath")
71
        reload(
72
            postprocessor
73
        )  # technical debt.  This shouldn't be necessary but here to bypass a bug
74

75
    def tearDown(self):
76
        """tearDown()...
77
        This method is called after each test() method. Add cleanup instructions here.
78
        Such cleanup instructions will likely undo those in the setUp() method.
79
        """
80
        FreeCAD.ActiveDocument.removeObject("testpath")
81

82
    def test000(self):
83
        """Test Output Generation.
84
        Empty path.  Produces only the preamble and postable.
85
        """
86

87
        self.docobj.Path = Path.Path([])
88
        postables = [self.docobj]
89

90
        # Test generating with header
91
        # Header contains a time stamp that messes up unit testing.  Only test
92
        # length of result.
93
        args = "--no-show-editor"
94
        gcode = postprocessor.export(postables, "-", args)
95
        self.assertTrue(len(gcode.splitlines()) == 13)
96

97
        # Test without header
98
        expected = """(Begin preamble)
99
G17 G90
100
G21
101
(Begin operation: testpath)
102
(Path: testpath)
103
(Finish operation: testpath)
104
(Begin postamble)
105
M5
106
G17 G90
107
M2
108
"""
109

110
        self.docobj.Path = Path.Path([])
111
        postables = [self.docobj]
112

113
        args = "--no-header --no-show-editor"
114
        # args = ("--no-header --no-comments --no-show-editor --precision=2")
115
        gcode = postprocessor.export(postables, "-", args)
116
        self.assertEqual(gcode, expected)
117

118
        # test without comments
119
        expected = """G17 G90
120
G21
121
M5
122
G17 G90
123
M2
124
"""
125

126
        args = "--no-header --no-comments --no-show-editor"
127
        # args = ("--no-header --no-comments --no-show-editor --precision=2")
128
        gcode = postprocessor.export(postables, "-", args)
129
        self.assertEqual(gcode, expected)
130

131
    def test010(self):
132
        """Test command Generation.
133
        Test Precision
134
        Test imperial / inches
135
        """
136
        c = Path.Command("G0 X10 Y20 Z30")
137

138
        self.docobj.Path = Path.Path([c])
139
        postables = [self.docobj]
140

141
        args = "--no-header --no-show-editor"
142
        gcode = postprocessor.export(postables, "-", args)
143
        result = gcode.splitlines()[5]
144
        expected = "G0 X10.000 Y20.000 Z30.000"
145
        self.assertEqual(result, expected)
146

147
        args = "--no-header --precision=2 --no-show-editor"
148
        gcode = postprocessor.export(postables, "-", args)
149
        result = gcode.splitlines()[5]
150
        expected = "G0 X10.00 Y20.00 Z30.00"
151
        self.assertEqual(result, expected)
152

153
    def test020(self):
154
        """
155
        Test Line Numbers
156
        """
157
        c = Path.Command("G0 X10 Y20 Z30")
158

159
        self.docobj.Path = Path.Path([c])
160
        postables = [self.docobj]
161

162
        args = "--no-header --line-numbers --no-show-editor"
163
        gcode = postprocessor.export(postables, "-", args)
164
        result = gcode.splitlines()[5]
165
        expected = "N150 G0 X10.000 Y20.000 Z30.000"
166
        self.assertEqual(result, expected)
167

168
    def test030(self):
169
        """
170
        Test Pre-amble
171
        """
172

173
        self.docobj.Path = Path.Path([])
174
        postables = [self.docobj]
175

176
        args = "--no-header --no-comments --preamble='G18 G55\n' --no-show-editor"
177
        gcode = postprocessor.export(postables, "-", args)
178
        result = gcode.splitlines()[0]
179
        self.assertEqual(result, "G18 G55")
180

181
    def test040(self):
182
        """
183
        Test Post-amble
184
        """
185
        self.docobj.Path = Path.Path([])
186
        postables = [self.docobj]
187
        args = "--no-header --no-comments --postamble='G0 Z50\nM2' --no-show-editor"
188
        gcode = postprocessor.export(postables, "-", args)
189
        result = gcode.splitlines()[-2]
190
        self.assertEqual(result, "G0 Z50")
191
        self.assertEqual(gcode.splitlines()[-1], "M2")
192

193
    def test050(self):
194
        """
195
        Test inches
196
        """
197

198
        c = Path.Command("G0 X10 Y20 Z30")
199
        self.docobj.Path = Path.Path([c])
200
        postables = [self.docobj]
201

202
        args = "--no-header --inches --no-show-editor"
203
        gcode = postprocessor.export(postables, "-", args)
204
        self.assertEqual(gcode.splitlines()[2], "G20")
205

206
        result = gcode.splitlines()[5]
207
        expected = "G0 X0.3937 Y0.7874 Z1.1811"
208
        self.assertEqual(result, expected)
209

210
        # Technical debt.   The following test fails.  Precision not working
211
        # with imperial units.
212

213
        # args = ("--no-header --inches --precision=2")
214
        # gcode = postprocessor.export(postables, "-", args)
215
        # result = gcode.splitlines()[5]
216
        # expected = "G0 X0.39 Y0.78 Z1.18 "
217
        # self.assertEqual(result, expected)
218

219
    def test060(self):
220
        """
221
        Test test modal
222
        Suppress the command name if the same as previous
223
        """
224
        c = Path.Command("G0 X10 Y20 Z30")
225
        c1 = Path.Command("G0 X10 Y30 Z30")
226

227
        self.docobj.Path = Path.Path([c, c1])
228
        postables = [self.docobj]
229

230
        #
231
        # The grbl postprocessor does not have a --modal option.
232
        #
233
        # args = "--no-header --modal --no-show-editor"
234
        # gcode = postprocessor.export(postables, "-", args)
235
        # result = gcode.splitlines()[6]
236
        # expected = "X10.000 Y30.000 Z30.000 "
237
        # self.assertEqual(result, expected)
238

239
    def test070(self):
240
        """
241
        Test axis modal
242
        Suppress the axis coordinate if the same as previous
243
        """
244
        c = Path.Command("G0 X10 Y20 Z30")
245
        c1 = Path.Command("G0 X10 Y30 Z30")
246

247
        self.docobj.Path = Path.Path([c, c1])
248
        postables = [self.docobj]
249

250
        #
251
        # The grbl postprocessor does not have a --axis-modal option.
252
        #
253
        # args = "--no-header --axis-modal --no-show-editor"
254
        # gcode = postprocessor.export(postables, "-", args)
255
        # result = gcode.splitlines()[6]
256
        # expected = "G0 Y30.000 "
257
        # self.assertEqual(result, expected)
258

259
    def test080(self):
260
        """
261
        Test tool change
262
        """
263
        c = Path.Command("M6 T2")
264
        c2 = Path.Command("M3 S3000")
265
        self.docobj.Path = Path.Path([c, c2])
266
        postables = [self.docobj]
267

268
        args = "--no-header --no-show-editor"
269
        gcode = postprocessor.export(postables, "-", args)
270
        self.assertEqual(gcode.splitlines()[6], "( M6 T2 )")
271
        self.assertEqual(gcode.splitlines()[7], "M3 S3000")
272

273
        # suppress TLO
274
        #
275
        # The grbl postprocessor does not have a --no-tlo option.
276
        #
277
        # args = "--no-header --no-tlo --no-show-editor"
278
        # gcode = postprocessor.export(postables, "-", args)
279
        # self.assertEqual(gcode.splitlines()[7], "M3 S3000 ")
280

281
    def test090(self):
282
        """
283
        Test comment
284
        """
285

286
        c = Path.Command("(comment)")
287

288
        self.docobj.Path = Path.Path([c])
289
        postables = [self.docobj]
290

291
        args = "--no-header --no-show-editor"
292
        gcode = postprocessor.export(postables, "-", args)
293
        result = gcode.splitlines()[5]
294
        expected = "(comment)"
295
        self.assertEqual(result, expected)
296

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.