Files
FreeCAD/src/Mod/Path/PathScripts/PathPocketShape.py
T
Russell Johnson fd6298d542 Path: Refactor and upgradeExtensions feature, and apply to Adaptive op
Path: Refactor `Extensions` Gui code into independent module. Move the `Extensions` Gui code to independent module so access to other operations will be easier.
Path: Add `Extensions` feature to Adaptive operation
Path: Isolate Adaptive GUI elements in preparation of Adaptive unit tests
Path: Implement `PathLog` debug module
Path: Implement `translate()` for multi-language message support
Path: Fix `StockType`check bug
Path: Relocate `getCutRegionWires()` to `FeatureExtensions` module
Path: Add `Extensions` property checks on document restored
Path: Improve `Extend Outline` feature implementation
Path: Initialize a waterline type extension
Path: Add enable/disable extensions feature. It is quite possible that many complex faces exist that have large quantities of both simple and complex edges.  For this reason, a manual push button to enable Extensions is useful so the users machine is not bogged down with extra or unnecessary computing time.  Extensions are not necessary at all times. This commit also includes an edge count threshold that will disable the Extensions feature temporarily upon initial loading of the Task Panel.  The manual enable button will do just that.
Path: Add enable extensions warning label
Path: Shorten enable/disable Extensions button message
Path: Remove run-time added Task Panel elements - this QButton and QLabel were moved to UI panel directly.
Path: Add include/ignore Edges button
Path: Improve extension preview rendering
Path: Fixes for `useOutline` modification and updates
Path: Add internal feature to cache calculated extensions for reuse
Path: Add `SetupProperties()` function and connect to GUI command
Path: Add `Avoid Face` extension to ignore base face. This feature allows for some simple access to the exterior of a selected face without clearing the face itself.  This will allow for an exterior clearing operation in a simple manner.
Path: Fix bug restricting extensions on internal closed-wires
2021-06-04 09:48:50 -05:00

252 lines
11 KiB
Python

# -*- coding: utf-8 -*-
# ***************************************************************************
# * Copyright (c) 2017 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 PathScripts.PathGeom as PathGeom
import PathScripts.PathLog as PathLog
import PathScripts.PathOp as PathOp
import PathScripts.PathPocketBase as PathPocketBase
from PySide import QtCore
# lazily loaded modules
from lazy_loader.lazy_loader import LazyLoader
Part = LazyLoader('Part', globals(), 'Part')
TechDraw = LazyLoader('TechDraw', globals(), 'TechDraw')
math = LazyLoader('math', globals(), 'math')
PathUtils = LazyLoader('PathScripts.PathUtils', globals(), 'PathScripts.PathUtils')
FeatureExtensions = LazyLoader('PathScripts.PathFeatureExtensions',
globals(),
'PathScripts.PathFeatureExtensions')
__title__ = "Path Pocket Shape Operation"
__author__ = "sliptonic (Brad Collette)"
__url__ = "https://www.freecadweb.org"
__doc__ = "Class and implementation of shape based Pocket operation."
PathLog.setLevel(PathLog.Level.INFO, PathLog.thisModule())
# PathLog.trackModule(PathLog.thisModule())
# Qt translation handling
def translate(context, text, disambig=None):
return QtCore.QCoreApplication.translate(context, text, disambig)
class ObjectPocket(PathPocketBase.ObjectPocket):
'''Proxy object for Pocket operation.'''
def areaOpFeatures(self, obj):
return super(self.__class__, self).areaOpFeatures(obj) | PathOp.FeatureLocations
def initPocketOp(self, obj):
'''initPocketOp(obj) ... setup receiver'''
if not hasattr(obj, 'UseOutline'):
obj.addProperty('App::PropertyBool', 'UseOutline', 'Pocket', QtCore.QT_TRANSLATE_NOOP('PathPocketShape', 'Uses the outline of the base geometry.'))
FeatureExtensions.initialize_properties(obj)
def areaOpOnDocumentRestored(self, obj):
'''opOnDocumentRestored(obj) ... adds the UseOutline property if it doesn't exist.'''
self.initPocketOp(obj)
def pocketInvertExtraOffset(self):
return False
def areaOpSetDefaultValues(self, obj, job):
'''areaOpSetDefaultValues(obj, job) ... set default values'''
obj.StepOver = 100
obj.ZigZagAngle = 45
obj.UseOutline = False
FeatureExtensions.set_default_property_values(obj, job)
def areaOpShapes(self, obj):
'''areaOpShapes(obj) ... return shapes representing the solids to be removed.'''
PathLog.track()
# self.isDebug = True if PathLog.getLevel(PathLog.thisModule()) == 4 else False
self.removalshapes = []
avoidFeatures = list()
# Get extensions and identify faces to avoid
extensions = FeatureExtensions.getExtensions(obj)
for e in extensions:
if e.avoid:
avoidFeatures.append(e.feature)
if obj.Base:
PathLog.debug('base items exist. Processing...')
self.horiz = []
self.vert = []
for (base, subList) in obj.Base:
for sub in subList:
if 'Face' in sub:
if sub not in avoidFeatures and not self.clasifySub(base, sub):
PathLog.error(translate('PathPocket', 'Pocket does not support shape %s.%s') % (base.Label, sub))
# Convert horizontal faces to use outline only if requested
if obj.UseOutline and self.horiz:
horiz = [Part.Face(f.Wire1) for f in self.horiz]
self.horiz = horiz
# Check if selected vertical faces form a loop
if len(self.vert) > 0:
self.vertical = PathGeom.combineConnectedShapes(self.vert)
self.vWires = [TechDraw.findShapeOutline(shape, 1, FreeCAD.Vector(0, 0, 1)) for shape in self.vertical]
for wire in self.vWires:
w = PathGeom.removeDuplicateEdges(wire)
face = Part.Face(w)
# face.tessellate(0.1)
if PathGeom.isRoughly(face.Area, 0):
PathLog.error(translate('PathPocket', 'Vertical faces do not form a loop - ignoring'))
else:
self.horiz.append(face)
# Add faces for extensions
self.exts = [] # pylint: disable=attribute-defined-outside-init
for ext in extensions:
if not ext.avoid:
wire = ext.getWire()
if wire:
faces = ext.getExtensionFaces(wire)
for f in faces:
self.horiz.append(f)
self.exts.append(f)
# check all faces and see if they are touching/overlapping and combine and simplify
self.horizontal = PathGeom.combineHorizontalFaces(self.horiz)
# Move all faces to final depth before extrusion
for h in self.horizontal:
h.translate(FreeCAD.Vector(0.0, 0.0, obj.FinalDepth.Value - h.BoundBox.ZMin))
# extrude all faces up to StartDepth and those are the removal shapes
extent = FreeCAD.Vector(0, 0, obj.StartDepth.Value - obj.FinalDepth.Value)
self.removalshapes = [(face.removeSplitter().extrude(extent), False) for face in self.horizontal]
else: # process the job base object as a whole
PathLog.debug("processing the whole job base object")
self.outlines = [Part.Face(TechDraw.findShapeOutline(base.Shape, 1, FreeCAD.Vector(0, 0, 1))) for base in self.model]
stockBB = self.stock.Shape.BoundBox
self.bodies = []
for outline in self.outlines:
outline.translate(FreeCAD.Vector(0, 0, stockBB.ZMin - 1))
body = outline.extrude(FreeCAD.Vector(0, 0, stockBB.ZLength + 2))
self.bodies.append(body)
self.removalshapes.append((self.stock.Shape.cut(body), False))
# Tessellate all working faces
# for (shape, hole) in self.removalshapes:
# shape.tessellate(0.05) # originally 0.1
if self.removalshapes:
obj.removalshape = self.removalshapes[0][0]
return self.removalshapes
# Support methods
def isVerticalExtrusionFace(self, face):
fBB = face.BoundBox
if PathGeom.isRoughly(fBB.ZLength, 0.0):
return False
extr = face.extrude(FreeCAD.Vector(0.0, 0.0, fBB.ZLength))
if hasattr(extr, "Volume"):
if PathGeom.isRoughly(extr.Volume, 0.0):
return True
return False
def clasifySub(self, bs, sub):
'''clasifySub(bs, sub)...
Given a base and a sub-feature name, returns True
if the sub-feature is a horizontally oriented flat face.
'''
face = bs.Shape.getElement(sub)
if type(face.Surface) == Part.Plane:
PathLog.debug('type() == Part.Plane')
if PathGeom.isVertical(face.Surface.Axis):
PathLog.debug(' -isVertical()')
# it's a flat horizontal face
self.horiz.append(face)
return True
elif PathGeom.isHorizontal(face.Surface.Axis):
PathLog.debug(' -isHorizontal()')
self.vert.append(face)
return True
else:
return False
elif type(face.Surface) == Part.Cylinder and PathGeom.isVertical(face.Surface.Axis):
PathLog.debug('type() == Part.Cylinder')
# vertical cylinder wall
if any(e.isClosed() for e in face.Edges):
PathLog.debug(' -e.isClosed()')
# complete cylinder
circle = Part.makeCircle(face.Surface.Radius, face.Surface.Center)
disk = Part.Face(Part.Wire(circle))
disk.translate(FreeCAD.Vector(0, 0, face.BoundBox.ZMin - disk.BoundBox.ZMin))
self.horiz.append(disk)
return True
else:
PathLog.debug(' -none isClosed()')
# partial cylinder wall
self.vert.append(face)
return True
elif type(face.Surface) == Part.SurfaceOfExtrusion:
# extrusion wall
PathLog.debug('type() == Part.SurfaceOfExtrusion')
# Save face to self.horiz for processing or display error
if self.isVerticalExtrusionFace(face):
self.vert.append(face)
return True
else:
PathLog.error(translate("Path", "Failed to identify vertical face from {}.".format(sub)))
else:
PathLog.debug(' -type(face.Surface): {}'.format(type(face.Surface)))
return False
# Eclass
def SetupProperties():
setup = PathPocketBase.SetupProperties() # Add properties from PocketBase module
setup.extend(FeatureExtensions.SetupProperties()) # Add properties from Extensions Feature
# Add properties initialized here in PocketShape
setup.append('UseOutline')
return setup
def Create(name, obj=None):
'''Create(name) ... Creates and returns a Pocket operation.'''
if obj is None:
obj = FreeCAD.ActiveDocument.addObject('Path::FeaturePython', name)
obj.Proxy = ObjectPocket(obj, name)
return obj