TclCommandSetOrigin.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 appTranslation 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. description = '%s %s' % ("--", "Set the origin at the specified x,y location.")
  26. # Dictionary of types from Tcl command, needs to be ordered
  27. arg_names = collections.OrderedDict([
  28. ('loc', str)
  29. ])
  30. # Dictionary of types from Tcl command, needs to be ordered , this is for options like -optionname value
  31. option_types = collections.OrderedDict([
  32. ('auto', str)
  33. ])
  34. # array of mandatory options for current Tcl command: required = {'name','outname'}
  35. required = []
  36. # structured help for current command, args needs to be ordered
  37. help = {
  38. 'main': "Will set the origin at the specified x,y location.\n"
  39. "If it is called without arguments it will set origin at (0, 0)",
  40. 'args': collections.OrderedDict([
  41. ('loc', 'Location to offset all the selected objects. NO SPACES ALLOWED in X and Y pair.\n'
  42. 'Use like this: 2,3'),
  43. ('auto', 'If set to True it will set the origin to the minimum x, y of the object selection bounding box.'
  44. '-auto=True is not correct but -auto 1 or -auto True is correct. True (1) or False (0).')
  45. ]),
  46. 'examples': ['set_origin 3,2', 'set_origin -auto 1', 'origin']
  47. }
  48. def execute(self, args, unnamed_args):
  49. """
  50. :param args:
  51. :param unnamed_args:
  52. :return:
  53. """
  54. loc = []
  55. if 'auto' in args:
  56. if bool(args['auto']) is True:
  57. objs = self.app.collection.get_list()
  58. minx, miny, __, ___ = get_bounds(objs)
  59. loc.append(0 - minx)
  60. loc.append(0 - miny)
  61. else:
  62. loc = [0, 0]
  63. elif 'loc' in args:
  64. try:
  65. location = [float(eval(coord)) for coord in str(args['loc']).split(",") if coord != '']
  66. except AttributeError as e:
  67. log.debug("TclCommandSetOrigin.execute --> %s" % str(e))
  68. location = (0, 0)
  69. loc.append(location[0])
  70. loc.append(location[1])
  71. if len(location) != 2:
  72. self.raise_tcl_error('%s: %s' % (
  73. _("Expected a pair of (x, y) coordinates. Got"), str(len(location))))
  74. return 'fail'
  75. else:
  76. loc = [0, 0]
  77. self.app.on_set_zero_click(event=None, location=loc, noplot=True, use_thread=False)
  78. msg = '[success] Tcl %s: %s' % (_('Origin set by offsetting all loaded objects with '),
  79. '{0:.4f}, {0:.4f}'.format(loc[0], loc[1]))
  80. self.app.inform_shell.emit(msg)