ToolCutOut.py 32 KB

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