TclCommandScale.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. from tclCommands.TclCommand import TclCommand
  2. import collections
  3. import logging
  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. log = logging.getLogger('base')
  11. class TclCommandScale(TclCommand):
  12. """
  13. Tcl shell command to resizes the object by a factor.
  14. example:
  15. scale my_geometry 4.2
  16. """
  17. # List of all command aliases, to be able use old names for backward compatibility (add_poly, add_polygon)
  18. aliases = ['scale']
  19. # Dictionary of types from Tcl command, needs to be ordered
  20. arg_names = collections.OrderedDict([
  21. ('name', str),
  22. ('factor', float)
  23. ])
  24. # Dictionary of types from Tcl command, needs to be ordered , this is for options like -optionname value
  25. option_types = collections.OrderedDict([
  26. ('x', float),
  27. ('y', float),
  28. ('origin', str)
  29. ])
  30. # array of mandatory options for current Tcl command: required = {'name','outname'}
  31. required = ['name']
  32. # structured help for current command, args needs to be ordered
  33. help = {
  34. 'main': "Resizes the object by a factor on X axis and a factor on Y axis, having as scale origin the point ",
  35. 'args': collections.OrderedDict([
  36. ('name', 'Name of the object (Gerber, Geometry or Excellon) to be resized. Required.'),
  37. ('factor', 'Fraction by which to scale on both axis.'),
  38. ('x', 'Fraction by which to scale on X axis. If "factor" is used then this parameter is ignored'),
  39. ('y', 'Fraction by which to scale on Y axis. If "factor" is used then this parameter is ignored'),
  40. ('origin', 'Reference used for scale.\n'
  41. 'The reference point can be:\n'
  42. '- "origin" which means point (0, 0)\n'
  43. '- "min_bounds" which means the lower left point of the bounding box\n'
  44. '- "center" which means the center point of the bounding box of the object.\n'
  45. '- a tuple in format (x,y) with the X and Y coordinates separated by a comma. NO SPACES ALLOWED')
  46. ]),
  47. 'examples': ['scale my_geometry 4.2',
  48. 'scale my_geo -x 3.1 -y 2.8',
  49. 'scale my_geo 1.2 -origin min_bounds',
  50. 'scale my_geometry -x 2 -origin 3.0,2.1']
  51. }
  52. def execute(self, args, unnamed_args):
  53. """
  54. :param args:
  55. :param unnamed_args:
  56. :return:
  57. """
  58. name = args['name']
  59. try:
  60. obj_to_scale = self.app.collection.get_by_name(name)
  61. except Exception as e:
  62. log.debug("TclCommandCopperClear.execute() --> %s" % str(e))
  63. self.raise_tcl_error("%s: %s" % (_("Could not retrieve box object"), name))
  64. return "Could not retrieve object: %s" % name
  65. if 'origin' not in args:
  66. xmin, ymin, xmax, ymax = obj_to_scale.bounds()
  67. c_x = xmin + (xmax - xmin) / 2
  68. c_y = ymin + (ymax - ymin) / 2
  69. point = (c_x, c_y)
  70. else:
  71. if args['origin'] == 'origin':
  72. point = (0, 0)
  73. elif args['origin'] == 'min_bounds':
  74. xmin, ymin, xmax, ymax = obj_to_scale.bounds()
  75. point = (xmin, ymin)
  76. elif args['origin'] == 'center':
  77. xmin, ymin, xmax, ymax = obj_to_scale.bounds()
  78. c_x = xmin + (xmax - xmin) / 2
  79. c_y = ymin + (ymax - ymin) / 2
  80. point = (c_x, c_y)
  81. else:
  82. try:
  83. point = eval(args['origin'])
  84. if not isinstance(point, tuple):
  85. raise Exception
  86. except Exception as e:
  87. self.raise_tcl_error('%s\n%s' % (_("Expected -origin <origin> or "
  88. "-origin <min_bounds> or "
  89. "-origin <center> or "
  90. "- origin 3.0,4.2."), str(e))
  91. )
  92. return 'fail'
  93. if 'factor' in args:
  94. factor = float(args['factor'])
  95. obj_to_scale.scale(factor, point=point)
  96. return
  97. if 'x' not in args and 'y' not in args:
  98. self.raise_tcl_error('%s' % _("Expected -x <value> -y <value>."))
  99. return 'fail'
  100. if 'x' in args and 'y' not in args:
  101. f_x = float(args['x'])
  102. obj_to_scale.scale(f_x, 0, point=point)
  103. elif 'x' not in args and 'y' in args:
  104. f_y = float(args['y'])
  105. obj_to_scale.scale(0, f_y, point=point)
  106. elif 'x' in args and 'y' in args:
  107. f_x = float(args['x'])
  108. f_y = float(args['y'])
  109. obj_to_scale.scale(f_x, f_y, point=point)