TclCommandMirror.py 3.4 KB

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