ToolCutOut.py 32 KB

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