TclCommandBbox.py 3.5 KB

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