TclCommandBounds.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. from tclCommands.TclCommand import TclCommand
  2. import collections
  3. import logging
  4. import gettext
  5. import appTranslation as fcTranslate
  6. import builtins
  7. fcTranslate.apply_language('strings')
  8. if '_' not in builtins.__dict__:
  9. _ = gettext.gettext
  10. log = logging.getLogger('base')
  11. class TclCommandBounds(TclCommand):
  12. """
  13. Tcl shell command to return the bounds values for a supplied list of objects (identified by their names).
  14. example:
  15. """
  16. # List of all command aliases, to be able use old names for backward compatibility (add_poly, add_polygon)
  17. aliases = ['get_bounds', 'bounds']
  18. description = '%s %s' % ("--", "Return in the console a list of bounds values for a list of objects.")
  19. # Dictionary of types from Tcl command, needs to be ordered
  20. arg_names = collections.OrderedDict([
  21. ('objects', str)
  22. ])
  23. # Dictionary of types from Tcl command, needs to be ordered , this is for options like -optionname value
  24. option_types = collections.OrderedDict([
  25. ])
  26. # array of mandatory options for current Tcl command: required = {'name','outname'}
  27. required = []
  28. # structured help for current command, args needs to be ordered
  29. help = {
  30. 'main': "Will return a list of bounds values, each set of bound values is "
  31. "a list itself: [xmin, ymin, xmax, ymax] corresponding to each of the provided objects.",
  32. 'args': collections.OrderedDict([
  33. ('objects', 'A list of object names separated by comma without spaces.'),
  34. ]),
  35. 'examples': ['bounds a_obj.GTL,b_obj.DRL']
  36. }
  37. def execute(self, args, unnamed_args):
  38. """
  39. :param args:
  40. :param unnamed_args:
  41. :return:
  42. """
  43. obj_list = []
  44. if 'objects' in args:
  45. try:
  46. obj_list = [str(obj_name) for obj_name in str(args['objects']).split(",") if obj_name != '']
  47. except AttributeError as e:
  48. log.debug("TclCommandBounds.execute --> %s" % str(e))
  49. if not obj_list:
  50. self.raise_tcl_error('%s: %s:' % (
  51. _("Expected a list of objects names separated by comma. Got"), str(args['objects'])))
  52. return 'fail'
  53. else:
  54. self.raise_tcl_error('%s: %s:' % (
  55. _("Expected a list of objects names separated by comma. Got"), str(args['objects'])))
  56. return 'fail'
  57. result_list = []
  58. for name in obj_list:
  59. obj = self.app.collection.get_by_name(name)
  60. xmin, ymin, xmax, ymax = obj.bounds()
  61. result_list.append([xmin, ymin, xmax, ymax])
  62. self.app.inform.emit('[success] %s ...' % _('TclCommand Bounds done.'))
  63. return result_list