TclCommandAddPolygon.py 2.2 KB

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