TclCommandAlignDrill.py 6.9 KB

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