TclCommandSubtractRectangle.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. from tclCommands.TclCommand import TclCommandSignaled
  2. import collections
  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 TclCommandSubtractRectangle(TclCommandSignaled):
  10. """
  11. Tcl shell command to subtract a rectangle from the given Geometry object.
  12. """
  13. # array of all command aliases, to be able use old names for backward compatibility (add_poly, add_polygon)
  14. aliases = ['subtract_rectangle']
  15. # Dictionary of types from Tcl command, needs to be ordered.
  16. # For positional arguments
  17. arg_names = collections.OrderedDict([
  18. ('name', str)
  19. ])
  20. # Dictionary of types from Tcl command, needs to be ordered.
  21. # For options like -optionname value
  22. option_types = collections.OrderedDict([
  23. ])
  24. # array of mandatory options for current Tcl command: required = {'name','outname'}
  25. required = ['name']
  26. # structured help for current command, args needs to be ordered
  27. help = {
  28. 'main': "Subtract rectange from the given Geometry object.",
  29. 'args': collections.OrderedDict([
  30. ('name', 'Name of the Geometry object from which to subtract.'),
  31. ('x0 y0', 'Bottom left corner coordinates.'),
  32. ('x1 y1', 'Top right corner coordinates.')
  33. ]),
  34. 'examples': ['subtract_rectangle geo_obj 8 8 15 15']
  35. }
  36. def execute(self, args, unnamed_args):
  37. """
  38. execute current TCL shell command
  39. :param args: array of known named arguments and options
  40. :param unnamed_args: array of other values which were passed into command
  41. without -somename and we do not have them in known arg_names
  42. :return: None or exception
  43. """
  44. if 'name' not in args:
  45. self.raise_tcl_error("%s:" % _("No Geometry name in args. Provide a name and try again."))
  46. return 'fail'
  47. obj_name = args['name']
  48. if len(unnamed_args) != 4:
  49. self.raise_tcl_error("Incomplete coordinates. There are 4 required: x0 y0 x1 y1.")
  50. return 'fail'
  51. x0 = float(unnamed_args[0])
  52. y0 = float(unnamed_args[1])
  53. x1 = float(unnamed_args[2])
  54. y1 = float(unnamed_args[3])
  55. try:
  56. obj = self.app.collection.get_by_name(str(obj_name))
  57. except Exception:
  58. return "Could not retrieve object: %s" % obj_name
  59. if obj is None:
  60. return "Object not found: %s" % obj_name
  61. obj.subtract_polygon([(x0, y0), (x1, y0), (x1, y1), (x0, y1)])