ToolCalibrateExcellon.py 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  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, FCCheckBox, OptionalInputSection, FCTable
  10. from shapely.geometry import Point
  11. from shapely.geometry.base import *
  12. import math
  13. from datetime import datetime
  14. import logging
  15. import gettext
  16. import FlatCAMTranslation as fcTranslate
  17. import builtins
  18. fcTranslate.apply_language('strings')
  19. if '_' not in builtins.__dict__:
  20. _ = gettext.gettext
  21. log = logging.getLogger('base')
  22. class ToolCalibrateExcellon(FlatCAMTool):
  23. toolName = _("Calibrate Excellon")
  24. def __init__(self, app):
  25. FlatCAMTool.__init__(self, app)
  26. self.app = app
  27. self.canvas = self.app.plotcanvas
  28. self.decimals = 4
  29. # ## Title
  30. title_label = QtWidgets.QLabel("%s" % self.toolName)
  31. title_label.setStyleSheet("""
  32. QLabel
  33. {
  34. font-size: 16px;
  35. font-weight: bold;
  36. }
  37. """)
  38. self.layout.addWidget(title_label)
  39. # ## Grid Layout
  40. i_grid_lay = QtWidgets.QGridLayout()
  41. self.layout.addLayout(i_grid_lay)
  42. i_grid_lay.setColumnStretch(0, 0)
  43. i_grid_lay.setColumnStretch(1, 1)
  44. i_grid_lay.setColumnStretch(2, 1)
  45. self.exc_object_combo = QtWidgets.QComboBox()
  46. self.exc_object_combo.setModel(self.app.collection)
  47. self.exc_object_combo.setRootModelIndex(self.app.collection.index(1, 0, QtCore.QModelIndex()))
  48. self.exc_object_combo.setCurrentIndex(1)
  49. self.excobj_label = QtWidgets.QLabel("<b>%s:</b>" % _("EXCELLON"))
  50. self.excobj_label.setToolTip(
  51. _("Excellon Object to be used as a source for reference points.")
  52. )
  53. i_grid_lay.addWidget(self.excobj_label, 0, 0)
  54. i_grid_lay.addWidget(self.exc_object_combo, 0, 1, 1, 2)
  55. i_grid_lay.addWidget(QtWidgets.QLabel(''), 1, 0)
  56. self.gcode_title_label = QtWidgets.QLabel('<b>%s</b>' % _('GCode Parameters'))
  57. self.gcode_title_label.setToolTip(
  58. _("Parameters used when creating the GCode in this tool.")
  59. )
  60. i_grid_lay.addWidget(self.gcode_title_label, 1, 0, 1, 3)
  61. # Travel Z entry
  62. travelz_lbl = QtWidgets.QLabel('%s:' % _("Travel Z"))
  63. travelz_lbl.setToolTip(
  64. _("Height (Z) for travelling between the points.")
  65. )
  66. self.travelz_entry = FCDoubleSpinner()
  67. self.travelz_entry.set_range(-9999.9999, 9999.9999)
  68. self.travelz_entry.set_precision(self.decimals)
  69. self.travelz_entry.setSingleStep(0.1)
  70. i_grid_lay.addWidget(travelz_lbl, 2, 0)
  71. i_grid_lay.addWidget(self.travelz_entry, 2, 1, 1, 2)
  72. # Verification Z entry
  73. verz_lbl = QtWidgets.QLabel('%s:' % _("Verification Z"))
  74. verz_lbl.setToolTip(
  75. _("Height (Z) for checking the point.")
  76. )
  77. self.verz_entry = FCDoubleSpinner()
  78. self.verz_entry.set_range(-9999.9999, 9999.9999)
  79. self.verz_entry.set_precision(self.decimals)
  80. self.verz_entry.setSingleStep(0.1)
  81. i_grid_lay.addWidget(verz_lbl, 3, 0)
  82. i_grid_lay.addWidget(self.verz_entry, 3, 1, 1, 2)
  83. # Zero the Z of the verification tool
  84. self.zeroz_cb = FCCheckBox('%s' % _("Zero Z tool"))
  85. self.zeroz_cb.setToolTip(
  86. _("Include a sequence to zero the height (Z)\n"
  87. "of the verification tool.")
  88. )
  89. i_grid_lay.addWidget(self.zeroz_cb, 4, 0, 1, 3)
  90. # Toochange Z entry
  91. toolchangez_lbl = QtWidgets.QLabel('%s:' % _("Toolchange Z"))
  92. toolchangez_lbl.setToolTip(
  93. _("Height (Z) for mounting the verification probe.")
  94. )
  95. self.toolchangez_entry = FCDoubleSpinner()
  96. self.toolchangez_entry.set_range(0.0000, 9999.9999)
  97. self.toolchangez_entry.set_precision(self.decimals)
  98. self.toolchangez_entry.setSingleStep(0.1)
  99. i_grid_lay.addWidget(toolchangez_lbl, 5, 0)
  100. i_grid_lay.addWidget(self.toolchangez_entry, 5, 1, 1, 2)
  101. self.z_ois = OptionalInputSection(self.zeroz_cb, [toolchangez_lbl, self.toolchangez_entry])
  102. i_grid_lay.addWidget(QtWidgets.QLabel(''), 6, 0, 1, 3)
  103. # ## Grid Layout
  104. grid_lay = QtWidgets.QGridLayout()
  105. self.layout.addLayout(grid_lay)
  106. grid_lay.setColumnStretch(0, 0)
  107. grid_lay.setColumnStretch(1, 1)
  108. grid_lay.setColumnStretch(2, 1)
  109. self.points_table_label = QtWidgets.QLabel('<b>%s</b>' % _('Calibration Points'))
  110. self.points_table_label.setToolTip(
  111. _("Contain the expected calibration points and the\n"
  112. "ones measured.")
  113. )
  114. grid_lay.addWidget(self.points_table_label, 1, 0, 1, 3)
  115. self.points_table = FCTable()
  116. self.points_table.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectRows)
  117. # self.points_table.setSizeAdjustPolicy(QtWidgets.QAbstractScrollArea.AdjustToContents)
  118. grid_lay.addWidget(self.points_table, 2, 0, 9, 3)
  119. self.points_table.setColumnCount(4)
  120. self.points_table.setHorizontalHeaderLabels(
  121. [
  122. '#',
  123. _("Name"),
  124. _("Target"),
  125. _("Found Delta")
  126. ]
  127. )
  128. self.points_table.setRowCount(8)
  129. row = 0
  130. # BOTTOM LEFT
  131. id_item_1 = QtWidgets.QTableWidgetItem('%d' % 1)
  132. flags = QtCore.Qt.ItemIsEnabled
  133. id_item_1.setFlags(flags)
  134. self.points_table.setItem(row, 0, id_item_1) # Tool name/id
  135. self.bottom_left_coordx_lbl = QtWidgets.QLabel('%s' % _('Bot Left X'))
  136. self.points_table.setCellWidget(row, 1, self.bottom_left_coordx_lbl)
  137. self.bottom_left_coordx_tgt = EvalEntry()
  138. self.points_table.setCellWidget(row, 2, self.bottom_left_coordx_tgt)
  139. self.bottom_left_coordx_tgt.setReadOnly(True)
  140. self.bottom_left_coordx_found = EvalEntry()
  141. self.points_table.setCellWidget(row, 3, self.bottom_left_coordx_found)
  142. row += 1
  143. self.bottom_left_coordy_lbl = QtWidgets.QLabel('%s' % _('Bot Left Y'))
  144. self.points_table.setCellWidget(row, 1, self.bottom_left_coordy_lbl)
  145. self.bottom_left_coordy_tgt = EvalEntry()
  146. self.points_table.setCellWidget(row, 2, self.bottom_left_coordy_tgt)
  147. self.bottom_left_coordy_tgt.setReadOnly(True)
  148. self.bottom_left_coordy_found = EvalEntry()
  149. self.points_table.setCellWidget(row, 3, self.bottom_left_coordy_found)
  150. self.bottom_left_coordx_found.set_value(_("Origin"))
  151. self.bottom_left_coordy_found.set_value(_("Origin"))
  152. self.bottom_left_coordx_found.setDisabled(True)
  153. self.bottom_left_coordy_found.setDisabled(True)
  154. row += 1
  155. # BOTTOM RIGHT
  156. id_item_2 = QtWidgets.QTableWidgetItem('%d' % 2)
  157. flags = QtCore.Qt.ItemIsEnabled
  158. id_item_2.setFlags(flags)
  159. self.points_table.setItem(row, 0, id_item_2) # Tool name/id
  160. self.bottom_right_coordx_lbl = QtWidgets.QLabel('%s' % _('Bot Right X'))
  161. self.points_table.setCellWidget(row, 1, self.bottom_right_coordx_lbl)
  162. self.bottom_right_coordx_tgt = EvalEntry()
  163. self.points_table.setCellWidget(row, 2, self.bottom_right_coordx_tgt)
  164. self.bottom_right_coordx_tgt.setReadOnly(True)
  165. self.bottom_right_coordx_found = EvalEntry()
  166. self.points_table.setCellWidget(row, 3, self.bottom_right_coordx_found)
  167. row += 1
  168. self.bottom_right_coordy_lbl = QtWidgets.QLabel('%s' % _('Bot Right Y'))
  169. self.points_table.setCellWidget(row, 1, self.bottom_right_coordy_lbl)
  170. self.bottom_right_coordy_tgt = EvalEntry()
  171. self.points_table.setCellWidget(row, 2, self.bottom_right_coordy_tgt)
  172. self.bottom_right_coordy_tgt.setReadOnly(True)
  173. self.bottom_right_coordy_found = EvalEntry()
  174. self.points_table.setCellWidget(row, 3, self.bottom_right_coordy_found)
  175. row += 1
  176. # TOP LEFT
  177. id_item_3 = QtWidgets.QTableWidgetItem('%d' % 3)
  178. flags = QtCore.Qt.ItemIsEnabled
  179. id_item_3.setFlags(flags)
  180. self.points_table.setItem(row, 0, id_item_3) # Tool name/id
  181. self.top_left_coordx_lbl = QtWidgets.QLabel('%s' % _('Top Left X'))
  182. self.points_table.setCellWidget(row, 1, self.top_left_coordx_lbl)
  183. self.top_left_coordx_tgt = EvalEntry()
  184. self.points_table.setCellWidget(row, 2, self.top_left_coordx_tgt)
  185. self.top_left_coordx_tgt.setReadOnly(True)
  186. self.top_left_coordx_found = EvalEntry()
  187. self.points_table.setCellWidget(row, 3, self.top_left_coordx_found)
  188. row += 1
  189. self.top_left_coordy_lbl = QtWidgets.QLabel('%s' % _('Top Left Y'))
  190. self.points_table.setCellWidget(row, 1, self.top_left_coordy_lbl)
  191. self.top_left_coordy_tgt = EvalEntry()
  192. self.points_table.setCellWidget(row, 2, self.top_left_coordy_tgt)
  193. self.top_left_coordy_tgt.setReadOnly(True)
  194. self.top_left_coordy_found = EvalEntry()
  195. self.points_table.setCellWidget(row, 3, self.top_left_coordy_found)
  196. row += 1
  197. # TOP RIGHT
  198. id_item_4 = QtWidgets.QTableWidgetItem('%d' % 4)
  199. flags = QtCore.Qt.ItemIsEnabled
  200. id_item_4.setFlags(flags)
  201. self.points_table.setItem(row, 0, id_item_4) # Tool name/id
  202. self.top_right_coordx_lbl = QtWidgets.QLabel('%s' % _('Top Right X'))
  203. self.points_table.setCellWidget(row, 1, self.top_right_coordx_lbl)
  204. self.top_right_coordx_tgt = EvalEntry()
  205. self.points_table.setCellWidget(row, 2, self.top_right_coordx_tgt)
  206. self.top_right_coordx_tgt.setReadOnly(True)
  207. self.top_right_coordx_found = EvalEntry()
  208. self.points_table.setCellWidget(row, 3, self.top_right_coordx_found)
  209. row += 1
  210. self.top_right_coordy_lbl = QtWidgets.QLabel('%s' % _('Top Right Y'))
  211. self.points_table.setCellWidget(row, 1, self.top_right_coordy_lbl)
  212. self.top_right_coordy_tgt = EvalEntry()
  213. self.points_table.setCellWidget(row, 2, self.top_right_coordy_tgt)
  214. self.top_right_coordy_tgt.setReadOnly(True)
  215. self.top_right_coordy_found = EvalEntry()
  216. self.points_table.setCellWidget(row, 3, self.top_right_coordy_found)
  217. vertical_header = self.points_table.verticalHeader()
  218. vertical_header.hide()
  219. self.points_table.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
  220. horizontal_header = self.points_table.horizontalHeader()
  221. horizontal_header.setMinimumSectionSize(10)
  222. horizontal_header.setDefaultSectionSize(70)
  223. self.points_table.setSizeAdjustPolicy(QtWidgets.QAbstractScrollArea.AdjustToContents)
  224. # for x in range(4):
  225. # self.points_table.resizeColumnToContents(x)
  226. self.points_table.resizeColumnsToContents()
  227. self.points_table.resizeRowsToContents()
  228. horizontal_header.setSectionResizeMode(0, QtWidgets.QHeaderView.Fixed)
  229. horizontal_header.resizeSection(0, 20)
  230. horizontal_header.setSectionResizeMode(1, QtWidgets.QHeaderView.Fixed)
  231. horizontal_header.setSectionResizeMode(2, QtWidgets.QHeaderView.Stretch)
  232. horizontal_header.setSectionResizeMode(3, QtWidgets.QHeaderView.Stretch)
  233. self.points_table.setMinimumHeight(self.points_table.getHeight() + 2)
  234. self.points_table.setMaximumHeight(self.points_table.getHeight() + 3)
  235. # # BOTTOM LEFT
  236. # self.bottom_left_lbl = QtWidgets.QLabel('<b>%s</b>' % _('Bottom Left'))
  237. # grid_lay.addWidget(self.bottom_left_lbl, 3, 0)
  238. # self.bottom_left_tgt_lbl = QtWidgets.QLabel('%s' % _('Target'))
  239. # grid_lay.addWidget(self.bottom_left_tgt_lbl, 3, 1)
  240. # self.bottom_left_found_lbl = QtWidgets.QLabel('%s' % _('Cal. Origin'))
  241. # grid_lay.addWidget(self.bottom_left_found_lbl, 3, 2)
  242. #
  243. # self.bottom_left_coordx_lbl = QtWidgets.QLabel('%s' % _('X'))
  244. # grid_lay.addWidget(self.bottom_left_coordx_lbl, 4, 0)
  245. # self.bottom_left_coordx_tgt = EvalEntry()
  246. # self.bottom_left_coordx_tgt.setReadOnly(True)
  247. # grid_lay.addWidget(self.bottom_left_coordx_tgt, 4, 1)
  248. # self.bottom_left_coordx_found = EvalEntry()
  249. # grid_lay.addWidget(self.bottom_left_coordx_found, 4, 2)
  250. #
  251. # self.bottom_left_coordy_lbl = QtWidgets.QLabel('%s' % _('Y'))
  252. # grid_lay.addWidget(self.bottom_left_coordy_lbl, 5, 0)
  253. # self.bottom_left_coordy_tgt = EvalEntry()
  254. # self.bottom_left_coordy_tgt.setReadOnly(True)
  255. # grid_lay.addWidget(self.bottom_left_coordy_tgt, 5, 1)
  256. # self.bottom_left_coordy_found = EvalEntry()
  257. # grid_lay.addWidget(self.bottom_left_coordy_found, 5, 2)
  258. #
  259. # self.bottom_left_coordx_found.set_value(_('Set Origin'))
  260. # self.bottom_left_coordy_found.set_value(_('Set Origin'))
  261. # self.bottom_left_coordx_found.setDisabled(True)
  262. # self.bottom_left_coordy_found.setDisabled(True)
  263. #
  264. # # BOTTOM RIGHT
  265. # self.bottom_right_lbl = QtWidgets.QLabel('<b>%s</b>' % _('Bottom Right'))
  266. # grid_lay.addWidget(self.bottom_right_lbl, 6, 0)
  267. # self.bottom_right_tgt_lbl = QtWidgets.QLabel('%s' % _('Target'))
  268. # grid_lay.addWidget(self.bottom_right_tgt_lbl, 6, 1)
  269. # self.bottom_right_found_lbl = QtWidgets.QLabel('%s' % _('Found Delta'))
  270. # grid_lay.addWidget(self.bottom_right_found_lbl, 6, 2)
  271. #
  272. # self.bottom_right_coordx_lbl = QtWidgets.QLabel('%s' % _('X'))
  273. # grid_lay.addWidget(self.bottom_right_coordx_lbl, 7, 0)
  274. # self.bottom_right_coordx_tgt = EvalEntry()
  275. # self.bottom_right_coordx_tgt.setReadOnly(True)
  276. # grid_lay.addWidget(self.bottom_right_coordx_tgt, 7, 1)
  277. # self.bottom_right_coordx_found = EvalEntry()
  278. # grid_lay.addWidget(self.bottom_right_coordx_found, 7, 2)
  279. #
  280. # self.bottom_right_coordy_lbl = QtWidgets.QLabel('%s' % _('Y'))
  281. # grid_lay.addWidget(self.bottom_right_coordy_lbl, 8, 0)
  282. # self.bottom_right_coordy_tgt = EvalEntry()
  283. # self.bottom_right_coordy_tgt.setReadOnly(True)
  284. # grid_lay.addWidget(self.bottom_right_coordy_tgt, 8, 1)
  285. # self.bottom_right_coordy_found = EvalEntry()
  286. # grid_lay.addWidget(self.bottom_right_coordy_found, 8, 2)
  287. #
  288. # # TOP LEFT
  289. # self.top_left_lbl = QtWidgets.QLabel('<b>%s</b>' % _('Top Left'))
  290. # grid_lay.addWidget(self.top_left_lbl, 9, 0)
  291. # self.top_left_tgt_lbl = QtWidgets.QLabel('%s' % _('Target'))
  292. # grid_lay.addWidget(self.top_left_tgt_lbl, 9, 1)
  293. # self.top_left_found_lbl = QtWidgets.QLabel('%s' % _('Found Delta'))
  294. # grid_lay.addWidget(self.top_left_found_lbl, 9, 2)
  295. #
  296. # self.top_left_coordx_lbl = QtWidgets.QLabel('%s' % _('X'))
  297. # grid_lay.addWidget(self.top_left_coordx_lbl, 10, 0)
  298. # self.top_left_coordx_tgt = EvalEntry()
  299. # self.top_left_coordx_tgt.setReadOnly(True)
  300. # grid_lay.addWidget(self.top_left_coordx_tgt, 10, 1)
  301. # self.top_left_coordx_found = EvalEntry()
  302. # grid_lay.addWidget(self.top_left_coordx_found, 10, 2)
  303. #
  304. # self.top_left_coordy_lbl = QtWidgets.QLabel('%s' % _('Y'))
  305. # grid_lay.addWidget(self.top_left_coordy_lbl, 11, 0)
  306. # self.top_left_coordy_tgt = EvalEntry()
  307. # self.top_left_coordy_tgt.setReadOnly(True)
  308. # grid_lay.addWidget(self.top_left_coordy_tgt, 11, 1)
  309. # self.top_left_coordy_found = EvalEntry()
  310. # grid_lay.addWidget(self.top_left_coordy_found, 11, 2)
  311. #
  312. # # TOP RIGHT
  313. # self.top_right_lbl = QtWidgets.QLabel('<b>%s</b>' % _('Top Right'))
  314. # grid_lay.addWidget(self.top_right_lbl, 12, 0)
  315. # self.top_right_tgt_lbl = QtWidgets.QLabel('%s' % _('Target'))
  316. # grid_lay.addWidget(self.top_right_tgt_lbl, 12, 1)
  317. # self.top_right_found_lbl = QtWidgets.QLabel('%s' % _('Found Delta'))
  318. # grid_lay.addWidget(self.top_right_found_lbl, 12, 2)
  319. #
  320. # self.top_right_coordx_lbl = QtWidgets.QLabel('%s' % _('X'))
  321. # grid_lay.addWidget(self.top_right_coordx_lbl, 13, 0)
  322. # self.top_right_coordx_tgt = EvalEntry()
  323. # self.top_right_coordx_tgt.setReadOnly(True)
  324. # grid_lay.addWidget(self.top_right_coordx_tgt, 13, 1)
  325. # self.top_right_coordx_found = EvalEntry()
  326. # grid_lay.addWidget(self.top_right_coordx_found, 13, 2)
  327. #
  328. # self.top_right_coordy_lbl = QtWidgets.QLabel('%s' % _('Y'))
  329. # grid_lay.addWidget(self.top_right_coordy_lbl, 14, 0)
  330. # self.top_right_coordy_tgt = EvalEntry()
  331. # self.top_right_coordy_tgt.setReadOnly(True)
  332. # grid_lay.addWidget(self.top_right_coordy_tgt, 14, 1)
  333. # self.top_right_coordy_found = EvalEntry()
  334. # grid_lay.addWidget(self.top_right_coordy_found, 14, 2)
  335. # STEP 1 #
  336. step_1 = QtWidgets.QLabel('<b>%s</b>' % _("STEP 1"))
  337. step_1.setToolTip(
  338. _("Pick four points by clicking inside the drill holes.\n"
  339. "Those four points should be in the four\n"
  340. "(as much as possible) corners of the Excellon object.")
  341. )
  342. grid_lay.addWidget(step_1, 15, 0, 1, 3)
  343. # ## Start Button
  344. self.start_button = QtWidgets.QPushButton(_("Acquire Calibration Points"))
  345. self.start_button.setToolTip(
  346. _("Pick four points by clicking inside the drill holes.\n"
  347. "Those four points should be in the four squares of\n"
  348. "the Excellon object.")
  349. )
  350. self.start_button.setStyleSheet("""
  351. QPushButton
  352. {
  353. font-weight: bold;
  354. }
  355. """)
  356. grid_lay.addWidget(self.start_button, 16, 0, 1, 3)
  357. # STEP 2 #
  358. step_2 = QtWidgets.QLabel('<b>%s</b>' % _("STEP 2"))
  359. step_2.setToolTip(
  360. _("Generate GCode file to locate and align the PCB by using\n"
  361. "the four points acquired above.")
  362. )
  363. grid_lay.addWidget(step_2, 17, 0, 1, 3)
  364. # ## GCode Button
  365. self.gcode_button = QtWidgets.QPushButton(_("Generate GCode"))
  366. self.gcode_button.setToolTip(
  367. _("Generate GCode file to locate and align the PCB by using\n"
  368. "the four points acquired above.")
  369. )
  370. self.gcode_button.setStyleSheet("""
  371. QPushButton
  372. {
  373. font-weight: bold;
  374. }
  375. """)
  376. grid_lay.addWidget(self.gcode_button, 18, 0, 1, 3)
  377. # STEP 3 #
  378. step_3 = QtWidgets.QLabel('<b>%s</b>' % _("STEP 3"))
  379. step_3.setToolTip(
  380. _("Calculate Scale and Skew factors based on the differences (delta)\n"
  381. "found when checking the PCB pattern. The differences must be filled\n"
  382. "in the fields Found (Delta).")
  383. )
  384. grid_lay.addWidget(step_3, 19, 0, 1, 3)
  385. # ## Factors Button
  386. self.generate_factors_button = QtWidgets.QPushButton(_("Calculate Factors"))
  387. self.generate_factors_button.setToolTip(
  388. _("Calculate Scale and Skew factors based on the differences (delta)\n"
  389. "found when checking the PCB pattern. The differences must be filled\n"
  390. "in the fields Found (Delta).")
  391. )
  392. self.generate_factors_button.setStyleSheet("""
  393. QPushButton
  394. {
  395. font-weight: bold;
  396. }
  397. """)
  398. grid_lay.addWidget(self.generate_factors_button, 20, 0, 1, 3)
  399. scale_lbl = QtWidgets.QLabel('<b>%s</b>' % _("Scale"))
  400. grid_lay.addWidget(scale_lbl, 21, 0, 1, 3)
  401. self.scalex_label = QtWidgets.QLabel(_("Factor X:"))
  402. self.scalex_label.setToolTip(
  403. _("Factor for Scale action over X axis.")
  404. )
  405. self.scalex_entry = FCDoubleSpinner()
  406. self.scalex_entry.set_range(0, 9999.9999)
  407. self.scalex_entry.set_precision(self.decimals)
  408. self.scalex_entry.setSingleStep(0.1)
  409. grid_lay.addWidget(self.scalex_label, 22, 0)
  410. grid_lay.addWidget(self.scalex_entry, 22, 1, 1, 2)
  411. self.scaley_label = QtWidgets.QLabel(_("Factor Y:"))
  412. self.scaley_label.setToolTip(
  413. _("Factor for Scale action over Y axis.")
  414. )
  415. self.scaley_entry = FCDoubleSpinner()
  416. self.scaley_entry.set_range(0, 9999.9999)
  417. self.scaley_entry.set_precision(self.decimals)
  418. self.scaley_entry.setSingleStep(0.1)
  419. grid_lay.addWidget(self.scaley_label, 23, 0)
  420. grid_lay.addWidget(self.scaley_entry, 23, 1, 1, 2)
  421. self.scale_button = QtWidgets.QPushButton(_("Scale"))
  422. self.scale_button.setToolTip(
  423. _("Apply Scale factors on the calibration points.")
  424. )
  425. self.scale_button.setStyleSheet("""
  426. QPushButton
  427. {
  428. font-weight: bold;
  429. }
  430. """)
  431. grid_lay.addWidget(self.scale_button, 24, 0, 1, 3)
  432. skew_lbl = QtWidgets.QLabel('<b>%s</b>' % _("Skew"))
  433. grid_lay.addWidget(skew_lbl, 25, 0, 1, 3)
  434. self.skewx_label = QtWidgets.QLabel(_("Angle X:"))
  435. self.skewx_label.setToolTip(
  436. _("Angle for Skew action, in degrees.\n"
  437. "Float number between -360 and 359.")
  438. )
  439. self.skewx_entry = FCDoubleSpinner()
  440. self.skewx_entry.set_range(-360, 360)
  441. self.skewx_entry.set_precision(self.decimals)
  442. self.skewx_entry.setSingleStep(0.1)
  443. grid_lay.addWidget(self.skewx_label, 26, 0)
  444. grid_lay.addWidget(self.skewx_entry, 26, 1, 1, 2)
  445. self.skewy_label = QtWidgets.QLabel(_("Angle Y:"))
  446. self.skewy_label.setToolTip(
  447. _("Angle for Skew action, in degrees.\n"
  448. "Float number between -360 and 359.")
  449. )
  450. self.skewy_entry = FCDoubleSpinner()
  451. self.skewy_entry.set_range(-360, 360)
  452. self.skewy_entry.set_precision(self.decimals)
  453. self.skewy_entry.setSingleStep(0.1)
  454. grid_lay.addWidget(self.skewy_label, 27, 0)
  455. grid_lay.addWidget(self.skewy_entry, 27, 1, 1, 2)
  456. self.skew_button = QtWidgets.QPushButton(_("Skew"))
  457. self.skew_button.setToolTip(
  458. _("Apply Skew factors on the calibration points.")
  459. )
  460. self.skew_button.setStyleSheet("""
  461. QPushButton
  462. {
  463. font-weight: bold;
  464. }
  465. """)
  466. grid_lay.addWidget(self.skew_button, 28, 0, 1, 3)
  467. # STEP 4 #
  468. step_4 = QtWidgets.QLabel('<b>%s</b>' % _("STEP 4"))
  469. step_4.setToolTip(
  470. _("Generate verification GCode file adjusted with\n"
  471. "the factors above.")
  472. )
  473. grid_lay.addWidget(step_4, 29, 0, 1, 3)
  474. # ## Adjusted GCode Button
  475. self.adj_gcode_button = QtWidgets.QPushButton(_("Generate Adjusted GCode"))
  476. self.adj_gcode_button.setToolTip(
  477. _("Generate verification GCode file adjusted with\n"
  478. "the factors above.")
  479. )
  480. self.adj_gcode_button.setStyleSheet("""
  481. QPushButton
  482. {
  483. font-weight: bold;
  484. }
  485. """)
  486. grid_lay.addWidget(self.adj_gcode_button, 30, 0, 1, 3)
  487. # STEP 5 #
  488. step_5 = QtWidgets.QLabel('<b>%s</b>' % _("STEP 5"))
  489. step_5.setToolTip(
  490. _("Ajust the Excellon and Cutout Geometry objects\n"
  491. "with the factors determined, and verified, above.")
  492. )
  493. grid_lay.addWidget(step_5, 31, 0, 1, 3)
  494. self.adj_exc_object_combo = QtWidgets.QComboBox()
  495. self.adj_exc_object_combo.setModel(self.app.collection)
  496. self.adj_exc_object_combo.setRootModelIndex(self.app.collection.index(1, 0, QtCore.QModelIndex()))
  497. self.adj_exc_object_combo.setCurrentIndex(1)
  498. self.adj_excobj_label = QtWidgets.QLabel("<b>%s:</b>" % _("EXCELLON"))
  499. self.adj_excobj_label.setToolTip(
  500. _("Excellon Object to be adjusted.")
  501. )
  502. grid_lay.addWidget(self.adj_excobj_label, 32, 0)
  503. grid_lay.addWidget(self.adj_exc_object_combo, 32, 1, 1, 2)
  504. self.adj_geo_object_combo = QtWidgets.QComboBox()
  505. self.adj_geo_object_combo.setModel(self.app.collection)
  506. self.adj_geo_object_combo.setRootModelIndex(self.app.collection.index(2, 0, QtCore.QModelIndex()))
  507. self.adj_geo_object_combo.setCurrentIndex(1)
  508. self.adj_geoobj_label = QtWidgets.QLabel("<b>%s:</b>" % _("GEOMETRY"))
  509. self.adj_geoobj_label.setToolTip(
  510. _("Geometry Object to be adjusted.")
  511. )
  512. grid_lay.addWidget(self.adj_geoobj_label, 33, 0)
  513. grid_lay.addWidget(self.adj_geo_object_combo, 33, 1, 1, 2)
  514. # ## Adjust Objects Button
  515. self.adj_obj_button = QtWidgets.QPushButton(_("Adjust Objects"))
  516. self.adj_obj_button.setToolTip(
  517. _("Adjust (scale and/or skew) the objects\n"
  518. "with the factors determined above.")
  519. )
  520. self.adj_obj_button.setStyleSheet("""
  521. QPushButton
  522. {
  523. font-weight: bold;
  524. }
  525. """)
  526. grid_lay.addWidget(self.adj_obj_button, 34, 0, 1, 3)
  527. grid_lay.addWidget(QtWidgets.QLabel(''), 35, 0)
  528. self.layout.addStretch()
  529. # ## Reset Tool
  530. self.reset_button = QtWidgets.QPushButton(_("Reset Tool"))
  531. self.reset_button.setToolTip(
  532. _("Will reset the tool parameters.")
  533. )
  534. self.reset_button.setStyleSheet("""
  535. QPushButton
  536. {
  537. font-weight: bold;
  538. }
  539. """)
  540. self.layout.addWidget(self.reset_button)
  541. self.mr = None
  542. self.units = ''
  543. # here store 4 points to be used for calibration
  544. self.click_points = list()
  545. # store the status of the grid
  546. self.grid_status_memory = None
  547. self.exc_obj = None
  548. # ## Signals
  549. self.start_button.clicked.connect(self.on_start_collect_points)
  550. self.gcode_button.clicked.connect(self.generate_verification_gcode)
  551. self.generate_factors_button.clicked.connect(self.calculate_factors)
  552. self.reset_button.clicked.connect(self.set_tool_ui)
  553. def run(self, toggle=True):
  554. self.app.report_usage("ToolCalibrateExcellon()")
  555. if toggle:
  556. # if the splitter is hidden, display it, else hide it but only if the current widget is the same
  557. if self.app.ui.splitter.sizes()[0] == 0:
  558. self.app.ui.splitter.setSizes([1, 1])
  559. else:
  560. try:
  561. if self.app.ui.tool_scroll_area.widget().objectName() == self.toolName:
  562. # if tab is populated with the tool but it does not have the focus, focus on it
  563. if not self.app.ui.notebook.currentWidget() is self.app.ui.tool_tab:
  564. # focus on Tool Tab
  565. self.app.ui.notebook.setCurrentWidget(self.app.ui.tool_tab)
  566. else:
  567. self.app.ui.splitter.setSizes([0, 1])
  568. except AttributeError:
  569. pass
  570. else:
  571. if self.app.ui.splitter.sizes()[0] == 0:
  572. self.app.ui.splitter.setSizes([1, 1])
  573. FlatCAMTool.run(self)
  574. self.set_tool_ui()
  575. self.app.ui.notebook.setTabText(2, _("Cal Exc Tool"))
  576. def install(self, icon=None, separator=None, **kwargs):
  577. FlatCAMTool.install(self, icon, separator, shortcut='ALT+E', **kwargs)
  578. def set_tool_ui(self):
  579. self.units = self.app.defaults['units'].upper()
  580. # ## Initialize form
  581. # self.mm_entry.set_value('%.*f' % (self.decimals, 0))
  582. def on_start_collect_points(self):
  583. # disengage the grid snapping since it will be hard to find the drills on grid
  584. if self.app.ui.grid_snap_btn.isChecked():
  585. self.grid_status_memory = True
  586. self.app.ui.grid_snap_btn.trigger()
  587. else:
  588. self.grid_status_memory = False
  589. self.mr = self.canvas.graph_event_connect('mouse_release', self.on_mouse_click_release)
  590. if self.app.is_legacy is False:
  591. self.canvas.graph_event_disconnect('mouse_release', self.app.on_mouse_click_release_over_plot)
  592. else:
  593. self.canvas.graph_event_disconnect(self.app.mr)
  594. selection_index = self.exc_object_combo.currentIndex()
  595. model_index = self.app.collection.index(selection_index, 0, self.exc_object_combo.rootModelIndex())
  596. try:
  597. self.exc_obj = model_index.internalPointer().obj
  598. except Exception:
  599. self.app.inform.emit('[WARNING_NOTCL] %s' % _("There is no Excellon object loaded ..."))
  600. return
  601. self.app.inform.emit(_("Click inside the First drill point. Bottom Left..."))
  602. def on_mouse_click_release(self, event):
  603. if event.button == 1:
  604. if self.app.is_legacy is False:
  605. event_pos = event.pos
  606. else:
  607. event_pos = (event.xdata, event.ydata)
  608. pos_canvas = self.canvas.translate_coords(event_pos)
  609. click_pt = Point([pos_canvas[0], pos_canvas[1]])
  610. for tool, tool_dict in self.exc_obj.tools.items():
  611. for geo in tool_dict['solid_geometry']:
  612. if click_pt.within(geo):
  613. center_pt = geo.centroid
  614. self.click_points.append(
  615. (
  616. float('%.*f' % (self.decimals, center_pt.x)),
  617. float('%.*f' % (self.decimals, center_pt.y))
  618. )
  619. )
  620. self.check_points()
  621. def check_points(self):
  622. if len(self.click_points) == 1:
  623. self.bottom_left_coordx_tgt.set_value(self.click_points[0][0])
  624. self.bottom_left_coordy_tgt.set_value(self.click_points[0][1])
  625. self.app.inform.emit(_("Click inside the Second drill point. Bottom Right..."))
  626. elif len(self.click_points) == 2:
  627. self.bottom_right_coordx_tgt.set_value(self.click_points[1][0])
  628. self.bottom_right_coordy_tgt.set_value(self.click_points[1][1])
  629. self.app.inform.emit(_("Click inside the Third drill point. Top Left..."))
  630. elif len(self.click_points) == 3:
  631. self.top_left_coordx_tgt.set_value(self.click_points[2][0])
  632. self.top_left_coordy_tgt.set_value(self.click_points[2][1])
  633. self.app.inform.emit(_("Click inside the Fourth drill point. Top Right..."))
  634. elif len(self.click_points) == 4:
  635. self.top_right_coordx_tgt.set_value(self.click_points[3][0])
  636. self.top_right_coordy_tgt.set_value(self.click_points[3][1])
  637. self.app.inform.emit('[success] %s' % _("Done. All four points have been acquired."))
  638. self.disconnect_cal_events()
  639. # restore the Grid snapping if it was active before
  640. if self.grid_status_memory is True:
  641. self.app.ui.grid_snap_btn.trigger()
  642. def gcode_header(self):
  643. log.debug("ToolCalibrateExcellon.gcode_header()")
  644. time_str = "{:%A, %d %B %Y at %H:%M}".format(datetime.now())
  645. gcode = '(G-CODE GENERATED BY FLATCAM v%s - www.flatcam.org - Version Date: %s)\n' % \
  646. (str(self.app.version), str(self.app.version_date)) + '\n'
  647. gcode += '(Name: ' + _('Verification GCode') + ')\n'
  648. gcode += '(Units: ' + self.units.upper() + ')\n' + "\n"
  649. gcode += '(Created on ' + time_str + ')\n' + '\n'
  650. gcode += 'G20\n' if self.units.upper() == 'IN' else 'G21\n'
  651. gcode += 'G90\n'
  652. gcode += 'G17\n'
  653. gcode += 'G94\n\n'
  654. return gcode
  655. def generate_verification_gcode(self):
  656. travel_z = '%.*f' % (self.decimals, self.travelz_entry.get_value())
  657. toolchange_z = '%.*f' % (self.decimals, self.toolchangez_entry.get_value())
  658. verification_z = '%.*f' % (self.decimals, self.verz_entry.get_value())
  659. if len(self.click_points) != 4:
  660. self.app.inform.emit('[WARNING_NOTCL] %s' % _("Cancelled. Four points are needed for GCode generation."))
  661. return 'fail'
  662. gcode = self.gcode_header()
  663. if self.zeroz_cb.get_value():
  664. gcode += 'M5\n'
  665. gcode += f'G00 Z{toolchange_z}\n'
  666. gcode += 'M0\n'
  667. gcode += 'G01 Z0\n'
  668. gcode += 'M0\n'
  669. gcode += f'G00 Z{toolchange_z}\n'
  670. gcode += 'M0\n'
  671. gcode += f'G00 Z{travel_z}\n'
  672. gcode += f'G00 X{self.click_points[0][0]} Y{self.click_points[0][1]}\n'
  673. gcode += f'G01 Z{verification_z}\n'
  674. gcode += 'M0\n'
  675. gcode += f'G00 Z{travel_z}\n'
  676. gcode += f'G00 X{self.click_points[2][0]} Y{self.click_points[2][1]}\n'
  677. gcode += f'G01 Z{verification_z}\n'
  678. gcode += 'M0\n'
  679. gcode += f'G00 Z{travel_z}\n'
  680. gcode += f'G00 X{self.click_points[3][0]} Y{self.click_points[3][1]}\n'
  681. gcode += f'G01 Z{verification_z}\n'
  682. gcode += 'M0\n'
  683. gcode += f'G00 Z{travel_z}\n'
  684. gcode += f'G00 X{self.click_points[1][0]} Y{self.click_points[1][1]}\n'
  685. gcode += f'G01 Z{verification_z}\n'
  686. gcode += 'M0\n'
  687. gcode += f'G00 Z{travel_z}\n'
  688. gcode += f'G00 X0 Y0\n'
  689. gcode += f'G00 Z{toolchange_z}\n'
  690. gcode += 'M2'
  691. _filter_ = "G-Code Files (*.nc);;All Files (*.*)"
  692. try:
  693. dir_file_to_save = self.app.get_last_save_folder() + '/' + 'ver_gcode'
  694. filename, _f = QtWidgets.QFileDialog.getSaveFileName(
  695. caption=_("Export Machine Code ..."),
  696. directory=dir_file_to_save,
  697. filter=_filter_
  698. )
  699. except TypeError:
  700. filename, _f = QtWidgets.QFileDialog.getSaveFileName(caption=_("Export Machine Code ..."), filter=_filter_)
  701. filename = str(filename)
  702. if filename == '':
  703. self.app.inform.emit('[WARNING_NOTCL] %s' % _("Export Machine Code cancelled ..."))
  704. return
  705. with open(filename, 'w') as f:
  706. f.write(gcode)
  707. def calculate_factors(self):
  708. origin_x = self.click_points[0][0]
  709. origin_y = self.click_points[0][1]
  710. top_left_x = float('%.*f' % (self.decimals, self.click_points[2][0]))
  711. top_left_y = float('%.*f' % (self.decimals, self.click_points[2][1]))
  712. try:
  713. top_left_dx = float('%.*f' % (self.decimals, self.top_left_coordx_found.get_value()))
  714. except TypeError:
  715. top_left_dx = top_left_x
  716. try:
  717. top_left_dy = float('%.*f' % (self.decimals, self.top_left_coordy_found.get_value()))
  718. except TypeError:
  719. top_left_dy = top_left_y
  720. top_right_x = float('%.*f' % (self.decimals, self.click_points[3][0]))
  721. top_right_y = float('%.*f' % (self.decimals, self.click_points[3][1]))
  722. try:
  723. top_right_dx = float('%.*f' % (self.decimals, self.top_right_coordx_found.get_value()))
  724. except TypeError:
  725. top_right_dx = top_right_x
  726. try:
  727. top_right_dy = float('%.*f' % (self.decimals, self.top_right_coordy_found.get_value()))
  728. except TypeError:
  729. top_right_dy = top_right_y
  730. bot_right_x = float('%.*f' % (self.decimals, self.click_points[1][0]))
  731. bot_right_y = float('%.*f' % (self.decimals, self.click_points[1][1]))
  732. try:
  733. bot_right_dx = float('%.*f' % (self.decimals, self.bottom_right_coordx_found.get_value()))
  734. except TypeError:
  735. bot_right_dx = bot_right_x
  736. try:
  737. bot_right_dy = float('%.*f' % (self.decimals, self.bottom_right_coordy_found.get_value()))
  738. except TypeError:
  739. bot_right_dy = bot_right_y
  740. # ------------------------------------------------------------------------------- #
  741. # --------------------------- FACTORS CALCULUS ---------------------------------- #
  742. # ------------------------------------------------------------------------------- #
  743. if top_left_dy != float('%.*f' % (self.decimals, 0.0)):
  744. # we have scale on Y
  745. scale_y = (top_left_dy + top_left_y - origin_y) / (top_left_y - origin_y)
  746. self.scaley_entry.set_value(scale_y)
  747. if top_left_dx != float('%.*f' % (self.decimals, 0.0)):
  748. # we have skew on X
  749. dx = top_left_dx
  750. dy = top_left_y - origin_y
  751. skew_angle_x = math.degrees(math.atan(dx / dy))
  752. self.skewx_entrx.set_value(skew_angle_x)
  753. if bot_right_dx != float('%.*f' % (self.decimals, 0.0)):
  754. # we have scale on X
  755. scale_x = (bot_right_dx + bot_right_x - origin_x) / (bot_right_x - origin_x)
  756. self.scalex_entry.set_value(scale_x)
  757. if bot_right_dy != float('%.*f' % (self.decimals, 0.0)):
  758. # we have skew on Y
  759. dx = bot_right_x - origin_x
  760. dy = bot_right_dy + origin_y
  761. skew_angle_y = math.degrees(math.atan(dy / dx))
  762. self.skewy_entry.set_value(skew_angle_y)
  763. def disconnect_cal_events(self):
  764. self.app.mr = self.canvas.graph_event_connect('mouse_release', self.app.on_mouse_click_release_over_plot)
  765. if self.app.is_legacy is False:
  766. self.canvas.graph_event_disconnect('mouse_release', self.on_mouse_click_release)
  767. else:
  768. self.canvas.graph_event_disconnect(self.mr)
  769. def reset_fields(self):
  770. self.exc_object_combo.setRootModelIndex(self.app.collection.index(1, 0, QtCore.QModelIndex()))
  771. # end of file