File size: 10,428 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# SPDX-License-Identifier: LGPL-2.1-or-later

# ***************************************************************************
# *   Copyright (c) 2019 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 pathlib

import Draft
import FreeCAD
import Path
import Path.Base.SetupSheetOpPrototype as PathSetupSheetOpPrototype
import Path.Main.Job as PathJob
import Path.Op.Helix as PathHelix
import CAMTests.PathTestUtils as PathTestUtils

FIXTURE_PATH = pathlib.Path(__file__).parent / "Fixtures"

Path.Log.setLevel(Path.Log.Level.INFO, Path.Log.thisModule())
# Path.Log.trackModule(Path.Log.thisModule())


class TestPathHelix(PathTestUtils.PathTestBase):
    RotateBy = 45

    def setUp(self):
        self.clone = None
        self.doc = FreeCAD.open(FreeCAD.getHomePath() + "Mod/CAM/CAMTests/test_holes00.fcstd")
        self.job = PathJob.Create("Job", [self.doc.Body])

        # the smallest hole in the fixture is 1mm in diameter, so our tool must be smaller.
        self.job.Tools.Group[0].Tool.Diameter = 0.9

    def tearDown(self):
        FreeCAD.closeDocument(self.doc.Name)

    def test00(self):
        """Verify Helix does not throw an exception."""

        op = PathHelix.Create("Helix")
        op.Proxy.execute(op)

    def testCreateWithPrototype(self):
        """Verify a Helix can be created on a SetupSheet's prototype instead of a real document object"""

        ptt = PathSetupSheetOpPrototype.OpPrototype("Helix")
        op = PathHelix.Create("OpPrototype.Helix", ptt)

    def test01(self):
        """Verify Helix generates proper holes from model"""

        op = PathHelix.Create("Helix")
        proxy = op.Proxy
        for base in op.Base:
            model = base[0]
            for sub in base[1]:
                pos = proxy.holePosition(op, model, sub)
                self.assertRoughly(round(pos.Length / 10, 0), proxy.holeDiameter(op, model, sub))

    def test02(self):
        """Verify Helix generates proper holes for rotated model"""

        op = PathHelix.Create("Helix")
        proxy = op.Proxy
        model = self.job.Model.Group[0]

        for deg in range(self.RotateBy, 360, self.RotateBy):
            model.Placement.Rotation = FreeCAD.Rotation(deg, 0, 0)
            for base in op.Base:
                model = base[0]
                for sub in base[1]:
                    pos = proxy.holePosition(op, model, sub)
                    # Path.Log.track(deg, pos, pos.Length)
                    self.assertRoughly(
                        round(pos.Length / 10, 0), proxy.holeDiameter(op, model, sub)
                    )

    def test03(self):
        """Verify Helix generates proper holes for rotated base model"""

        for deg in range(self.RotateBy, 360, self.RotateBy):
            self.tearDown()
            self.doc = FreeCAD.open(FreeCAD.getHomePath() + "Mod/CAM/CAMTests/test_holes00.fcstd")
            self.doc.Body.Placement.Rotation = FreeCAD.Rotation(deg, 0, 0)

            self.job = PathJob.Create("Job", [self.doc.Body])
            self.job.Tools.Group[0].Tool.Diameter = 0.5

            op = PathHelix.Create("Helix")
            proxy = op.Proxy
            model = self.job.Model.Group[0]

            for base in op.Base:
                model = base[0]
                for sub in base[1]:
                    pos = proxy.holePosition(op, model, sub)
                    # Path.Log.track(deg, pos, pos.Length)
                    self.assertRoughly(
                        round(pos.Length / 10, 0), proxy.holeDiameter(op, model, sub)
                    )

    def test04(self):
        """Verify Helix generates proper holes for rotated clone base model"""
        for deg in range(self.RotateBy, 360, self.RotateBy):
            self.tearDown()
            self.doc = FreeCAD.open(FreeCAD.getHomePath() + "Mod/CAM/CAMTests/test_holes00.fcstd")
            self.clone = Draft.clone(self.doc.Body)
            self.clone.Placement.Rotation = FreeCAD.Rotation(deg, 0, 0)

            self.job = PathJob.Create("Job", [self.clone])
            self.job.Tools.Group[0].Tool.Diameter = 0.5

            op = PathHelix.Create("Helix")
            proxy = op.Proxy
            model = self.job.Model.Group[0]

            for base in op.Base:
                model = base[0]
                for sub in base[1]:
                    pos = proxy.holePosition(op, model, sub)
                    # Path.Log.track(deg, pos, pos.Length)
                    self.assertRoughly(
                        round(pos.Length / 10, 0), proxy.holeDiameter(op, model, sub)
                    )

    def testPathDirection(self):
        """Verify that the generated paths obeys the given parameters"""
        helix = PathHelix.Create("Helix")

        def check(start_side, cut_mode, expected_direction):
            with self.subTest(f"({start_side}, {cut_mode}) => {expected_direction}"):
                helix.StartSide = start_side
                helix.CutMode = cut_mode

                self.assertSuccessfulRecompute(self.doc, helix)

                self.assertEqual(
                    helix.Direction,
                    expected_direction,
                    msg=f"Direction was not correctly determined",
                )
                self.assertPathDirection(
                    helix.Path,
                    expected_direction,
                    msg=f"Path with wrong direction generated",
                )

        check("Inside", "Conventional", "CW")
        check("Outside", "Climb", "CW")
        check("Inside", "Climb", "CCW")
        check("Outside", "Conventional", "CCW")

    def testRecomputeHelixFromV021(self):
        """Verify that we can still open and recompute a Helix created with older FreeCAD"""
        self.tearDown()
        self.doc = FreeCAD.openDocument(str(FIXTURE_PATH / "OpHelix_v0-21.FCStd"))
        created_with = f"created with {self.doc.getProgramVersion()}"

        def check(helix, direction, start_side, cut_mode):
            with self.subTest(f"{helix.Name}: ({direction}, {start_side}) => {cut_mode}"):
                # no recompute yet, i.e. check original as precondition
                self.assertPathDirection(
                    helix.Path,
                    direction,
                    msg=f"Path direction does not match fixture for {helix.Name} {created_with}",
                )
                self.assertEqual(
                    helix.Direction,
                    direction,
                    msg=f"Direction does not match fixture for {helix.Name} {created_with}",
                )
                self.assertEqual(
                    helix.StartSide,
                    start_side,
                    msg=f"StartSide does not match fixture for {helix.Name} {created_with}",
                )

                # now see whether we can recompute the object from the old document
                helix.enforceRecompute()
                self.assertSuccessfulRecompute(
                    self.doc, helix, msg=f"Cannot recompute {helix.Name} {created_with}"
                )
                self.assertEqual(
                    helix.Direction,
                    direction,
                    msg=f"Direction changed after recomputing {helix.Name} {created_with}",
                )
                self.assertEqual(
                    helix.StartSide,
                    start_side,
                    msg=f"StartSide changed after recomputing {helix.Name} {created_with}",
                )
                self.assertEqual(
                    helix.CutMode,
                    cut_mode,
                    msg=f"CutMode not correctly derived for {helix.Name} {created_with}",
                )
                self.assertPathDirection(
                    helix.Path,
                    direction,
                    msg=f"Path with wrong direction generated for {helix.Name} {created_with}",
                )

        # object names and expected values defined in the fixture
        check(self.doc.Helix, "CW", "Inside", "Conventional")
        check(self.doc.Helix001, "CW", "Outside", "Climb")
        check(self.doc.Helix002, "CCW", "Inside", "Climb")
        check(self.doc.Helix003, "CCW", "Outside", "Conventional")

    def assertPathDirection(self, path, expected_direction, msg=None):
        """Asserts that the given path goes into the expected direction.

        For the general case we'd need to check the sign of the second derivative,
        but as we know we work on a helix here, we can take a short cut and just
        look at the G2/G3 arc commands.
        """
        has_g2 = any(filter(lambda cmd: cmd.Name == "G2", path.Commands))
        has_g3 = any(filter(lambda cmd: cmd.Name == "G3", path.Commands))
        if has_g2 and not has_g3:
            self.assertEqual("CW", expected_direction, msg)
        elif has_g3 and not has_g2:
            self.assertEqual("CCW", expected_direction, msg)
        else:
            raise NotImplementedError("Cannot determine direction for arbitrary paths")