TclCommandAddRectangle.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. from tclCommands.TclCommand import *
  2. class TclCommandAddRectangle(TclCommandSignaled):
  3. """
  4. Tcl shell command to add a rectange to the given Geometry object.
  5. """
  6. # array of all command aliases, to be able use old names for backward compatibility (add_poly, add_polygon)
  7. aliases = ['add_rectangle']
  8. description = '%s %s' % ("--", "Creates a rectangle in the given Geometry object.")
  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': "Creates a rectangle in 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': ["add_rectangle geo_name 0 0 10 10"]
  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. name = args['name']
  43. x0 = args['x0']
  44. y0 = args['y0']
  45. x1 = args['x1']
  46. y1 = args['y1']
  47. if unnamed_args:
  48. self.raise_tcl_error(
  49. "Too many arguments. Correct format: %s" % '["add_rectangle geo_name xmin ymin xmax ymax"]')
  50. try:
  51. obj = self.app.collection.get_by_name(str(name))
  52. except Exception:
  53. return "Could not retrieve object: %s" % name
  54. if obj is None:
  55. return "Object not found: %s" % name
  56. if obj.kind != 'geometry':
  57. self.raise_tcl_error('Expected Geometry, got %s %s.' % (name, type(obj)))
  58. obj.add_polygon([(x0, y0), (x1, y0), (x1, y1), (x0, y1)])