TclCommandSetOrigin.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 TclCommandSetOrigin(TclCommand):
  11. """
  12. Tcl shell command to clear the text in the Tcl Shell browser.
  13. example:
  14. """
  15. # List of all command aliases, to be able use old names for backward compatibility (add_poly, add_polygon)
  16. aliases = ['set_origin', 'origin']
  17. # Dictionary of types from Tcl command, needs to be ordered
  18. arg_names = collections.OrderedDict([
  19. ('loc', 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. ('auto', bool)
  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 set the origin at the specified x,y location.",
  30. 'args': collections.OrderedDict([
  31. ('loc', 'Location to offset all the selected objects. No spaces between x and y pair. Use like this: 2,3'),
  32. ('auto', 'If set to 1 it will set the origin to the minimum x, y of the object selection bounding box.'
  33. '-auto=1 is not correct but -auto 1 or -auto True is correct.')
  34. ]),
  35. 'examples': ['set_origin 3,2', 'set_origin -auto 1']
  36. }
  37. def execute(self, args, unnamed_args):
  38. """
  39. :param args:
  40. :param unnamed_args:
  41. :return:
  42. """
  43. print(args)
  44. loc = list()
  45. if 'auto' in args:
  46. if args['auto'] == 1:
  47. objs = self.app.collection.get_list()
  48. minx, miny, __, ___ = get_bounds(objs)
  49. loc.append(0 - minx)
  50. loc.append(0 - miny)
  51. else:
  52. loc = [0, 0]
  53. elif 'loc' in args:
  54. try:
  55. location = [float(eval(coord)) for coord in str(args['loc']).split(",") if coord != '']
  56. except AttributeError as e:
  57. log.debug("TclCommandSetOrigin.execute --> %s" % str(e))
  58. location = (0, 0)
  59. loc.append(location[0])
  60. loc.append(location[1])
  61. if len(location) != 2:
  62. self.raise_tcl_error('%s: %s' % (
  63. _("Expected a pair of (x, y) coordinates. Got"), str(len(location))))
  64. return 'fail'
  65. else:
  66. loc = [0, 0]
  67. self.app.on_set_zero_click(event=None, location=loc, noplot=True, use_thread=False)
  68. self.app.inform.emit('[success] Tcl %s: %s' %
  69. (_('Origin set by offsetting all loaded objects with '),
  70. '{0:.4f}, {0:.4f}'.format(loc[0], loc[1])))