2019-01-03 19:25:08 +00:00
|
|
|
from tclCommands.TclCommand import TclCommandSignaled
|
2019-10-24 23:10:52 +00:00
|
|
|
|
|
|
|
import logging
|
|
|
|
import collections
|
2019-02-06 19:37:50 +00:00
|
|
|
from copy import deepcopy
|
2019-07-09 10:58:33 +00:00
|
|
|
from shapely.ops import cascaded_union
|
|
|
|
from shapely.geometry import Polygon, LineString, LinearRing
|
2019-01-03 19:25:08 +00:00
|
|
|
|
2020-04-24 04:23:14 +00:00
|
|
|
import gettext
|
|
|
|
import FlatCAMTranslation as fcTranslate
|
|
|
|
import builtins
|
|
|
|
|
2019-10-24 23:10:52 +00:00
|
|
|
log = logging.getLogger('base')
|
|
|
|
|
2020-04-24 04:23:14 +00:00
|
|
|
fcTranslate.apply_language('strings')
|
|
|
|
if '_' not in builtins.__dict__:
|
|
|
|
_ = gettext.gettext
|
|
|
|
|
2019-05-19 14:15:24 +00:00
|
|
|
|
2019-01-03 19:25:08 +00:00
|
|
|
class TclCommandGeoCutout(TclCommandSignaled):
|
|
|
|
"""
|
2019-07-09 10:58:33 +00:00
|
|
|
Tcl shell command to create a board cutout geometry.
|
|
|
|
Allow cutout for any shape.
|
|
|
|
Cuts holding gaps from geometry.
|
2019-02-06 14:59:17 +00:00
|
|
|
|
|
|
|
example:
|
2019-01-03 19:25:08 +00:00
|
|
|
|
2019-02-06 14:59:17 +00:00
|
|
|
"""
|
2019-01-03 19:25:08 +00:00
|
|
|
|
2019-02-06 14:59:17 +00:00
|
|
|
# List of all command aliases, to be able use old
|
|
|
|
# names for backward compatibility (add_poly, add_polygon)
|
|
|
|
aliases = ['geocutout', 'geoc']
|
|
|
|
|
2020-04-13 16:15:20 +00:00
|
|
|
description = '%s %s' % ("--", "Creates board cutout from an object (Gerber or Geometry) of any shape.")
|
|
|
|
|
2019-02-06 14:59:17 +00:00
|
|
|
# Dictionary of types from Tcl command, needs to be ordered
|
2019-01-03 19:25:08 +00:00
|
|
|
arg_names = collections.OrderedDict([
|
2019-02-06 14:59:17 +00:00
|
|
|
('name', str),
|
2019-01-03 19:25:08 +00:00
|
|
|
])
|
|
|
|
|
2019-02-06 14:59:17 +00:00
|
|
|
# Dictionary of types from Tcl command, needs to be ordered,
|
|
|
|
# this is for options like -optionname value
|
2019-01-03 19:25:08 +00:00
|
|
|
option_types = collections.OrderedDict([
|
|
|
|
('dia', float),
|
|
|
|
('margin', float),
|
|
|
|
('gapsize', float),
|
2020-04-13 02:50:59 +00:00
|
|
|
('gaps', str),
|
|
|
|
('outname', str)
|
2019-01-03 19:25:08 +00:00
|
|
|
])
|
|
|
|
|
|
|
|
# array of mandatory options for current Tcl command: required = {'name','outname'}
|
|
|
|
required = ['name']
|
|
|
|
|
|
|
|
# structured help for current command, args needs to be ordered
|
|
|
|
help = {
|
2020-04-13 16:15:20 +00:00
|
|
|
'main': 'Creates board cutout from an object (Gerber or Geometry) of any shape.',
|
2019-01-03 19:25:08 +00:00
|
|
|
'args': collections.OrderedDict([
|
2020-04-09 01:13:04 +00:00
|
|
|
('name', 'Name of the object to be cutout. Required'),
|
2019-01-03 19:25:08 +00:00
|
|
|
('dia', 'Tool diameter.'),
|
|
|
|
('margin', 'Margin over bounds.'),
|
2019-02-06 14:59:17 +00:00
|
|
|
('gapsize', 'size of gap.'),
|
|
|
|
('gaps', "type of gaps. Can be: 'tb' = top-bottom, 'lr' = left-right, '2tb' = 2top-2bottom, "
|
2020-04-13 02:50:59 +00:00
|
|
|
"'2lr' = 2left-2right, '4' = 4 cuts, '8' = 8 cuts"),
|
|
|
|
('outname', 'Name of the resulting Geometry object.'),
|
2019-01-03 19:25:08 +00:00
|
|
|
]),
|
2020-04-09 01:13:04 +00:00
|
|
|
'examples': [" #isolate margin for example from Fritzing arduino shield or any svg etc\n" +
|
2019-02-06 14:59:17 +00:00
|
|
|
" isolate BCu_margin -dia 3 -overlap 1\n" +
|
|
|
|
"\n" +
|
|
|
|
" #create exteriors from isolated object\n" +
|
|
|
|
" exteriors BCu_margin_iso -outname BCu_margin_iso_exterior\n" +
|
|
|
|
"\n" +
|
|
|
|
" #delete isolated object if you dond need id anymore\n" +
|
|
|
|
" delete BCu_margin_iso\n" +
|
|
|
|
"\n" +
|
|
|
|
" #finally cut holding gaps\n" +
|
2020-04-13 02:50:59 +00:00
|
|
|
" geocutout BCu_margin_iso_exterior -dia 3 -gapsize 0.6 -gaps 4 -outname cutout_geo\n"]
|
2019-01-03 19:25:08 +00:00
|
|
|
}
|
|
|
|
|
2019-02-06 20:32:32 +00:00
|
|
|
flat_geometry = []
|
|
|
|
|
2019-01-03 19:25:08 +00:00
|
|
|
def execute(self, args, unnamed_args):
|
|
|
|
"""
|
|
|
|
|
2019-02-06 14:59:17 +00:00
|
|
|
:param args:
|
|
|
|
:param unnamed_args:
|
|
|
|
:return:
|
2019-01-03 19:25:08 +00:00
|
|
|
"""
|
|
|
|
|
2019-07-09 10:58:33 +00:00
|
|
|
# def subtract_rectangle(obj_, x0, y0, x1, y1):
|
|
|
|
# pts = [(x0, y0), (x1, y0), (x1, y1), (x0, y1)]
|
|
|
|
# obj_.subtract_polygon(pts)
|
2019-01-03 19:25:08 +00:00
|
|
|
|
2019-02-06 20:32:32 +00:00
|
|
|
def substract_rectangle_geo(geo, x0, y0, x1, y1):
|
|
|
|
pts = [(x0, y0), (x1, y0), (x1, y1), (x0, y1)]
|
|
|
|
|
|
|
|
def flatten(geometry=None, reset=True, pathonly=False):
|
|
|
|
"""
|
|
|
|
Creates a list of non-iterable linear geometry objects.
|
|
|
|
Polygons are expanded into its exterior and interiors if specified.
|
|
|
|
|
|
|
|
Results are placed in flat_geometry
|
|
|
|
|
|
|
|
:param geometry: Shapely type or list or list of list of such.
|
|
|
|
:param reset: Clears the contents of self.flat_geometry.
|
|
|
|
:param pathonly: Expands polygons into linear elements.
|
|
|
|
"""
|
|
|
|
|
|
|
|
if reset:
|
|
|
|
self.flat_geometry = []
|
|
|
|
|
2019-05-19 14:15:24 +00:00
|
|
|
# If iterable, expand recursively.
|
2019-02-06 20:32:32 +00:00
|
|
|
try:
|
2019-05-19 14:15:24 +00:00
|
|
|
for geo_el in geometry:
|
|
|
|
if geo_el is not None:
|
|
|
|
flatten(geometry=geo_el,
|
2019-02-06 20:32:32 +00:00
|
|
|
reset=False,
|
|
|
|
pathonly=pathonly)
|
|
|
|
|
2019-05-19 14:15:24 +00:00
|
|
|
# Not iterable, do the actual indexing and add.
|
2019-02-06 20:32:32 +00:00
|
|
|
except TypeError:
|
|
|
|
if pathonly and type(geometry) == Polygon:
|
|
|
|
self.flat_geometry.append(geometry.exterior)
|
|
|
|
flatten(geometry=geometry.interiors,
|
|
|
|
reset=False,
|
|
|
|
pathonly=True)
|
|
|
|
else:
|
|
|
|
self.flat_geometry.append(geometry)
|
|
|
|
|
|
|
|
return self.flat_geometry
|
|
|
|
|
|
|
|
flat_geometry = flatten(geo, pathonly=True)
|
|
|
|
|
|
|
|
polygon = Polygon(pts)
|
|
|
|
toolgeo = cascaded_union(polygon)
|
|
|
|
diffs = []
|
|
|
|
for target in flat_geometry:
|
|
|
|
if type(target) == LineString or type(target) == LinearRing:
|
|
|
|
diffs.append(target.difference(toolgeo))
|
|
|
|
else:
|
|
|
|
log.warning("Not implemented.")
|
|
|
|
return cascaded_union(diffs)
|
|
|
|
|
2019-02-06 14:59:17 +00:00
|
|
|
if 'name' in args:
|
|
|
|
name = args['name']
|
|
|
|
else:
|
|
|
|
self.app.inform.emit(
|
2020-04-24 04:23:14 +00:00
|
|
|
"[WARNING] %s" % _("The name of the object for which cutout is done is missing. Add it and retry."))
|
2019-02-06 14:59:17 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
if 'margin' in args:
|
2019-12-08 20:11:39 +00:00
|
|
|
margin = float(args['margin'])
|
2019-02-06 14:59:17 +00:00
|
|
|
else:
|
2020-04-13 16:15:20 +00:00
|
|
|
margin = float(self.app.defaults["tools_cutoutmargin"])
|
2019-02-06 14:59:17 +00:00
|
|
|
|
|
|
|
if 'dia' in args:
|
2019-12-08 20:11:39 +00:00
|
|
|
dia = float(args['dia'])
|
2019-02-06 14:59:17 +00:00
|
|
|
else:
|
2020-04-13 16:15:20 +00:00
|
|
|
dia = float(self.app.defaults["tools_cutouttooldia"])
|
2019-02-06 14:59:17 +00:00
|
|
|
|
|
|
|
if 'gaps' in args:
|
|
|
|
gaps = args['gaps']
|
|
|
|
else:
|
2020-04-13 16:15:20 +00:00
|
|
|
gaps = str(self.app.defaults["tools_gaps_ff"])
|
2019-02-06 14:59:17 +00:00
|
|
|
|
|
|
|
if 'gapsize' in args:
|
2019-12-08 20:11:39 +00:00
|
|
|
gapsize = float(args['gapsize'])
|
2019-02-06 14:59:17 +00:00
|
|
|
else:
|
2020-04-13 16:15:20 +00:00
|
|
|
gapsize = float(self.app.defaults["tools_cutoutgapsize"])
|
2019-02-06 14:59:17 +00:00
|
|
|
|
2020-04-13 02:50:59 +00:00
|
|
|
if 'outname' in args:
|
|
|
|
outname = args['outname']
|
|
|
|
else:
|
|
|
|
outname = str(name) + "_cutout"
|
|
|
|
|
2019-02-06 14:59:17 +00:00
|
|
|
# Get source object.
|
2019-01-03 19:25:08 +00:00
|
|
|
try:
|
2019-02-06 14:59:17 +00:00
|
|
|
cutout_obj = self.app.collection.get_by_name(str(name))
|
2019-05-19 14:15:24 +00:00
|
|
|
except Exception as e:
|
|
|
|
log.debug("TclCommandGeoCutout --> %s" % str(e))
|
2019-02-06 14:59:17 +00:00
|
|
|
return "Could not retrieve object: %s" % name
|
|
|
|
|
|
|
|
if 0 in {dia}:
|
2020-04-24 04:23:14 +00:00
|
|
|
self.app.inform.emit(
|
|
|
|
"[WARNING] %s" % _("Tool Diameter is zero value. Change it to a positive real number."))
|
2019-03-28 22:26:00 +00:00
|
|
|
return "Tool Diameter is zero value. Change it to a positive real number."
|
2019-02-06 14:59:17 +00:00
|
|
|
|
2019-05-19 14:15:24 +00:00
|
|
|
if gaps not in ['lr', 'tb', '2lr', '2tb', '4', '8']:
|
2020-04-24 04:23:14 +00:00
|
|
|
self.app.inform.emit(
|
|
|
|
"[WARNING] %s" % _("Gaps value can be only one of: 'lr', 'tb', '2lr', '2tb', 4 or 8."))
|
2019-02-06 14:59:17 +00:00
|
|
|
return
|
2019-01-03 19:25:08 +00:00
|
|
|
|
|
|
|
# Get min and max data for each object as we just cut rectangles across X or Y
|
2019-02-06 14:59:17 +00:00
|
|
|
xmin, ymin, xmax, ymax = cutout_obj.bounds()
|
2019-06-07 20:14:00 +00:00
|
|
|
cutout_obj.options['xmin'] = xmin
|
|
|
|
cutout_obj.options['ymin'] = ymin
|
|
|
|
cutout_obj.options['xmax'] = xmax
|
|
|
|
cutout_obj.options['ymax'] = ymax
|
|
|
|
|
2019-02-06 14:59:17 +00:00
|
|
|
px = 0.5 * (xmin + xmax) + margin
|
|
|
|
py = 0.5 * (ymin + ymax) + margin
|
|
|
|
lenghtx = (xmax - xmin) + (margin * 2)
|
|
|
|
lenghty = (ymax - ymin) + (margin * 2)
|
|
|
|
|
2019-02-06 20:32:32 +00:00
|
|
|
gapsize = gapsize / 2 + (dia / 2)
|
|
|
|
|
|
|
|
try:
|
|
|
|
gaps_u = int(gaps)
|
|
|
|
except ValueError:
|
|
|
|
gaps_u = gaps
|
2019-02-06 14:59:17 +00:00
|
|
|
|
2020-04-27 09:34:56 +00:00
|
|
|
if cutout_obj.kind == 'geometry':
|
2019-02-06 14:59:17 +00:00
|
|
|
# rename the obj name so it can be identified as cutout
|
2019-06-03 01:47:29 +00:00
|
|
|
# cutout_obj.options["name"] += "_cutout"
|
|
|
|
|
|
|
|
# if gaps_u == 8 or gaps_u == '2lr':
|
|
|
|
# subtract_rectangle(cutout_obj,
|
|
|
|
# xmin - gapsize, # botleft_x
|
|
|
|
# py - gapsize + lenghty / 4, # botleft_y
|
|
|
|
# xmax + gapsize, # topright_x
|
|
|
|
# py + gapsize + lenghty / 4) # topright_y
|
|
|
|
# subtract_rectangle(cutout_obj,
|
|
|
|
# xmin - gapsize,
|
|
|
|
# py - gapsize - lenghty / 4,
|
|
|
|
# xmax + gapsize,
|
|
|
|
# py + gapsize - lenghty / 4)
|
|
|
|
#
|
|
|
|
# if gaps_u == 8 or gaps_u == '2tb':
|
|
|
|
# subtract_rectangle(cutout_obj,
|
|
|
|
# px - gapsize + lenghtx / 4,
|
|
|
|
# ymin - gapsize,
|
|
|
|
# px + gapsize + lenghtx / 4,
|
|
|
|
# ymax + gapsize)
|
|
|
|
# subtract_rectangle(cutout_obj,
|
|
|
|
# px - gapsize - lenghtx / 4,
|
|
|
|
# ymin - gapsize,
|
|
|
|
# px + gapsize - lenghtx / 4,
|
|
|
|
# ymax + gapsize)
|
|
|
|
#
|
|
|
|
# if gaps_u == 4 or gaps_u == 'lr':
|
|
|
|
# subtract_rectangle(cutout_obj,
|
|
|
|
# xmin - gapsize,
|
|
|
|
# py - gapsize,
|
|
|
|
# xmax + gapsize,
|
|
|
|
# py + gapsize)
|
|
|
|
#
|
|
|
|
# if gaps_u == 4 or gaps_u == 'tb':
|
|
|
|
# subtract_rectangle(cutout_obj,
|
|
|
|
# px - gapsize,
|
|
|
|
# ymin - gapsize,
|
|
|
|
# px + gapsize,
|
|
|
|
# ymax + gapsize)
|
|
|
|
|
|
|
|
def geo_init(geo_obj, app_obj):
|
|
|
|
geo = deepcopy(cutout_obj.solid_geometry)
|
|
|
|
|
|
|
|
if gaps_u == 8 or gaps_u == '2lr':
|
|
|
|
geo = substract_rectangle_geo(geo,
|
|
|
|
xmin - gapsize, # botleft_x
|
|
|
|
py - gapsize + lenghty / 4, # botleft_y
|
|
|
|
xmax + gapsize, # topright_x
|
|
|
|
py + gapsize + lenghty / 4) # topright_y
|
|
|
|
geo = substract_rectangle_geo(geo,
|
|
|
|
xmin - gapsize,
|
|
|
|
py - gapsize - lenghty / 4,
|
|
|
|
xmax + gapsize,
|
|
|
|
py + gapsize - lenghty / 4)
|
|
|
|
|
|
|
|
if gaps_u == 8 or gaps_u == '2tb':
|
|
|
|
geo = substract_rectangle_geo(geo,
|
|
|
|
px - gapsize + lenghtx / 4,
|
|
|
|
ymin - gapsize,
|
|
|
|
px + gapsize + lenghtx / 4,
|
|
|
|
ymax + gapsize)
|
|
|
|
geo = substract_rectangle_geo(geo,
|
|
|
|
px - gapsize - lenghtx / 4,
|
|
|
|
ymin - gapsize,
|
|
|
|
px + gapsize - lenghtx / 4,
|
|
|
|
ymax + gapsize)
|
|
|
|
|
|
|
|
if gaps_u == 4 or gaps_u == 'lr':
|
|
|
|
geo = substract_rectangle_geo(geo,
|
|
|
|
xmin - gapsize,
|
|
|
|
py - gapsize,
|
|
|
|
xmax + gapsize,
|
|
|
|
py + gapsize)
|
|
|
|
|
|
|
|
if gaps_u == 4 or gaps_u == 'tb':
|
|
|
|
geo = substract_rectangle_geo(geo,
|
|
|
|
px - gapsize,
|
|
|
|
ymin - gapsize,
|
|
|
|
px + gapsize,
|
|
|
|
ymax + gapsize)
|
|
|
|
geo_obj.solid_geometry = deepcopy(geo)
|
2019-06-07 20:14:00 +00:00
|
|
|
geo_obj.options['xmin'] = cutout_obj.options['xmin']
|
|
|
|
geo_obj.options['ymin'] = cutout_obj.options['ymin']
|
|
|
|
geo_obj.options['xmax'] = cutout_obj.options['xmax']
|
|
|
|
geo_obj.options['ymax'] = cutout_obj.options['ymax']
|
|
|
|
|
2019-06-03 01:47:29 +00:00
|
|
|
app_obj.disable_plots(objects=[cutout_obj])
|
|
|
|
|
2020-04-24 04:23:14 +00:00
|
|
|
app_obj.inform.emit("[success] %s" % _("Any-form Cutout operation finished."))
|
2019-06-03 01:47:29 +00:00
|
|
|
|
2019-09-15 22:39:11 +00:00
|
|
|
self.app.new_object('geometry', outname, geo_init, plot=False)
|
2019-06-03 01:47:29 +00:00
|
|
|
|
|
|
|
# cutout_obj.plot()
|
|
|
|
# self.app.inform.emit("[success] Any-form Cutout operation finished.")
|
|
|
|
# self.app.plots_updated.emit()
|
2020-04-27 09:34:56 +00:00
|
|
|
elif cutout_obj.kind == 'gerber':
|
2019-02-06 14:59:17 +00:00
|
|
|
|
|
|
|
def geo_init(geo_obj, app_obj):
|
2019-02-06 19:37:50 +00:00
|
|
|
try:
|
2019-02-19 12:53:55 +00:00
|
|
|
geo = cutout_obj.isolation_geometry((dia / 2), iso_type=0, corner=2, follow=None)
|
2019-05-19 14:15:24 +00:00
|
|
|
except Exception as exc:
|
|
|
|
log.debug("TclCommandGeoCutout.execute() --> %s" % str(exc))
|
2019-02-06 19:37:50 +00:00
|
|
|
return 'fail'
|
2019-02-06 14:59:17 +00:00
|
|
|
|
2019-02-06 20:32:32 +00:00
|
|
|
if gaps_u == 8 or gaps_u == '2lr':
|
|
|
|
geo = substract_rectangle_geo(geo,
|
2019-05-19 14:15:24 +00:00
|
|
|
xmin - gapsize, # botleft_x
|
|
|
|
py - gapsize + lenghty / 4, # botleft_y
|
|
|
|
xmax + gapsize, # topright_x
|
|
|
|
py + gapsize + lenghty / 4) # topright_y
|
2019-02-06 20:32:32 +00:00
|
|
|
geo = substract_rectangle_geo(geo,
|
2019-05-19 14:15:24 +00:00
|
|
|
xmin - gapsize,
|
|
|
|
py - gapsize - lenghty / 4,
|
|
|
|
xmax + gapsize,
|
|
|
|
py + gapsize - lenghty / 4)
|
2019-02-06 20:32:32 +00:00
|
|
|
|
|
|
|
if gaps_u == 8 or gaps_u == '2tb':
|
|
|
|
geo = substract_rectangle_geo(geo,
|
2019-05-19 14:15:24 +00:00
|
|
|
px - gapsize + lenghtx / 4,
|
|
|
|
ymin - gapsize,
|
|
|
|
px + gapsize + lenghtx / 4,
|
|
|
|
ymax + gapsize)
|
2019-02-06 20:32:32 +00:00
|
|
|
geo = substract_rectangle_geo(geo,
|
2019-05-19 14:15:24 +00:00
|
|
|
px - gapsize - lenghtx / 4,
|
|
|
|
ymin - gapsize,
|
|
|
|
px + gapsize - lenghtx / 4,
|
|
|
|
ymax + gapsize)
|
2019-02-06 20:32:32 +00:00
|
|
|
|
|
|
|
if gaps_u == 4 or gaps_u == 'lr':
|
|
|
|
geo = substract_rectangle_geo(geo,
|
2019-05-19 14:15:24 +00:00
|
|
|
xmin - gapsize,
|
|
|
|
py - gapsize,
|
|
|
|
xmax + gapsize,
|
|
|
|
py + gapsize)
|
2019-02-06 20:32:32 +00:00
|
|
|
|
|
|
|
if gaps_u == 4 or gaps_u == 'tb':
|
|
|
|
geo = substract_rectangle_geo(geo,
|
2019-05-19 14:15:24 +00:00
|
|
|
px - gapsize,
|
|
|
|
ymin - gapsize,
|
|
|
|
px + gapsize,
|
|
|
|
ymax + gapsize)
|
2019-06-02 11:04:14 +00:00
|
|
|
geo_obj.solid_geometry = deepcopy(geo)
|
2019-06-07 20:14:00 +00:00
|
|
|
geo_obj.options['xmin'] = cutout_obj.options['xmin']
|
|
|
|
geo_obj.options['ymin'] = cutout_obj.options['ymin']
|
|
|
|
geo_obj.options['xmax'] = cutout_obj.options['xmax']
|
|
|
|
geo_obj.options['ymax'] = cutout_obj.options['ymax']
|
2020-04-24 04:23:14 +00:00
|
|
|
app_obj.inform.emit("[success] %s" % _("Any-form Cutout operation finished."))
|
2019-02-06 20:32:32 +00:00
|
|
|
|
2019-09-15 22:39:11 +00:00
|
|
|
self.app.new_object('geometry', outname, geo_init, plot=False)
|
2019-02-06 14:59:17 +00:00
|
|
|
|
|
|
|
cutout_obj = self.app.collection.get_by_name(outname)
|
|
|
|
else:
|
2020-04-24 04:23:14 +00:00
|
|
|
self.app.inform.emit("[ERROR] %s" % _("Cancelled. Object type is not supported."))
|
2019-02-06 14:59:17 +00:00
|
|
|
return
|