FreeCAD

Форк
0
/
TestRefactoredMach3Mach4Post.py 
285 строк · 10.2 Кб
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 refactored_mach3_mach4_post as postprocessor
31

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

35

36
class TestRefactoredMach3Mach4Post(PathTestUtils.PathTestBase):
37
    @classmethod
38
    def setUpClass(cls) -> None:
39
        """setUpClass()...
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.
46
        """
47

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

51
    @classmethod
52
    def tearDownClass(cls) -> None:
53
        """tearDownClass()...
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 not
57
        have access to the class `self` reference.  This method is able to
58
        call static methods within this same class.
59
        """
60
        # Close geometry document without saving
61
        FreeCAD.closeDocument(FreeCAD.ActiveDocument.Name)
62

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

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

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

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

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

98
        # Test without header
99
        expected = """(Begin preamble)
100
G17 G54 G40 G49 G80 G90
101
G21
102
(Begin operation: testpath)
103
(Machine: mach3_4, mm/min)
104
(Finish operation: testpath)
105
(Begin postamble)
106
M05
107
G17 G54 G90 G80 G40
108
M2
109
"""
110

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

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

119
        # test without comments
120
        expected = """G17 G54 G40 G49 G80 G90
121
G21
122
M05
123
G17 G54 G90 G80 G40
124
M2
125
"""
126

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

132
    def test010(self):
133
        """Test command Generation.
134
        Test Precision
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' --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
        args = "--no-header --inches --precision=2 --no-show-editor"
211
        gcode = postprocessor.export(postables, "-", args)
212
        result = gcode.splitlines()[5]
213
        expected = "G0 X0.39 Y0.79 Z1.18"
214
        self.assertEqual(result, expected)
215

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

224
        self.docobj.Path = Path.Path([c, c1])
225
        postables = [self.docobj]
226

227
        args = "--no-header --modal --no-show-editor"
228
        gcode = postprocessor.export(postables, "-", args)
229
        result = gcode.splitlines()[6]
230
        expected = "X10.000 Y30.000 Z30.000"
231
        self.assertEqual(result, expected)
232

233
    def test070(self):
234
        """
235
        Test axis modal
236
        Suppress the axis coordinate if the same as previous
237
        """
238
        c = Path.Command("G0 X10 Y20 Z30")
239
        c1 = Path.Command("G0 X10 Y30 Z30")
240

241
        self.docobj.Path = Path.Path([c, c1])
242
        postables = [self.docobj]
243

244
        args = "--no-header --axis-modal --no-show-editor"
245
        gcode = postprocessor.export(postables, "-", args)
246
        result = gcode.splitlines()[6]
247
        expected = "G0 Y30.000"
248
        self.assertEqual(result, expected)
249

250
    def test080(self):
251
        """
252
        Test tool change
253
        """
254
        c = Path.Command("M6 T2")
255
        c2 = Path.Command("M3 S3000")
256
        self.docobj.Path = Path.Path([c, c2])
257
        postables = [self.docobj]
258

259
        args = "--no-header --no-show-editor"
260
        gcode = postprocessor.export(postables, "-", args)
261
        self.assertEqual(gcode.splitlines()[6], "M5")
262
        self.assertEqual(gcode.splitlines()[7], "M6 T2")
263
        self.assertEqual(gcode.splitlines()[8], "G43 H2")
264
        self.assertEqual(gcode.splitlines()[9], "M3 S3000")
265

266
        # suppress TLO
267
        args = "--no-header --no-tlo --no-show-editor"
268
        gcode = postprocessor.export(postables, "-", args)
269
        self.assertEqual(gcode.splitlines()[8], "M3 S3000")
270

271
    def test090(self):
272
        """
273
        Test comment
274
        """
275

276
        c = Path.Command("(comment)")
277

278
        self.docobj.Path = Path.Path([c])
279
        postables = [self.docobj]
280

281
        args = "--no-header --no-show-editor"
282
        gcode = postprocessor.export(postables, "-", args)
283
        result = gcode.splitlines()[5]
284
        expected = "(comment)"
285
        self.assertEqual(result, expected)
286

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

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

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

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