TclCommandAddRectangle.py 2.2 KB

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