TclCommandSubtractPoly.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. from tclCommands.TclCommand import TclCommandSignaled
  2. import collections
  3. class TclCommandSubtractPoly(TclCommandSignaled):
  4. """
  5. Tcl shell command to create a new empty Geometry object.
  6. """
  7. # array of all command aliases, to be able use old names for backward compatibility (add_poly, add_polygon)
  8. aliases = ['subtract_poly']
  9. description = '%s %s' % ("--", "Subtract polygon from the given Geometry object. "
  10. "The coordinates are provided in X Y pairs.")
  11. # Dictionary of types from Tcl command, needs to be ordered.
  12. # For positional arguments
  13. arg_names = collections.OrderedDict([
  14. ('name', str)
  15. ])
  16. # Dictionary of types from Tcl command, needs to be ordered.
  17. # For options like -optionname value
  18. option_types = collections.OrderedDict([
  19. ])
  20. # array of mandatory options for current Tcl command: required = {'name','outname'}
  21. required = ['name']
  22. # structured help for current command, args needs to be ordered
  23. help = {
  24. 'main': "Subtract polygon from the given Geometry object. The coordinates are provided in X Y pairs.\n"
  25. "If the number of coordinates is not even then the 'Incomplete coordinate' error is raised.\n"
  26. "If last coordinates are not the same as the first ones, the polygon will be completed automatically.",
  27. 'args': collections.OrderedDict([
  28. ('name', 'Name of the Geometry object from which to subtract. Required.'),
  29. ('x0 y0 x1 y1 x2 y2 ...', 'Points defining the polygon.')
  30. ]),
  31. 'examples': ['subtract_poly my_geo 0 0 2 1 3 3 4 4 0 0']
  32. }
  33. def execute(self, args, unnamed_args):
  34. """
  35. execute current TCL shell command
  36. :param args: array of known named arguments and options
  37. :param unnamed_args: array of other values which were passed into command
  38. without -somename and we do not have them in known arg_names
  39. :return: None or exception
  40. """
  41. obj_name = args['name']
  42. if len(unnamed_args) % 2 != 0:
  43. return "Incomplete coordinate."
  44. points = [
  45. [float(unnamed_args[2 * i]), float(unnamed_args[2 * i + 1])] for i in range(int(len(unnamed_args) / 2))
  46. ]
  47. try:
  48. obj = self.app.collection.get_by_name(str(obj_name))
  49. except Exception:
  50. return "Could not retrieve object: %s" % obj_name
  51. if obj is None:
  52. return "Object not found: %s" % obj_name
  53. obj.subtract_polygon(points)