TclCommandMirror.py 4.1 KB

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