TclCommandAddPolyline.py 2.3 KB

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