TclCommandBounds.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. from tclCommands.TclCommand import TclCommand
  2. import collections
  3. import logging
  4. import gettext
  5. import FlatCAMTranslation 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. # Dictionary of types from Tcl command, needs to be ordered
  19. arg_names = collections.OrderedDict([
  20. ('objects', str)
  21. ])
  22. # Dictionary of types from Tcl command, needs to be ordered , this is for options like -optionname value
  23. option_types = collections.OrderedDict([
  24. ])
  25. # array of mandatory options for current Tcl command: required = {'name','outname'}
  26. required = []
  27. # structured help for current command, args needs to be ordered
  28. help = {
  29. 'main': "Will return a list of bounds values, each set of bound values is "
  30. "a list itself: [xmin, ymin, xmax, ymax].",
  31. 'args': collections.OrderedDict([
  32. ('objects', 'A list of object names separated by comma without spaces.'),
  33. ]),
  34. 'examples': ['bounds a_obj.GTL,b_obj.DRL']
  35. }
  36. def execute(self, args, unnamed_args):
  37. """
  38. :param args:
  39. :param unnamed_args:
  40. :return:
  41. """
  42. obj_list = list()
  43. if 'objects' in args:
  44. try:
  45. obj_list = [str(obj_name) for obj_name in str(args['objects']).split(",") if obj_name != '']
  46. except AttributeError as e:
  47. log.debug("TclCommandBounds.execute --> %s" % str(e))
  48. if not obj_list:
  49. self.raise_tcl_error('%s: %s:' % (
  50. _("Expected a list of objects names separated by comma. Got"), str(args['objects'])))
  51. return 'fail'
  52. else:
  53. self.raise_tcl_error('%s: %s:' % (
  54. _("Expected a list of objects names separated by comma. Got"), str(args['objects'])))
  55. return 'fail'
  56. result_list = list()
  57. for name in obj_list:
  58. obj = self.app.collection.get_by_name(name)
  59. xmin, ymin, xmax, ymax = obj.bounds()
  60. result_list.append([xmin, ymin, xmax, ymax])
  61. self.app.inform.emit('[success] %s ...' %
  62. _('TclCommand Bounds done.'))
  63. return result_list