TclCommandDelete.py 2.5 KB

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