ToolOptimal.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. # ##########################################################
  2. # FlatCAM: 2D Post-processing for Manufacturing #
  3. # http://flatcam.org #
  4. # File Author: Marius Adrian Stanciu (c) #
  5. # Date: 09/27/2019 #
  6. # MIT Licence #
  7. # ##########################################################
  8. from FlatCAMTool import FlatCAMTool
  9. from FlatCAMObj import *
  10. from shapely.geometry import Point
  11. from shapely import affinity
  12. from shapely.ops import nearest_points
  13. from PyQt5 import QtCore
  14. import gettext
  15. import FlatCAMTranslation as fcTranslate
  16. import builtins
  17. fcTranslate.apply_language('strings')
  18. if '_' not in builtins.__dict__:
  19. _ = gettext.gettext
  20. class ToolOptimal(FlatCAMTool):
  21. toolName = _("Optimal Tool")
  22. update_text = pyqtSignal(list)
  23. update_sec_distances = pyqtSignal(dict)
  24. def __init__(self, app):
  25. FlatCAMTool.__init__(self, app)
  26. self.units = self.app.ui.general_defaults_form.general_app_group.units_radio.get_value().upper()
  27. self.decimals = 4
  28. # ## Title
  29. title_label = QtWidgets.QLabel("%s" % self.toolName)
  30. title_label.setStyleSheet("""
  31. QLabel
  32. {
  33. font-size: 16px;
  34. font-weight: bold;
  35. }
  36. """)
  37. self.layout.addWidget(title_label)
  38. # ## Form Layout
  39. form_lay = QtWidgets.QFormLayout()
  40. self.layout.addLayout(form_lay)
  41. form_lay.addRow(QtWidgets.QLabel(""))
  42. # ## Gerber Object to mirror
  43. self.gerber_object_combo = QtWidgets.QComboBox()
  44. self.gerber_object_combo.setModel(self.app.collection)
  45. self.gerber_object_combo.setRootModelIndex(self.app.collection.index(0, 0, QtCore.QModelIndex()))
  46. self.gerber_object_combo.setCurrentIndex(1)
  47. self.gerber_object_label = QtWidgets.QLabel("<b>%s:</b>" % _("GERBER"))
  48. self.gerber_object_label.setToolTip(
  49. "Gerber object for which to find the minimum distance between copper features."
  50. )
  51. form_lay.addRow(self.gerber_object_label, self.gerber_object_combo)
  52. # Precision = nr of decimals
  53. self.precision_label = QtWidgets.QLabel('%s:' % _("Precision"))
  54. self.precision_label.setToolTip(_("Number of decimals kept for found distances."))
  55. self.precision_spinner = FCSpinner()
  56. self.precision_spinner.set_range(2, 10)
  57. self.precision_spinner.setWrapping(True)
  58. form_lay.addRow(self.precision_label, self.precision_spinner)
  59. # Results Title
  60. self.title_res_label = QtWidgets.QLabel('<b>%s:</b>' % _("Minimum distance"))
  61. self.title_res_label.setToolTip(_("Display minimum distance between copper features."))
  62. form_lay.addRow(self.title_res_label)
  63. # Result value
  64. self.result_label = QtWidgets.QLabel('%s:' % _("Determined"))
  65. self.result_entry = FCEntry()
  66. self.result_entry.setReadOnly(True)
  67. self.units_lbl = QtWidgets.QLabel(self.units.lower())
  68. self.units_lbl.setDisabled(True)
  69. hlay = QtWidgets.QHBoxLayout()
  70. hlay.addWidget(self.result_entry)
  71. hlay.addWidget(self.units_lbl)
  72. form_lay.addRow(self.result_label, hlay)
  73. # Frequency of minimum encounter
  74. self.freq_label = QtWidgets.QLabel('%s:' % _("Occurring"))
  75. self.freq_label.setToolTip(_("How many times this minimum is found."))
  76. self.freq_entry = FCEntry()
  77. self.freq_entry.setReadOnly(True)
  78. form_lay.addRow(self.freq_label, self.freq_entry)
  79. # Control if to display the locations of where the minimum was found
  80. self.locations_cb = FCCheckBox(_("Minimum points coordinates"))
  81. self.locations_cb.setToolTip(_("Coordinates for points where minimum distance was found."))
  82. form_lay.addRow(self.locations_cb)
  83. # Locations where minimum was found
  84. self.locations_textb = FCTextArea(parent=self)
  85. self.locations_textb.setReadOnly(True)
  86. self.locations_textb.setToolTip(_("Coordinates for points where minimum distance was found."))
  87. stylesheet = """
  88. QTextEdit { selection-background-color:yellow;
  89. selection-color:black;
  90. }
  91. """
  92. self.locations_textb.setStyleSheet(stylesheet)
  93. form_lay.addRow(self.locations_textb)
  94. # Jump button
  95. self.locate_button = QtWidgets.QPushButton(_("Jump to selected position"))
  96. self.locate_button.setToolTip(
  97. _("Select a position in the Locations text box and then\n"
  98. "click this button.")
  99. )
  100. self.locate_button.setMinimumWidth(60)
  101. self.locate_button.setDisabled(True)
  102. form_lay.addRow(self.locate_button)
  103. # Other distances in Gerber
  104. self.title_second_res_label = QtWidgets.QLabel('<b>%s:</b>' % _("Other distances"))
  105. self.title_second_res_label.setToolTip(_("Will display other distances in the Gerber file ordered from\n"
  106. "the minimum to the maximum, not including the absolute minimum."))
  107. form_lay.addRow(self.title_second_res_label)
  108. # Control if to display the locations of where the minimum was found
  109. self.sec_locations_cb = FCCheckBox(_("Other distances points coordinates"))
  110. self.sec_locations_cb.setToolTip(_("Other distances and the coordinates for points\n"
  111. "where the distance was found."))
  112. form_lay.addRow(self.sec_locations_cb)
  113. # this way I can hide/show the frame
  114. self.sec_locations_frame = QtWidgets.QFrame()
  115. self.sec_locations_frame.setContentsMargins(0, 0, 0, 0)
  116. self.layout.addWidget(self.sec_locations_frame)
  117. self.distances_box = QtWidgets.QVBoxLayout()
  118. self.distances_box.setContentsMargins(0, 0, 0, 0)
  119. self.sec_locations_frame.setLayout(self.distances_box)
  120. # Other distances
  121. self.distances_textb = FCTextArea(parent=self)
  122. self.distances_textb.setReadOnly(True)
  123. self.distances_textb.setToolTip(_("Gerber distances."))
  124. stylesheet = """
  125. QTextEdit { selection-background-color:yellow;
  126. selection-color:black;
  127. }
  128. """
  129. self.distances_textb.setStyleSheet(stylesheet)
  130. self.distances_box.addWidget(self.distances_textb)
  131. # Locations where minimum was found
  132. self.locations_sec_textb = FCTextArea(parent=self)
  133. self.locations_sec_textb.setReadOnly(True)
  134. self.locations_sec_textb.setToolTip(_("Coordinates for points where the selected distance was found."))
  135. stylesheet = """
  136. QTextEdit { selection-background-color:yellow;
  137. selection-color:black;
  138. }
  139. """
  140. self.locations_sec_textb.setStyleSheet(stylesheet)
  141. self.distances_box.addWidget(self.locations_sec_textb)
  142. # Jump button
  143. self.locate_sec_button = QtWidgets.QPushButton(_("Jump to selected position"))
  144. self.locate_sec_button.setToolTip(
  145. _("Select a position in the Locations text box and then\n"
  146. "click this button.")
  147. )
  148. self.locate_sec_button.setMinimumWidth(60)
  149. self.locate_sec_button.setDisabled(True)
  150. self.distances_box.addWidget(self.locate_sec_button)
  151. # GO button
  152. self.calculate_button = QtWidgets.QPushButton(_("Find Minimum"))
  153. self.calculate_button.setToolTip(
  154. _("Calculate the minimum distance between copper features,\n"
  155. "this will allow the determination of the right tool to\n"
  156. "use for isolation or copper clearing.")
  157. )
  158. self.calculate_button.setMinimumWidth(60)
  159. self.layout.addWidget(self.calculate_button)
  160. self.loc_ois = OptionalHideInputSection(self.locations_cb, [self.locations_textb, self.locate_button])
  161. self.sec_loc_ois = OptionalHideInputSection(self.sec_locations_cb, [self.sec_locations_frame])
  162. # this is the line selected in the textbox with the locations of the minimum
  163. self.selected_text = ''
  164. # this is the line selected in the textbox with the locations of the other distances found in the Gerber object
  165. self.selected_locations_text = ''
  166. # dict to hold the distances between every two elements in Gerber as keys and the actual locations where that
  167. # distances happen as values
  168. self.min_dict = dict()
  169. # ## Signals
  170. self.calculate_button.clicked.connect(self.find_minimum_distance)
  171. self.locate_button.clicked.connect(self.on_locate_position)
  172. self.update_text.connect(self.on_update_text)
  173. self.locations_textb.cursorPositionChanged.connect(self.on_textbox_clicked)
  174. self.locate_sec_button.clicked.connect(self.on_locate_sec_position)
  175. self.update_sec_distances.connect(self.on_update_sec_distances_txt)
  176. self.distances_textb.cursorPositionChanged.connect(self.on_distances_textb_clicked)
  177. self.locations_sec_textb.cursorPositionChanged.connect(self.on_locations_sec_clicked)
  178. self.layout.addStretch()
  179. def install(self, icon=None, separator=None, **kwargs):
  180. FlatCAMTool.install(self, icon, separator, shortcut='ALT+O', **kwargs)
  181. def run(self, toggle=True):
  182. self.app.report_usage("ToolOptimal()")
  183. self.result_entry.set_value(0.0)
  184. self.freq_entry.set_value('0')
  185. if toggle:
  186. # if the splitter is hidden, display it, else hide it but only if the current widget is the same
  187. if self.app.ui.splitter.sizes()[0] == 0:
  188. self.app.ui.splitter.setSizes([1, 1])
  189. else:
  190. try:
  191. if self.app.ui.tool_scroll_area.widget().objectName() == self.toolName:
  192. # if tab is populated with the tool but it does not have the focus, focus on it
  193. if not self.app.ui.notebook.currentWidget() is self.app.ui.tool_tab:
  194. # focus on Tool Tab
  195. self.app.ui.notebook.setCurrentWidget(self.app.ui.tool_tab)
  196. else:
  197. self.app.ui.splitter.setSizes([0, 1])
  198. except AttributeError:
  199. pass
  200. else:
  201. if self.app.ui.splitter.sizes()[0] == 0:
  202. self.app.ui.splitter.setSizes([1, 1])
  203. FlatCAMTool.run(self)
  204. self.set_tool_ui()
  205. self.app.ui.notebook.setTabText(2, _("Optimal Tool"))
  206. def set_tool_ui(self):
  207. self.precision_spinner.set_value(int(self.decimals))
  208. self.locations_textb.clear()
  209. # new cursor - select all document
  210. cursor = self.locations_textb.textCursor()
  211. cursor.select(QtGui.QTextCursor.Document)
  212. # clear previous selection highlight
  213. tmp = cursor.blockFormat()
  214. tmp.clearBackground()
  215. cursor.setBlockFormat(tmp)
  216. self.locations_textb.setVisible(False)
  217. self.locate_button.setVisible(False)
  218. self.result_entry.set_value(0.0)
  219. self.freq_entry.set_value('0')
  220. self.reset_fields()
  221. def find_minimum_distance(self):
  222. self.units = self.app.ui.general_defaults_form.general_app_group.units_radio.get_value().upper()
  223. self.decimals = int(self.precision_spinner.get_value())
  224. selection_index = self.gerber_object_combo.currentIndex()
  225. model_index = self.app.collection.index(selection_index, 0, self.gerber_object_combo.rootModelIndex())
  226. try:
  227. fcobj = model_index.internalPointer().obj
  228. except Exception as e:
  229. log.debug("ToolOptimal.find_minimum_distance() --> %s" % str(e))
  230. self.app.inform.emit('[WARNING_NOTCL] %s' % _("There is no Gerber object loaded ..."))
  231. return
  232. if not isinstance(fcobj, FlatCAMGerber):
  233. self.app.inform.emit('[ERROR_NOTCL] %s' % _("Only Gerber objects can be evaluated."))
  234. return
  235. proc = self.app.proc_container.new(_("Working..."))
  236. def job_thread(app_obj):
  237. app_obj.inform.emit(_("Optimal Tool. Started to search for the minimum distance between copper features."))
  238. try:
  239. old_disp_number = 0
  240. pol_nr = 0
  241. app_obj.proc_container.update_view_text(' %d%%' % 0)
  242. total_geo = list()
  243. for ap in list(fcobj.apertures.keys()):
  244. if 'geometry' in fcobj.apertures[ap]:
  245. app_obj.inform.emit(
  246. '%s: %s' % (_("Optimal Tool. Parsing geometry for aperture"), str(ap)))
  247. for geo_el in fcobj.apertures[ap]['geometry']:
  248. if self.app.abort_flag:
  249. # graceful abort requested by the user
  250. raise FlatCAMApp.GracefulException
  251. if 'solid' in geo_el and geo_el['solid'] is not None and geo_el['solid'].is_valid:
  252. total_geo.append(geo_el['solid'])
  253. app_obj.inform.emit(
  254. _("Optimal Tool. Creating a buffer for the object geometry."))
  255. total_geo = MultiPolygon(total_geo)
  256. total_geo = total_geo.buffer(0)
  257. geo_len = len(total_geo)
  258. geo_len = (geo_len * (geo_len - 1)) / 2
  259. app_obj.inform.emit(
  260. '%s: %s' % (_("Optimal Tool. Finding the distances between each two elements. Iterations"),
  261. str(geo_len)))
  262. self.min_dict = dict()
  263. idx = 1
  264. for geo in total_geo:
  265. for s_geo in total_geo[idx:]:
  266. if self.app.abort_flag:
  267. # graceful abort requested by the user
  268. raise FlatCAMApp.GracefulException
  269. # minimize the number of distances by not taking into considerations those that are too small
  270. dist = geo.distance(s_geo)
  271. dist = float('%.*f' % (self.decimals, dist))
  272. loc_1, loc_2 = nearest_points(geo, s_geo)
  273. dx = loc_1.x - loc_2.x
  274. dy = loc_1.y - loc_2.y
  275. loc = (float('%.*f' % (self.decimals, (min(loc_1.x, loc_2.x) + (abs(dx) / 2)))),
  276. float('%.*f' % (self.decimals, (min(loc_1.y, loc_2.y) + (abs(dy) / 2)))))
  277. if dist in self.min_dict:
  278. self.min_dict[dist].append(loc)
  279. else:
  280. self.min_dict[dist] = [loc]
  281. pol_nr += 1
  282. disp_number = int(np.interp(pol_nr, [0, geo_len], [0, 100]))
  283. if old_disp_number < disp_number <= 100:
  284. app_obj.proc_container.update_view_text(' %d%%' % disp_number)
  285. old_disp_number = disp_number
  286. idx += 1
  287. app_obj.inform.emit(
  288. _("Optimal Tool. Finding the minimum distance."))
  289. min_list = list(self.min_dict.keys())
  290. min_dist = min(min_list)
  291. min_dist_string = '%.*f' % (self.decimals, float(min_dist))
  292. self.result_entry.set_value(min_dist_string)
  293. freq = len(self.min_dict[min_dist])
  294. freq = '%d' % int(freq)
  295. self.freq_entry.set_value(freq)
  296. min_locations = self.min_dict.pop(min_dist)
  297. self.update_text.emit(min_locations)
  298. self.update_sec_distances.emit(self.min_dict)
  299. app_obj.inform.emit('[success] %s' % _("Optimal Tool. Finished successfully."))
  300. except Exception as ee:
  301. proc.done()
  302. log.debug(str(ee))
  303. return
  304. proc.done()
  305. self.app.worker_task.emit({'fcn': job_thread, 'params': [self.app]})
  306. def on_locate_position(self):
  307. # cursor = self.locations_textb.textCursor()
  308. # self.selected_text = cursor.selectedText()
  309. try:
  310. loc = eval(self.selected_text)
  311. self.app.on_jump_to(custom_location=loc)
  312. except Exception as e:
  313. log.debug("ToolOptimal.on_locate_position() --> %s" % str(e))
  314. self.app.inform.emit("[ERROR_NOTCL] The selected text is no valid location in the format (x, y).")
  315. def on_update_text(self, data):
  316. txt = ''
  317. for loc in data:
  318. txt += '%s\n' % str(loc)
  319. self.locations_textb.setPlainText(txt)
  320. self.locate_button.setDisabled(False)
  321. def on_textbox_clicked(self):
  322. # new cursor - select all document
  323. cursor = self.locations_textb.textCursor()
  324. cursor.select(QtGui.QTextCursor.Document)
  325. # clear previous selection highlight
  326. tmp = cursor.blockFormat()
  327. tmp.clearBackground()
  328. cursor.setBlockFormat(tmp)
  329. # new cursor - select the current line
  330. cursor = self.locations_textb.textCursor()
  331. cursor.select(QtGui.QTextCursor.LineUnderCursor)
  332. # highlight the current selected line
  333. tmp = cursor.blockFormat()
  334. tmp.setBackground(QtGui.QBrush(QtCore.Qt.yellow))
  335. cursor.setBlockFormat(tmp)
  336. self.selected_text = cursor.selectedText()
  337. def on_update_sec_distances_txt(self, data):
  338. distance_list = sorted(list(data.keys()))
  339. txt = ''
  340. for loc in distance_list:
  341. txt += '%s\n' % str(loc)
  342. self.distances_textb.setPlainText(txt)
  343. self.locate_sec_button.setDisabled(False)
  344. def on_distances_textb_clicked(self):
  345. # new cursor - select all document
  346. cursor = self.distances_textb.textCursor()
  347. cursor.select(QtGui.QTextCursor.Document)
  348. # clear previous selection highlight
  349. tmp = cursor.blockFormat()
  350. tmp.clearBackground()
  351. cursor.setBlockFormat(tmp)
  352. # new cursor - select the current line
  353. cursor = self.distances_textb.textCursor()
  354. cursor.select(QtGui.QTextCursor.LineUnderCursor)
  355. # highlight the current selected line
  356. tmp = cursor.blockFormat()
  357. tmp.setBackground(QtGui.QBrush(QtCore.Qt.yellow))
  358. cursor.setBlockFormat(tmp)
  359. distance_text = cursor.selectedText()
  360. key_in_min_dict = eval(distance_text)
  361. self.on_update_locations_text(dist=key_in_min_dict)
  362. def on_update_locations_text(self, dist):
  363. distance_list = self.min_dict[dist]
  364. txt = ''
  365. for loc in distance_list:
  366. txt += '%s\n' % str(loc)
  367. self.locations_sec_textb.setPlainText(txt)
  368. def on_locations_sec_clicked(self):
  369. # new cursor - select all document
  370. cursor = self.locations_sec_textb.textCursor()
  371. cursor.select(QtGui.QTextCursor.Document)
  372. # clear previous selection highlight
  373. tmp = cursor.blockFormat()
  374. tmp.clearBackground()
  375. cursor.setBlockFormat(tmp)
  376. # new cursor - select the current line
  377. cursor = self.locations_sec_textb.textCursor()
  378. cursor.select(QtGui.QTextCursor.LineUnderCursor)
  379. # highlight the current selected line
  380. tmp = cursor.blockFormat()
  381. tmp.setBackground(QtGui.QBrush(QtCore.Qt.yellow))
  382. cursor.setBlockFormat(tmp)
  383. self.selected_locations_text = cursor.selectedText()
  384. def on_locate_sec_position(self):
  385. try:
  386. loc = eval(self.selected_locations_text)
  387. self.app.on_jump_to(custom_location=loc)
  388. except Exception as e:
  389. log.debug("ToolOptimal.on_locate_sec_position() --> %s" % str(e))
  390. self.app.inform.emit("[ERROR_NOTCL] The selected text is no valid location in the format (x, y).")
  391. def reset_fields(self):
  392. self.gerber_object_combo.setRootModelIndex(self.app.collection.index(0, 0, QtCore.QModelIndex()))
  393. self.gerber_object_combo.setCurrentIndex(0)