ToolCutOut.py 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  1. from FlatCAMTool import FlatCAMTool
  2. from ObjectCollection import *
  3. from FlatCAMApp import *
  4. from shapely.geometry import box
  5. import gettext
  6. import FlatCAMTranslation as fcTranslate
  7. fcTranslate.apply_language('ToolCutOut')
  8. class CutOut(FlatCAMTool):
  9. toolName = _("Cutout PCB")
  10. gapFinished = pyqtSignal()
  11. def __init__(self, app):
  12. FlatCAMTool.__init__(self, app)
  13. self.app = app
  14. self.canvas = app.plotcanvas
  15. ## Title
  16. title_label = QtWidgets.QLabel("%s" % self.toolName)
  17. title_label.setStyleSheet("""
  18. QLabel
  19. {
  20. font-size: 16px;
  21. font-weight: bold;
  22. }
  23. """)
  24. self.layout.addWidget(title_label)
  25. ## Form Layout
  26. form_layout = QtWidgets.QFormLayout()
  27. self.layout.addLayout(form_layout)
  28. ## Type of object to be cutout
  29. self.type_obj_combo = QtWidgets.QComboBox()
  30. self.type_obj_combo.addItem("Gerber")
  31. self.type_obj_combo.addItem("Excellon")
  32. self.type_obj_combo.addItem("Geometry")
  33. # we get rid of item1 ("Excellon") as it is not suitable for creating film
  34. self.type_obj_combo.view().setRowHidden(1, True)
  35. self.type_obj_combo.setItemIcon(0, QtGui.QIcon("share/flatcam_icon16.png"))
  36. # self.type_obj_combo.setItemIcon(1, QtGui.QIcon("share/drill16.png"))
  37. self.type_obj_combo.setItemIcon(2, QtGui.QIcon("share/geometry16.png"))
  38. self.type_obj_combo_label = QtWidgets.QLabel(_("Obj Type:"))
  39. self.type_obj_combo_label.setToolTip(
  40. _("Specify the type of object to be cutout.\n"
  41. "It can be of type: Gerber or Geometry.\n"
  42. "What is selected here will dictate the kind\n"
  43. "of objects that will populate the 'Object' combobox.")
  44. )
  45. self.type_obj_combo_label.setFixedWidth(60)
  46. form_layout.addRow(self.type_obj_combo_label, self.type_obj_combo)
  47. ## Object to be cutout
  48. self.obj_combo = QtWidgets.QComboBox()
  49. self.obj_combo.setModel(self.app.collection)
  50. self.obj_combo.setRootModelIndex(self.app.collection.index(0, 0, QtCore.QModelIndex()))
  51. self.obj_combo.setCurrentIndex(1)
  52. self.object_label = QtWidgets.QLabel(_("Object:"))
  53. self.object_label.setToolTip(
  54. _("Object to be cutout. ")
  55. )
  56. form_layout.addRow(self.object_label, self.obj_combo)
  57. # Tool Diameter
  58. self.dia = FCEntry()
  59. self.dia_label = QtWidgets.QLabel(_("Tool Dia:"))
  60. self.dia_label.setToolTip(
  61. _( "Diameter of the tool used to cutout\n"
  62. "the PCB shape out of the surrounding material.")
  63. )
  64. form_layout.addRow(self.dia_label, self.dia)
  65. # Margin
  66. self.margin = FCEntry()
  67. self.margin_label = QtWidgets.QLabel(_("Margin:"))
  68. self.margin_label.setToolTip(
  69. _( "Margin over bounds. A positive value here\n"
  70. "will make the cutout of the PCB further from\n"
  71. "the actual PCB border")
  72. )
  73. form_layout.addRow(self.margin_label, self.margin)
  74. # Gapsize
  75. self.gapsize = FCEntry()
  76. self.gapsize_label = QtWidgets.QLabel(_("Gap size:"))
  77. self.gapsize_label.setToolTip(
  78. _( "The size of the bridge gaps in the cutout\n"
  79. "used to keep the board connected to\n"
  80. "the surrounding material (the one \n"
  81. "from which the PCB is cutout).")
  82. )
  83. form_layout.addRow(self.gapsize_label, self.gapsize)
  84. # How gaps wil be rendered:
  85. # lr - left + right
  86. # tb - top + bottom
  87. # 4 - left + right +top + bottom
  88. # 2lr - 2*left + 2*right
  89. # 2tb - 2*top + 2*bottom
  90. # 8 - 2*left + 2*right +2*top + 2*bottom
  91. ## Title2
  92. title_param_label = QtWidgets.QLabel("<font size=4><b>%s</b></font>" % _('A. Automatic Bridge Gaps'))
  93. title_param_label.setToolTip(
  94. _("This section handle creation of automatic bridge gaps.")
  95. )
  96. self.layout.addWidget(title_param_label)
  97. ## Form Layout
  98. form_layout_2 = QtWidgets.QFormLayout()
  99. self.layout.addLayout(form_layout_2)
  100. # Gaps
  101. gaps_label = QtWidgets.QLabel(_('Gaps:'))
  102. gaps_label.setToolTip(
  103. _("Number of gaps used for the Automatic cutout.\n"
  104. "There can be maximum 8 bridges/gaps.\n"
  105. "The choices are:\n"
  106. "- lr - left + right\n"
  107. "- tb - top + bottom\n"
  108. "- 4 - left + right +top + bottom\n"
  109. "- 2lr - 2*left + 2*right\n"
  110. "- 2tb - 2*top + 2*bottom\n"
  111. "- 8 - 2*left + 2*right +2*top + 2*bottom")
  112. )
  113. gaps_label.setFixedWidth(60)
  114. self.gaps = FCComboBox()
  115. gaps_items = ['LR', 'TB', '4', '2LR', '2TB', '8']
  116. for it in gaps_items:
  117. self.gaps.addItem(it)
  118. self.gaps.setStyleSheet('background-color: rgb(255,255,255)')
  119. form_layout_2.addRow(gaps_label, self.gaps)
  120. ## Buttons
  121. hlay = QtWidgets.QHBoxLayout()
  122. self.layout.addLayout(hlay)
  123. title_ff_label = QtWidgets.QLabel("<b>%s</b>" % _('FreeForm:'))
  124. title_ff_label.setToolTip(
  125. _("The cutout shape can be of ny shape.\n"
  126. "Useful when the PCB has a non-rectangular shape.")
  127. )
  128. hlay.addWidget(title_ff_label)
  129. hlay.addStretch()
  130. self.ff_cutout_object_btn = QtWidgets.QPushButton(_("Generate Geo"))
  131. self.ff_cutout_object_btn.setToolTip(
  132. _("Cutout the selected object.\n"
  133. "The cutout shape can be of any shape.\n"
  134. "Useful when the PCB has a non-rectangular shape.")
  135. )
  136. hlay.addWidget(self.ff_cutout_object_btn)
  137. hlay2 = QtWidgets.QHBoxLayout()
  138. self.layout.addLayout(hlay2)
  139. title_rct_label = QtWidgets.QLabel("<b>%s</b>" % _('Rectangular:'))
  140. title_rct_label.setToolTip(
  141. _("The resulting cutout shape is\n"
  142. "always a rectangle shape and it will be\n"
  143. "the bounding box of the Object.")
  144. )
  145. hlay2.addWidget(title_rct_label)
  146. hlay2.addStretch()
  147. self.rect_cutout_object_btn = QtWidgets.QPushButton(_("Generate Geo"))
  148. self.rect_cutout_object_btn.setToolTip(
  149. _("Cutout the selected object.\n"
  150. "The resulting cutout shape is\n"
  151. "always a rectangle shape and it will be\n"
  152. "the bounding box of the Object.")
  153. )
  154. hlay2.addWidget(self.rect_cutout_object_btn)
  155. ## Title5
  156. title_manual_label = QtWidgets.QLabel("<font size=4><b>%s</b></font>" % _('B. Manual Bridge Gaps'))
  157. title_manual_label.setToolTip(
  158. _("This section handle creation of manual bridge gaps.\n"
  159. "This is done by mouse clicking on the perimeter of the\n"
  160. "Geometry object that is used as a cutout object. ")
  161. )
  162. self.layout.addWidget(title_manual_label)
  163. ## Form Layout
  164. form_layout_3 = QtWidgets.QFormLayout()
  165. self.layout.addLayout(form_layout_3)
  166. ## Manual Geo Object
  167. self.man_object_combo = QtWidgets.QComboBox()
  168. self.man_object_combo.setModel(self.app.collection)
  169. self.man_object_combo.setRootModelIndex(self.app.collection.index(2, 0, QtCore.QModelIndex()))
  170. self.man_object_combo.setCurrentIndex(1)
  171. self.man_object_label = QtWidgets.QLabel(_("Geo Obj:"))
  172. self.man_object_label.setToolTip(
  173. _("Geometry object used to create the manual cutout.")
  174. )
  175. self.man_object_label.setFixedWidth(60)
  176. # e_lab_0 = QtWidgets.QLabel('')
  177. form_layout_3.addRow(self.man_object_label, self.man_object_combo)
  178. # form_layout_3.addRow(e_lab_0)
  179. hlay3 = QtWidgets.QHBoxLayout()
  180. self.layout.addLayout(hlay3)
  181. self.man_geo_label = QtWidgets.QLabel(_("Manual Geo:"))
  182. self.man_geo_label.setToolTip(
  183. _("If the object to be cutout is a Gerber\n"
  184. "first create a Geometry that surrounds it,\n"
  185. "to be used as the cutout, if one doesn't exist yet.\n"
  186. "Select the source Gerber file in the top object combobox.")
  187. )
  188. hlay3.addWidget(self.man_geo_label)
  189. hlay3.addStretch()
  190. self.man_geo_creation_btn = QtWidgets.QPushButton(_("Generate Geo"))
  191. self.man_geo_creation_btn.setToolTip(
  192. _("If the object to be cutout is a Gerber\n"
  193. "first create a Geometry that surrounds it,\n"
  194. "to be used as the cutout, if one doesn't exist yet.\n"
  195. "Select the source Gerber file in the top object combobox.")
  196. )
  197. hlay3.addWidget(self.man_geo_creation_btn)
  198. hlay4 = QtWidgets.QHBoxLayout()
  199. self.layout.addLayout(hlay4)
  200. self.man_bridge_gaps_label = QtWidgets.QLabel(_("Manual Add Bridge Gaps:"))
  201. self.man_bridge_gaps_label.setToolTip(
  202. _("Use the left mouse button (LMB) click\n"
  203. "to create a bridge gap to separate the PCB from\n"
  204. "the surrounding material.")
  205. )
  206. hlay4.addWidget(self.man_bridge_gaps_label)
  207. hlay4.addStretch()
  208. self.man_gaps_creation_btn = QtWidgets.QPushButton(_("Generate Gap"))
  209. self.man_gaps_creation_btn.setToolTip(
  210. _("Use the left mouse button (LMB) click\n"
  211. "to create a bridge gap to separate the PCB from\n"
  212. "the surrounding material.\n"
  213. "The LMB click has to be done on the perimeter of\n"
  214. "the Geometry object used as a cutout geometry.")
  215. )
  216. hlay4.addWidget(self.man_gaps_creation_btn)
  217. self.layout.addStretch()
  218. self.cutting_gapsize = 0.0
  219. self.cutting_dia = 0.0
  220. # true if we want to repeat the gap without clicking again on the button
  221. self.repeat_gap = False
  222. def on_type_obj_index_changed(self, index):
  223. obj_type = self.type_obj_combo.currentIndex()
  224. self.obj_combo.setRootModelIndex(self.app.collection.index(obj_type, 0, QtCore.QModelIndex()))
  225. self.obj_combo.setCurrentIndex(0)
  226. def run(self, toggle=False):
  227. self.app.report_usage("ToolCutOut()")
  228. if toggle:
  229. # if the splitter is hidden, display it, else hide it but only if the current widget is the same
  230. if self.app.ui.splitter.sizes()[0] == 0:
  231. self.app.ui.splitter.setSizes([1, 1])
  232. else:
  233. try:
  234. if self.app.ui.tool_scroll_area.widget().objectName() == self.toolName:
  235. self.app.ui.splitter.setSizes([0, 1])
  236. except AttributeError:
  237. pass
  238. FlatCAMTool.run(self)
  239. self.set_tool_ui()
  240. self.app.ui.notebook.setTabText(2, "Cutout Tool")
  241. def install(self, icon=None, separator=None, **kwargs):
  242. FlatCAMTool.install(self, icon, separator, shortcut='ALT+U', **kwargs)
  243. def set_tool_ui(self):
  244. self.reset_fields()
  245. self.dia.set_value(float(self.app.defaults["tools_cutouttooldia"]))
  246. self.margin.set_value(float(self.app.defaults["tools_cutoutmargin"]))
  247. self.gapsize.set_value(float(self.app.defaults["tools_cutoutgapsize"]))
  248. self.gaps.set_value(4)
  249. ## Signals
  250. self.ff_cutout_object_btn.clicked.connect(self.on_freeform_cutout)
  251. self.rect_cutout_object_btn.clicked.connect(self.on_rectangular_cutout)
  252. self.type_obj_combo.currentIndexChanged.connect(self.on_type_obj_index_changed)
  253. self.man_geo_creation_btn.clicked.connect(self.on_manual_geo)
  254. self.man_gaps_creation_btn.clicked.connect(self.on_manual_gap_click)
  255. self.gapFinished.connect(self.on_gap_finished)
  256. def on_freeform_cutout(self):
  257. def subtract_rectangle(obj_, x0, y0, x1, y1):
  258. pts = [(x0, y0), (x1, y0), (x1, y1), (x0, y1)]
  259. obj_.subtract_polygon(pts)
  260. name = self.obj_combo.currentText()
  261. # Get source object.
  262. try:
  263. cutout_obj = self.app.collection.get_by_name(str(name))
  264. except:
  265. self.app.inform.emit(_("[ERROR_NOTCL]Could not retrieve object: %s") % name)
  266. return "Could not retrieve object: %s" % name
  267. if cutout_obj is None:
  268. self.app.inform.emit(_("[ERROR_NOTCL]There is no object selected for Cutout.\nSelect one and try again."))
  269. return
  270. try:
  271. dia = float(self.dia.get_value())
  272. except ValueError:
  273. # try to convert comma to decimal point. if it's still not working error message and return
  274. try:
  275. dia = float(self.dia.get_value().replace(',', '.'))
  276. except ValueError:
  277. self.app.inform.emit(_("[WARNING_NOTCL] Tool diameter value is missing or wrong format. "
  278. "Add it and retry."))
  279. return
  280. if 0 in {dia}:
  281. self.app.inform.emit(_("[WARNING_NOTCL]Tool Diameter is zero value. Change it to a positive integer."))
  282. return "Tool Diameter is zero value. Change it to a positive integer."
  283. try:
  284. margin = float(self.margin.get_value())
  285. except ValueError:
  286. # try to convert comma to decimal point. if it's still not working error message and return
  287. try:
  288. margin = float(self.margin.get_value().replace(',', '.'))
  289. except ValueError:
  290. self.app.inform.emit(_("[WARNING_NOTCL] Margin value is missing or wrong format. "
  291. "Add it and retry."))
  292. return
  293. try:
  294. gapsize = float(self.gapsize.get_value())
  295. except ValueError:
  296. # try to convert comma to decimal point. if it's still not working error message and return
  297. try:
  298. gapsize = float(self.gapsize.get_value().replace(',', '.'))
  299. except ValueError:
  300. self.app.inform.emit(_("[WARNING_NOTCL] Gap size value is missing or wrong format. "
  301. "Add it and retry."))
  302. return
  303. try:
  304. gaps = self.gaps.get_value()
  305. except TypeError:
  306. self.app.inform.emit(_("[WARNING_NOTCL] Number of gaps value is missing. Add it and retry."))
  307. return
  308. if gaps not in ['LR', 'TB', '2LR', '2TB', '4', '8']:
  309. self.app.inform.emit(_("[WARNING_NOTCL] Gaps value can be only one of: 'lr', 'tb', '2lr', '2tb', 4 or 8. "
  310. "Fill in a correct value and retry. "))
  311. return
  312. if cutout_obj.multigeo is True:
  313. self.app.inform.emit(_("[ERROR]Cutout operation cannot be done on a multi-geo Geometry.\n"
  314. "Optionally, this Multi-geo Geometry can be converted to Single-geo Geometry,\n"
  315. "and after that perform Cutout."))
  316. return
  317. # Get min and max data for each object as we just cut rectangles across X or Y
  318. xmin, ymin, xmax, ymax = cutout_obj.bounds()
  319. px = 0.5 * (xmin + xmax) + margin
  320. py = 0.5 * (ymin + ymax) + margin
  321. lenghtx = (xmax - xmin) + (margin * 2)
  322. lenghty = (ymax - ymin) + (margin * 2)
  323. gapsize = gapsize / 2 + (dia / 2)
  324. if isinstance(cutout_obj,FlatCAMGeometry):
  325. # rename the obj name so it can be identified as cutout
  326. cutout_obj.options["name"] += "_cutout"
  327. else:
  328. def geo_init(geo_obj, app_obj):
  329. geo = cutout_obj.solid_geometry.convex_hull
  330. geo_obj.solid_geometry = geo.buffer(margin + abs(dia / 2))
  331. outname = cutout_obj.options["name"] + "_cutout"
  332. self.app.new_object('geometry', outname, geo_init)
  333. cutout_obj = self.app.collection.get_by_name(outname)
  334. if gaps == '8' or gaps == '2LR':
  335. subtract_rectangle(cutout_obj,
  336. xmin - gapsize, # botleft_x
  337. py - gapsize + lenghty / 4, # botleft_y
  338. xmax + gapsize, # topright_x
  339. py + gapsize + lenghty / 4) # topright_y
  340. subtract_rectangle(cutout_obj,
  341. xmin - gapsize,
  342. py - gapsize - lenghty / 4,
  343. xmax + gapsize,
  344. py + gapsize - lenghty / 4)
  345. if gaps == '8' or gaps == '2TB':
  346. subtract_rectangle(cutout_obj,
  347. px - gapsize + lenghtx / 4,
  348. ymin - gapsize,
  349. px + gapsize + lenghtx / 4,
  350. ymax + gapsize)
  351. subtract_rectangle(cutout_obj,
  352. px - gapsize - lenghtx / 4,
  353. ymin - gapsize,
  354. px + gapsize - lenghtx / 4,
  355. ymax + gapsize)
  356. if gaps == '4' or gaps == 'LR':
  357. subtract_rectangle(cutout_obj,
  358. xmin - gapsize,
  359. py - gapsize,
  360. xmax + gapsize,
  361. py + gapsize)
  362. if gaps == '4' or gaps == 'TB':
  363. subtract_rectangle(cutout_obj,
  364. px - gapsize,
  365. ymin - gapsize,
  366. px + gapsize,
  367. ymax + gapsize)
  368. cutout_obj.plot()
  369. self.app.inform.emit(_("[success] Any form CutOut operation finished."))
  370. self.app.ui.notebook.setCurrentWidget(self.app.ui.project_tab)
  371. self.app.should_we_save = True
  372. def on_rectangular_cutout(self):
  373. def subtract_rectangle(obj_, x0, y0, x1, y1):
  374. pts = [(x0, y0), (x1, y0), (x1, y1), (x0, y1)]
  375. obj_.subtract_polygon(pts)
  376. name = self.obj_combo.currentText()
  377. # Get source object.
  378. try:
  379. cutout_obj = self.app.collection.get_by_name(str(name))
  380. except:
  381. self.app.inform.emit(_("[ERROR_NOTCL]Could not retrieve object: %s") % name)
  382. return "Could not retrieve object: %s" % name
  383. if cutout_obj is None:
  384. self.app.inform.emit(_("[ERROR_NOTCL]Object not found: %s") % cutout_obj)
  385. try:
  386. dia = float(self.dia.get_value())
  387. except ValueError:
  388. # try to convert comma to decimal point. if it's still not working error message and return
  389. try:
  390. dia = float(self.dia.get_value().replace(',', '.'))
  391. except ValueError:
  392. self.app.inform.emit(_("[WARNING_NOTCL] Tool diameter value is missing or wrong format. "
  393. "Add it and retry."))
  394. return
  395. if 0 in {dia}:
  396. self.app.inform.emit(_("[ERROR_NOTCL]Tool Diameter is zero value. Change it to a positive integer."))
  397. return "Tool Diameter is zero value. Change it to a positive integer."
  398. try:
  399. margin = float(self.margin.get_value())
  400. except ValueError:
  401. # try to convert comma to decimal point. if it's still not working error message and return
  402. try:
  403. margin = float(self.margin.get_value().replace(',', '.'))
  404. except ValueError:
  405. self.app.inform.emit(_("[WARNING_NOTCL] Margin value is missing or wrong format. "
  406. "Add it and retry."))
  407. return
  408. try:
  409. gapsize = float(self.gapsize.get_value())
  410. except ValueError:
  411. # try to convert comma to decimal point. if it's still not working error message and return
  412. try:
  413. gapsize = float(self.gapsize.get_value().replace(',', '.'))
  414. except ValueError:
  415. self.app.inform.emit(_("[WARNING_NOTCL] Gap size value is missing or wrong format. "
  416. "Add it and retry."))
  417. return
  418. try:
  419. gaps = self.gaps.get_value()
  420. except TypeError:
  421. self.app.inform.emit(_("[WARNING_NOTCL] Number of gaps value is missing. Add it and retry."))
  422. return
  423. if gaps not in ['LR', 'TB', '2LR', '2TB', '4', '8']:
  424. self.app.inform.emit(_("[WARNING_NOTCL] Gaps value can be only one of: 'lr', 'tb', '2lr', '2tb', 4 or 8. "
  425. "Fill in a correct value and retry. "))
  426. return
  427. if cutout_obj.multigeo is True:
  428. self.app.inform.emit(_("[ERROR]Cutout operation cannot be done on a multi-geo Geometry.\n"
  429. "Optionally, this Multi-geo Geometry can be converted to Single-geo Geometry,\n"
  430. "and after that perform Cutout."))
  431. return
  432. # Get min and max data for each object as we just cut rectangles across X or Y
  433. xmin, ymin, xmax, ymax = cutout_obj.bounds()
  434. geo = box(xmin, ymin, xmax, ymax)
  435. px = 0.5 * (xmin + xmax) + margin
  436. py = 0.5 * (ymin + ymax) + margin
  437. lenghtx = (xmax - xmin) + (margin * 2)
  438. lenghty = (ymax - ymin) + (margin * 2)
  439. gapsize = gapsize / 2 + (dia / 2)
  440. def geo_init(geo_obj, app_obj):
  441. geo_obj.solid_geometry = geo.buffer(margin + abs(dia / 2))
  442. outname = cutout_obj.options["name"] + "_cutout"
  443. self.app.new_object('geometry', outname, geo_init)
  444. cutout_obj = self.app.collection.get_by_name(outname)
  445. if gaps == '8' or gaps == '2LR':
  446. subtract_rectangle(cutout_obj,
  447. xmin - gapsize, # botleft_x
  448. py - gapsize + lenghty / 4, # botleft_y
  449. xmax + gapsize, # topright_x
  450. py + gapsize + lenghty / 4) # topright_y
  451. subtract_rectangle(cutout_obj,
  452. xmin - gapsize,
  453. py - gapsize - lenghty / 4,
  454. xmax + gapsize,
  455. py + gapsize - lenghty / 4)
  456. if gaps == '8' or gaps == '2TB':
  457. subtract_rectangle(cutout_obj,
  458. px - gapsize + lenghtx / 4,
  459. ymin - gapsize,
  460. px + gapsize + lenghtx / 4,
  461. ymax + gapsize)
  462. subtract_rectangle(cutout_obj,
  463. px - gapsize - lenghtx / 4,
  464. ymin - gapsize,
  465. px + gapsize - lenghtx / 4,
  466. ymax + gapsize)
  467. if gaps == '4' or gaps == 'LR':
  468. subtract_rectangle(cutout_obj,
  469. xmin - gapsize,
  470. py - gapsize,
  471. xmax + gapsize,
  472. py + gapsize)
  473. if gaps == '4' or gaps == 'TB':
  474. subtract_rectangle(cutout_obj,
  475. px - gapsize,
  476. ymin - gapsize,
  477. px + gapsize,
  478. ymax + gapsize)
  479. cutout_obj.plot()
  480. self.app.inform.emit(_("[success] Any form CutOut operation finished."))
  481. self.app.ui.notebook.setCurrentWidget(self.app.ui.project_tab)
  482. self.app.should_we_save = True
  483. def on_manual_gap_click(self):
  484. self.app.inform.emit(_("Click on the selected geometry object perimeter to create a bridge gap ..."))
  485. self.app.geo_editor.tool_shape.enabled = True
  486. try:
  487. self.cutting_dia = float(self.dia.get_value())
  488. except ValueError:
  489. # try to convert comma to decimal point. if it's still not working error message and return
  490. try:
  491. self.cutting_dia = float(self.dia.get_value().replace(',', '.'))
  492. except ValueError:
  493. self.app.inform.emit(_("[WARNING_NOTCL] Tool diameter value is missing or wrong format. "
  494. "Add it and retry."))
  495. return
  496. if 0 in {self.cutting_dia}:
  497. self.app.inform.emit(_("[ERROR_NOTCL]Tool Diameter is zero value. Change it to a positive integer."))
  498. return "Tool Diameter is zero value. Change it to a positive integer."
  499. try:
  500. self.cutting_gapsize = float(self.gapsize.get_value())
  501. except ValueError:
  502. # try to convert comma to decimal point. if it's still not working error message and return
  503. try:
  504. self.cutting_gapsize = float(self.gapsize.get_value().replace(',', '.'))
  505. except ValueError:
  506. self.app.inform.emit(_("[WARNING_NOTCL] Gap size value is missing or wrong format. "
  507. "Add it and retry."))
  508. return
  509. self.app.plotcanvas.vis_disconnect('key_press', self.app.ui.keyPressEvent)
  510. self.app.plotcanvas.vis_disconnect('mouse_press', self.app.on_mouse_click_over_plot)
  511. self.app.plotcanvas.vis_disconnect('mouse_release', self.app.on_mouse_click_release_over_plot)
  512. self.app.plotcanvas.vis_disconnect('mouse_move', self.app.on_mouse_move_over_plot)
  513. self.app.plotcanvas.vis_connect('key_press', self.on_key_press)
  514. self.app.plotcanvas.vis_connect('mouse_move', self.on_mouse_move)
  515. self.app.plotcanvas.vis_connect('mouse_release', self.doit)
  516. # To be called after clicking on the plot.
  517. def doit(self, event):
  518. # do paint single only for left mouse clicks
  519. if event.button == 1:
  520. self.app.inform.emit(_("Making manual bridge gap..."))
  521. pos = self.app.plotcanvas.vispy_canvas.translate_coords(event.pos)
  522. self.on_manual_cutout(click_pos=pos)
  523. self.app.plotcanvas.vis_disconnect('key_press', self.on_key_press)
  524. self.app.plotcanvas.vis_disconnect('mouse_move', self.on_mouse_move)
  525. self.app.plotcanvas.vis_disconnect('mouse_release', self.doit)
  526. self.app.plotcanvas.vis_connect('key_press', self.app.ui.keyPressEvent)
  527. self.app.plotcanvas.vis_connect('mouse_press', self.app.on_mouse_click_over_plot)
  528. self.app.plotcanvas.vis_connect('mouse_release', self.app.on_mouse_click_release_over_plot)
  529. self.app.plotcanvas.vis_connect('mouse_move', self.app.on_mouse_move_over_plot)
  530. self.app.geo_editor.tool_shape.clear(update=True)
  531. self.app.geo_editor.tool_shape.enabled = False
  532. self.gapFinished.emit()
  533. def on_manual_cutout(self, click_pos):
  534. name = self.man_object_combo.currentText()
  535. # Get source object.
  536. try:
  537. cutout_obj = self.app.collection.get_by_name(str(name))
  538. except:
  539. self.app.inform.emit(_("[ERROR_NOTCL]Could not retrieve Geoemtry object: %s") % name)
  540. return "Could not retrieve object: %s" % name
  541. if cutout_obj is None:
  542. self.app.inform.emit(_("[ERROR_NOTCL]Geometry object for manual cutout not found: %s") % cutout_obj)
  543. return
  544. # use the snapped position as reference
  545. snapped_pos = self.app.geo_editor.snap(click_pos[0], click_pos[1])
  546. cut_poly = self.cutting_geo(pos=(snapped_pos[0], snapped_pos[1]))
  547. cutout_obj.subtract_polygon(cut_poly)
  548. cutout_obj.plot()
  549. self.app.inform.emit(_("[success] Added manual Bridge Gap."))
  550. self.app.should_we_save = True
  551. def on_gap_finished(self):
  552. # if CTRL key modifier is pressed then repeat the bridge gap cut
  553. key_modifier = QtWidgets.QApplication.keyboardModifiers()
  554. if key_modifier == Qt.ControlModifier:
  555. self.on_manual_gap_click()
  556. def on_manual_geo(self):
  557. name = self.obj_combo.currentText()
  558. # Get source object.
  559. try:
  560. cutout_obj = self.app.collection.get_by_name(str(name))
  561. except:
  562. self.app.inform.emit(_("[ERROR_NOTCL]Could not retrieve Gerber object: %s") % name)
  563. return "Could not retrieve object: %s" % name
  564. if cutout_obj is None:
  565. self.app.inform.emit(_("[ERROR_NOTCL]There is no Gerber object selected for Cutout.\n"
  566. "Select one and try again."))
  567. return
  568. if not isinstance(cutout_obj, FlatCAMGerber):
  569. self.app.inform.emit(_("[ERROR_NOTCL]The selected object has to be of Gerber type.\n"
  570. "Select a Gerber file and try again."))
  571. return
  572. try:
  573. dia = float(self.dia.get_value())
  574. except ValueError:
  575. # try to convert comma to decimal point. if it's still not working error message and return
  576. try:
  577. dia = float(self.dia.get_value().replace(',', '.'))
  578. except ValueError:
  579. self.app.inform.emit(_("[WARNING_NOTCL] Tool diameter value is missing or wrong format. "
  580. "Add it and retry."))
  581. return
  582. if 0 in {dia}:
  583. self.app.inform.emit(_("[ERROR_NOTCL]Tool Diameter is zero value. Change it to a positive integer."))
  584. return "Tool Diameter is zero value. Change it to a positive integer."
  585. try:
  586. margin = float(self.margin.get_value())
  587. except ValueError:
  588. # try to convert comma to decimal point. if it's still not working error message and return
  589. try:
  590. margin = float(self.margin.get_value().replace(',', '.'))
  591. except ValueError:
  592. self.app.inform.emit(_("[WARNING_NOTCL] Margin value is missing or wrong format. "
  593. "Add it and retry."))
  594. return
  595. def geo_init(geo_obj, app_obj):
  596. geo = cutout_obj.solid_geometry.convex_hull
  597. geo_obj.solid_geometry = geo.buffer(margin + abs(dia / 2))
  598. outname = cutout_obj.options["name"] + "_cutout"
  599. self.app.new_object('geometry', outname, geo_init)
  600. def cutting_geo(self, pos):
  601. self.cutting_gapsize = self.cutting_gapsize / 2 + (self.cutting_dia / 2)
  602. offset = self.cutting_gapsize / 2
  603. # cutting area definition
  604. orig_x = pos[0]
  605. orig_y = pos[1]
  606. xmin = orig_x - offset
  607. ymin = orig_y - offset
  608. xmax = orig_x + offset
  609. ymax = orig_y + offset
  610. cut_poly = box(xmin, ymin, xmax, ymax)
  611. return cut_poly
  612. def on_mouse_move(self, event):
  613. self.app.on_mouse_move_over_plot(event=event)
  614. pos = self.canvas.vispy_canvas.translate_coords(event.pos)
  615. event.xdata, event.ydata = pos[0], pos[1]
  616. try:
  617. x = float(event.xdata)
  618. y = float(event.ydata)
  619. except TypeError:
  620. return
  621. snap_x, snap_y = self.app.geo_editor.snap(x, y)
  622. geo = self.cutting_geo(pos=(snap_x, snap_y))
  623. # Remove any previous utility shape
  624. self.app.geo_editor.tool_shape.clear(update=True)
  625. self.draw_utility_geometry(geo=geo)
  626. def draw_utility_geometry(self, geo):
  627. self.app.geo_editor.tool_shape.add(
  628. shape=geo,
  629. color=(self.app.defaults["global_draw_color"] + '80'),
  630. update=False,
  631. layer=0,
  632. tolerance=None)
  633. self.app.geo_editor.tool_shape.redraw()
  634. def on_key_press(self, event):
  635. # events out of the self.app.collection view (it's about Project Tab) are of type int
  636. if type(event) is int:
  637. key = event
  638. # events from the GUI are of type QKeyEvent
  639. elif type(event) == QtGui.QKeyEvent:
  640. key = event.key()
  641. # events from Vispy are of type KeyEvent
  642. else:
  643. key = event.key
  644. # Escape = Deselect All
  645. if key == QtCore.Qt.Key_Escape or key == 'Escape':
  646. self.app.plotcanvas.vis_disconnect('key_press', self.on_key_press)
  647. self.app.plotcanvas.vis_disconnect('mouse_move', self.on_mouse_move)
  648. self.app.plotcanvas.vis_disconnect('mouse_release', self.doit)
  649. self.app.plotcanvas.vis_connect('key_press', self.app.ui.keyPressEvent)
  650. self.app.plotcanvas.vis_connect('mouse_press', self.app.on_mouse_click_over_plot)
  651. self.app.plotcanvas.vis_connect('mouse_release', self.app.on_mouse_click_release_over_plot)
  652. self.app.plotcanvas.vis_connect('mouse_move', self.app.on_mouse_move_over_plot)
  653. # Remove any previous utility shape
  654. self.app.geo_editor.tool_shape.clear(update=True)
  655. self.app.geo_editor.tool_shape.enabled = False
  656. def reset_fields(self):
  657. self.obj_combo.setRootModelIndex(self.app.collection.index(0, 0, QtCore.QModelIndex()))