TclCommandScale.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. from ObjectCollection import *
  2. from tclCommands.TclCommand import TclCommand
  3. class TclCommandScale(TclCommand):
  4. """
  5. Tcl shell command to resizes the object by a factor.
  6. example:
  7. scale my_geometry 4.2
  8. """
  9. # List of all command aliases, to be able use old names for backward compatibility (add_poly, add_polygon)
  10. aliases = ['scale']
  11. # Dictionary of types from Tcl command, needs to be ordered
  12. arg_names = collections.OrderedDict([
  13. ('name', str),
  14. ('factor', float)
  15. ])
  16. # Dictionary of types from Tcl command, needs to be ordered , this is for options like -optionname value
  17. option_types = collections.OrderedDict([
  18. ('x', float),
  19. ('y', float),
  20. ('origin', str)
  21. ])
  22. # array of mandatory options for current Tcl command: required = {'name','outname'}
  23. required = ['name']
  24. # structured help for current command, args needs to be ordered
  25. help = {
  26. 'main': "Resizes the object by a factor on X axis and a factor on Y axis, having as scale origin the point ",
  27. 'args': collections.OrderedDict([
  28. ('name', 'Name of the object to resize.'),
  29. ('factor', 'Fraction by which to scale on both axis. '),
  30. ('x', 'Fraction by which to scale on X axis. If "factor" is used then this parameter is ignored'),
  31. ('y', 'Fraction by which to scale on Y axis. If "factor" is used then this parameter is ignored'),
  32. ('origin', 'Reference used for scale. It can be: "origin" which means point (0, 0) or "min_bounds" which '
  33. 'means the lower left point of the bounding box or it can be "center" which means the center '
  34. 'of the bounding box.')
  35. ]),
  36. 'examples': ['scale my_geometry 4.2',
  37. 'scale my_geo -x 3.1 -y 2.8',
  38. 'scale my_geo 1.2 -origin min_bounds']
  39. }
  40. def execute(self, args, unnamed_args):
  41. """
  42. :param args:
  43. :param unnamed_args:
  44. :return:
  45. """
  46. name = args['name']
  47. try:
  48. obj_to_scale = self.app.collection.get_by_name(name)
  49. except Exception as e:
  50. log.debug("TclCommandCopperClear.execute() --> %s" % str(e))
  51. self.raise_tcl_error("%s: %s" % (_("Could not retrieve box object"), name))
  52. return "Could not retrieve object: %s" % name
  53. if 'origin' not in args:
  54. xmin, ymin, xmax, ymax = obj_to_scale.bounds()
  55. c_x = xmin + (xmax - xmin) / 2
  56. c_y = ymin + (ymax - ymin) / 2
  57. point = (c_x, c_y)
  58. else:
  59. if args['origin'] == 'origin':
  60. point = (0, 0)
  61. elif args['origin'] == 'min_bounds':
  62. xmin, ymin, xmax, ymax = obj_to_scale.bounds()
  63. point = (xmin, ymin)
  64. elif args['origin'] == 'center':
  65. xmin, ymin, xmax, ymax = obj_to_scale.bounds()
  66. c_x = xmin + (xmax - xmin) / 2
  67. c_y = ymin + (ymax - ymin) / 2
  68. point = (c_x, c_y)
  69. else:
  70. self.raise_tcl_error('%s' % _("Expected -origin <origin> or -origin <min_bounds> or -origin <center>."))
  71. return 'fail'
  72. if 'factor' in args:
  73. factor = float(args['factor'])
  74. obj_to_scale.scale(factor, point=point)
  75. return
  76. if 'x' not in args and 'y' not in args:
  77. self.raise_tcl_error('%s' % _("Expected -x <value> -y <value>."))
  78. return 'fail'
  79. if 'x' in args and 'y' not in args:
  80. f_x = float(args['x'])
  81. obj_to_scale.scale(f_x, 0, point=point)
  82. elif 'x' not in args and 'y' in args:
  83. f_y = float(args['y'])
  84. obj_to_scale.scale(0, f_y, point=point)
  85. elif 'x' in args and 'y' in args:
  86. f_x = float(args['x'])
  87. f_y = float(args['y'])
  88. obj_to_scale.scale(f_x, f_y, point=point)