TclCommandMirror.py 3.3 KB

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