TclCommandAddPolygon.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. description = '%s %s' % ("--", "Creates a polygon in the given Geometry object.")
  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 obj.kind != '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. nr_points = int(len(unnamed_args) / 2)
  45. points = [[float(unnamed_args[2*i]), float(unnamed_args[2*i+1])] for i in range(nr_points)]
  46. obj.add_polygon(points)