TclCommandAddPolygon.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. from ObjectCollection import *
  2. from tclCommands.TclCommand import TclCommandSignaled
  3. class TclCommandAddPolygon(TclCommandSignaled):
  4. """
  5. Tcl shell command to create a polygon in 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_polygon', 'add_poly']
  9. # dictionary of types from Tcl command, needs to be ordered
  10. arg_names = collections.OrderedDict([
  11. ('name', str)
  12. ])
  13. # dictionary of types from Tcl command, needs to be ordered , this is for options like -optionname value
  14. option_types = collections.OrderedDict()
  15. # array of mandatory options for current Tcl command: required = {'name','outname'}
  16. required = ['name']
  17. # structured help for current command, args needs to be ordered
  18. help = {
  19. 'main': "Creates a polygon in the given Geometry object.",
  20. 'args': collections.OrderedDict([
  21. ('name', 'Name of the Geometry object to which to append the polygon.'),
  22. ('xi, yi', 'Coordinates of points in the polygon.')
  23. ]),
  24. 'examples': [
  25. 'add_polygon <name> <x0> <y0> <x1> <y1> <x2> <y2> [x3 y3 [...]]'
  26. ]
  27. }
  28. def execute(self, args, unnamed_args):
  29. """
  30. execute current TCL shell command
  31. :param args: array of known named arguments and options
  32. :param unnamed_args: array of other values which were passed into command
  33. without -somename and we do not have them in known arg_names
  34. :return: None or exception
  35. """
  36. name = args['name']
  37. obj = self.app.collection.get_by_name(name)
  38. if obj is None:
  39. self.raise_tcl_error("Object not found: %s" % name)
  40. if not isinstance(obj, Geometry):
  41. self.raise_tcl_error('Expected Geometry, got %s %s.' % (name, type(obj)))
  42. if len(unnamed_args) % 2 != 0:
  43. self.raise_tcl_error("Incomplete coordinates.")
  44. points = [[float(unnamed_args[2*i]), float(unnamed_args[2*i+1])] for i in range(len(unnamed_args)/2)]
  45. obj.add_polygon(points)
  46. obj.plot()