TclCommandSetOrigin.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 set the origin to zero or to a specified location for all loaded objects in FlatCAM.
  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. loc = list()
  44. if 'auto' in args:
  45. if args['auto'] == 1:
  46. objs = self.app.collection.get_list()
  47. minx, miny, __, ___ = get_bounds(objs)
  48. loc.append(0 - minx)
  49. loc.append(0 - miny)
  50. else:
  51. loc = [0, 0]
  52. elif 'loc' in args:
  53. try:
  54. location = [float(eval(coord)) for coord in str(args['loc']).split(",") if coord != '']
  55. except AttributeError as e:
  56. log.debug("TclCommandSetOrigin.execute --> %s" % str(e))
  57. location = (0, 0)
  58. loc.append(location[0])
  59. loc.append(location[1])
  60. if len(location) != 2:
  61. self.raise_tcl_error('%s: %s' % (
  62. _("Expected a pair of (x, y) coordinates. Got"), str(len(location))))
  63. return 'fail'
  64. else:
  65. loc = [0, 0]
  66. self.app.on_set_zero_click(event=None, location=loc, noplot=True, use_thread=False)
  67. self.app.inform.emit('[success] Tcl %s: %s' %
  68. (_('Origin set by offsetting all loaded objects with '),
  69. '{0:.4f}, {0:.4f}'.format(loc[0], loc[1])))