ToolCutOut.py 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  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. if toggle:
  238. # if the splitter is hidden, display it, else hide it but only if the current widget is the same
  239. if self.app.ui.splitter.sizes()[0] == 0:
  240. self.app.ui.splitter.setSizes([1, 1])
  241. else:
  242. try:
  243. if self.app.ui.tool_scroll_area.widget().objectName() == self.toolName:
  244. self.app.ui.splitter.setSizes([0, 1])
  245. except AttributeError:
  246. pass
  247. FlatCAMTool.run(self)
  248. self.set_tool_ui()
  249. self.app.ui.notebook.setTabText(2, "Cutout Tool")
  250. def install(self, icon=None, separator=None, **kwargs):
  251. FlatCAMTool.install(self, icon, separator, shortcut='ALT+U', **kwargs)
  252. def set_tool_ui(self):
  253. self.reset_fields()
  254. self.dia.set_value(float(self.app.defaults["tools_cutouttooldia"]))
  255. self.margin.set_value(float(self.app.defaults["tools_cutoutmargin"]))
  256. self.gapsize.set_value(float(self.app.defaults["tools_cutoutgapsize"]))
  257. self.gaps.set_value(4)
  258. self.gapFinished.connect(self.on_gap_finished)
  259. def on_freeform_cutout(self):
  260. def subtract_rectangle(obj_, x0, y0, x1, y1):
  261. pts = [(x0, y0), (x1, y0), (x1, y1), (x0, y1)]
  262. obj_.subtract_polygon(pts)
  263. name = self.obj_combo.currentText()
  264. # Get source object.
  265. try:
  266. cutout_obj = self.app.collection.get_by_name(str(name))
  267. except:
  268. self.app.inform.emit(_("[ERROR_NOTCL]Could not retrieve object: %s") % name)
  269. return "Could not retrieve object: %s" % name
  270. if cutout_obj is None:
  271. self.app.inform.emit(_("[ERROR_NOTCL]There is no object selected for Cutout.\nSelect one and try again."))
  272. return
  273. try:
  274. dia = float(self.dia.get_value())
  275. except ValueError:
  276. # try to convert comma to decimal point. if it's still not working error message and return
  277. try:
  278. dia = float(self.dia.get_value().replace(',', '.'))
  279. except ValueError:
  280. self.app.inform.emit(_("[WARNING_NOTCL] Tool diameter value is missing or wrong format. "
  281. "Add it and retry."))
  282. return
  283. if 0 in {dia}:
  284. self.app.inform.emit(_("[WARNING_NOTCL]Tool Diameter is zero value. Change it to a positive integer."))
  285. return "Tool Diameter is zero value. Change it to a positive integer."
  286. try:
  287. margin = float(self.margin.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. margin = float(self.margin.get_value().replace(',', '.'))
  292. except ValueError:
  293. self.app.inform.emit(_("[WARNING_NOTCL] Margin value is missing or wrong format. "
  294. "Add it and retry."))
  295. return
  296. try:
  297. gapsize = float(self.gapsize.get_value())
  298. except ValueError:
  299. # try to convert comma to decimal point. if it's still not working error message and return
  300. try:
  301. gapsize = float(self.gapsize.get_value().replace(',', '.'))
  302. except ValueError:
  303. self.app.inform.emit(_("[WARNING_NOTCL] Gap size value is missing or wrong format. "
  304. "Add it and retry."))
  305. return
  306. try:
  307. gaps = self.gaps.get_value()
  308. except TypeError:
  309. self.app.inform.emit(_("[WARNING_NOTCL] Number of gaps value is missing. Add it and retry."))
  310. return
  311. if gaps not in ['LR', 'TB', '2LR', '2TB', '4', '8']:
  312. self.app.inform.emit(_("[WARNING_NOTCL] Gaps value can be only one of: 'lr', 'tb', '2lr', '2tb', 4 or 8. "
  313. "Fill in a correct value and retry. "))
  314. return
  315. if cutout_obj.multigeo is True:
  316. self.app.inform.emit(_("[ERROR]Cutout operation cannot be done on a multi-geo Geometry.\n"
  317. "Optionally, this Multi-geo Geometry can be converted to Single-geo Geometry,\n"
  318. "and after that perform Cutout."))
  319. return
  320. # Get min and max data for each object as we just cut rectangles across X or Y
  321. xmin, ymin, xmax, ymax = cutout_obj.bounds()
  322. px = 0.5 * (xmin + xmax) + margin
  323. py = 0.5 * (ymin + ymax) + margin
  324. lenghtx = (xmax - xmin) + (margin * 2)
  325. lenghty = (ymax - ymin) + (margin * 2)
  326. gapsize = gapsize / 2 + (dia / 2)
  327. if isinstance(cutout_obj,FlatCAMGeometry):
  328. # rename the obj name so it can be identified as cutout
  329. cutout_obj.options["name"] += "_cutout"
  330. else:
  331. def geo_init(geo_obj, app_obj):
  332. geo = cutout_obj.solid_geometry.convex_hull
  333. geo_obj.solid_geometry = geo.buffer(margin + abs(dia / 2))
  334. outname = cutout_obj.options["name"] + "_cutout"
  335. self.app.new_object('geometry', outname, geo_init)
  336. cutout_obj = self.app.collection.get_by_name(outname)
  337. if gaps == '8' or gaps == '2LR':
  338. subtract_rectangle(cutout_obj,
  339. xmin - gapsize, # botleft_x
  340. py - gapsize + lenghty / 4, # botleft_y
  341. xmax + gapsize, # topright_x
  342. py + gapsize + lenghty / 4) # topright_y
  343. subtract_rectangle(cutout_obj,
  344. xmin - gapsize,
  345. py - gapsize - lenghty / 4,
  346. xmax + gapsize,
  347. py + gapsize - lenghty / 4)
  348. if gaps == '8' or gaps == '2TB':
  349. subtract_rectangle(cutout_obj,
  350. px - gapsize + lenghtx / 4,
  351. ymin - gapsize,
  352. px + gapsize + lenghtx / 4,
  353. ymax + gapsize)
  354. subtract_rectangle(cutout_obj,
  355. px - gapsize - lenghtx / 4,
  356. ymin - gapsize,
  357. px + gapsize - lenghtx / 4,
  358. ymax + gapsize)
  359. if gaps == '4' or gaps == 'LR':
  360. subtract_rectangle(cutout_obj,
  361. xmin - gapsize,
  362. py - gapsize,
  363. xmax + gapsize,
  364. py + gapsize)
  365. if gaps == '4' or gaps == 'TB':
  366. subtract_rectangle(cutout_obj,
  367. px - gapsize,
  368. ymin - gapsize,
  369. px + gapsize,
  370. ymax + gapsize)
  371. cutout_obj.plot()
  372. self.app.inform.emit(_("[success] Any form CutOut operation finished."))
  373. self.app.ui.notebook.setCurrentWidget(self.app.ui.project_tab)
  374. self.app.should_we_save = True
  375. def on_rectangular_cutout(self):
  376. def subtract_rectangle(obj_, x0, y0, x1, y1):
  377. pts = [(x0, y0), (x1, y0), (x1, y1), (x0, y1)]
  378. obj_.subtract_polygon(pts)
  379. name = self.obj_combo.currentText()
  380. # Get source object.
  381. try:
  382. cutout_obj = self.app.collection.get_by_name(str(name))
  383. except:
  384. self.app.inform.emit(_("[ERROR_NOTCL]Could not retrieve object: %s") % name)
  385. return "Could not retrieve object: %s" % name
  386. if cutout_obj is None:
  387. self.app.inform.emit(_("[ERROR_NOTCL]Object not found: %s") % cutout_obj)
  388. try:
  389. dia = float(self.dia.get_value())
  390. except ValueError:
  391. # try to convert comma to decimal point. if it's still not working error message and return
  392. try:
  393. dia = float(self.dia.get_value().replace(',', '.'))
  394. except ValueError:
  395. self.app.inform.emit(_("[WARNING_NOTCL] Tool diameter value is missing or wrong format. "
  396. "Add it and retry."))
  397. return
  398. if 0 in {dia}:
  399. self.app.inform.emit(_("[ERROR_NOTCL]Tool Diameter is zero value. Change it to a positive integer."))
  400. return "Tool Diameter is zero value. Change it to a positive integer."
  401. try:
  402. margin = float(self.margin.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. margin = float(self.margin.get_value().replace(',', '.'))
  407. except ValueError:
  408. self.app.inform.emit(_("[WARNING_NOTCL] Margin value is missing or wrong format. "
  409. "Add it and retry."))
  410. return
  411. try:
  412. gapsize = float(self.gapsize.get_value())
  413. except ValueError:
  414. # try to convert comma to decimal point. if it's still not working error message and return
  415. try:
  416. gapsize = float(self.gapsize.get_value().replace(',', '.'))
  417. except ValueError:
  418. self.app.inform.emit(_("[WARNING_NOTCL] Gap size value is missing or wrong format. "
  419. "Add it and retry."))
  420. return
  421. try:
  422. gaps = self.gaps.get_value()
  423. except TypeError:
  424. self.app.inform.emit(_("[WARNING_NOTCL] Number of gaps value is missing. Add it and retry."))
  425. return
  426. if gaps not in ['LR', 'TB', '2LR', '2TB', '4', '8']:
  427. self.app.inform.emit(_("[WARNING_NOTCL] Gaps value can be only one of: 'lr', 'tb', '2lr', '2tb', 4 or 8. "
  428. "Fill in a correct value and retry. "))
  429. return
  430. if cutout_obj.multigeo is True:
  431. self.app.inform.emit(_("[ERROR]Cutout operation cannot be done on a multi-geo Geometry.\n"
  432. "Optionally, this Multi-geo Geometry can be converted to Single-geo Geometry,\n"
  433. "and after that perform Cutout."))
  434. return
  435. # Get min and max data for each object as we just cut rectangles across X or Y
  436. xmin, ymin, xmax, ymax = cutout_obj.bounds()
  437. geo = box(xmin, ymin, xmax, ymax)
  438. px = 0.5 * (xmin + xmax) + margin
  439. py = 0.5 * (ymin + ymax) + margin
  440. lenghtx = (xmax - xmin) + (margin * 2)
  441. lenghty = (ymax - ymin) + (margin * 2)
  442. gapsize = gapsize / 2 + (dia / 2)
  443. def geo_init(geo_obj, app_obj):
  444. geo_obj.solid_geometry = geo.buffer(margin + abs(dia / 2))
  445. outname = cutout_obj.options["name"] + "_cutout"
  446. self.app.new_object('geometry', outname, geo_init)
  447. cutout_obj = self.app.collection.get_by_name(outname)
  448. if gaps == '8' or gaps == '2LR':
  449. subtract_rectangle(cutout_obj,
  450. xmin - gapsize, # botleft_x
  451. py - gapsize + lenghty / 4, # botleft_y
  452. xmax + gapsize, # topright_x
  453. py + gapsize + lenghty / 4) # topright_y
  454. subtract_rectangle(cutout_obj,
  455. xmin - gapsize,
  456. py - gapsize - lenghty / 4,
  457. xmax + gapsize,
  458. py + gapsize - lenghty / 4)
  459. if gaps == '8' or gaps == '2TB':
  460. subtract_rectangle(cutout_obj,
  461. px - gapsize + lenghtx / 4,
  462. ymin - gapsize,
  463. px + gapsize + lenghtx / 4,
  464. ymax + gapsize)
  465. subtract_rectangle(cutout_obj,
  466. px - gapsize - lenghtx / 4,
  467. ymin - gapsize,
  468. px + gapsize - lenghtx / 4,
  469. ymax + gapsize)
  470. if gaps == '4' or gaps == 'LR':
  471. subtract_rectangle(cutout_obj,
  472. xmin - gapsize,
  473. py - gapsize,
  474. xmax + gapsize,
  475. py + gapsize)
  476. if gaps == '4' or gaps == 'TB':
  477. subtract_rectangle(cutout_obj,
  478. px - gapsize,
  479. ymin - gapsize,
  480. px + gapsize,
  481. ymax + gapsize)
  482. cutout_obj.plot()
  483. self.app.inform.emit(_("[success] Any form CutOut operation finished."))
  484. self.app.ui.notebook.setCurrentWidget(self.app.ui.project_tab)
  485. self.app.should_we_save = True
  486. def on_manual_gap_click(self):
  487. self.app.inform.emit(_("Click on the selected geometry object perimeter to create a bridge gap ..."))
  488. self.app.geo_editor.tool_shape.enabled = True
  489. try:
  490. self.cutting_dia = float(self.dia.get_value())
  491. except ValueError:
  492. # try to convert comma to decimal point. if it's still not working error message and return
  493. try:
  494. self.cutting_dia = float(self.dia.get_value().replace(',', '.'))
  495. except ValueError:
  496. self.app.inform.emit(_("[WARNING_NOTCL] Tool diameter value is missing or wrong format. "
  497. "Add it and retry."))
  498. return
  499. if 0 in {self.cutting_dia}:
  500. self.app.inform.emit(_("[ERROR_NOTCL]Tool Diameter is zero value. Change it to a positive integer."))
  501. return "Tool Diameter is zero value. Change it to a positive integer."
  502. try:
  503. self.cutting_gapsize = float(self.gapsize.get_value())
  504. except ValueError:
  505. # try to convert comma to decimal point. if it's still not working error message and return
  506. try:
  507. self.cutting_gapsize = float(self.gapsize.get_value().replace(',', '.'))
  508. except ValueError:
  509. self.app.inform.emit(_("[WARNING_NOTCL] Gap size value is missing or wrong format. "
  510. "Add it and retry."))
  511. return
  512. self.app.plotcanvas.vis_disconnect('key_press', self.app.ui.keyPressEvent)
  513. self.app.plotcanvas.vis_disconnect('mouse_press', self.app.on_mouse_click_over_plot)
  514. self.app.plotcanvas.vis_disconnect('mouse_release', self.app.on_mouse_click_release_over_plot)
  515. self.app.plotcanvas.vis_disconnect('mouse_move', self.app.on_mouse_move_over_plot)
  516. self.app.plotcanvas.vis_connect('key_press', self.on_key_press)
  517. self.app.plotcanvas.vis_connect('mouse_move', self.on_mouse_move)
  518. self.app.plotcanvas.vis_connect('mouse_release', self.doit)
  519. # To be called after clicking on the plot.
  520. def doit(self, event):
  521. # do paint single only for left mouse clicks
  522. if event.button == 1:
  523. self.app.inform.emit(_("Making manual bridge gap..."))
  524. pos = self.app.plotcanvas.vispy_canvas.translate_coords(event.pos)
  525. self.on_manual_cutout(click_pos=pos)
  526. self.app.plotcanvas.vis_disconnect('key_press', self.on_key_press)
  527. self.app.plotcanvas.vis_disconnect('mouse_move', self.on_mouse_move)
  528. self.app.plotcanvas.vis_disconnect('mouse_release', self.doit)
  529. self.app.plotcanvas.vis_connect('key_press', self.app.ui.keyPressEvent)
  530. self.app.plotcanvas.vis_connect('mouse_press', self.app.on_mouse_click_over_plot)
  531. self.app.plotcanvas.vis_connect('mouse_release', self.app.on_mouse_click_release_over_plot)
  532. self.app.plotcanvas.vis_connect('mouse_move', self.app.on_mouse_move_over_plot)
  533. self.app.geo_editor.tool_shape.clear(update=True)
  534. self.app.geo_editor.tool_shape.enabled = False
  535. self.gapFinished.emit()
  536. def on_manual_cutout(self, click_pos):
  537. name = self.man_object_combo.currentText()
  538. # Get source object.
  539. try:
  540. cutout_obj = self.app.collection.get_by_name(str(name))
  541. except:
  542. self.app.inform.emit(_("[ERROR_NOTCL]Could not retrieve Geoemtry object: %s") % name)
  543. return "Could not retrieve object: %s" % name
  544. if cutout_obj is None:
  545. self.app.inform.emit(_("[ERROR_NOTCL]Geometry object for manual cutout not found: %s") % cutout_obj)
  546. return
  547. # use the snapped position as reference
  548. snapped_pos = self.app.geo_editor.snap(click_pos[0], click_pos[1])
  549. cut_poly = self.cutting_geo(pos=(snapped_pos[0], snapped_pos[1]))
  550. cutout_obj.subtract_polygon(cut_poly)
  551. cutout_obj.plot()
  552. self.app.inform.emit(_("[success] Added manual Bridge Gap."))
  553. self.app.should_we_save = True
  554. def on_gap_finished(self):
  555. # if CTRL key modifier is pressed then repeat the bridge gap cut
  556. key_modifier = QtWidgets.QApplication.keyboardModifiers()
  557. if key_modifier == Qt.ControlModifier:
  558. self.on_manual_gap_click()
  559. def on_manual_geo(self):
  560. name = self.obj_combo.currentText()
  561. # Get source object.
  562. try:
  563. cutout_obj = self.app.collection.get_by_name(str(name))
  564. except:
  565. self.app.inform.emit(_("[ERROR_NOTCL]Could not retrieve Gerber object: %s") % name)
  566. return "Could not retrieve object: %s" % name
  567. if cutout_obj is None:
  568. self.app.inform.emit(_("[ERROR_NOTCL]There is no Gerber object selected for Cutout.\n"
  569. "Select one and try again."))
  570. return
  571. if not isinstance(cutout_obj, FlatCAMGerber):
  572. self.app.inform.emit(_("[ERROR_NOTCL]The selected object has to be of Gerber type.\n"
  573. "Select a Gerber file and try again."))
  574. return
  575. try:
  576. dia = float(self.dia.get_value())
  577. except ValueError:
  578. # try to convert comma to decimal point. if it's still not working error message and return
  579. try:
  580. dia = float(self.dia.get_value().replace(',', '.'))
  581. except ValueError:
  582. self.app.inform.emit(_("[WARNING_NOTCL] Tool diameter value is missing or wrong format. "
  583. "Add it and retry."))
  584. return
  585. if 0 in {dia}:
  586. self.app.inform.emit(_("[ERROR_NOTCL]Tool Diameter is zero value. Change it to a positive integer."))
  587. return "Tool Diameter is zero value. Change it to a positive integer."
  588. try:
  589. margin = float(self.margin.get_value())
  590. except ValueError:
  591. # try to convert comma to decimal point. if it's still not working error message and return
  592. try:
  593. margin = float(self.margin.get_value().replace(',', '.'))
  594. except ValueError:
  595. self.app.inform.emit(_("[WARNING_NOTCL] Margin value is missing or wrong format. "
  596. "Add it and retry."))
  597. return
  598. def geo_init(geo_obj, app_obj):
  599. geo = cutout_obj.solid_geometry.convex_hull
  600. geo_obj.solid_geometry = geo.buffer(margin + abs(dia / 2))
  601. outname = cutout_obj.options["name"] + "_cutout"
  602. self.app.new_object('geometry', outname, geo_init)
  603. def cutting_geo(self, pos):
  604. self.cutting_gapsize = self.cutting_gapsize / 2 + (self.cutting_dia / 2)
  605. offset = self.cutting_gapsize / 2
  606. # cutting area definition
  607. orig_x = pos[0]
  608. orig_y = pos[1]
  609. xmin = orig_x - offset
  610. ymin = orig_y - offset
  611. xmax = orig_x + offset
  612. ymax = orig_y + offset
  613. cut_poly = box(xmin, ymin, xmax, ymax)
  614. return cut_poly
  615. def on_mouse_move(self, event):
  616. self.app.on_mouse_move_over_plot(event=event)
  617. pos = self.canvas.vispy_canvas.translate_coords(event.pos)
  618. event.xdata, event.ydata = pos[0], pos[1]
  619. try:
  620. x = float(event.xdata)
  621. y = float(event.ydata)
  622. except TypeError:
  623. return
  624. snap_x, snap_y = self.app.geo_editor.snap(x, y)
  625. geo = self.cutting_geo(pos=(snap_x, snap_y))
  626. # Remove any previous utility shape
  627. self.app.geo_editor.tool_shape.clear(update=True)
  628. self.draw_utility_geometry(geo=geo)
  629. def draw_utility_geometry(self, geo):
  630. self.app.geo_editor.tool_shape.add(
  631. shape=geo,
  632. color=(self.app.defaults["global_draw_color"] + '80'),
  633. update=False,
  634. layer=0,
  635. tolerance=None)
  636. self.app.geo_editor.tool_shape.redraw()
  637. def on_key_press(self, event):
  638. # events out of the self.app.collection view (it's about Project Tab) are of type int
  639. if type(event) is int:
  640. key = event
  641. # events from the GUI are of type QKeyEvent
  642. elif type(event) == QtGui.QKeyEvent:
  643. key = event.key()
  644. # events from Vispy are of type KeyEvent
  645. else:
  646. key = event.key
  647. # Escape = Deselect All
  648. if key == QtCore.Qt.Key_Escape or key == 'Escape':
  649. self.app.plotcanvas.vis_disconnect('key_press', self.on_key_press)
  650. self.app.plotcanvas.vis_disconnect('mouse_move', self.on_mouse_move)
  651. self.app.plotcanvas.vis_disconnect('mouse_release', self.doit)
  652. self.app.plotcanvas.vis_connect('key_press', self.app.ui.keyPressEvent)
  653. self.app.plotcanvas.vis_connect('mouse_press', self.app.on_mouse_click_over_plot)
  654. self.app.plotcanvas.vis_connect('mouse_release', self.app.on_mouse_click_release_over_plot)
  655. self.app.plotcanvas.vis_connect('mouse_move', self.app.on_mouse_move_over_plot)
  656. # Remove any previous utility shape
  657. self.app.geo_editor.tool_shape.clear(update=True)
  658. self.app.geo_editor.tool_shape.enabled = False
  659. def reset_fields(self):
  660. self.obj_combo.setRootModelIndex(self.app.collection.index(0, 0, QtCore.QModelIndex()))