2019-10-24 23:10:52 +00:00
|
|
|
import collections
|
2019-08-24 17:52:35 +00:00
|
|
|
from tclCommands.TclCommand import TclCommand
|
2019-10-24 23:10:52 +00:00
|
|
|
|
|
|
|
from shapely.ops import cascaded_union
|
|
|
|
|
2019-08-24 17:52:35 +00:00
|
|
|
import gettext
|
|
|
|
import FlatCAMTranslation as fcTranslate
|
|
|
|
import builtins
|
|
|
|
|
|
|
|
fcTranslate.apply_language('strings')
|
|
|
|
if '_' not in builtins.__dict__:
|
|
|
|
_ = gettext.gettext
|
|
|
|
|
|
|
|
|
2019-08-24 18:25:25 +00:00
|
|
|
class TclCommandBbox(TclCommand):
|
2019-08-24 17:52:35 +00:00
|
|
|
"""
|
|
|
|
Tcl shell command to follow a Gerber file
|
|
|
|
"""
|
|
|
|
|
|
|
|
# array of all command aliases, to be able use old names for backward compatibility (add_poly, add_polygon)
|
2019-08-24 18:25:25 +00:00
|
|
|
aliases = ['bounding_box', 'bbox']
|
2019-08-24 17:52:35 +00:00
|
|
|
|
2020-04-13 16:15:20 +00:00
|
|
|
description = '%s %s' % ("--", "Creates a rectangular Geometry object that surrounds the object.")
|
|
|
|
|
2019-08-24 17:52:35 +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([
|
|
|
|
('outname', str),
|
|
|
|
('margin', float),
|
2020-04-13 16:15:20 +00:00
|
|
|
('rounded', str)
|
2019-08-24 17:52:35 +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-09 01:13:04 +00:00
|
|
|
'main': "Creates a rectangular Geometry object that surrounds the object.",
|
2019-08-24 17:52:35 +00:00
|
|
|
'args': collections.OrderedDict([
|
2019-08-24 18:25:25 +00:00
|
|
|
('name', 'Object name for which to create bounding box. String'),
|
|
|
|
('margin', "Distance of the edges of the box to the nearest polygon."
|
2019-08-24 17:52:35 +00:00
|
|
|
"Float number."),
|
2020-04-28 16:34:23 +00:00
|
|
|
('rounded', "If the bounding box has to have rounded corners their radius is equal to the margin. "
|
2020-04-13 16:15:20 +00:00
|
|
|
"True (1) or False (0)."),
|
|
|
|
('outname', 'Name of the resulting Geometry object. String.')
|
2019-08-24 17:52:35 +00:00
|
|
|
]),
|
2019-08-24 18:25:25 +00:00
|
|
|
'examples': ['bbox name -outname name_bbox']
|
2019-08-24 17:52:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
def execute(self, args, unnamed_args):
|
|
|
|
"""
|
|
|
|
execute current TCL shell command
|
|
|
|
|
|
|
|
:param args: array of known named arguments and options
|
|
|
|
:param unnamed_args: array of other values which were passed into command
|
|
|
|
without -somename and we do not have them in known arg_names
|
|
|
|
:return: None or exception
|
|
|
|
"""
|
|
|
|
|
|
|
|
name = args['name']
|
|
|
|
|
|
|
|
if 'outname' not in args:
|
2019-08-24 18:25:25 +00:00
|
|
|
args['outname'] = name + "_bbox"
|
2019-08-24 17:52:35 +00:00
|
|
|
|
|
|
|
obj = self.app.collection.get_by_name(name)
|
|
|
|
if obj is None:
|
|
|
|
self.raise_tcl_error("%s: %s" % (_("Object not found"), name))
|
|
|
|
|
2020-04-28 16:34:23 +00:00
|
|
|
if obj.kind != 'gerber' and obj.kind != 'geometry':
|
2019-08-24 18:25:25 +00:00
|
|
|
self.raise_tcl_error('%s %s: %s.' % (
|
2020-04-27 09:34:56 +00:00
|
|
|
_("Expected GerberObject or GeometryObject, got"), name, type(obj)))
|
2019-08-24 17:52:35 +00:00
|
|
|
|
|
|
|
if 'margin' not in args:
|
2019-08-24 18:25:25 +00:00
|
|
|
args['margin'] = float(self.app.defaults["gerber_bboxmargin"])
|
2019-08-24 17:52:35 +00:00
|
|
|
margin = args['margin']
|
|
|
|
|
2020-04-22 11:37:03 +00:00
|
|
|
if 'rounded' in args:
|
|
|
|
try:
|
|
|
|
par = args['rounded'].capitalize()
|
|
|
|
except AttributeError:
|
|
|
|
par = args['rounded']
|
|
|
|
rounded = bool(eval(par))
|
|
|
|
else:
|
|
|
|
rounded = bool(eval(self.app.defaults["gerber_bboxrounded"]))
|
2019-08-24 17:52:35 +00:00
|
|
|
|
|
|
|
del args['name']
|
|
|
|
|
|
|
|
try:
|
|
|
|
def geo_init(geo_obj, app_obj):
|
2020-04-27 09:34:56 +00:00
|
|
|
# assert geo_obj.kind == 'geometry'
|
2019-08-24 17:52:35 +00:00
|
|
|
|
2019-08-24 18:25:25 +00:00
|
|
|
# Bounding box with rounded corners
|
|
|
|
geo = cascaded_union(obj.solid_geometry)
|
|
|
|
bounding_box = geo.envelope.buffer(float(margin))
|
|
|
|
if not rounded: # Remove rounded corners
|
2019-08-24 17:52:35 +00:00
|
|
|
bounding_box = bounding_box.envelope
|
2019-08-24 18:25:25 +00:00
|
|
|
geo_obj.solid_geometry = bounding_box
|
2019-08-24 17:52:35 +00:00
|
|
|
|
2019-09-15 22:39:11 +00:00
|
|
|
self.app.new_object("geometry", args['outname'], geo_init, plot=False)
|
2019-08-24 17:52:35 +00:00
|
|
|
except Exception as e:
|
|
|
|
return "Operation failed: %s" % str(e)
|