TclCommandSetOrigin.py 3.3 KB

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