TclCommandBbox.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. from ObjectCollection import *
  2. from tclCommands.TclCommand import TclCommand
  3. import gettext
  4. import FlatCAMTranslation as fcTranslate
  5. import builtins
  6. fcTranslate.apply_language('strings')
  7. if '_' not in builtins.__dict__:
  8. _ = gettext.gettext
  9. class TclCommandBbox(TclCommand):
  10. """
  11. Tcl shell command to follow a Gerber file
  12. """
  13. # array of all command aliases, to be able use old names for backward compatibility (add_poly, add_polygon)
  14. aliases = ['bounding_box', 'bbox']
  15. # dictionary of types from Tcl command, needs to be ordered
  16. arg_names = collections.OrderedDict([
  17. ('name', str)
  18. ])
  19. # dictionary of types from Tcl command, needs to be ordered , this is for options like -optionname value
  20. option_types = collections.OrderedDict([
  21. ('outname', str),
  22. ('margin', float),
  23. ('rounded', bool)
  24. ])
  25. # array of mandatory options for current Tcl command: required = {'name','outname'}
  26. required = ['name']
  27. # structured help for current command, args needs to be ordered
  28. help = {
  29. 'main': "Creates a Geometry object that surrounds the object.",
  30. 'args': collections.OrderedDict([
  31. ('name', 'Object name for which to create bounding box. String'),
  32. ('outname', 'Name of the resulting Geometry object. String.'),
  33. ('margin', "Distance of the edges of the box to the nearest polygon."
  34. "Float number."),
  35. ('rounded', "If the bounding box is to have rounded corners their radius is equal to the margin. "
  36. "True or False.")
  37. ]),
  38. 'examples': ['bbox name -outname name_bbox']
  39. }
  40. def execute(self, args, unnamed_args):
  41. """
  42. execute current TCL shell command
  43. :param args: array of known named arguments and options
  44. :param unnamed_args: array of other values which were passed into command
  45. without -somename and we do not have them in known arg_names
  46. :return: None or exception
  47. """
  48. name = args['name']
  49. if 'outname' not in args:
  50. args['outname'] = name + "_bbox"
  51. obj = self.app.collection.get_by_name(name)
  52. if obj is None:
  53. self.raise_tcl_error("%s: %s" % (_("Object not found"), name))
  54. if not isinstance(obj, FlatCAMGerber) and not isinstance(obj, FlatCAMGeometry):
  55. self.raise_tcl_error('%s %s: %s.' % (
  56. _("Expected FlatCAMGerber or FlatCAMGeometry, got"), name, type(obj)))
  57. if 'margin' not in args:
  58. args['margin'] = float(self.app.defaults["gerber_bboxmargin"])
  59. margin = args['margin']
  60. if 'rounded' not in args:
  61. args['rounded'] = self.app.defaults["gerber_bboxrounded"]
  62. rounded = args['rounded']
  63. del args['name']
  64. try:
  65. def geo_init(geo_obj, app_obj):
  66. assert isinstance(geo_obj, FlatCAMGeometry)
  67. # Bounding box with rounded corners
  68. geo = cascaded_union(obj.solid_geometry)
  69. bounding_box = geo.envelope.buffer(float(margin))
  70. if not rounded: # Remove rounded corners
  71. bounding_box = bounding_box.envelope
  72. geo_obj.solid_geometry = bounding_box
  73. self.app.new_object("geometry", args['outname'], geo_init, plot=False)
  74. except Exception as e:
  75. return "Operation failed: %s" % str(e)