TclCommandMirror.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. from tclCommands.TclCommand import *
  2. class TclCommandMirror(TclCommandSignaled):
  3. """
  4. Tcl shell command to mirror an object.
  5. """
  6. # array of all command aliases, to be able use
  7. # old names for backward compatibility (add_poly, add_polygon)
  8. aliases = ['mirror']
  9. # Dictionary of types from Tcl command, needs to be ordered.
  10. # For positional arguments
  11. arg_names = collections.OrderedDict([
  12. ('name', str)
  13. ])
  14. # Dictionary of types from Tcl command, needs to be ordered.
  15. # For options like -optionname value
  16. option_types = collections.OrderedDict([
  17. ('axis', str),
  18. ('box', str),
  19. ('dist', float)
  20. ])
  21. # array of mandatory options for current Tcl command: required = {'name','outname'}
  22. required = ['name', 'axis']
  23. # structured help for current command, args needs to be ordered
  24. help = {
  25. 'main': "Opens an Excellon file.",
  26. 'args': collections.OrderedDict([
  27. ('name', 'Name of the object (Gerber or Excellon) to mirror.'),
  28. ('box', 'Name of object which act as box (cutout for example.)'),
  29. ('axis', 'Mirror axis parallel to the X or Y axis.'),
  30. ('dist', 'Distance of the mirror axis to the X or Y axis.')
  31. ]),
  32. 'examples': []
  33. }
  34. def execute(self, args, unnamed_args):
  35. """
  36. Execute this TCL shell command
  37. :param args: array of known named arguments and options
  38. :param unnamed_args: array of other values which were passed into command
  39. without -somename and we do not have them in known arg_names
  40. :return: None or exception
  41. """
  42. name = args['name']
  43. # Get source object.
  44. try:
  45. obj = self.app.collection.get_by_name(str(name))
  46. except:
  47. return "Could not retrieve object: %s" % name
  48. if obj is None:
  49. return "Object not found: %s" % name
  50. if not isinstance(obj, FlatCAMGerber) and \
  51. not isinstance(obj, FlatCAMExcellon) and \
  52. not isinstance(obj, FlatCAMGeometry):
  53. return "ERROR: Only Gerber, Excellon and Geometry objects can be mirrored."
  54. # Axis
  55. try:
  56. axis = args['axis'].upper()
  57. except KeyError:
  58. return "ERROR: Specify -axis X or -axis Y"
  59. # Box
  60. if 'box' in args:
  61. try:
  62. box = self.app.collection.get_by_name(args['box'])
  63. except:
  64. return "Could not retrieve object box: %s" % args['box']
  65. if box is None:
  66. return "Object box not found: %s" % args['box']
  67. try:
  68. xmin, ymin, xmax, ymax = box.bounds()
  69. px = 0.5 * (xmin + xmax)
  70. py = 0.5 * (ymin + ymax)
  71. obj.mirror(axis, [px, py])
  72. obj.plot()
  73. except Exception as e:
  74. return "Operation failed: %s" % str(e)
  75. else:
  76. try:
  77. dist = float(args['dist'])
  78. except KeyError:
  79. dist = 0.0
  80. except ValueError:
  81. return "Invalid distance: %s" % args['dist']
  82. try:
  83. obj.mirror(axis, [dist, dist])
  84. obj.plot()
  85. except Exception as e:
  86. return "Operation failed: %s" % str(e)