TclCommandSubtractRectangle.py 2.7 KB

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