TclCommandSubtractRectangle.py 2.9 KB

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