TclCommandAlignDrill.py 7.5 KB

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