File size: 15,589 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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
# SPDX-License-Identifier: LGPL-2.1-or-later

# ***************************************************************************
# *   Copyright (c) 2014 Yorik van Havre <yorik@uncreated.net>              *
# *   Copyright (c) 2020 Schildkroet                                        *
# *                                                                         *
# *   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 Part
import Path
import Path.Base.FeedRate as PathFeedRate
import Path.Base.Generator.drill as drill
import Path.Base.Generator.linking as linking
import Path.Base.MachineState as PathMachineState
import Path.Op.Base as PathOp
import Path.Op.CircularHoleBase as PathCircularHoleBase
import PathScripts.PathUtils as PathUtils
from PySide.QtCore import QT_TRANSLATE_NOOP

__title__ = "CAM Drilling Operation"
__author__ = "sliptonic (Brad Collette)"
__url__ = "https://www.freecad.org"
__doc__ = "CAM Drilling operation."
__contributors__ = "IMBack!"

if False:
    Path.Log.setLevel(Path.Log.Level.DEBUG, Path.Log.thisModule())
    Path.Log.trackModule(Path.Log.thisModule())
else:
    Path.Log.setLevel(Path.Log.Level.INFO, Path.Log.thisModule())

translate = FreeCAD.Qt.translate


class ObjectDrilling(PathCircularHoleBase.ObjectOp):
    """Proxy object for Drilling operation."""

    @classmethod
    def propertyEnumerations(self, dataType="data"):
        """helixOpPropertyEnumerations(dataType="data")... return property enumeration lists of specified dataType.
        Args:
            dataType = 'data', 'raw', 'translated'
        Notes:
        'data' is list of internal string literals used in code
        'raw' is list of (translated_text, data_string) tuples
        'translated' is list of translated string literals
        """

        # Enumeration lists for App::PropertyEnumeration properties
        enums = {
            "ExtraOffset": [
                (translate("CAM_Drilling", "None"), "None"),
                (translate("CAM_Drilling", "Drill Tip"), "Drill Tip"),
                (translate("CAM_Drilling", "2x Drill Tip"), "2x Drill Tip"),
            ],  # extra drilling depth to clear drill taper
        }

        if dataType == "raw":
            return enums

        data = list()
        idx = 0 if dataType == "translated" else 1

        Path.Log.debug(enums)

        for k, v in enumerate(enums):
            data.append((v, [tup[idx] for tup in enums[v]]))
        Path.Log.debug(data)

        return data

    def circularHoleFeatures(self, obj):
        """circularHoleFeatures(obj) ... drilling works on anything, turn on all Base geometries and Locations."""
        return PathOp.FeatureBaseGeometry | PathOp.FeatureLocations | PathOp.FeatureCoolant

    def onDocumentRestored(self, obj):
        if hasattr(obj, "chipBreakEnabled"):
            obj.renameProperty("chipBreakEnabled", "ChipBreakEnabled")
        elif not hasattr(obj, "ChipBreakEnabled"):
            obj.addProperty(
                "App::PropertyBool",
                "ChipBreakEnabled",
                "Drill",
                QT_TRANSLATE_NOOP("App::Property", "Use chipbreaking"),
            )

        if hasattr(obj, "feedRetractEnabled"):
            obj.renameProperty("feedRetractEnabled", "FeedRetractEnabled")
        elif not hasattr(obj, "FeedRetractEnabled"):
            obj.addProperty(
                "App::PropertyBool",
                "FeedRetractEnabled",
                "Drill",
                QT_TRANSLATE_NOOP("App::Property", "Use G85 boring cycle with feed out"),
            )

        if hasattr(obj, "RetractMode"):
            obj.removeProperty("RetractMode")

        # Migration: Remove RetractHeight property and adjust StartDepth if needed
        if hasattr(obj, "RetractHeight"):
            # If RetractHeight was higher than StartDepth, migrate to StartDepth
            if obj.RetractHeight.Value > obj.StartDepth.Value:
                Path.Log.warning(
                    f"Migrating RetractHeight ({obj.RetractHeight.Value}) to StartDepth. "
                    f"Old StartDepth was {obj.StartDepth.Value}"
                )
                obj.StartDepth = obj.RetractHeight.Value
            obj.removeProperty("RetractHeight")

        if not hasattr(obj, "KeepToolDown"):
            obj.addProperty(
                "App::PropertyBool",
                "KeepToolDown",
                "Drill",
                QT_TRANSLATE_NOOP(
                    "App::Property",
                    "Apply G99 retraction: only retract to StartDepth between holes in this operation",
                ),
            )

    def initCircularHoleOperation(self, obj):
        """initCircularHoleOperation(obj) ... add drilling specific properties to obj."""
        obj.addProperty(
            "App::PropertyLength",
            "PeckDepth",
            "Drill",
            QT_TRANSLATE_NOOP(
                "App::Property",
                "Incremental Drill depth before retracting to clear chips",
            ),
        )
        obj.addProperty(
            "App::PropertyBool",
            "PeckEnabled",
            "Drill",
            QT_TRANSLATE_NOOP("App::Property", "Enable pecking"),
        )
        obj.addProperty(
            "App::PropertyBool",
            "ChipBreakEnabled",
            "Drill",
            QT_TRANSLATE_NOOP("App::Property", "Use chipbreaking"),
        )
        obj.addProperty(
            "App::PropertyFloat",
            "DwellTime",
            "Drill",
            QT_TRANSLATE_NOOP("App::Property", "The time to dwell between peck cycles"),
        )
        obj.addProperty(
            "App::PropertyBool",
            "DwellEnabled",
            "Drill",
            QT_TRANSLATE_NOOP("App::Property", "Enable dwell"),
        )
        obj.addProperty(
            "App::PropertyBool",
            "AddTipLength",
            "Drill",
            QT_TRANSLATE_NOOP(
                "App::Property",
                "Calculate the tip length and subtract from final depth",
            ),
        )
        obj.addProperty(
            "App::PropertyEnumeration",
            "ExtraOffset",
            "Drill",
            QT_TRANSLATE_NOOP("App::Property", "How far the drilling depth is extended"),
        )
        obj.addProperty(
            "App::PropertyBool",
            "KeepToolDown",
            "Drill",
            QT_TRANSLATE_NOOP(
                "App::Property",
                "Apply G99 retraction: only retract to StartDepth between holes in this operation",
            ),
        )
        obj.addProperty(
            "App::PropertyBool",
            "FeedRetractEnabled",
            "Drill",
            QT_TRANSLATE_NOOP("App::Property", "Use G85 boring cycle with feed out"),
        )

        for n in self.propertyEnumerations():
            setattr(obj, n[0], n[1])

    def circularHoleExecute(self, obj, holes):
        """circularHoleExecute(obj, holes) ... generate drill operation for each hole in holes."""
        Path.Log.track()
        machinestate = PathMachineState.MachineState()
        # We should be at clearance height.

        mode = "G99" if obj.KeepToolDown else "G98"

        # Validate that SafeHeight doesn't exceed ClearanceHeight
        safe_height = obj.SafeHeight.Value
        if safe_height > obj.ClearanceHeight.Value:
            Path.Log.warning(
                f"SafeHeight ({safe_height}) is above ClearanceHeight ({obj.ClearanceHeight.Value}). "
                f"Using ClearanceHeight instead."
            )
            safe_height = obj.ClearanceHeight.Value

        # Calculate offsets to add to target edge
        endoffset = 0.0
        if obj.ExtraOffset == "Drill Tip":
            endoffset = PathUtils.drillTipLength(self.tool)
        elif obj.ExtraOffset == "2x Drill Tip":
            endoffset = PathUtils.drillTipLength(self.tool) * 2

        # compute the drilling targets
        edgelist = []
        for hole in holes:
            v1 = FreeCAD.Vector(hole["x"], hole["y"], obj.StartDepth.Value)
            v2 = FreeCAD.Vector(hole["x"], hole["y"], obj.FinalDepth.Value - endoffset)
            edgelist.append(Part.makeLine(v1, v2))

        # build list of solids for collision detection.
        # Include base objects from job
        solids = []
        for base in self.job.Model.Group:
            solids.append(base.Shape)

        # http://linuxcnc.org/docs/html/gcode/g-code.html#gcode:g98-g99

        # This section is technical debt. The computation of the
        # target shapes should be factored out for reuse.
        # This will likely mean refactoring upstream CircularHoleBase to pass
        # spotshapes instead of holes.

        # Start computing the Path
        self.commandlist.append(Path.Command("(Begin Drilling)"))

        # Make sure tool is at a clearance height
        command = Path.Command("G0", {"Z": obj.ClearanceHeight.Value})
        machinestate.addCommand(command)

        # machine.addCommand(command)
        self.commandlist.append(command)

        # iterate the edgelist and generate gcode
        firstMove = True
        for edge in edgelist:
            Path.Log.debug(edge)

            # Get the target start point
            startPoint = edge.Vertexes[0].Point

            # Get linking moves from current to start of target
            if firstMove:  # Build manually
                command = Path.Command("G0", {"X": startPoint.x, "Y": startPoint.y})
                self.commandlist.append(command)
                machinestate.addCommand(command)
                command = Path.Command("G0", {"Z": safe_height})
                self.commandlist.append(command)
                machinestate.addCommand(command)
                firstMove = False

            else:  # Check if we need linking moves
                # For G99 mode, tool is at StartDepth (R-plane) after previous hole
                # Check if direct move at retract plane would collide with model
                current_pos = machinestate.getPosition()
                target_at_retract_plane = FreeCAD.Vector(startPoint.x, startPoint.y, current_pos.z)

                # Check collision at the retract plane (current Z height)
                collision_detected = linking.check_collision(
                    start_position=current_pos,
                    target_position=target_at_retract_plane,
                    solids=solids,
                )

                if collision_detected:
                    # Cannot traverse at retract plane - need to break cycle group
                    # Retract to safe height, traverse, then plunge to safe height for new cycle
                    target_at_safe_height = FreeCAD.Vector(startPoint.x, startPoint.y, safe_height)
                    linking_moves = linking.get_linking_moves(
                        start_position=current_pos,
                        target_position=target_at_safe_height,
                        local_clearance=safe_height,
                        global_clearance=obj.ClearanceHeight.Value,
                        tool_shape=self.tool.Shape,
                        solids=solids,
                    )
                    self.commandlist.extend(linking_moves)
                    for move in linking_moves:
                        machinestate.addCommand(move)
                # else: no collision - G99 cycle continues, tool stays at retract plane

            # Perform drilling
            dwelltime = obj.DwellTime if obj.DwellEnabled else 0.0
            peckdepth = obj.PeckDepth.Value if obj.PeckEnabled else 0.0
            repeat = 1  # technical debt:  Add a repeat property for user control
            chipBreak = obj.ChipBreakEnabled and obj.PeckEnabled

            # Save Z position before canned cycle for G98 retract
            z_before_cycle = machinestate.Z

            try:
                drillcommands = drill.generate(
                    edge,
                    dwelltime,
                    peckdepth,
                    repeat,
                    obj.StartDepth.Value,
                    chipBreak=chipBreak,
                    feedRetract=obj.FeedRetractEnabled,
                )

            except ValueError as e:  # any targets that fail the generator are ignored
                Path.Log.info(e)
                continue

            # Set RetractMode annotation for each command
            for command in drillcommands:
                annotations = command.Annotations
                annotations["RetractMode"] = mode
                command.Annotations = annotations
                self.commandlist.append(command)
                machinestate.addCommand(command)

            # Update Z position based on RetractMode
            # G98: retract to initial Z (Z before cycle started)
            # G99: retract to R parameter (StartDepth)
            if mode == "G98":
                machinestate.Z = z_before_cycle
            else:  # G99
                machinestate.Z = obj.StartDepth.Value

        # Apply feedrates to commands
        PathFeedRate.setFeedRate(self.commandlist, obj.ToolController)

    def opSetDefaultValues(self, obj, job):
        """opSetDefaultValues(obj, job) ... set default values for drilling operation"""
        obj.ExtraOffset = "None"
        obj.KeepToolDown = False  # default to safest option: G98

        if hasattr(job.SetupSheet, "PeckDepth"):
            obj.PeckDepth = job.SetupSheet.PeckDepth
        elif self.applyExpression(obj, "PeckDepth", "OpToolDiameter*0.75"):
            obj.PeckDepth = 1

        if hasattr(job.SetupSheet, "DwellTime"):
            obj.DwellTime = job.SetupSheet.DwellTime
        else:
            obj.DwellTime = 1


def SetupProperties():
    setup = []
    setup.append("PeckDepth")
    setup.append("PeckEnabled")
    setup.append("DwellTime")
    setup.append("DwellEnabled")
    setup.append("AddTipLength")
    setup.append("ExtraOffset")
    setup.append("KeepToolDown")
    return setup


def Create(name, obj=None, parentJob=None):
    """Create(name) ... Creates and returns a Drilling operation."""
    if obj is None:
        obj = FreeCAD.ActiveDocument.addObject("Path::FeaturePython", name)

    obj.Proxy = ObjectDrilling(obj, name, parentJob)
    if obj.Proxy:
        obj.Proxy.findAllHoles(obj)

    return obj