FreeCAD

Форк
0
/
TestMach3Mach4Post.py 
289 строк · 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 mach3_mach4_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 TestMach3Mach4Post(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 is able
59
        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()) == 13)
98

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

231
        args = "--no-header --modal --no-show-editor"
232
        gcode = postprocessor.export(postables, "-", args)
233
        result = gcode.splitlines()[6]
234
        expected = "X10.000 Y30.000 Z30.000"
235
        self.assertEqual(result, expected)
236

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

245
        self.docobj.Path = Path.Path([c, c1])
246
        postables = [self.docobj]
247

248
        args = "--no-header --axis-modal --no-show-editor"
249
        gcode = postprocessor.export(postables, "-", args)
250
        result = gcode.splitlines()[6]
251
        expected = "G0 Y30.000"
252
        self.assertEqual(result, expected)
253

254
    def test080(self):
255
        """
256
        Test tool change
257
        """
258
        c = Path.Command("M6 T2")
259
        c2 = Path.Command("M3 S3000")
260
        self.docobj.Path = Path.Path([c, c2])
261
        postables = [self.docobj]
262

263
        args = "--no-header --no-show-editor"
264
        gcode = postprocessor.export(postables, "-", args)
265
        self.assertEqual(gcode.splitlines()[5], "M5")
266
        self.assertEqual(gcode.splitlines()[6], "M6 T2 ")
267
        self.assertEqual(gcode.splitlines()[7], "G43 H2")
268
        self.assertEqual(gcode.splitlines()[8], "M3 S3000")
269

270
        # suppress TLO
271
        args = "--no-header --no-tlo --no-show-editor"
272
        gcode = postprocessor.export(postables, "-", args)
273
        self.assertEqual(gcode.splitlines()[7], "M3 S3000")
274

275
    def test090(self):
276
        """
277
        Test comment
278
        """
279

280
        c = Path.Command("(comment)")
281

282
        self.docobj.Path = Path.Path([c])
283
        postables = [self.docobj]
284

285
        args = "--no-header --no-show-editor"
286
        gcode = postprocessor.export(postables, "-", args)
287
        result = gcode.splitlines()[5]
288
        expected = "(comment)"
289
        self.assertEqual(result, expected)
290

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

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

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

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