ToolCalibrateExcellon.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. # ##########################################################
  2. # FlatCAM: 2D Post-processing for Manufacturing #
  3. # File Author: Marius Adrian Stanciu (c) #
  4. # Date: 3/10/2019 #
  5. # MIT Licence #
  6. # ##########################################################
  7. from PyQt5 import QtWidgets, QtCore
  8. from FlatCAMTool import FlatCAMTool
  9. from flatcamGUI.GUIElements import FCDoubleSpinner, EvalEntry
  10. import math
  11. from shapely.geometry import Point
  12. from shapely.geometry.base import *
  13. import gettext
  14. import FlatCAMTranslation as fcTranslate
  15. import builtins
  16. fcTranslate.apply_language('strings')
  17. if '_' not in builtins.__dict__:
  18. _ = gettext.gettext
  19. class ToolCalibrateExcellon(FlatCAMTool):
  20. toolName = _("Calibrate Excellon")
  21. def __init__(self, app):
  22. FlatCAMTool.__init__(self, app)
  23. self.app = app
  24. self.canvas = self.app.plotcanvas
  25. self.decimals = 4
  26. # ## Title
  27. title_label = QtWidgets.QLabel("%s" % self.toolName)
  28. title_label.setStyleSheet("""
  29. QLabel
  30. {
  31. font-size: 16px;
  32. font-weight: bold;
  33. }
  34. """)
  35. self.layout.addWidget(title_label)
  36. # ## Grid Layout
  37. grid_lay = QtWidgets.QGridLayout()
  38. self.layout.addLayout(grid_lay)
  39. grid_lay.setColumnStretch(0, 0)
  40. grid_lay.setColumnStretch(1, 1)
  41. grid_lay.setColumnStretch(2, 1)
  42. self.exc_object_combo = QtWidgets.QComboBox()
  43. self.exc_object_combo.setModel(self.app.collection)
  44. self.exc_object_combo.setRootModelIndex(self.app.collection.index(1, 0, QtCore.QModelIndex()))
  45. self.exc_object_combo.setCurrentIndex(1)
  46. self.excobj_label = QtWidgets.QLabel("<b>%s:</b>" % _("EXCELLON"))
  47. self.excobj_label.setToolTip(
  48. _("Excellon Object to be mirrored.")
  49. )
  50. grid_lay.addWidget(self.excobj_label, 0, 0)
  51. grid_lay.addWidget(self.exc_object_combo, 0, 1, 1, 2)
  52. grid_lay.addWidget(QtWidgets.QLabel(''), 1, 0)
  53. self.points_table_label = QtWidgets.QLabel('<b>%s</b>' % _('Calibration Points'))
  54. self.points_table_label.setToolTip(
  55. _("Contain the expected calibration points and the\n"
  56. "ones measured.")
  57. )
  58. grid_lay.addWidget(self.points_table_label, 2, 0, 1, 2)
  59. # BOTTOM LEFT
  60. self.bottom_left_lbl = QtWidgets.QLabel('<b>%s</b>' % _('Bottom Left'))
  61. grid_lay.addWidget(self.bottom_left_lbl, 3, 0)
  62. self.bottom_left_tgt_lbl = QtWidgets.QLabel('%s' % _('Target'))
  63. grid_lay.addWidget(self.bottom_left_tgt_lbl, 3, 1)
  64. self.bottom_left_found_lbl = QtWidgets.QLabel('%s' % _('Cal. Origin'))
  65. grid_lay.addWidget(self.bottom_left_found_lbl, 3, 2)
  66. self.bottom_left_coordx_lbl = QtWidgets.QLabel('%s' % _('X'))
  67. grid_lay.addWidget(self.bottom_left_coordx_lbl, 4, 0)
  68. self.bottom_left_coordx_tgt = EvalEntry()
  69. self.bottom_left_coordx_tgt.setDisabled(True)
  70. grid_lay.addWidget(self.bottom_left_coordx_tgt, 4, 1)
  71. self.bottom_left_coordx_found = EvalEntry()
  72. grid_lay.addWidget(self.bottom_left_coordx_found, 4, 2)
  73. self.bottom_left_coordy_lbl = QtWidgets.QLabel('%s' % _('Y'))
  74. grid_lay.addWidget(self.bottom_left_coordy_lbl, 5, 0)
  75. self.bottom_left_coordy_tgt = EvalEntry()
  76. self.bottom_left_coordy_tgt.setDisabled(True)
  77. grid_lay.addWidget(self.bottom_left_coordy_tgt, 5, 1)
  78. self.bottom_left_coordy_found = EvalEntry()
  79. grid_lay.addWidget(self.bottom_left_coordy_found, 5, 2)
  80. self.bottom_left_coordx_found.set_value(_('Set Origin'))
  81. self.bottom_left_coordy_found.set_value(_('Set Origin'))
  82. self.bottom_left_coordx_found.setDisabled(True)
  83. self.bottom_left_coordy_found.setDisabled(True)
  84. # BOTTOM RIGHT
  85. self.bottom_right_lbl = QtWidgets.QLabel('<b>%s</b>' % _('Bottom Right'))
  86. grid_lay.addWidget(self.bottom_right_lbl, 6, 0)
  87. self.bottom_right_tgt_lbl = QtWidgets.QLabel('%s' % _('Target'))
  88. grid_lay.addWidget(self.bottom_right_tgt_lbl, 6, 1)
  89. self.bottom_right_found_lbl = QtWidgets.QLabel('%s' % _('Found Delta'))
  90. grid_lay.addWidget(self.bottom_right_found_lbl, 6, 2)
  91. self.bottom_right_coordx_lbl = QtWidgets.QLabel('%s' % _('X'))
  92. grid_lay.addWidget(self.bottom_right_coordx_lbl, 7, 0)
  93. self.bottom_right_coordx_tgt = EvalEntry()
  94. self.bottom_right_coordx_tgt.setDisabled(True)
  95. grid_lay.addWidget(self.bottom_right_coordx_tgt, 7, 1)
  96. self.bottom_right_coordx_found = EvalEntry()
  97. grid_lay.addWidget(self.bottom_right_coordx_found, 7, 2)
  98. self.bottom_right_coordy_lbl = QtWidgets.QLabel('%s' % _('Y'))
  99. grid_lay.addWidget(self.bottom_right_coordy_lbl, 8, 0)
  100. self.bottom_right_coordy_tgt = EvalEntry()
  101. self.bottom_right_coordy_tgt.setDisabled(True)
  102. grid_lay.addWidget(self.bottom_right_coordy_tgt, 8, 1)
  103. self.bottom_right_coordy_found = EvalEntry()
  104. grid_lay.addWidget(self.bottom_right_coordy_found, 8, 2)
  105. # TOP LEFT
  106. self.top_left_lbl = QtWidgets.QLabel('<b>%s</b>' % _('Top Left'))
  107. grid_lay.addWidget(self.top_left_lbl, 9, 0)
  108. self.top_left_tgt_lbl = QtWidgets.QLabel('%s' % _('Target'))
  109. grid_lay.addWidget(self.top_left_tgt_lbl, 9, 1)
  110. self.top_left_found_lbl = QtWidgets.QLabel('%s' % _('Found Delta'))
  111. grid_lay.addWidget(self.top_left_found_lbl, 9, 2)
  112. self.top_left_coordx_lbl = QtWidgets.QLabel('%s' % _('X'))
  113. grid_lay.addWidget(self.top_left_coordx_lbl, 10, 0)
  114. self.top_left_coordx_tgt = EvalEntry()
  115. self.top_left_coordx_tgt.setDisabled(True)
  116. grid_lay.addWidget(self.top_left_coordx_tgt, 10, 1)
  117. self.top_left_coordx_found = EvalEntry()
  118. grid_lay.addWidget(self.top_left_coordx_found, 10, 2)
  119. self.top_left_coordy_lbl = QtWidgets.QLabel('%s' % _('Y'))
  120. grid_lay.addWidget(self.top_left_coordy_lbl, 11, 0)
  121. self.top_left_coordy_tgt = EvalEntry()
  122. self.top_left_coordy_tgt.setDisabled(True)
  123. grid_lay.addWidget(self.top_left_coordy_tgt, 11, 1)
  124. self.top_left_coordy_found = EvalEntry()
  125. grid_lay.addWidget(self.top_left_coordy_found, 11, 2)
  126. # TOP RIGHT
  127. self.top_right_lbl = QtWidgets.QLabel('<b>%s</b>' % _('Top Right'))
  128. grid_lay.addWidget(self.top_right_lbl, 12, 0)
  129. self.top_right_tgt_lbl = QtWidgets.QLabel('%s' % _('Target'))
  130. grid_lay.addWidget(self.top_right_tgt_lbl, 12, 1)
  131. self.top_right_found_lbl = QtWidgets.QLabel('%s' % _('Found Delta'))
  132. grid_lay.addWidget(self.top_right_found_lbl, 12, 2)
  133. self.top_right_coordx_lbl = QtWidgets.QLabel('%s' % _('X'))
  134. grid_lay.addWidget(self.top_right_coordx_lbl, 13, 0)
  135. self.top_right_coordx_tgt = EvalEntry()
  136. self.top_right_coordx_tgt.setDisabled(True)
  137. grid_lay.addWidget(self.top_right_coordx_tgt, 13, 1)
  138. self.top_right_coordx_found = EvalEntry()
  139. grid_lay.addWidget(self.top_right_coordx_found, 13, 2)
  140. self.top_right_coordy_lbl = QtWidgets.QLabel('%s' % _('Y'))
  141. grid_lay.addWidget(self.top_right_coordy_lbl, 14, 0)
  142. self.top_right_coordy_tgt = EvalEntry()
  143. self.top_right_coordy_tgt.setDisabled(True)
  144. grid_lay.addWidget(self.top_right_coordy_tgt, 14, 1)
  145. self.top_right_coordy_found = EvalEntry()
  146. grid_lay.addWidget(self.top_right_coordy_found, 14, 2)
  147. step_1 = QtWidgets.QLabel('<b>%s</b>' % _("STEP 1"))
  148. step_1.setToolTip(
  149. _("Pick four points by clicking inside the drill holes.\n"
  150. "Those four points should be in the four squares of\n"
  151. "the Excellon object.")
  152. )
  153. grid_lay.addWidget(step_1, 15, 0, 1, 3)
  154. # ## Start Button
  155. self.start_button = QtWidgets.QPushButton(_("Acquire Calibration Points"))
  156. self.start_button.setToolTip(
  157. _("Pick four points by clicking inside the drill holes.\n"
  158. "Those four points should be in the four squares of\n"
  159. "the Excellon object.")
  160. )
  161. grid_lay.addWidget(self.start_button, 16, 0, 1, 3)
  162. step_2 = QtWidgets.QLabel('<b>%s</b>' % _("STEP 2"))
  163. step_2.setToolTip(
  164. _("Generate GCode file to locate and align the PCB by using\n"
  165. "the four points acquired above.")
  166. )
  167. grid_lay.addWidget(step_2, 17, 0, 1, 3)
  168. # ## GCode Button
  169. self.gcode_button = QtWidgets.QPushButton(_("Generate GCode"))
  170. self.gcode_button.setToolTip(
  171. _("Generate GCode file to locate and align the PCB by using\n"
  172. "the four points acquired above.")
  173. )
  174. grid_lay.addWidget(self.gcode_button, 18, 0, 1, 3)
  175. step_3 = QtWidgets.QLabel('<b>%s</b>' % _("STEP 3"))
  176. step_3.setToolTip(
  177. _("Calculate Scale and Skew factors based on the differences (delta)\n"
  178. "found when checking the PCB pattern. The differences must be filled\n"
  179. "in the fields Found (Delta).")
  180. )
  181. grid_lay.addWidget(step_3, 19, 0, 1, 3)
  182. # ## Factors Button
  183. self.generate_factors_button = QtWidgets.QPushButton(_("Calculate Factors"))
  184. self.generate_factors_button.setToolTip(
  185. _("Calculate Scale and Skew factors based on the differences (delta)\n"
  186. "found when checking the PCB pattern. The differences must be filled\n"
  187. "in the fields Found (Delta).")
  188. )
  189. grid_lay.addWidget(self.generate_factors_button, 20, 0, 1, 3)
  190. scale_lbl = QtWidgets.QLabel('<b>%s</b>' % _("Scale"))
  191. grid_lay.addWidget(scale_lbl, 21, 0, 1, 3)
  192. self.scalex_label = QtWidgets.QLabel(_("Factor X:"))
  193. self.scalex_label.setToolTip(
  194. _("Factor for Scale action over X axis.")
  195. )
  196. self.scalex_entry = EvalEntry()
  197. grid_lay.addWidget(self.scalex_label, 22, 0)
  198. grid_lay.addWidget(self.scalex_entry, 22, 1, 1, 2)
  199. self.scaley_label = QtWidgets.QLabel(_("Factor Y:"))
  200. self.scaley_label.setToolTip(
  201. _("Factor for Scale action over Y axis.")
  202. )
  203. self.scaley_entry = EvalEntry()
  204. grid_lay.addWidget(self.scaley_label, 23, 0)
  205. grid_lay.addWidget(self.scaley_entry, 23, 1, 1, 2)
  206. skew_lbl = QtWidgets.QLabel('<b>%s</b>' % _("Skew"))
  207. grid_lay.addWidget(skew_lbl, 24, 0, 1, 3)
  208. self.skewx_label = QtWidgets.QLabel(_("Angle X:"))
  209. self.skewx_label.setToolTip(
  210. _("Angle for Skew action, in degrees.\n"
  211. "Float number between -360 and 359.")
  212. )
  213. self.skewx_entry = EvalEntry()
  214. grid_lay.addWidget(self.skewx_label, 25, 0)
  215. grid_lay.addWidget(self.skewx_entry, 25, 1, 1, 2)
  216. self.skewy_label = QtWidgets.QLabel(_("Angle Y:"))
  217. self.skewy_label.setToolTip(
  218. _("Angle for Skew action, in degrees.\n"
  219. "Float number between -360 and 359.")
  220. )
  221. self.skewy_entry = EvalEntry()
  222. grid_lay.addWidget(self.skewy_label, 26, 0)
  223. grid_lay.addWidget(self.skewy_entry, 26, 1, 1, 2)
  224. step_4 = QtWidgets.QLabel('<b>%s</b>' % _("STEP 4"))
  225. step_4.setToolTip(
  226. _("Generate verification GCode file adjusted with\n"
  227. "the factors above.")
  228. )
  229. grid_lay.addWidget(step_4, 27, 0, 1, 3)
  230. # ## GCode Button
  231. self.gcode_button = QtWidgets.QPushButton(_("Generate Adjusted GCode"))
  232. self.gcode_button.setToolTip(
  233. _("Generate verification GCode file adjusted with\n"
  234. "the factors above.")
  235. )
  236. grid_lay.addWidget(self.gcode_button, 28, 0, 1, 3)
  237. self.layout.addStretch()
  238. self.mr = None
  239. self.units = ''
  240. # here store 4 points to be used for calibration
  241. self.click_points = list()
  242. self.exc_obj = None
  243. # ## Signals
  244. self.start_button.clicked.connect(self.on_start_collect_points)
  245. def run(self, toggle=True):
  246. self.app.report_usage("ToolCalibrateExcellon()")
  247. if toggle:
  248. # if the splitter is hidden, display it, else hide it but only if the current widget is the same
  249. if self.app.ui.splitter.sizes()[0] == 0:
  250. self.app.ui.splitter.setSizes([1, 1])
  251. else:
  252. try:
  253. if self.app.ui.tool_scroll_area.widget().objectName() == self.toolName:
  254. # if tab is populated with the tool but it does not have the focus, focus on it
  255. if not self.app.ui.notebook.currentWidget() is self.app.ui.tool_tab:
  256. # focus on Tool Tab
  257. self.app.ui.notebook.setCurrentWidget(self.app.ui.tool_tab)
  258. else:
  259. self.app.ui.splitter.setSizes([0, 1])
  260. except AttributeError:
  261. pass
  262. else:
  263. if self.app.ui.splitter.sizes()[0] == 0:
  264. self.app.ui.splitter.setSizes([1, 1])
  265. FlatCAMTool.run(self)
  266. self.set_tool_ui()
  267. self.app.ui.notebook.setTabText(2, _("Cal Exc Tool"))
  268. def install(self, icon=None, separator=None, **kwargs):
  269. FlatCAMTool.install(self, icon, separator, shortcut='ALT+E', **kwargs)
  270. def set_tool_ui(self):
  271. self.units = self.app.ui.general_defaults_form.general_app_group.units_radio.get_value().upper()
  272. # ## Initialize form
  273. # self.mm_entry.set_value('%.*f' % (self.decimals, 0))
  274. def on_start_collect_points(self):
  275. self.mr = self.canvas.graph_event_connect('mouse_release', self.on_mouse_click_release)
  276. if self.app.is_legacy is False:
  277. self.canvas.graph_event_disconnect('mouse_release', self.app.on_mouse_click_release_over_plot)
  278. else:
  279. self.canvas.graph_event_disconnect(self.app.mr)
  280. selection_index = self.exc_object_combo.currentIndex()
  281. model_index = self.app.collection.index(selection_index, 0, self.exc_object_combo.rootModelIndex())
  282. try:
  283. self.exc_obj = model_index.internalPointer().obj
  284. except Exception as e:
  285. self.app.inform.emit('[WARNING_NOTCL] %s' % _("There is no Excellon object loaded ..."))
  286. return
  287. self.app.inform.emit(_("Click inside the First drill point. Bottom Left..."))
  288. def on_mouse_click_release(self, event):
  289. if event.button == 1:
  290. if self.app.is_legacy is False:
  291. event_pos = event.pos
  292. else:
  293. event_pos = (event.xdata, event.ydata)
  294. pos_canvas = self.canvas.translate_coords(event_pos)
  295. click_pt = Point([pos_canvas[0], pos_canvas[1]])
  296. for tool, tool_dict in self.exc_obj.tools.items():
  297. for geo in tool_dict['solid_geometry']:
  298. if click_pt.within(geo):
  299. center_pt = geo.centroid
  300. self.click_points.append(
  301. (
  302. float('%.*f' % (self.decimals, center_pt.x)),
  303. float('%.*f' % (self.decimals, center_pt.y))
  304. )
  305. )
  306. self.check_points()
  307. def check_points(self):
  308. if len(self.click_points) == 1:
  309. self.bottom_left_coordx_tgt.set_value(self.click_points[0][0])
  310. self.bottom_left_coordy_tgt.set_value(self.click_points[0][1])
  311. self.app.inform.emit(_("Click inside the Second drill point. Bottom Right..."))
  312. elif len(self.click_points) == 2:
  313. self.bottom_right_coordx_tgt.set_value(self.click_points[1][0])
  314. self.bottom_right_coordy_tgt.set_value(self.click_points[1][1])
  315. self.app.inform.emit(_("Click inside the Third drill point. Top Left..."))
  316. elif len(self.click_points) == 3:
  317. self.top_left_coordx_tgt.set_value(self.click_points[2][0])
  318. self.top_left_coordy_tgt.set_value(self.click_points[2][1])
  319. self.app.inform.emit(_("Click inside the Fourth drill point. Top Right..."))
  320. elif len(self.click_points) == 4:
  321. self.top_right_coordx_tgt.set_value(self.click_points[3][0])
  322. self.top_right_coordy_tgt.set_value(self.click_points[3][1])
  323. self.app.inform.emit('[success] %s' % _("Done. All four points have been acquired."))
  324. self.disconnect_cal_events()
  325. def disconnect_cal_events(self):
  326. self.app.mr = self.canvas.graph_event_connect('mouse_release', self.app.on_mouse_click_release_over_plot)
  327. if self.app.is_legacy is False:
  328. self.canvas.graph_event_disconnect('mouse_release', self.on_mouse_click_release)
  329. else:
  330. self.canvas.graph_event_disconnect(self.mr)
  331. def reset_fields(self):
  332. self.exc_object_combo.setRootModelIndex(self.app.collection.index(1, 0, QtCore.QModelIndex()))
  333. # end of file