TclCommandDelete.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. from tclCommands.TclCommand import TclCommand
  2. import collections
  3. class TclCommandDelete(TclCommand):
  4. """
  5. Tcl shell command to delete an object.
  6. example:
  7. """
  8. # List of all command aliases, to be able use old names for backward compatibility (add_poly, add_polygon)
  9. aliases = ['delete', 'del']
  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. ('f', bool)
  17. ])
  18. # array of mandatory options for current Tcl command: required = {'name','outname'}
  19. required = []
  20. # structured help for current command, args needs to be ordered
  21. help = {
  22. 'main': 'Deletes the given object. If no name is given will delete all objects.',
  23. 'args': collections.OrderedDict([
  24. ('name', 'Name of the Object.'),
  25. ('f', 'Use this parameter to force deletion.')
  26. ]),
  27. 'examples': ['del new_geo -f True\n'
  28. 'delete new_geo -f 1\n'
  29. 'del new_geo -f\n'
  30. 'del new_geo']
  31. }
  32. def execute(self, args, unnamed_args):
  33. """
  34. :param args:
  35. :param unnamed_args:
  36. :return:
  37. """
  38. obj_name = None
  39. try:
  40. obj_name = args['name']
  41. delete_all = False
  42. except KeyError:
  43. delete_all = True
  44. is_forced = False
  45. if 'f' in args:
  46. try:
  47. if args['f'] is None:
  48. is_forced = True
  49. else:
  50. is_forced = True if eval(str(args['f'])) else False
  51. except KeyError:
  52. is_forced = True
  53. if delete_all is False:
  54. try:
  55. # deselect all to avoid delete selected object when run delete from shell
  56. self.app.collection.set_all_inactive()
  57. self.app.collection.set_active(str(obj_name))
  58. self.app.on_delete(force_deletion=is_forced)
  59. except Exception as e:
  60. return "Command failed: %s" % str(e)
  61. else:
  62. try:
  63. self.app.collection.set_all_active()
  64. self.app.on_delete(force_deletion=is_forced)
  65. except Exception as e:
  66. return "Command failed: %s" % str(e)