File size: 5,094 Bytes
985c397 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | # SPDX-License-Identifier: LGPL-2.1-or-later
# ***************************************************************************
# * Copyright (c) 2016 sliptonic <shopinthewoods@gmail.com> *
# * *
# * This program is free software; you can redistribute it and/or modify *
# * it under the terms of the GNU Lesser General Public License (LGPL) *
# * as published by the Free Software Foundation; either version 2 of *
# * the License, or (at your option) any later version. *
# * for detail see the LICENCE text file. *
# * *
# * This program is distributed in the hope that it will be useful, *
# * but WITHOUT ANY WARRANTY; without even the implied warranty of *
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
# * GNU Library General Public License for more details. *
# * *
# * You should have received a copy of the GNU Library General Public *
# * License along with this program; if not, write to the Free Software *
# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
# * USA *
# * *
# ***************************************************************************
import FreeCAD
import Path
from CAMTests.PathTestUtils import PathTestBase
class TestPathCore(PathTestBase):
def test00(self):
"""Test Path command core functionality"""
# create empty command
c = Path.Command()
self.assertIsInstance(c, Path.Command)
# change name
c.Name = "G1"
self.assertEqual(c.Name, "G1")
# Assign Parameters
c.Parameters = {"X": 1, "Y": 0}
self.assertEqual(c.Parameters, {"Y": 0.0, "X": 1.0})
# change parameters
c.Parameters = {"X": 1, "Y": 0.5}
self.assertEqual(c.Parameters, {"Y": 0.5, "X": 1})
# output gcode
self.assertEqual(c.toGCode(), "G1 X1.000000 Y0.500000")
# create and assign name in one
c2 = Path.Command("G2")
self.assertEqual(c2.Name, "G2")
# Create Path and parameters in one
c3 = Path.Command("G1", {"X": 34, "Y": 1.2})
self.assertEqual(str(c3), "Command G1 [ X:34 Y:1.2 ]")
c4 = Path.Command("G1X4Y5")
self.assertEqual(str(c4), "Command G1 [ X:4 Y:5 ]")
# use placement
self.assertEqual(str(c3.Placement), "Placement [Pos=(34,1.2,0), Yaw-Pitch-Roll=(0,0,0)]")
self.assertEqual(c3.toGCode(), "G1 X34.000000 Y1.200000")
p1 = FreeCAD.Placement()
p1.Base = FreeCAD.Vector(3, 2, 1)
self.assertEqual(str(p1), "Placement [Pos=(3,2,1), Yaw-Pitch-Roll=(0,0,0)]")
c5 = Path.Command("g1", p1)
self.assertEqual(str(c5), "Command G1 [ X:3 Y:2 Z:1 ]")
p2 = FreeCAD.Placement()
p2.Base = FreeCAD.Vector(5, 0, 0)
# overwrite placement
c5.Placement = p2
self.assertEqual(str(c5), "Command G1 [ X:5 ]")
self.assertEqual(c5.x, 5.0)
# overwrite individual parameters
c5.x = 10
self.assertEqual(c5.x, 10.0)
c5.y = 2
self.assertEqual(str(c5), "Command G1 [ X:10 Y:2 ]")
# set from gcode
c3.setFromGCode("G1X1Y0")
self.assertEqual(str(c3), "Command G1 [ X:1 Y:0 ]")
def test10(self):
"""Test Path Object core functionality"""
c1 = Path.Command("g1", {"x": 1, "y": 0})
c2 = Path.Command("g1", {"x": 0, "y": 2})
p = Path.Path([c1, c2])
self.assertAlmostEqual(str(p), "Path [ size:2 length:3.2361 ]", places=4)
self.assertEqual(str(p.Commands), "[Command G1 [ X:1 Y:0 ], Command G1 [ X:0 Y:2 ]]")
self.assertAlmostEqual(p.Length, 3.2361, places=4)
p.addCommands(c1)
self.assertEqual(
p.toGCode(),
"G1 X1.000000 Y0.000000\nG1 X0.000000 Y2.000000\nG1 X1.000000 Y0.000000\n",
)
lines = """
G0X-0.5905Y-0.3937S3000M03
G0Z0.125
G1Z-0.004F3
G1X0.9842Y-0.3937F14.17
G1X0.9842Y0.433
G1X-0.5905Y0.433
G1X-0.5905Y-0.3937
G0Z0.5
"""
output = """G0 S3000.000000 X-0.590500 Y-0.393700
M03
G0 Z0.125000
G1 F3.000000 Z-0.004000
G1 F14.170000 X0.984200 Y-0.393700
G1 X0.984200 Y0.433000
G1 X-0.590500 Y0.433000
G1 X-0.590500 Y-0.393700
G0 Z0.500000
"""
# create a path directly form a piece of gcode.
p = Path.Path()
p.setFromGCode(lines)
self.assertEqual(p.toGCode(), output)
def test50(self):
"""Test Path.Length calculation"""
commands = []
commands.append(Path.Command("G1", {"X": 1}))
commands.append(Path.Command("G1", {"Y": 1}))
path = Path.Path(commands)
self.assertEqual(path.Length, 2)
|