FreeCAD

Форк
0
/
TestPathLanguage.py 
120 строк · 5.9 Кб
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 Path.Base.Language as PathLanguage
24
import Tests.PathTestUtils as PathTestUtils
25
import math
26

27
PI = math.pi
28

29

30
def MNVR(gcode, begin=None):
31
    # 'turns out the replace() isn't really necessary
32
    # leave it here anyway for clarity
33
    return PathLanguage.Maneuver.FromGCode(gcode.replace("/", "\n"), begin)
34

35

36
def INSTR(gcode, begin=None):
37
    return MNVR(gcode, begin).instr[0]
38

39

40
class TestPathLanguage(PathTestUtils.PathTestBase):
41
    """Unit tests for the Language classes."""
42

43
    def assertTangents(self, instr, t1):
44
        """Assert that the two tangent angles are identical"""
45
        t0 = instr.anglesOfTangents()
46
        self.assertRoughly(t0[0], t1[0])
47
        self.assertRoughly(t0[1], t1[1])
48

49
    def test00(self):
50
        """Verify G0 instruction construction"""
51
        self.assertEqual(str(MNVR("")), "")
52
        self.assertEqual(len(MNVR("").instr), 0)
53

54
        self.assertEqual(str(MNVR("G0")), "G0{}")
55
        self.assertEqual(str(MNVR("G0X3")), "G0{'X': 3.0}")
56
        self.assertEqual(str(MNVR("G0X3Y7")), "G0{'X': 3.0, 'Y': 7.0}")
57
        self.assertEqual(str(MNVR("G0X3Y7/G0Z0")), "G0{'X': 3.0, 'Y': 7.0}\nG0{'Z': 0.0}")
58
        self.assertEqual(len(MNVR("G0X3Y7").instr), 1)
59
        self.assertEqual(len(MNVR("G0X3Y7/G0Z0").instr), 2)
60
        self.assertEqual(type(MNVR("G0X3Y7").instr[0]), PathLanguage.MoveStraight)
61

62
    def test10(self):
63
        """Verify G1 instruction construction"""
64
        self.assertEqual(str(MNVR("G1")), "G1{}")
65
        self.assertEqual(str(MNVR("G1X3")), "G1{'X': 3.0}")
66
        self.assertEqual(str(MNVR("G1X3Y7")), "G1{'X': 3.0, 'Y': 7.0}")
67
        self.assertEqual(str(MNVR("G1X3Y7/G1Z0")), "G1{'X': 3.0, 'Y': 7.0}\nG1{'Z': 0.0}")
68
        self.assertEqual(len(MNVR("G1X3Y7").instr), 1)
69
        self.assertEqual(len(MNVR("G1X3Y7/G1Z0").instr), 2)
70
        self.assertEqual(type(MNVR("G1X3Y7").instr[0]), PathLanguage.MoveStraight)
71

72
    def test20(self):
73
        """Verify G2 instruction construction"""
74
        self.assertEqual(str(MNVR("G2X2Y2I1")), "G2{'I': 1.0, 'X': 2.0, 'Y': 2.0}")
75
        self.assertEqual(len(MNVR("G2X2Y2I1").instr), 1)
76
        self.assertEqual(type(MNVR("G2X2Y2I1").instr[0]), PathLanguage.MoveArcCW)
77

78
    def test30(self):
79
        """Verify G3 instruction construction"""
80
        self.assertEqual(str(MNVR("G3X2Y2I1")), "G3{'I': 1.0, 'X': 2.0, 'Y': 2.0}")
81
        self.assertEqual(len(MNVR("G3X2Y2I1").instr), 1)
82
        self.assertEqual(type(MNVR("G3X2Y2I1").instr[0]), PathLanguage.MoveArcCCW)
83

84
    def test40(self):
85
        """Verify pathLength correctness"""
86
        self.assertRoughly(MNVR("G1X3").instr[0].pathLength(), 3)
87
        self.assertRoughly(MNVR("G1X-7").instr[0].pathLength(), 7)
88
        self.assertRoughly(MNVR("G1X3").instr[0].pathLength(), 3)
89

90
        self.assertRoughly(MNVR("G1X3Y4").instr[0].pathLength(), 5)
91
        self.assertRoughly(MNVR("G1X3Y-4").instr[0].pathLength(), 5)
92
        self.assertRoughly(MNVR("G1X-3Y-4").instr[0].pathLength(), 5)
93
        self.assertRoughly(MNVR("G1X-3Y4").instr[0].pathLength(), 5)
94

95
        self.assertRoughly(MNVR("G2X2I1").instr[0].pathLength(), PI)
96
        self.assertRoughly(MNVR("G2X1Y1I1").instr[0].pathLength(), PI / 2)
97

98
        self.assertRoughly(MNVR("G3X2I1").instr[0].pathLength(), PI)
99
        self.assertRoughly(MNVR("G3X1Y1I1").instr[0].pathLength(), 3 * PI / 2)
100

101
    def test50(self):
102
        """Verify tangents of moves."""
103

104
        self.assertTangents(INSTR("G1 X0  Y0"), (0, 0))  # by declaration
105
        self.assertTangents(INSTR("G1 X1  Y0"), (0, 0))
106
        self.assertTangents(INSTR("G1 X-1 Y0"), (PI, PI))
107
        self.assertTangents(INSTR("G1 X0  Y1"), (PI / 2, PI / 2))
108
        self.assertTangents(INSTR("G1 X0  Y-1"), (-PI / 2, -PI / 2))
109
        self.assertTangents(INSTR("G1 X1  Y1"), (PI / 4, PI / 4))
110
        self.assertTangents(INSTR("G1 X-1 Y1"), (3 * PI / 4, 3 * PI / 4))
111
        self.assertTangents(INSTR("G1 X-1 Y -1"), (-3 * PI / 4, -3 * PI / 4))
112
        self.assertTangents(INSTR("G1 X1  Y-1"), (-PI / 4, -PI / 4))
113

114
        self.assertTangents(INSTR("G2 X2  Y0  I1 J0"), (PI / 2, -PI / 2))
115
        self.assertTangents(INSTR("G2 X2  Y2  I1 J1"), (3 * PI / 4, -PI / 4))
116
        self.assertTangents(INSTR("G2 X0  Y-2 I0 J-1"), (0, -PI))
117

118
        self.assertTangents(INSTR("G3 X2  Y0  I1 J0"), (-PI / 2, PI / 2))
119
        self.assertTangents(INSTR("G3 X2  Y2  I1 J1"), (-PI / 4, 3 * PI / 4))
120
        self.assertTangents(INSTR("G3 X0  Y-2 I0 J-1"), (PI, 0))
121

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

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

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

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