TclCommandScale.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. from tclCommands.TclCommand import TclCommand
  2. import collections
  3. import logging
  4. import gettext
  5. import appTranslation 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. description = '%s %s' % ("--", "Will scale the geometry of a named object. Does not create a new object.")
  20. # Dictionary of types from Tcl command, needs to be ordered
  21. arg_names = collections.OrderedDict([
  22. ('name', str),
  23. ('factor', float)
  24. ])
  25. # Dictionary of types from Tcl command, needs to be ordered , this is for options like -optionname value
  26. option_types = collections.OrderedDict([
  27. ('x', float),
  28. ('y', float),
  29. ('origin', str)
  30. ])
  31. # array of mandatory options for current Tcl command: required = {'name','outname'}
  32. required = ['name']
  33. # structured help for current command, args needs to be ordered
  34. help = {
  35. 'main': "Resizes the object by a factor on X axis and a factor on Y axis, having as scale origin the point ",
  36. 'args': collections.OrderedDict([
  37. ('name', 'Name of the object (Gerber, Geometry or Excellon) to be resized. Required.'),
  38. ('factor', 'Fraction by which to scale on both axis.'),
  39. ('x', 'Fraction by which to scale on X axis. If "factor" is used then this parameter is ignored'),
  40. ('y', 'Fraction by which to scale on Y axis. If "factor" is used then this parameter is ignored'),
  41. ('origin', 'Reference used for scale.\n'
  42. 'The reference point can be:\n'
  43. '- "origin" which means point (0, 0)\n'
  44. '- "min_bounds" which means the lower left point of the bounding box\n'
  45. '- "center" which means the center point of the bounding box of the object.\n'
  46. '- a tuple in format (x,y) with the X and Y coordinates separated by a comma. NO SPACES ALLOWED')
  47. ]),
  48. 'examples': ['scale my_geometry 4.2',
  49. 'scale my_geo -x 3.1 -y 2.8',
  50. 'scale my_geo 1.2 -origin min_bounds',
  51. 'scale my_geometry -x 2 -origin 3.0,2.1']
  52. }
  53. def execute(self, args, unnamed_args):
  54. """
  55. :param args:
  56. :param unnamed_args:
  57. :return:
  58. """
  59. name = args['name']
  60. try:
  61. obj_to_scale = self.app.collection.get_by_name(name)
  62. except Exception as e:
  63. log.debug("TclCommandCopperClear.execute() --> %s" % str(e))
  64. self.raise_tcl_error("%s: %s" % (_("Could not retrieve box object"), name))
  65. return "Could not retrieve object: %s" % name
  66. if 'origin' not in args:
  67. xmin, ymin, xmax, ymax = obj_to_scale.bounds()
  68. c_x = xmin + (xmax - xmin) / 2
  69. c_y = ymin + (ymax - ymin) / 2
  70. point = (c_x, c_y)
  71. else:
  72. if args['origin'] == 'origin':
  73. point = (0, 0)
  74. elif args['origin'] == 'min_bounds':
  75. xmin, ymin, xmax, ymax = obj_to_scale.bounds()
  76. point = (xmin, ymin)
  77. elif args['origin'] == 'center':
  78. xmin, ymin, xmax, ymax = obj_to_scale.bounds()
  79. c_x = xmin + (xmax - xmin) / 2
  80. c_y = ymin + (ymax - ymin) / 2
  81. point = (c_x, c_y)
  82. else:
  83. try:
  84. point = eval(args['origin'])
  85. if not isinstance(point, tuple):
  86. raise Exception
  87. except Exception as e:
  88. self.raise_tcl_error('%s\n%s' % (_("Expected -origin <origin> or "
  89. "-origin <min_bounds> or "
  90. "-origin <center> or "
  91. "- origin 3.0,4.2."), str(e)))
  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)