File size: 6,172 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# -*- coding: utf-8 -*-
# ***************************************************************************
# *   Copyright (c) 2025 Brad Collette                                      *
# *                                                                         *
# *   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.                                *
# *                                                                         *
# ***************************************************************************

import FreeCAD
import Part
import CAMTests.PathTestUtils as PathTestUtils
import Path.Base.Generator.linking as generator
import unittest


class TestGetLinkingMoves(PathTestUtils.PathTestBase):
    def setUp(self):
        self.start = FreeCAD.Vector(0, 0, 0)
        self.target = FreeCAD.Vector(10, 0, 0)
        self.local_clearance = 2.0
        self.global_clearance = 5.0
        self.tool = Part.makeCylinder(1, 5)

    def test_simple_move(self):
        cmds = generator.get_linking_moves(
            start_position=self.start,
            target_position=self.target,
            local_clearance=self.local_clearance,
            global_clearance=self.global_clearance,
            tool_shape=self.tool,
            solids=[],
        )
        self.assertGreater(len(cmds), 0)
        self.assertEqual(cmds[0].Name, "G0")

    def test_same_position_returns_empty(self):
        cmds = generator.get_linking_moves(
            start_position=self.start,
            target_position=self.start,
            local_clearance=self.local_clearance,
            global_clearance=self.global_clearance,
            tool_shape=self.tool,
            solids=[],
        )
        self.assertEqual(len(cmds), 0)

    def test_negative_retract_offset_raises(self):
        with self.assertRaises(ValueError):
            generator.get_linking_moves(
                start_position=self.start,
                target_position=self.target,
                local_clearance=self.local_clearance,
                global_clearance=self.global_clearance,
                tool_shape=self.tool,
                retract_height_offset=-1,
            )

    def test_clearance_violation_raises(self):
        with self.assertRaises(ValueError):
            generator.get_linking_moves(
                start_position=self.start,
                target_position=self.target,
                local_clearance=10.0,
                global_clearance=5.0,
                tool_shape=self.tool,
            )

    def test_path_blocked_by_solid(self):
        blocking_box = Part.makeBox(20, 20, 10)
        blocking_box.translate(FreeCAD.Vector(-5, -5, 0))
        with self.assertRaises(RuntimeError):
            generator.get_linking_moves(
                start_position=self.start,
                target_position=self.target,
                local_clearance=self.local_clearance,
                global_clearance=self.global_clearance,
                tool_shape=self.tool,
                solids=[blocking_box],
            )

    def test_plunge_to_zero_depth(self):
        """Test that plunge moves correctly go to Z=0 (regression test for depth==0 bug)"""
        start = FreeCAD.Vector(0, 0, 1)  # Start below clearance
        target = FreeCAD.Vector(10, 10, 0)  # Target depth is 0

        cmds = generator.get_linking_moves(
            start_position=start,
            target_position=target,
            local_clearance=self.local_clearance,
            global_clearance=self.global_clearance,
            tool_shape=self.tool,
            solids=[],
        )

        # Verify we got commands
        self.assertGreater(len(cmds), 0)

        # All commands should have complete XYZ coordinates
        for cmd in cmds:
            self.assertIn("X", cmd.Parameters, "Command missing X coordinate")
            self.assertIn("Y", cmd.Parameters, "Command missing Y coordinate")
            self.assertIn("Z", cmd.Parameters, "Command missing Z coordinate")

        # The last command should be the plunge to target depth (Z=0)
        last_cmd = cmds[-1]
        self.assertAlmostEqual(last_cmd.Parameters["X"], target.x, places=5)
        self.assertAlmostEqual(last_cmd.Parameters["Y"], target.y, places=5)
        self.assertAlmostEqual(
            last_cmd.Parameters["Z"],
            target.z,
            places=5,
            msg="Final plunge should go to target Z=0, not clearance height",
        )

    def test_plunge_to_negative_depth(self):
        """Test that plunge moves correctly go to negative Z depths"""
        start = FreeCAD.Vector(0, 0, 1)  # Start below clearance
        target = FreeCAD.Vector(10, 10, -2)  # Target depth is negative

        cmds = generator.get_linking_moves(
            start_position=start,
            target_position=target,
            local_clearance=self.local_clearance,
            global_clearance=self.global_clearance,
            tool_shape=self.tool,
            solids=[],
        )

        # The last command should be the plunge to target depth (Z=-2)
        last_cmd = cmds[-1]
        self.assertAlmostEqual(
            last_cmd.Parameters["Z"],
            target.z,
            places=5,
            msg="Final plunge should go to target Z=-2",
        )

    @unittest.skip("not yet implemented")
    def test_zero_retract_offset_uses_local_clearance(self):
        cmds = generator.get_linking_moves(
            start_position=self.start,
            target_position=FreeCAD.Vector(10, 0, 5),
            local_clearance=self.local_clearance,
            global_clearance=self.global_clearance,
            tool_shape=self.tool,
            retract_height_offset=0,
        )
        self.assertTrue(any(cmd for cmd in cmds if cmd.Parameters.get("Z") == self.local_clearance))

    @unittest.skip("not yet implemented")
    def test_path_generated_without_local_safe(self):
        pass