TclCommandBounds.py 2.6 KB

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