TclCommandDelete.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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', str)
  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.\n'
  27. 'Can be used without value which will be auto assumed to be True.\n'
  28. 'Or it can have a value: True (1) or False (0).')
  29. ]),
  30. 'examples': ['del new_geo -f True\n'
  31. 'delete new_geo -f 1\n'
  32. 'del new_geo -f\n'
  33. 'del new_geo']
  34. }
  35. def execute(self, args, unnamed_args):
  36. """
  37. :param args:
  38. :param unnamed_args:
  39. :return:
  40. """
  41. obj_name = None
  42. try:
  43. obj_name = args['name']
  44. delete_all = False
  45. except KeyError:
  46. delete_all = True
  47. is_forced = False
  48. if 'f' in args:
  49. try:
  50. if args['f'] is None:
  51. is_forced = True
  52. else:
  53. try:
  54. par = args['f'].capitalize()
  55. except AttributeError:
  56. par = args['f']
  57. is_forced = bool(eval(par))
  58. except KeyError:
  59. is_forced = True
  60. if delete_all is False:
  61. try:
  62. # deselect all to avoid delete selected object when run delete from shell
  63. self.app.collection.set_all_inactive()
  64. self.app.collection.set_active(str(obj_name))
  65. self.app.on_delete(force_deletion=is_forced)
  66. except Exception as e:
  67. return "Command failed: %s" % str(e)
  68. else:
  69. try:
  70. self.app.collection.set_all_active()
  71. self.app.on_delete(force_deletion=is_forced)
  72. except Exception as e:
  73. return "Command failed: %s" % str(e)