TclCommandAlignDrill.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. from tclCommands.TclCommand import *
  2. class TclCommandAlignDrill(TclCommandSignaled):
  3. """
  4. Tcl shell command to create excellon with drills for aligment.
  5. """
  6. # array of all command aliases, to be able use old names for
  7. # backward compatibility (add_poly, add_polygon)
  8. aliases = ['aligndrill']
  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. ('box', str),
  18. ('axis', str),
  19. ('holes', str),
  20. ('grid', float),
  21. ('minoffset', float),
  22. ('gridoffset', float),
  23. ('axisoffset', float),
  24. ('dia', float),
  25. ('dist', float),
  26. ])
  27. # array of mandatory options for current Tcl command: required = {'name','outname'}
  28. required = ['name', 'axis']
  29. # structured help for current command, args needs to be ordered
  30. help = {
  31. 'main': "Create excellon with drills for aligment.",
  32. 'args': collections.OrderedDict([
  33. ('name', 'Name of the object (Gerber or Excellon) to mirror.'),
  34. ('dia', 'Tool diameter'),
  35. ('box', 'Name of object which act as box (cutout for example.)'),
  36. ('grid', 'Aligning to grid, for those, who have aligning pins'
  37. 'inside table in grid (-5,0),(5,0),(15,0)...'),
  38. ('gridoffset', 'offset of grid from 0 position.'),
  39. ('minoffset', 'min and max distance between align hole and pcb.'),
  40. ('axisoffset', 'Offset on second axis before aligment holes'),
  41. ('axis', 'Mirror axis parallel to the X or Y axis.'),
  42. ('dist', 'Distance of the mirror axis to the X or Y axis.')
  43. ]),
  44. 'examples': []
  45. }
  46. def execute(self, args, unnamed_args):
  47. """
  48. execute current TCL shell command
  49. :param args: array of known named arguments and options
  50. :param unnamed_args: array of other values which were passed into command
  51. without -somename and we do not have them in known arg_names
  52. :return: None or exception
  53. """
  54. name = args['name']
  55. # Get source object.
  56. try:
  57. obj = self.app.collection.get_by_name(str(name))
  58. except:
  59. return "Could not retrieve object: %s" % name
  60. if obj is None:
  61. return "Object not found: %s" % name
  62. if not isinstance(obj, FlatCAMGeometry) and \
  63. not isinstance(obj, FlatCAMGerber) and \
  64. not isinstance(obj, FlatCAMExcellon):
  65. return "ERROR: Only Gerber, Geometry and Excellon objects can be used."
  66. # Axis
  67. try:
  68. axis = args['axis'].upper()
  69. except KeyError:
  70. return "ERROR: Specify -axis X or -axis Y"
  71. if not ('holes' in args or ('grid' in args and 'gridoffset' in args)):
  72. return "ERROR: Specify -holes or -grid with -gridoffset "
  73. if 'holes' in args:
  74. try:
  75. holes = eval("[" + args['holes'] + "]")
  76. except KeyError:
  77. return "ERROR: Wrong -holes format (X1,Y1),(X2,Y2)"
  78. xscale, yscale = {"X": (1.0, -1.0), "Y": (-1.0, 1.0)}[axis]
  79. # Tools
  80. tools = {"1": {"C": args['dia']}}
  81. def alligndrill_init_me(init_obj, app_obj):
  82. """
  83. This function is used to initialize the new
  84. object once it's created.
  85. :param init_obj: The new object.
  86. :param app_obj: The application (FlatCAMApp)
  87. :return: None
  88. """
  89. drills = []
  90. if 'holes' in args:
  91. for hole in holes:
  92. point = Point(hole)
  93. point_mirror = affinity.scale(point, xscale, yscale, origin=(px, py))
  94. drills.append({"point": point, "tool": "1"})
  95. drills.append({"point": point_mirror, "tool": "1"})
  96. else:
  97. if 'box' not in args:
  98. return "ERROR: -grid can be used only for -box"
  99. if 'axisoffset' in args:
  100. axisoffset = args['axisoffset']
  101. else:
  102. axisoffset = 0
  103. # This will align hole to given aligngridoffset and minimal offset from pcb, based on selected axis
  104. if axis == "X":
  105. firstpoint = args['gridoffset']
  106. while (xmin - args['minoffset']) < firstpoint:
  107. firstpoint = firstpoint - args['grid']
  108. lastpoint = args['gridoffset']
  109. while (xmax + args['minoffset']) > lastpoint:
  110. lastpoint = lastpoint + args['grid']
  111. localholes = (firstpoint, axisoffset), (lastpoint, axisoffset)
  112. else:
  113. firstpoint = args['gridoffset']
  114. while (ymin - args['minoffset']) < firstpoint:
  115. firstpoint = firstpoint - args['grid']
  116. lastpoint = args['gridoffset']
  117. while (ymax + args['minoffset']) > lastpoint:
  118. lastpoint = lastpoint + args['grid']
  119. localholes = (axisoffset, firstpoint), (axisoffset, lastpoint)
  120. for hole in localholes:
  121. point = Point(hole)
  122. point_mirror = affinity.scale(point, xscale, yscale, origin=(px, py))
  123. drills.append({"point": point, "tool": "1"})
  124. drills.append({"point": point_mirror, "tool": "1"})
  125. init_obj.tools = tools
  126. init_obj.drills = drills
  127. init_obj.create_geometry()
  128. # Box
  129. if 'box' in args:
  130. try:
  131. box = self.app.collection.get_by_name(args['box'])
  132. except:
  133. return "Could not retrieve object box: %s" % args['box']
  134. if box is None:
  135. return "Object box not found: %s" % args['box']
  136. try:
  137. xmin, ymin, xmax, ymax = box.bounds()
  138. px = 0.5 * (xmin + xmax)
  139. py = 0.5 * (ymin + ymax)
  140. obj.app.new_object("excellon",
  141. name + "_aligndrill",
  142. alligndrill_init_me)
  143. except Exception as e:
  144. return "Operation failed: %s" % str(e)
  145. else:
  146. try:
  147. dist = float(args['dist'])
  148. except KeyError:
  149. dist = 0.0
  150. except ValueError:
  151. return "Invalid distance: %s" % args['dist']
  152. try:
  153. px = dist
  154. py = dist
  155. obj.app.new_object("excellon", name + "_alligndrill", alligndrill_init_me)
  156. except Exception as e:
  157. return "Operation failed: %s" % str(e)
  158. return 'Ok'