TclCommandSubtractPoly.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. from tclCommands.TclCommand import *
  2. class TclCommandSubtractPoly(TclCommandSignaled):
  3. """
  4. Tcl shell command to create a new empty Geometry object.
  5. """
  6. # array of all command aliases, to be able use old names for backward compatibility (add_poly, add_polygon)
  7. aliases = ['subtract_poly']
  8. # Dictionary of types from Tcl command, needs to be ordered.
  9. # For positional arguments
  10. arg_names = collections.OrderedDict([
  11. ('name', str)
  12. ])
  13. # Dictionary of types from Tcl command, needs to be ordered.
  14. # For options like -optionname value
  15. option_types = collections.OrderedDict([
  16. ])
  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': "Subtract polygon from the given Geometry object.",
  22. 'args': collections.OrderedDict([
  23. ('name', 'Name of the Geometry object from which to subtract.'),
  24. ('x0 y0 x1 y1 x2 y2 ...', 'Points defining the polygon.')
  25. ]),
  26. 'examples': []
  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. obj_name = args['name']
  37. if len(unnamed_args) % 2 != 0:
  38. return "Incomplete coordinate."
  39. points = [[float(unnamed_args[2 * i]), float(unnamed_args[2 * i + 1])] for i in range(len(unnamed_args) / 2)]
  40. try:
  41. obj = self.app.collection.get_by_name(str(obj_name))
  42. except:
  43. return "Could not retrieve object: %s" % obj_name
  44. if obj is None:
  45. return "Object not found: %s" % obj_name
  46. obj.subtract_polygon(points)