TclCommandAddRectangle.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. from ObjectCollection import *
  2. from tclCommands.TclCommand import TclCommandSignaled
  3. class TclCommandAddRectangle(TclCommandSignaled):
  4. """
  5. Tcl shell command to add a rectange to 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 = ['add_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. ('x0', float),
  14. ('y0', float),
  15. ('x1', float),
  16. ('y1', float)
  17. ])
  18. # Dictionary of types from Tcl command, needs to be ordered.
  19. # For options like -optionname value
  20. option_types = collections.OrderedDict([
  21. ])
  22. # array of mandatory options for current Tcl command: required = {'name','outname'}
  23. required = ['name', 'x0', 'y0', 'x1', 'y1']
  24. # structured help for current command, args needs to be ordered
  25. help = {
  26. 'main': "Add a rectange to the given Geometry object.",
  27. 'args': collections.OrderedDict([
  28. ('name', 'Name of the Geometry object in which to add the rectangle.'),
  29. ('x0 y0', 'Bottom left corner coordinates.'),
  30. ('x1 y1', 'Top right corner coordinates.')
  31. ]),
  32. 'examples': []
  33. }
  34. def execute(self, args, unnamed_args):
  35. """
  36. execute current TCL shell command
  37. :param args: array of known named arguments and options
  38. :param unnamed_args: array of other values which were passed into command
  39. without -somename and we do not have them in known arg_names
  40. :return: None or exception
  41. """
  42. obj_name = args['name']
  43. x0 = args['x0']
  44. y0 = args['y0']
  45. x1 = args['x1']
  46. y1 = args['y1']
  47. try:
  48. obj = self.app.collection.get_by_name(str(obj_name))
  49. except:
  50. return "Could not retrieve object: %s" % obj_name
  51. if obj is None:
  52. return "Object not found: %s" % obj_name
  53. obj.add_polygon([(x0, y0), (x1, y0), (x1, y1), (x0, y1)])