ToolCalibrateExcellon.py 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  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
  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, 2, 0, 1, 3)
  115. # BOTTOM LEFT
  116. self.bottom_left_lbl = QtWidgets.QLabel('<b>%s</b>' % _('Bottom Left'))
  117. grid_lay.addWidget(self.bottom_left_lbl, 3, 0)
  118. self.bottom_left_tgt_lbl = QtWidgets.QLabel('%s' % _('Target'))
  119. grid_lay.addWidget(self.bottom_left_tgt_lbl, 3, 1)
  120. self.bottom_left_found_lbl = QtWidgets.QLabel('%s' % _('Cal. Origin'))
  121. grid_lay.addWidget(self.bottom_left_found_lbl, 3, 2)
  122. self.bottom_left_coordx_lbl = QtWidgets.QLabel('%s' % _('X'))
  123. grid_lay.addWidget(self.bottom_left_coordx_lbl, 4, 0)
  124. self.bottom_left_coordx_tgt = EvalEntry()
  125. self.bottom_left_coordx_tgt.setReadOnly(True)
  126. grid_lay.addWidget(self.bottom_left_coordx_tgt, 4, 1)
  127. self.bottom_left_coordx_found = EvalEntry()
  128. grid_lay.addWidget(self.bottom_left_coordx_found, 4, 2)
  129. self.bottom_left_coordy_lbl = QtWidgets.QLabel('%s' % _('Y'))
  130. grid_lay.addWidget(self.bottom_left_coordy_lbl, 5, 0)
  131. self.bottom_left_coordy_tgt = EvalEntry()
  132. self.bottom_left_coordy_tgt.setReadOnly(True)
  133. grid_lay.addWidget(self.bottom_left_coordy_tgt, 5, 1)
  134. self.bottom_left_coordy_found = EvalEntry()
  135. grid_lay.addWidget(self.bottom_left_coordy_found, 5, 2)
  136. self.bottom_left_coordx_found.set_value(_('Set Origin'))
  137. self.bottom_left_coordy_found.set_value(_('Set Origin'))
  138. self.bottom_left_coordx_found.setDisabled(True)
  139. self.bottom_left_coordy_found.setDisabled(True)
  140. # BOTTOM RIGHT
  141. self.bottom_right_lbl = QtWidgets.QLabel('<b>%s</b>' % _('Bottom Right'))
  142. grid_lay.addWidget(self.bottom_right_lbl, 6, 0)
  143. self.bottom_right_tgt_lbl = QtWidgets.QLabel('%s' % _('Target'))
  144. grid_lay.addWidget(self.bottom_right_tgt_lbl, 6, 1)
  145. self.bottom_right_found_lbl = QtWidgets.QLabel('%s' % _('Found Delta'))
  146. grid_lay.addWidget(self.bottom_right_found_lbl, 6, 2)
  147. self.bottom_right_coordx_lbl = QtWidgets.QLabel('%s' % _('X'))
  148. grid_lay.addWidget(self.bottom_right_coordx_lbl, 7, 0)
  149. self.bottom_right_coordx_tgt = EvalEntry()
  150. self.bottom_right_coordx_tgt.setReadOnly(True)
  151. grid_lay.addWidget(self.bottom_right_coordx_tgt, 7, 1)
  152. self.bottom_right_coordx_found = EvalEntry()
  153. grid_lay.addWidget(self.bottom_right_coordx_found, 7, 2)
  154. self.bottom_right_coordy_lbl = QtWidgets.QLabel('%s' % _('Y'))
  155. grid_lay.addWidget(self.bottom_right_coordy_lbl, 8, 0)
  156. self.bottom_right_coordy_tgt = EvalEntry()
  157. self.bottom_right_coordy_tgt.setReadOnly(True)
  158. grid_lay.addWidget(self.bottom_right_coordy_tgt, 8, 1)
  159. self.bottom_right_coordy_found = EvalEntry()
  160. grid_lay.addWidget(self.bottom_right_coordy_found, 8, 2)
  161. # TOP LEFT
  162. self.top_left_lbl = QtWidgets.QLabel('<b>%s</b>' % _('Top Left'))
  163. grid_lay.addWidget(self.top_left_lbl, 9, 0)
  164. self.top_left_tgt_lbl = QtWidgets.QLabel('%s' % _('Target'))
  165. grid_lay.addWidget(self.top_left_tgt_lbl, 9, 1)
  166. self.top_left_found_lbl = QtWidgets.QLabel('%s' % _('Found Delta'))
  167. grid_lay.addWidget(self.top_left_found_lbl, 9, 2)
  168. self.top_left_coordx_lbl = QtWidgets.QLabel('%s' % _('X'))
  169. grid_lay.addWidget(self.top_left_coordx_lbl, 10, 0)
  170. self.top_left_coordx_tgt = EvalEntry()
  171. self.top_left_coordx_tgt.setReadOnly(True)
  172. grid_lay.addWidget(self.top_left_coordx_tgt, 10, 1)
  173. self.top_left_coordx_found = EvalEntry()
  174. grid_lay.addWidget(self.top_left_coordx_found, 10, 2)
  175. self.top_left_coordy_lbl = QtWidgets.QLabel('%s' % _('Y'))
  176. grid_lay.addWidget(self.top_left_coordy_lbl, 11, 0)
  177. self.top_left_coordy_tgt = EvalEntry()
  178. self.top_left_coordy_tgt.setReadOnly(True)
  179. grid_lay.addWidget(self.top_left_coordy_tgt, 11, 1)
  180. self.top_left_coordy_found = EvalEntry()
  181. grid_lay.addWidget(self.top_left_coordy_found, 11, 2)
  182. # TOP RIGHT
  183. self.top_right_lbl = QtWidgets.QLabel('<b>%s</b>' % _('Top Right'))
  184. grid_lay.addWidget(self.top_right_lbl, 12, 0)
  185. self.top_right_tgt_lbl = QtWidgets.QLabel('%s' % _('Target'))
  186. grid_lay.addWidget(self.top_right_tgt_lbl, 12, 1)
  187. self.top_right_found_lbl = QtWidgets.QLabel('%s' % _('Found Delta'))
  188. grid_lay.addWidget(self.top_right_found_lbl, 12, 2)
  189. self.top_right_coordx_lbl = QtWidgets.QLabel('%s' % _('X'))
  190. grid_lay.addWidget(self.top_right_coordx_lbl, 13, 0)
  191. self.top_right_coordx_tgt = EvalEntry()
  192. self.top_right_coordx_tgt.setReadOnly(True)
  193. grid_lay.addWidget(self.top_right_coordx_tgt, 13, 1)
  194. self.top_right_coordx_found = EvalEntry()
  195. grid_lay.addWidget(self.top_right_coordx_found, 13, 2)
  196. self.top_right_coordy_lbl = QtWidgets.QLabel('%s' % _('Y'))
  197. grid_lay.addWidget(self.top_right_coordy_lbl, 14, 0)
  198. self.top_right_coordy_tgt = EvalEntry()
  199. self.top_right_coordy_tgt.setReadOnly(True)
  200. grid_lay.addWidget(self.top_right_coordy_tgt, 14, 1)
  201. self.top_right_coordy_found = EvalEntry()
  202. grid_lay.addWidget(self.top_right_coordy_found, 14, 2)
  203. # STEP 1 #
  204. step_1 = QtWidgets.QLabel('<b>%s</b>' % _("STEP 1"))
  205. step_1.setToolTip(
  206. _("Pick four points by clicking inside the drill holes.\n"
  207. "Those four points should be in the four\n"
  208. "(as much as possible) corners of the Excellon object.")
  209. )
  210. grid_lay.addWidget(step_1, 15, 0, 1, 3)
  211. # ## Start Button
  212. self.start_button = QtWidgets.QPushButton(_("Acquire Calibration Points"))
  213. self.start_button.setToolTip(
  214. _("Pick four points by clicking inside the drill holes.\n"
  215. "Those four points should be in the four squares of\n"
  216. "the Excellon object.")
  217. )
  218. grid_lay.addWidget(self.start_button, 16, 0, 1, 3)
  219. # STEP 2 #
  220. step_2 = QtWidgets.QLabel('<b>%s</b>' % _("STEP 2"))
  221. step_2.setToolTip(
  222. _("Generate GCode file to locate and align the PCB by using\n"
  223. "the four points acquired above.")
  224. )
  225. grid_lay.addWidget(step_2, 17, 0, 1, 3)
  226. # ## GCode Button
  227. self.gcode_button = QtWidgets.QPushButton(_("Generate GCode"))
  228. self.gcode_button.setToolTip(
  229. _("Generate GCode file to locate and align the PCB by using\n"
  230. "the four points acquired above.")
  231. )
  232. grid_lay.addWidget(self.gcode_button, 18, 0, 1, 3)
  233. # STEP 3 #
  234. step_3 = QtWidgets.QLabel('<b>%s</b>' % _("STEP 3"))
  235. step_3.setToolTip(
  236. _("Calculate Scale and Skew factors based on the differences (delta)\n"
  237. "found when checking the PCB pattern. The differences must be filled\n"
  238. "in the fields Found (Delta).")
  239. )
  240. grid_lay.addWidget(step_3, 19, 0, 1, 3)
  241. # ## Factors Button
  242. self.generate_factors_button = QtWidgets.QPushButton(_("Calculate Factors"))
  243. self.generate_factors_button.setToolTip(
  244. _("Calculate Scale and Skew factors based on the differences (delta)\n"
  245. "found when checking the PCB pattern. The differences must be filled\n"
  246. "in the fields Found (Delta).")
  247. )
  248. grid_lay.addWidget(self.generate_factors_button, 20, 0, 1, 3)
  249. scale_lbl = QtWidgets.QLabel('<b>%s</b>' % _("Scale"))
  250. grid_lay.addWidget(scale_lbl, 21, 0, 1, 3)
  251. self.scalex_label = QtWidgets.QLabel(_("Factor X:"))
  252. self.scalex_label.setToolTip(
  253. _("Factor for Scale action over X axis.")
  254. )
  255. self.scalex_entry = FCDoubleSpinner()
  256. self.scalex_entry.set_range(0, 9999.9999)
  257. self.scalex_entry.set_precision(self.decimals)
  258. self.scalex_entry.setSingleStep(0.1)
  259. grid_lay.addWidget(self.scalex_label, 22, 0)
  260. grid_lay.addWidget(self.scalex_entry, 22, 1, 1, 2)
  261. self.scaley_label = QtWidgets.QLabel(_("Factor Y:"))
  262. self.scaley_label.setToolTip(
  263. _("Factor for Scale action over Y axis.")
  264. )
  265. self.scaley_entry = FCDoubleSpinner()
  266. self.scaley_entry.set_range(0, 9999.9999)
  267. self.scaley_entry.set_precision(self.decimals)
  268. self.scaley_entry.setSingleStep(0.1)
  269. grid_lay.addWidget(self.scaley_label, 23, 0)
  270. grid_lay.addWidget(self.scaley_entry, 23, 1, 1, 2)
  271. skew_lbl = QtWidgets.QLabel('<b>%s</b>' % _("Skew"))
  272. grid_lay.addWidget(skew_lbl, 24, 0, 1, 3)
  273. self.skewx_label = QtWidgets.QLabel(_("Angle X:"))
  274. self.skewx_label.setToolTip(
  275. _("Angle for Skew action, in degrees.\n"
  276. "Float number between -360 and 359.")
  277. )
  278. self.skewx_entry = FCDoubleSpinner()
  279. self.skewx_entry.set_range(-360, 360)
  280. self.skewx_entry.set_precision(self.decimals)
  281. self.skewx_entry.setSingleStep(0.1)
  282. grid_lay.addWidget(self.skewx_label, 25, 0)
  283. grid_lay.addWidget(self.skewx_entry, 25, 1, 1, 2)
  284. self.skewy_label = QtWidgets.QLabel(_("Angle Y:"))
  285. self.skewy_label.setToolTip(
  286. _("Angle for Skew action, in degrees.\n"
  287. "Float number between -360 and 359.")
  288. )
  289. self.skewy_entry = FCDoubleSpinner()
  290. self.skewy_entry.set_range(-360, 360)
  291. self.skewy_entry.set_precision(self.decimals)
  292. self.skewy_entry.setSingleStep(0.1)
  293. grid_lay.addWidget(self.skewy_label, 26, 0)
  294. grid_lay.addWidget(self.skewy_entry, 26, 1, 1, 2)
  295. # STEP 4 #
  296. step_4 = QtWidgets.QLabel('<b>%s</b>' % _("STEP 4"))
  297. step_4.setToolTip(
  298. _("Generate verification GCode file adjusted with\n"
  299. "the factors above.")
  300. )
  301. grid_lay.addWidget(step_4, 27, 0, 1, 3)
  302. # ## Adjusted GCode Button
  303. self.adj_gcode_button = QtWidgets.QPushButton(_("Generate Adjusted GCode"))
  304. self.adj_gcode_button.setToolTip(
  305. _("Generate verification GCode file adjusted with\n"
  306. "the factors above.")
  307. )
  308. grid_lay.addWidget(self.adj_gcode_button, 28, 0, 1, 3)
  309. # STEP 5 #
  310. step_5 = QtWidgets.QLabel('<b>%s</b>' % _("STEP 5"))
  311. step_5.setToolTip(
  312. _("Ajust the Excellon and Cutout Geometry objects\n"
  313. "with the factors determined, and verified, above.")
  314. )
  315. grid_lay.addWidget(step_5, 29, 0, 1, 3)
  316. self.adj_exc_object_combo = QtWidgets.QComboBox()
  317. self.adj_exc_object_combo.setModel(self.app.collection)
  318. self.adj_exc_object_combo.setRootModelIndex(self.app.collection.index(1, 0, QtCore.QModelIndex()))
  319. self.adj_exc_object_combo.setCurrentIndex(1)
  320. self.adj_excobj_label = QtWidgets.QLabel("<b>%s:</b>" % _("EXCELLON"))
  321. self.adj_excobj_label.setToolTip(
  322. _("Excellon Object to be adjusted.")
  323. )
  324. grid_lay.addWidget(self.adj_excobj_label, 30, 0)
  325. grid_lay.addWidget(self.adj_exc_object_combo, 30, 1, 1, 2)
  326. self.adj_geo_object_combo = QtWidgets.QComboBox()
  327. self.adj_geo_object_combo.setModel(self.app.collection)
  328. self.adj_geo_object_combo.setRootModelIndex(self.app.collection.index(2, 0, QtCore.QModelIndex()))
  329. self.adj_geo_object_combo.setCurrentIndex(1)
  330. self.adj_geoobj_label = QtWidgets.QLabel("<b>%s:</b>" % _("GEOMETRY"))
  331. self.adj_geoobj_label.setToolTip(
  332. _("Geometry Object to be adjusted.")
  333. )
  334. grid_lay.addWidget(self.adj_geoobj_label, 31, 0)
  335. grid_lay.addWidget(self.adj_geo_object_combo, 31, 1, 1, 2)
  336. # ## Adjust Objects Button
  337. self.adj_obj_button = QtWidgets.QPushButton(_("Adjust Objects"))
  338. self.adj_obj_button.setToolTip(
  339. _("Adjust (scale and / or skew) the objects\n"
  340. "with the factors determined above.")
  341. )
  342. grid_lay.addWidget(self.adj_obj_button, 32, 0, 1, 3)
  343. grid_lay.addWidget(QtWidgets.QLabel(''), 33, 0)
  344. self.layout.addStretch()
  345. self.mr = None
  346. self.units = ''
  347. # here store 4 points to be used for calibration
  348. self.click_points = list()
  349. # store the status of the grid
  350. self.grid_status_memory = None
  351. self.exc_obj = None
  352. # ## Signals
  353. self.start_button.clicked.connect(self.on_start_collect_points)
  354. self.gcode_button.clicked.connect(self.generate_verification_gcode)
  355. self.generate_factors_button.clicked.connect(self.calculate_factors)
  356. def run(self, toggle=True):
  357. self.app.report_usage("ToolCalibrateExcellon()")
  358. if toggle:
  359. # if the splitter is hidden, display it, else hide it but only if the current widget is the same
  360. if self.app.ui.splitter.sizes()[0] == 0:
  361. self.app.ui.splitter.setSizes([1, 1])
  362. else:
  363. try:
  364. if self.app.ui.tool_scroll_area.widget().objectName() == self.toolName:
  365. # if tab is populated with the tool but it does not have the focus, focus on it
  366. if not self.app.ui.notebook.currentWidget() is self.app.ui.tool_tab:
  367. # focus on Tool Tab
  368. self.app.ui.notebook.setCurrentWidget(self.app.ui.tool_tab)
  369. else:
  370. self.app.ui.splitter.setSizes([0, 1])
  371. except AttributeError:
  372. pass
  373. else:
  374. if self.app.ui.splitter.sizes()[0] == 0:
  375. self.app.ui.splitter.setSizes([1, 1])
  376. FlatCAMTool.run(self)
  377. self.set_tool_ui()
  378. self.app.ui.notebook.setTabText(2, _("Cal Exc Tool"))
  379. def install(self, icon=None, separator=None, **kwargs):
  380. FlatCAMTool.install(self, icon, separator, shortcut='ALT+E', **kwargs)
  381. def set_tool_ui(self):
  382. self.units = self.app.ui.general_defaults_form.general_app_group.units_radio.get_value().upper()
  383. # ## Initialize form
  384. # self.mm_entry.set_value('%.*f' % (self.decimals, 0))
  385. def on_start_collect_points(self):
  386. # disengage the grid snapping since it will be hard to find the drills on grid
  387. if self.app.ui.grid_snap_btn.isChecked():
  388. self.grid_status_memory = True
  389. self.app.ui.grid_snap_btn.trigger()
  390. else:
  391. self.grid_status_memory = False
  392. self.mr = self.canvas.graph_event_connect('mouse_release', self.on_mouse_click_release)
  393. if self.app.is_legacy is False:
  394. self.canvas.graph_event_disconnect('mouse_release', self.app.on_mouse_click_release_over_plot)
  395. else:
  396. self.canvas.graph_event_disconnect(self.app.mr)
  397. selection_index = self.exc_object_combo.currentIndex()
  398. model_index = self.app.collection.index(selection_index, 0, self.exc_object_combo.rootModelIndex())
  399. try:
  400. self.exc_obj = model_index.internalPointer().obj
  401. except Exception as e:
  402. self.app.inform.emit('[WARNING_NOTCL] %s' % _("There is no Excellon object loaded ..."))
  403. return
  404. self.app.inform.emit(_("Click inside the First drill point. Bottom Left..."))
  405. def on_mouse_click_release(self, event):
  406. if event.button == 1:
  407. if self.app.is_legacy is False:
  408. event_pos = event.pos
  409. else:
  410. event_pos = (event.xdata, event.ydata)
  411. pos_canvas = self.canvas.translate_coords(event_pos)
  412. click_pt = Point([pos_canvas[0], pos_canvas[1]])
  413. for tool, tool_dict in self.exc_obj.tools.items():
  414. for geo in tool_dict['solid_geometry']:
  415. if click_pt.within(geo):
  416. center_pt = geo.centroid
  417. self.click_points.append(
  418. (
  419. float('%.*f' % (self.decimals, center_pt.x)),
  420. float('%.*f' % (self.decimals, center_pt.y))
  421. )
  422. )
  423. self.check_points()
  424. def check_points(self):
  425. if len(self.click_points) == 1:
  426. self.bottom_left_coordx_tgt.set_value(self.click_points[0][0])
  427. self.bottom_left_coordy_tgt.set_value(self.click_points[0][1])
  428. self.app.inform.emit(_("Click inside the Second drill point. Bottom Right..."))
  429. elif len(self.click_points) == 2:
  430. self.bottom_right_coordx_tgt.set_value(self.click_points[1][0])
  431. self.bottom_right_coordy_tgt.set_value(self.click_points[1][1])
  432. self.app.inform.emit(_("Click inside the Third drill point. Top Left..."))
  433. elif len(self.click_points) == 3:
  434. self.top_left_coordx_tgt.set_value(self.click_points[2][0])
  435. self.top_left_coordy_tgt.set_value(self.click_points[2][1])
  436. self.app.inform.emit(_("Click inside the Fourth drill point. Top Right..."))
  437. elif len(self.click_points) == 4:
  438. self.top_right_coordx_tgt.set_value(self.click_points[3][0])
  439. self.top_right_coordy_tgt.set_value(self.click_points[3][1])
  440. self.app.inform.emit('[success] %s' % _("Done. All four points have been acquired."))
  441. self.disconnect_cal_events()
  442. # restore the Grid snapping if it was active before
  443. if self.grid_status_memory is True:
  444. self.app.ui.grid_snap_btn.trigger()
  445. def gcode_header(self):
  446. log.debug("ToolCalibrateExcellon.gcode_header()")
  447. time_str = "{:%A, %d %B %Y at %H:%M}".format(datetime.now())
  448. gcode = '(G-CODE GENERATED BY FLATCAM v%s - www.flatcam.org - Version Date: %s)\n' % \
  449. (str(self.app.version), str(self.app.version_date)) + '\n'
  450. gcode += '(Name: ' + _('Verification GCode') + ')\n'
  451. gcode += '(Units: ' + self.units.upper() + ')\n' + "\n"
  452. gcode += '(Created on ' + time_str + ')\n' + '\n'
  453. gcode += 'G20\n' if self.units.upper() == 'IN' else 'G21\n'
  454. gcode += 'G90\n'
  455. gcode += 'G17\n'
  456. gcode += 'G94\n\n'
  457. return gcode
  458. def generate_verification_gcode(self):
  459. travel_z = '%.*f' % (self.decimals, self.travelz_entry.get_value())
  460. toolchange_z = '%.*f' % (self.decimals, self.toolchangez_entry.get_value())
  461. verification_z = '%.*f' % (self.decimals, self.verz_entry.get_value())
  462. if len(self.click_points) != 4:
  463. self.app.inform.emit('[WARNING_NOTCL] %s' % _("Cancelled. Four points are needed for GCode generation."))
  464. return 'fail'
  465. gcode = self.gcode_header()
  466. if self.zeroz_cb.get_value():
  467. gcode += 'M5\n'
  468. gcode += f'G00 Z{toolchange_z}\n'
  469. gcode += 'M0\n'
  470. gcode += 'G01 Z0\n'
  471. gcode += 'M0\n'
  472. gcode += f'G00 Z{toolchange_z}\n'
  473. gcode += 'M0\n'
  474. gcode += f'G00 Z{travel_z}\n'
  475. gcode += f'G00 X{self.click_points[0][0]} Y{self.click_points[0][1]}\n'
  476. gcode += f'G01 Z{verification_z}\n'
  477. gcode += 'M0\n'
  478. gcode += f'G00 Z{travel_z}\n'
  479. gcode += f'G00 X{self.click_points[2][0]} Y{self.click_points[2][1]}\n'
  480. gcode += f'G01 Z{verification_z}\n'
  481. gcode += 'M0\n'
  482. gcode += f'G00 Z{travel_z}\n'
  483. gcode += f'G00 X{self.click_points[3][0]} Y{self.click_points[3][1]}\n'
  484. gcode += f'G01 Z{verification_z}\n'
  485. gcode += 'M0\n'
  486. gcode += f'G00 Z{travel_z}\n'
  487. gcode += f'G00 X{self.click_points[1][0]} Y{self.click_points[1][1]}\n'
  488. gcode += f'G01 Z{verification_z}\n'
  489. gcode += 'M0\n'
  490. gcode += f'G00 Z{travel_z}\n'
  491. gcode += f'G00 X0 Y0\n'
  492. gcode += f'G00 Z{toolchange_z}\n'
  493. gcode += 'M2'
  494. _filter_ = "G-Code Files (*.nc);;All Files (*.*)"
  495. try:
  496. dir_file_to_save = self.app.get_last_save_folder() + '/' + 'ver_gcode'
  497. filename, _f = QtWidgets.QFileDialog.getSaveFileName(
  498. caption=_("Export Machine Code ..."),
  499. directory=dir_file_to_save,
  500. filter=_filter_
  501. )
  502. except TypeError:
  503. filename, _f = QtWidgets.QFileDialog.getSaveFileName(caption=_("Export Machine Code ..."), filter=_filter_)
  504. filename = str(filename)
  505. if filename == '':
  506. self.app.inform.emit('[WARNING_NOTCL] %s' % _("Export Machine Code cancelled ..."))
  507. return
  508. with open(filename, 'w') as f:
  509. f.write(gcode)
  510. def calculate_factors(self):
  511. origin_x = self.click_points[0][0]
  512. origin_y = self.click_points[0][1]
  513. top_left_x = float('%.*f' % (self.decimals, self.click_points[2][0]))
  514. top_left_y = float('%.*f' % (self.decimals, self.click_points[2][1]))
  515. try:
  516. top_left_dx = float('%.*f' % (self.decimals, self.top_left_coordx_found.get_value()))
  517. except TypeError:
  518. top_left_dx = top_left_x
  519. try:
  520. top_left_dy = float('%.*f' % (self.decimals, self.top_left_coordy_found.get_value()))
  521. except TypeError:
  522. top_left_dy = top_left_y
  523. top_right_x = float('%.*f' % (self.decimals, self.click_points[3][0]))
  524. top_right_y = float('%.*f' % (self.decimals, self.click_points[3][1]))
  525. try:
  526. top_right_dx = float('%.*f' % (self.decimals, self.top_right_coordx_found.get_value()))
  527. except TypeError:
  528. top_right_dx = top_right_x
  529. try:
  530. top_right_dy = float('%.*f' % (self.decimals, self.top_right_coordy_found.get_value()))
  531. except TypeError:
  532. top_right_dy = top_right_y
  533. bot_right_x = float('%.*f' % (self.decimals, self.click_points[1][0]))
  534. bot_right_y = float('%.*f' % (self.decimals, self.click_points[1][1]))
  535. try:
  536. bot_right_dx = float('%.*f' % (self.decimals, self.bottom_right_coordx_found.get_value()))
  537. except TypeError:
  538. bot_right_dx = bot_right_x
  539. try:
  540. bot_right_dy = float('%.*f' % (self.decimals, self.bottom_right_coordy_found.get_value()))
  541. except TypeError:
  542. bot_right_dy = bot_right_y
  543. if top_left_dy != float('%.*f' % (self.decimals, 0.0)):
  544. # we have scale on Y
  545. scale_y = (top_left_dy + top_left_y - origin_y) / (top_left_y - origin_y)
  546. self.scaley_entry.set_value(scale_y)
  547. if bot_right_dx != float('%.*f' % (self.decimals, 0.0)):
  548. # we have scale on X
  549. scale_x = (bot_right_dx + bot_right_x - origin_x) / (bot_right_x - origin_x)
  550. self.scalex_entry.set_value(scale_x)
  551. if bot_right_dy != float('%.*f' % (self.decimals, 0.0)):
  552. # we have skew on Y
  553. dy = bot_right_dy + origin_y
  554. dx = bot_right_x - origin_x
  555. skew_angle_y = math.degrees(math.atan(dy / dx))
  556. self.skewx_entry.set_value(skew_angle_y)
  557. def disconnect_cal_events(self):
  558. self.app.mr = self.canvas.graph_event_connect('mouse_release', self.app.on_mouse_click_release_over_plot)
  559. if self.app.is_legacy is False:
  560. self.canvas.graph_event_disconnect('mouse_release', self.on_mouse_click_release)
  561. else:
  562. self.canvas.graph_event_disconnect(self.mr)
  563. def reset_fields(self):
  564. self.exc_object_combo.setRootModelIndex(self.app.collection.index(1, 0, QtCore.QModelIndex()))
  565. # end of file