TclCommandSubtractRectangle.py 2.4 KB

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