TclCommandSetOrigin.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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.\n"
  38. "If it is called without arguments it will set origin at (0, 0)",
  39. 'args': collections.OrderedDict([
  40. ('loc', 'Location to offset all the selected objects. NO SPACES ALLOWED in X and Y pair.\n'
  41. 'Use like this: 2,3'),
  42. ('auto', 'If set to True it will set the origin to the minimum x, y of the object selection bounding box.'
  43. '-auto=True is not correct but -auto 1 or -auto True is correct.')
  44. ]),
  45. 'examples': ['set_origin 3,2', 'set_origin -auto 1', 'origin']
  46. }
  47. def execute(self, args, unnamed_args):
  48. """
  49. :param args:
  50. :param unnamed_args:
  51. :return:
  52. """
  53. loc = []
  54. if 'auto' in args:
  55. if bool(args['auto']) is True:
  56. objs = self.app.collection.get_list()
  57. minx, miny, __, ___ = get_bounds(objs)
  58. loc.append(0 - minx)
  59. loc.append(0 - miny)
  60. else:
  61. loc = [0, 0]
  62. elif 'loc' in args:
  63. try:
  64. location = [float(eval(coord)) for coord in str(args['loc']).split(",") if coord != '']
  65. except AttributeError as e:
  66. log.debug("TclCommandSetOrigin.execute --> %s" % str(e))
  67. location = (0, 0)
  68. loc.append(location[0])
  69. loc.append(location[1])
  70. if len(location) != 2:
  71. self.raise_tcl_error('%s: %s' % (
  72. _("Expected a pair of (x, y) coordinates. Got"), str(len(location))))
  73. return 'fail'
  74. else:
  75. loc = [0, 0]
  76. self.app.on_set_zero_click(event=None, location=loc, noplot=True, use_thread=False)
  77. self.app.inform.emit('[success] Tcl %s: %s' %
  78. (_('Origin set by offsetting all loaded objects with '),
  79. '{0:.4f}, {0:.4f}'.format(loc[0], loc[1])))