TclCommandAlignDrill.py 7.3 KB

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