File size: 15,831 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 | # 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 *
# * *
# ***************************************************************************
from PySide.QtCore import QT_TRANSLATE_NOOP
import FreeCAD
import Path
import Path.Base.Util as PathUtil
import Path.Dressup.Utils as PathDressup
import Path.Main.Stock as PathStock
import PathScripts.PathUtils as PathUtils
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
def _vstr(v):
if v:
return "(%.2f, %.2f, %.2f)" % (v.x, v.y, v.z)
return "-"
class DressupPathBoundary(object):
def promoteStockToBoundary(self, stock):
"""Ensure stock object has boundary properties set."""
if stock:
if not hasattr(stock, "IsBoundary"):
stock.addProperty("App::PropertyBool", "IsBoundary", "Base")
stock.IsBoundary = True
if hasattr(stock, "setEditorMode"):
stock.setEditorMode("IsBoundary", 3)
stock.Label = "Boundary"
def __init__(self, obj, base, job):
obj.addProperty(
"App::PropertyLink",
"Base",
"Base",
QT_TRANSLATE_NOOP("App::Property", "The base path to modify"),
)
obj.Base = base
obj.addProperty(
"App::PropertyLink",
"Stock",
"Boundary",
QT_TRANSLATE_NOOP(
"App::Property",
"Solid object to be used to limit the generated Path.",
),
)
obj.Stock = PathStock.CreateFromBase(job)
self.promoteStockToBoundary(obj.Stock)
obj.addProperty(
"App::PropertyBool",
"Inside",
"Boundary",
QT_TRANSLATE_NOOP(
"App::Property",
"Determines if Boundary describes an inclusion or exclusion mask.",
),
)
obj.Inside = True
obj.addProperty(
"App::PropertyBool",
"KeepToolDown",
"Boundary",
QT_TRANSLATE_NOOP(
"App::Property",
"Keep tool down.",
),
)
obj.KeepToolDown = False
self.obj = obj
self.safeHeight = None
self.clearanceHeight = None
def dumps(self):
return None
def loads(self, state):
return None
def onChanged(self, obj, prop):
if prop == "Path" and obj.ViewObject:
obj.ViewObject.signalChangeIcon()
# If Stock is changed, ensure boundary stock properties are set
if prop == "Stock" and obj.Stock:
self.promoteStockToBoundary(obj.Stock)
def onDocumentRestored(self, obj):
self.obj = obj
# Ensure Stock property exists and is flagged as boundary stock
self.promoteStockToBoundary(obj.Stock)
if not hasattr(obj, "KeepToolDown"):
obj.addProperty(
"App::PropertyBool",
"KeepToolDown",
"Boundary",
QT_TRANSLATE_NOOP(
"App::Property",
"Keep tool down.",
),
)
def onDelete(self, obj, args):
if obj.Base:
job = PathUtils.findParentJob(obj)
if job:
job.Proxy.addOperation(obj.Base, obj)
if obj.Base.ViewObject:
obj.Base.ViewObject.Visibility = True
obj.Base = None
if hasattr(obj, "Stock") and obj.Stock:
obj.Document.removeObject(obj.Stock.Name)
obj.Stock = None
return True
def execute(self, obj):
if not hasattr(obj, "Stock") or obj.Stock is None:
Path.Log.error("BoundaryStock (Stock) missing; cannot execute dressup.")
obj.Path = Path.Path([])
return
if not hasattr(obj.Stock, "Shape") or obj.Stock.Shape is None:
Path.Log.error("Boundary stock has no Shape; cannot execute dressup.")
obj.Path = Path.Path([])
return
pb = PathBoundary(obj.Base, obj.Stock.Shape, obj.Inside, obj.KeepToolDown)
obj.Path = pb.execute()
# Eclass
class PathBoundary:
"""class PathBoundary...
This class requires a base operation, boundary shape, and optional inside boolean (default is True).
The `execute()` method returns a Path object with path commands limited to cut paths inside or outside
the provided boundary shape.
"""
def __init__(self, baseOp, boundaryShape, inside=True, keepToolDown=False):
self.baseOp = baseOp
self.boundary = boundaryShape
self.inside = inside
self.safeHeight = None
self.clearanceHeight = None
self.strG0ZsafeHeight = None
self.strG0ZclearanceHeight = None
self.keepToolDown = keepToolDown
def boundaryCommands(
self, begin, end, verticalFeed, horizFeed=None, keepToolDown=False, isStartMovements=False
):
Path.Log.track(_vstr(begin), _vstr(end))
if end and Path.Geom.pointsCoincide(begin, end):
return []
cmds = []
if isStartMovements or not keepToolDown:
if begin.z < self.safeHeight:
cmds.append(self.strG0ZsafeHeight)
if begin.z < self.clearanceHeight:
cmds.append(self.strG0ZclearanceHeight)
if end:
cmds.append(Path.Command("G0", {"X": end.x, "Y": end.y}))
if end.z < self.clearanceHeight:
cmds.append(Path.Command("G0", {"Z": max(self.safeHeight, end.z)}))
if end.z < self.safeHeight:
cmds.append(Path.Command("G1", {"Z": end.z, "F": verticalFeed}))
else:
if end:
if horizFeed and Path.Geom.isRoughly(begin.z, end.z, 0.001):
speed = horizFeed
else:
verticalFeed
cmds.append(Path.Command("G1", {"X": end.x, "Y": end.y, "Z": end.z, "F": speed}))
return cmds
def execute(self):
if (
not self.baseOp
or not self.baseOp.isDerivedFrom("Path::Feature")
or not self.baseOp.Path
):
return None
path = PathUtils.getPathWithPlacement(self.baseOp)
if len(path.Commands) == 0:
Path.Log.warning("No Path Commands for %s" % self.baseOp.Label)
return []
tc = PathDressup.toolController(self.baseOp)
self.safeHeight = float(PathUtil.opProperty(self.baseOp, "SafeHeight"))
self.clearanceHeight = float(PathUtil.opProperty(self.baseOp, "ClearanceHeight"))
self.strG0ZsafeHeight = Path.Command( # was a Feed rate with G1
"G0", {"Z": self.safeHeight, "F": tc.VertRapid.Value}
)
self.strG0ZclearanceHeight = Path.Command("G0", {"Z": self.clearanceHeight})
cmd = path.Commands[0]
pos = cmd.Placement.Base # bogus m/c position to create first edge
bogusX = True
bogusY = True
commands = [cmd]
lastExit = None
isStartMovements = True
for cmd in path.Commands[1:]:
if cmd.Name in Path.Geom.CmdMoveAll:
if bogusX:
bogusX = "X" not in cmd.Parameters
if bogusY:
bogusY = "Y" not in cmd.Parameters
edge = Path.Geom.edgeForCmd(cmd, pos)
if edge and cmd.Name in Path.Geom.CmdMoveDrill:
inside = edge.common(self.boundary).Edges
outside = edge.cut(self.boundary).Edges
if 1 == len(inside) and 0 == len(outside):
commands.append(cmd)
if edge and not cmd.Name in Path.Geom.CmdMoveDrill:
inside = edge.common(self.boundary).Edges
outside = edge.cut(self.boundary).Edges
if not self.inside: # UI "inside boundary" param
tmp = inside
inside = outside
outside = tmp
# it's really a shame that one cannot trust the sequence and/or
# orientation of edges
if 1 == len(inside) and 0 == len(outside):
Path.Log.track(_vstr(pos), _vstr(lastExit), " + ", cmd)
# cmd fully included by boundary
if lastExit:
if not (
bogusX or bogusY
): # don't insert false paths based on bogus m/c position
commands.extend(
self.boundaryCommands(lastExit, pos, tc.VertFeed.Value)
)
lastExit = None
commands.append(cmd)
pos = Path.Geom.commandEndPoint(cmd, pos)
elif 0 == len(inside) and 1 == len(outside):
Path.Log.track(_vstr(pos), _vstr(lastExit), " - ", cmd)
# cmd fully excluded by boundary
if not lastExit:
lastExit = pos
pos = Path.Geom.commandEndPoint(cmd, pos)
else:
Path.Log.track(_vstr(pos), _vstr(lastExit), len(inside), len(outside), cmd)
# cmd pierces boundary
while inside or outside:
ie = [e for e in inside if Path.Geom.edgeConnectsTo(e, pos)]
Path.Log.track(ie)
if ie:
e = ie[0]
LastPt = e.valueAt(e.LastParameter)
flip = Path.Geom.pointsCoincide(pos, LastPt)
newPos = e.valueAt(e.FirstParameter) if flip else LastPt
# inside edges are taken at this point (see swap of inside/outside
# above - so we can just connect the dots ...
if lastExit:
if not (bogusX or bogusY):
commands.extend(
self.boundaryCommands(
lastExit,
pos,
tc.VertFeed.Value,
tc.HorizFeed.Value,
self.keepToolDown,
isStartMovements,
)
)
isStartMovements = False
lastExit = None
Path.Log.track(e, flip)
if not (
bogusX or bogusY
): # don't insert false paths based on bogus m/c position
commands.extend(
Path.Geom.cmdsForEdge(
e,
flip,
tc.HorizFeed.Value,
tc.VertFeed.Value,
)
)
inside.remove(e)
pos = newPos
lastExit = newPos
else:
oe = [e for e in outside if Path.Geom.edgeConnectsTo(e, pos)]
Path.Log.track(oe)
if oe:
e = oe[0]
ptL = e.valueAt(e.LastParameter)
flip = Path.Geom.pointsCoincide(pos, ptL)
newPos = e.valueAt(e.FirstParameter) if flip else ptL
# outside edges are never taken at this point (see swap of
# inside/outside above) - so just move along ...
outside.remove(e)
pos = newPos
else:
Path.Log.error("huh?")
import Part
Part.show(Part.Vertex(pos), "pos")
for e in inside:
Part.show(e, "ei")
for e in outside:
Part.show(e, "eo")
raise Exception("This is not supposed to happen")
# Eif
# Eif
# Ewhile
# Eif
# pos = Path.Geom.commandEndPoint(cmd, pos)
# Eif
else:
Path.Log.track("no-move", cmd)
commands.append(cmd)
if lastExit:
commands.extend(self.boundaryCommands(lastExit, None, tc.VertFeed.Value))
lastExit = None
Path.Log.track(commands)
return Path.Path(commands)
# Eclass
def Create(base, name="DressupPathBoundary"):
"""Create(base, name='DressupPathBoundary') ... creates a dressup limiting base's Path to a boundary."""
if not base.isDerivedFrom("Path::Feature"):
Path.Log.error(
translate("CAM_DressupPathBoundary", "The selected object is not a path") + "\n"
)
return None
obj = FreeCAD.ActiveDocument.addObject("Path::FeaturePython", name)
job = PathUtils.findParentJob(base)
obj.Proxy = DressupPathBoundary(obj, base, job)
job.Proxy.addOperation(obj, base, True)
return obj
|