TclCommandScale.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 to resize.'),
  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. It can be: "origin" which means point (0, 0) or "min_bounds" which '
  41. 'means the lower left point of the bounding box or it can be "center" which means the center '
  42. 'of the bounding box.')
  43. ]),
  44. 'examples': ['scale my_geometry 4.2',
  45. 'scale my_geo -x 3.1 -y 2.8',
  46. 'scale my_geo 1.2 -origin min_bounds']
  47. }
  48. def execute(self, args, unnamed_args):
  49. """
  50. :param args:
  51. :param unnamed_args:
  52. :return:
  53. """
  54. name = args['name']
  55. try:
  56. obj_to_scale = self.app.collection.get_by_name(name)
  57. except Exception as e:
  58. log.debug("TclCommandCopperClear.execute() --> %s" % str(e))
  59. self.raise_tcl_error("%s: %s" % (_("Could not retrieve box object"), name))
  60. return "Could not retrieve object: %s" % name
  61. if 'origin' not in args:
  62. xmin, ymin, xmax, ymax = obj_to_scale.bounds()
  63. c_x = xmin + (xmax - xmin) / 2
  64. c_y = ymin + (ymax - ymin) / 2
  65. point = (c_x, c_y)
  66. else:
  67. if args['origin'] == 'origin':
  68. point = (0, 0)
  69. elif args['origin'] == 'min_bounds':
  70. xmin, ymin, xmax, ymax = obj_to_scale.bounds()
  71. point = (xmin, ymin)
  72. elif args['origin'] == 'center':
  73. xmin, ymin, xmax, ymax = obj_to_scale.bounds()
  74. c_x = xmin + (xmax - xmin) / 2
  75. c_y = ymin + (ymax - ymin) / 2
  76. point = (c_x, c_y)
  77. else:
  78. self.raise_tcl_error('%s' % _("Expected -origin <origin> or -origin <min_bounds> or -origin <center>."))
  79. return 'fail'
  80. if 'factor' in args:
  81. factor = float(args['factor'])
  82. obj_to_scale.scale(factor, point=point)
  83. return
  84. if 'x' not in args and 'y' not in args:
  85. self.raise_tcl_error('%s' % _("Expected -x <value> -y <value>."))
  86. return 'fail'
  87. if 'x' in args and 'y' not in args:
  88. f_x = float(args['x'])
  89. obj_to_scale.scale(f_x, 0, point=point)
  90. elif 'x' not in args and 'y' in args:
  91. f_y = float(args['y'])
  92. obj_to_scale.scale(0, f_y, point=point)
  93. elif 'x' in args and 'y' in args:
  94. f_x = float(args['x'])
  95. f_y = float(args['y'])
  96. obj_to_scale.scale(f_x, f_y, point=point)