2019-01-03 19:25:08 +00:00
|
|
|
from tclCommands.TclCommand import TclCommand
|
2019-10-24 23:10:52 +00:00
|
|
|
|
|
|
|
import collections
|
|
|
|
import logging
|
|
|
|
|
2019-07-09 10:58:33 +00:00
|
|
|
from shapely.ops import cascaded_union
|
|
|
|
from shapely.geometry import LineString
|
2019-01-03 19:25:08 +00:00
|
|
|
|
2019-10-24 23:10:52 +00:00
|
|
|
log = logging.getLogger('base')
|
|
|
|
|
2019-01-03 19:25:08 +00:00
|
|
|
|
|
|
|
class TclCommandCutout(TclCommand):
|
|
|
|
"""
|
2019-01-25 18:05:27 +00:00
|
|
|
Tcl shell command to create a board cutout geometry. Rectangular shape only.
|
2019-01-03 19:25:08 +00:00
|
|
|
|
|
|
|
example:
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
# List of all command aliases, to be able use old
|
|
|
|
# names for backward compatibility (add_poly, add_polygon)
|
|
|
|
aliases = ['cutout']
|
|
|
|
|
2020-04-13 16:15:20 +00:00
|
|
|
description = '%s %s' % ("--", "Creates board cutout from an object (Gerber or Geometry) with a rectangular shape.")
|
|
|
|
|
2019-01-03 19:25:08 +00:00
|
|
|
# Dictionary of types from Tcl command, needs to be ordered
|
|
|
|
arg_names = collections.OrderedDict([
|
|
|
|
('name', str),
|
|
|
|
])
|
|
|
|
|
|
|
|
# Dictionary of types from Tcl command, needs to be ordered,
|
|
|
|
# this is for options like -optionname value
|
|
|
|
option_types = collections.OrderedDict([
|
|
|
|
('dia', float),
|
|
|
|
('margin', float),
|
|
|
|
('gapsize', float),
|
2020-04-13 16:15:20 +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) with a rectangular shape.',
|
2019-01-03 19:25:08 +00:00
|
|
|
'args': collections.OrderedDict([
|
|
|
|
('name', 'Name of the object.'),
|
2020-04-13 16:15:20 +00:00
|
|
|
('dia', 'Tool diameter.'),
|
|
|
|
('margin', 'Margin over bounds.'),
|
|
|
|
('gapsize', 'Size of gap.'),
|
|
|
|
('gaps', "Type of gaps. Can be: 'tb' = top-bottom, 'lr' = left-right and '4' = one each side."),
|
|
|
|
('outname', 'Name of the object to create.')
|
2019-01-03 19:25:08 +00:00
|
|
|
]),
|
2020-04-13 16:15:20 +00:00
|
|
|
'examples': ['cutout new_geo -dia 1.2 -margin 0.1 -gapsize 1 -gaps "tb" -outname cut_geo']
|
2019-01-03 19:25:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
def execute(self, args, unnamed_args):
|
|
|
|
"""
|
|
|
|
|
|
|
|
:param args:
|
|
|
|
:param unnamed_args:
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
|
2019-01-25 18:05:27 +00:00
|
|
|
if 'name' in args:
|
|
|
|
name = args['name']
|
|
|
|
else:
|
|
|
|
self.app.inform.emit(
|
2019-02-03 13:13:09 +00:00
|
|
|
"[WARNING]The name of the object for which cutout is done is missing. Add it and retry.")
|
2019-01-25 18:05:27 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
if 'margin' in args:
|
2019-12-08 20:11:39 +00:00
|
|
|
margin_par = float(args['margin'])
|
2019-01-25 18:05:27 +00:00
|
|
|
else:
|
2020-04-13 16:15:20 +00:00
|
|
|
margin_par = float(self.app.defaults["tools_cutoutmargin"])
|
2019-01-25 18:05:27 +00:00
|
|
|
|
|
|
|
if 'dia' in args:
|
2019-12-08 20:11:39 +00:00
|
|
|
dia_par = float(args['dia'])
|
2019-01-25 18:05:27 +00:00
|
|
|
else:
|
2020-04-13 16:15:20 +00:00
|
|
|
dia_par = float(self.app.defaults["tools_cutouttooldia"])
|
2019-01-25 18:05:27 +00:00
|
|
|
|
|
|
|
if 'gaps' in args:
|
|
|
|
gaps_par = args['gaps']
|
|
|
|
else:
|
2020-04-13 16:15:20 +00:00
|
|
|
gaps_par = str(self.app.defaults["tools_gaps_ff"])
|
2019-01-25 18:05:27 +00:00
|
|
|
|
|
|
|
if 'gapsize' in args:
|
2019-12-08 20:11:39 +00:00
|
|
|
gapsize_par = float(args['gapsize'])
|
2019-01-25 18:05:27 +00:00
|
|
|
else:
|
2020-04-13 16:15:20 +00:00
|
|
|
gapsize_par = float(self.app.defaults["tools_cutoutgapsize"])
|
|
|
|
|
|
|
|
if 'outname' in args:
|
|
|
|
outname = args['outname']
|
|
|
|
else:
|
|
|
|
outname = name + "_cutout"
|
2019-01-03 19:25:08 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
obj = self.app.collection.get_by_name(str(name))
|
2019-07-09 10:58:33 +00:00
|
|
|
except Exception as e:
|
|
|
|
log.debug("TclCommandCutout.execute() --> %s" % str(e))
|
2019-01-03 19:25:08 +00:00
|
|
|
return "Could not retrieve object: %s" % name
|
|
|
|
|
|
|
|
def geo_init_me(geo_obj, app_obj):
|
2019-07-09 10:58:33 +00:00
|
|
|
margin = margin_par + dia_par / 2
|
2019-01-25 18:05:27 +00:00
|
|
|
gap_size = dia_par + gapsize_par
|
|
|
|
|
2019-01-03 19:25:08 +00:00
|
|
|
minx, miny, maxx, maxy = obj.bounds()
|
|
|
|
minx -= margin
|
|
|
|
maxx += margin
|
|
|
|
miny -= margin
|
|
|
|
maxy += margin
|
|
|
|
midx = 0.5 * (minx + maxx)
|
|
|
|
midy = 0.5 * (miny + maxy)
|
|
|
|
hgap = 0.5 * gap_size
|
|
|
|
pts = [[midx - hgap, maxy],
|
|
|
|
[minx, maxy],
|
|
|
|
[minx, midy + hgap],
|
|
|
|
[minx, midy - hgap],
|
|
|
|
[minx, miny],
|
|
|
|
[midx - hgap, miny],
|
|
|
|
[midx + hgap, miny],
|
|
|
|
[maxx, miny],
|
|
|
|
[maxx, midy - hgap],
|
|
|
|
[maxx, midy + hgap],
|
|
|
|
[maxx, maxy],
|
|
|
|
[midx + hgap, maxy]]
|
|
|
|
cases = {"tb": [[pts[0], pts[1], pts[4], pts[5]],
|
|
|
|
[pts[6], pts[7], pts[10], pts[11]]],
|
|
|
|
"lr": [[pts[9], pts[10], pts[1], pts[2]],
|
|
|
|
[pts[3], pts[4], pts[7], pts[8]]],
|
|
|
|
"4": [[pts[0], pts[1], pts[2]],
|
|
|
|
[pts[3], pts[4], pts[5]],
|
|
|
|
[pts[6], pts[7], pts[8]],
|
|
|
|
[pts[9], pts[10], pts[11]]]}
|
2019-01-25 18:05:27 +00:00
|
|
|
cuts = cases[gaps_par]
|
2019-01-03 19:25:08 +00:00
|
|
|
geo_obj.solid_geometry = cascaded_union([LineString(segment) for segment in cuts])
|
|
|
|
|
|
|
|
try:
|
2020-04-13 16:15:20 +00:00
|
|
|
self.app.new_object("geometry", outname, geo_init_me, plot=False)
|
2019-03-28 22:26:00 +00:00
|
|
|
self.app.inform.emit("[success] Rectangular-form Cutout operation finished.")
|
2019-01-03 19:25:08 +00:00
|
|
|
except Exception as e:
|
|
|
|
return "Operation failed: %s" % str(e)
|