ToolCutOut.py 33 KB

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