FreeCAD

Форк
0
/
TestCentroidPost.py 
307 строк · 10.6 Кб
1
# -*- coding: utf-8 -*-
2
# ***************************************************************************
3
# *   Copyright (c) 2022 sliptonic <shopinthewoods@gmail.com>               *
4
# *   Copyright (c) 2022 Larry Woestman <LarryWoestman2@gmail.com>          *
5
# *                                                                         *
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.                                 *
11
# *                                                                         *
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.                  *
16
# *                                                                         *
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  *
20
# *   USA                                                                   *
21
# *                                                                         *
22
# ***************************************************************************
23

24
from importlib import reload
25

26
import FreeCAD
27

28
import Path
29
import Tests.PathTestUtils as PathTestUtils
30
from Path.Post.scripts import centroid_post as postprocessor
31

32

33
Path.Log.setLevel(Path.Log.Level.DEBUG, Path.Log.thisModule())
34
Path.Log.trackModule(Path.Log.thisModule())
35

36

37
class TestCentroidPost(PathTestUtils.PathTestBase):
38
    @classmethod
39
    def setUpClass(cls):
40
        """setUpClass()...
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.
47
        """
48

49
        # Open existing FreeCAD document with test geometry
50
        FreeCAD.newDocument("Unnamed")
51

52
    @classmethod
53
    def tearDownClass(cls):
54
        """tearDownClass()...
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.
60
        """
61
        # Close geometry document without saving
62
        FreeCAD.closeDocument(FreeCAD.ActiveDocument.Name)
63

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

77
    def tearDown(self):
78
        """tearDown()...
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.
81
        """
82
        FreeCAD.ActiveDocument.removeObject("testpath")
83

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

89
        self.docobj.Path = Path.Path([])
90
        postables = [self.docobj]
91

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)
98

99
        # Test without header
100
        expected = """G90 G80 G40 G49
101
;begin preamble
102
G53 G00 G17
103
G21
104
;begin operation
105
;end operation: testpath
106
;begin postamble
107
M5
108
M25
109
G49 H0
110
G90 G80 G40 G49
111
M99
112
"""
113

114
        self.docobj.Path = Path.Path([])
115
        postables = [self.docobj]
116

117
        args = "--no-header --no-show-editor"
118
        gcode = postprocessor.export(postables, "-", args)
119
        self.assertEqual(gcode, expected)
120

121
        # test without comments
122
        expected = """G90 G80 G40 G49
123
G53 G00 G17
124
G21
125
M5
126
M25
127
G49 H0
128
G90 G80 G40 G49
129
M99
130
"""
131

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)
136

137
    def test010(self):
138
        """Test command Generation.
139
        Test Precision
140
        """
141
        c = Path.Command("G0 X10 Y20 Z30")
142

143
        self.docobj.Path = Path.Path([c])
144
        postables = [self.docobj]
145

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)
151

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)
157

158
    def test020(self):
159
        """
160
        Test Line Numbers
161
        """
162
        c = Path.Command("G0 X10 Y20 Z30")
163

164
        self.docobj.Path = Path.Path([c])
165
        postables = [self.docobj]
166

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)
172

173
    def test030(self):
174
        """
175
        Test Pre-amble
176
        """
177

178
        self.docobj.Path = Path.Path([])
179
        postables = [self.docobj]
180

181
        #
182
        # The original centroid postprocessor does not have a
183
        # --preamble option.  We end up with the default preamble.
184
        #
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")
189

190
    def test040(self):
191
        """
192
        Test Post-amble
193
        """
194
        self.docobj.Path = Path.Path([])
195
        postables = [self.docobj]
196
        #
197
        # The original centroid postprocessor does not have a
198
        # --postamble option.  We end up with the default postamble.
199
        #
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")
203

204
    def test050(self):
205
        """
206
        Test inches
207
        """
208

209
        c = Path.Command("G0 X10 Y20 Z30")
210
        self.docobj.Path = Path.Path([c])
211
        postables = [self.docobj]
212

213
        args = "--no-header --inches --no-show-editor"
214
        gcode = postprocessor.export(postables, "-", args)
215
        self.assertEqual(gcode.splitlines()[3], "G20")
216

217
        result = gcode.splitlines()[5]
218
        expected = "G0 X0.3937 Y0.7874 Z1.1811"
219
        self.assertEqual(result, expected)
220

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)
226

227
    def test060(self):
228
        """
229
        Test test modal
230
        Suppress the command name if the same as previous
231
        """
232
        c = Path.Command("G0 X10 Y20 Z30")
233
        c1 = Path.Command("G0 X10 Y30 Z30")
234

235
        self.docobj.Path = Path.Path([c, c1])
236
        postables = [self.docobj]
237

238
        #
239
        # The original centroid postprocessor does not have a
240
        # --modal option.  We end up with the original gcode.
241
        #
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)
247

248
    def test070(self):
249
        """
250
        Test axis modal
251
        Suppress the axis coordinate if the same as previous
252
        """
253
        c = Path.Command("G0 X10 Y20 Z30")
254
        c1 = Path.Command("G0 X10 Y30 Z30")
255

256
        self.docobj.Path = Path.Path([c, c1])
257
        postables = [self.docobj]
258

259
        #
260
        # The original centroid postprocessor does not have an
261
        # --axis-modal option.  We end up with the original gcode.
262
        #
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)
268

269
    def test080(self):
270
        """
271
        Test tool change
272
        """
273
        c = Path.Command("M6 T2")
274
        c2 = Path.Command("M3 S3000")
275
        self.docobj.Path = Path.Path([c, c2])
276
        postables = [self.docobj]
277

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")
283

284
        # suppress TLO
285
        #
286
        # The original centroid postprocessor does not have an
287
        # --no-tlo option.  We end up with the original gcode.
288
        #
289
        args = "--no-header --no-tlo --no-show-editor"
290
        gcode = postprocessor.export(postables, "-", args)
291
        self.assertEqual(gcode.splitlines()[6], "M3 S3000")
292

293
    def test090(self):
294
        """
295
        Test comment
296
        """
297

298
        c = Path.Command("(comment)")
299

300
        self.docobj.Path = Path.Path([c])
301
        postables = [self.docobj]
302

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)
308

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

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

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

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