TclCommandAddPolygon.py 2.1 KB

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