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