TclCommandAlignDrill.py 7.7 KB

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