TclCommandSubtractPoly.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. from ObjectCollection import *
  2. from tclCommands.TclCommand import TclCommandSignaled
  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. # Dictionary of types from Tcl command, needs to be ordered.
  10. # For positional arguments
  11. arg_names = collections.OrderedDict([
  12. ('name', str)
  13. ])
  14. # Dictionary of types from Tcl command, needs to be ordered.
  15. # For options like -optionname value
  16. option_types = collections.OrderedDict([
  17. ])
  18. # array of mandatory options for current Tcl command: required = {'name','outname'}
  19. required = ['name']
  20. # structured help for current command, args needs to be ordered
  21. help = {
  22. 'main': "Subtract polygon from the given Geometry object.",
  23. 'args': collections.OrderedDict([
  24. ('name', 'Name of the Geometry object from which to subtract.'),
  25. ('x0 y0 x1 y1 x2 y2 ...', 'Points defining the polygon.')
  26. ]),
  27. 'examples': []
  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. obj_name = args['name']
  38. if len(unnamed_args) % 2 != 0:
  39. return "Incomplete coordinate."
  40. points = [[float(unnamed_args[2 * i]), float(unnamed_args[2 * i + 1])] for i in range(len(unnamed_args) / 2)]
  41. try:
  42. obj = self.app.collection.get_by_name(str(obj_name))
  43. except:
  44. return "Could not retrieve object: %s" % obj_name
  45. if obj is None:
  46. return "Object not found: %s" % obj_name
  47. obj.subtract_polygon(points)