TclCommandSetOrigin.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # ##########################################################
  2. # FlatCAM: 2D Post-processing for Manufacturing #
  3. # File Author: Marius Adrian Stanciu (c) #
  4. # Date: 8/17/2019 #
  5. # MIT Licence #
  6. # ##########################################################
  7. from tclCommands.TclCommand import TclCommand
  8. from ObjectCollection import *
  9. from camlib import get_bounds
  10. import gettext
  11. import FlatCAMTranslation as fcTranslate
  12. import builtins
  13. fcTranslate.apply_language('strings')
  14. if '_' not in builtins.__dict__:
  15. _ = gettext.gettext
  16. class TclCommandSetOrigin(TclCommand):
  17. """
  18. Tcl shell command to set the origin to zero or to a specified location for all loaded objects in FlatCAM.
  19. example:
  20. """
  21. # List of all command aliases, to be able use old names for backward compatibility (add_poly, add_polygon)
  22. aliases = ['set_origin', 'origin']
  23. # Dictionary of types from Tcl command, needs to be ordered
  24. arg_names = collections.OrderedDict([
  25. ('loc', str)
  26. ])
  27. # Dictionary of types from Tcl command, needs to be ordered , this is for options like -optionname value
  28. option_types = collections.OrderedDict([
  29. ('auto', bool)
  30. ])
  31. # array of mandatory options for current Tcl command: required = {'name','outname'}
  32. required = []
  33. # structured help for current command, args needs to be ordered
  34. help = {
  35. 'main': "Will set the origin at the specified x,y location.",
  36. 'args': collections.OrderedDict([
  37. ('loc', 'Location to offset all the selected objects. No spaces between x and y pair. Use like this: 2,3'),
  38. ('auto', 'If set to 1 it will set the origin to the minimum x, y of the object selection bounding box.'
  39. '-auto=1 is not correct but -auto 1 or -auto True is correct.')
  40. ]),
  41. 'examples': ['set_origin 3,2', 'set_origin -auto 1']
  42. }
  43. def execute(self, args, unnamed_args):
  44. """
  45. :param args:
  46. :param unnamed_args:
  47. :return:
  48. """
  49. loc = list()
  50. if 'auto' in args:
  51. if args['auto'] == 1:
  52. objs = self.app.collection.get_list()
  53. minx, miny, __, ___ = get_bounds(objs)
  54. loc.append(0 - minx)
  55. loc.append(0 - miny)
  56. else:
  57. loc = [0, 0]
  58. elif 'loc' in args:
  59. try:
  60. location = [float(eval(coord)) for coord in str(args['loc']).split(",") if coord != '']
  61. except AttributeError as e:
  62. log.debug("TclCommandSetOrigin.execute --> %s" % str(e))
  63. location = (0, 0)
  64. loc.append(location[0])
  65. loc.append(location[1])
  66. if len(location) != 2:
  67. self.raise_tcl_error('%s: %s' % (
  68. _("Expected a pair of (x, y) coordinates. Got"), str(len(location))))
  69. return 'fail'
  70. else:
  71. loc = [0, 0]
  72. self.app.on_set_zero_click(event=None, location=loc, noplot=True, use_thread=False)
  73. self.app.inform.emit('[success] Tcl %s: %s' %
  74. (_('Origin set by offsetting all loaded objects with '),
  75. '{0:.4f}, {0:.4f}'.format(loc[0], loc[1])))