TclCommandMirror.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. from tclCommands.TclCommand import TclCommandSignaled
  2. import collections
  3. class TclCommandMirror(TclCommandSignaled):
  4. """
  5. Tcl shell command to mirror an object.
  6. """
  7. # array of all command aliases, to be able use
  8. # old names for backward compatibility (add_poly, add_polygon)
  9. aliases = ['mirror']
  10. description = '%s %s' % ("--", "Will mirror the geometry of a named object. Does not create a new object.")
  11. # Dictionary of types from Tcl command, needs to be ordered.
  12. # For positional arguments
  13. arg_names = collections.OrderedDict([
  14. ('name', str)
  15. ])
  16. # Dictionary of types from Tcl command, needs to be ordered.
  17. # For options like -optionname value
  18. option_types = collections.OrderedDict([
  19. ('axis', str),
  20. ('box', str),
  21. ('origin', str)
  22. ])
  23. # array of mandatory options for current Tcl command: required = {'name','outname'}
  24. required = ['name']
  25. # structured help for current command, args needs to be ordered
  26. help = {
  27. 'main': "Will mirror the geometry of a named object. Does not create a new object.",
  28. 'args': collections.OrderedDict([
  29. ('name', 'Name of the object (Gerber, Geometry or Excellon) to be mirrored. Required.'),
  30. ('axis', 'Mirror axis parallel to the X or Y axis.'),
  31. ('box', 'Name of object which act as box (cutout for example.)'),
  32. ('origin', 'Reference point . It is used only if the box is not used. Format (x,y).\n'
  33. 'Comma will separate the X and Y coordinates.\n'
  34. 'WARNING: no spaces are allowed. If uncertain enclose the two values inside parenthesis.\n'
  35. 'See the example.')
  36. ]),
  37. 'examples': ['mirror obj_name -box box_geo -axis X -origin 3.2,4.7']
  38. }
  39. def execute(self, args, unnamed_args):
  40. """
  41. Execute this TCL shell command
  42. :param args: array of known named arguments and options
  43. :param unnamed_args: array of other values which were passed into command
  44. without -somename and we do not have them in known arg_names
  45. :return: None or exception
  46. """
  47. name = args['name']
  48. # Get source object.
  49. try:
  50. obj = self.app.collection.get_by_name(str(name))
  51. except Exception:
  52. return "Could not retrieve object: %s" % name
  53. if obj is None:
  54. return "Object not found: %s" % name
  55. if obj.kind != 'gerber' and obj.kind != 'geometry' and obj.kind != 'excellon':
  56. return "ERROR: Only Gerber, Excellon and Geometry objects can be mirrored."
  57. # Axis
  58. if 'axis' in args:
  59. try:
  60. axis = args['axis'].upper()
  61. except KeyError:
  62. axis = 'Y'
  63. else:
  64. axis = 'Y'
  65. # Box
  66. if 'box' in args:
  67. try:
  68. box = self.app.collection.get_by_name(args['box'])
  69. except Exception:
  70. return "Could not retrieve object box: %s" % args['box']
  71. if box is None:
  72. return "Object box not found: %s" % args['box']
  73. try:
  74. xmin, ymin, xmax, ymax = box.bounds()
  75. px = 0.5 * (xmin + xmax)
  76. py = 0.5 * (ymin + ymax)
  77. obj.mirror(axis, [px, py])
  78. obj.plot()
  79. return
  80. except Exception as e:
  81. return "Operation failed: %s" % str(e)
  82. # Origin
  83. if 'origin' in args:
  84. try:
  85. origin_val = eval(args['origin'])
  86. x = float(origin_val[0])
  87. y = float(origin_val[1])
  88. except KeyError:
  89. x, y = (0, 0)
  90. except ValueError:
  91. return "Invalid distance: %s" % str(args['origin'])
  92. try:
  93. obj.mirror(axis, [x, y])
  94. except Exception as e:
  95. return "Operation failed: %s" % str(e)