ToolCutout.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. from FlatCAMTool import FlatCAMTool
  2. from copy import copy,deepcopy
  3. from ObjectCollection import *
  4. from FlatCAMApp import *
  5. from PyQt5 import QtGui, QtCore, QtWidgets
  6. from GUIElements import IntEntry, RadioSet, LengthEntry
  7. from FlatCAMObj import FlatCAMGeometry, FlatCAMExcellon, FlatCAMGerber
  8. class ToolCutout(FlatCAMTool):
  9. toolName = "Cutout PCB"
  10. def __init__(self, app):
  11. FlatCAMTool.__init__(self, app)
  12. ## Title
  13. title_label = QtWidgets.QLabel("<font size=4><b>%s</b></font>" % self.toolName)
  14. self.layout.addWidget(title_label)
  15. ## Form Layout
  16. form_layout = QtWidgets.QFormLayout()
  17. self.layout.addLayout(form_layout)
  18. ## Type of object to be cutout
  19. self.type_obj_combo = QtWidgets.QComboBox()
  20. self.type_obj_combo.addItem("Gerber")
  21. self.type_obj_combo.addItem("Excellon")
  22. self.type_obj_combo.addItem("Geometry")
  23. # we get rid of item1 ("Excellon") as it is not suitable for creating film
  24. self.type_obj_combo.view().setRowHidden(1, True)
  25. self.type_obj_combo.setItemIcon(0, QtGui.QIcon("share/flatcam_icon16.png"))
  26. # self.type_obj_combo.setItemIcon(1, QtGui.QIcon("share/drill16.png"))
  27. self.type_obj_combo.setItemIcon(2, QtGui.QIcon("share/geometry16.png"))
  28. self.type_obj_combo_label = QtWidgets.QLabel("Object Type:")
  29. self.type_obj_combo_label.setToolTip(
  30. "Specify the type of object to be cutout.\n"
  31. "It can be of type: Gerber or Geometry.\n"
  32. "What is selected here will dictate the kind\n"
  33. "of objects that will populate the 'Object' combobox."
  34. )
  35. form_layout.addRow(self.type_obj_combo_label, self.type_obj_combo)
  36. ## Object to be cutout
  37. self.obj_combo = QtWidgets.QComboBox()
  38. self.obj_combo.setModel(self.app.collection)
  39. self.obj_combo.setRootModelIndex(self.app.collection.index(0, 0, QtCore.QModelIndex()))
  40. self.obj_combo.setCurrentIndex(1)
  41. self.object_label = QtWidgets.QLabel("Object:")
  42. self.object_label.setToolTip(
  43. "Object to be cutout. "
  44. )
  45. form_layout.addRow(self.object_label, self.obj_combo)
  46. # Tool Diameter
  47. self.dia = FCEntry()
  48. self.dia_label = QtWidgets.QLabel("Tool Dia:")
  49. self.dia_label.setToolTip(
  50. "Diameter of the tool used to cutout\n"
  51. "the PCB shape out of the surrounding material."
  52. )
  53. form_layout.addRow(self.dia_label, self.dia)
  54. # Margin
  55. self.margin = FCEntry()
  56. self.margin_label = QtWidgets.QLabel("Margin:")
  57. self.margin_label.setToolTip(
  58. "Margin over bounds. A positive value here\n"
  59. "will make the cutout of the PCB further from\n"
  60. "the actual PCB border"
  61. )
  62. form_layout.addRow(self.margin_label, self.margin)
  63. # Gapsize
  64. self.gapsize = FCEntry()
  65. self.gapsize_label = QtWidgets.QLabel("Gap size:")
  66. self.gapsize_label.setToolTip(
  67. "The size of the gaps in the cutout\n"
  68. "used to keep the board connected to\n"
  69. "the surrounding material (the one \n"
  70. "from which the PCB is cutout)."
  71. )
  72. form_layout.addRow(self.gapsize_label, self.gapsize)
  73. ## Title2
  74. title_ff_label = QtWidgets.QLabel("<font size=4><b>FreeForm Cutout</b></font>")
  75. self.layout.addWidget(title_ff_label)
  76. ## Form Layout
  77. form_layout_2 = QtWidgets.QFormLayout()
  78. self.layout.addLayout(form_layout_2)
  79. # How gaps wil be rendered:
  80. # lr - left + right
  81. # tb - top + bottom
  82. # 4 - left + right +top + bottom
  83. # 2lr - 2*left + 2*right
  84. # 2tb - 2*top + 2*bottom
  85. # 8 - 2*left + 2*right +2*top + 2*bottom
  86. # Gaps
  87. self.gaps = FCEntry()
  88. self.gaps_label = QtWidgets.QLabel("Type of gaps: ")
  89. self.gaps_label.setToolTip(
  90. "Number of gaps used for the cutout.\n"
  91. "There can be maximum 8 bridges/gaps.\n"
  92. "The choices are:\n"
  93. "- lr - left + right\n"
  94. "- tb - top + bottom\n"
  95. "- 4 - left + right +top + bottom\n"
  96. "- 2lr - 2*left + 2*right\n"
  97. "- 2tb - 2*top + 2*bottom\n"
  98. "- 8 - 2*left + 2*right +2*top + 2*bottom"
  99. )
  100. form_layout_2.addRow(self.gaps_label, self.gaps)
  101. ## Buttons
  102. hlay = QtWidgets.QHBoxLayout()
  103. self.layout.addLayout(hlay)
  104. hlay.addStretch()
  105. self.ff_cutout_object_btn = QtWidgets.QPushButton(" FreeForm Cutout Object ")
  106. self.ff_cutout_object_btn.setToolTip(
  107. "Cutout the selected object.\n"
  108. "The cutout shape can be any shape.\n"
  109. "Useful when the PCB has a non-rectangular shape.\n"
  110. "But if the object to be cutout is of Gerber Type,\n"
  111. "it needs to be an outline of the actual board shape."
  112. )
  113. hlay.addWidget(self.ff_cutout_object_btn)
  114. ## Title3
  115. title_rct_label = QtWidgets.QLabel("<font size=4><b>Rectangular Cutout</b></font>")
  116. self.layout.addWidget(title_rct_label)
  117. ## Form Layout
  118. form_layout_3 = QtWidgets.QFormLayout()
  119. self.layout.addLayout(form_layout_3)
  120. gapslabel_rect = QtWidgets.QLabel('Type of gaps:')
  121. gapslabel_rect.setToolTip(
  122. "Where to place the gaps:\n"
  123. "- one gap Top / one gap Bottom\n"
  124. "- one gap Left / one gap Right\n"
  125. "- one gap on each of the 4 sides."
  126. )
  127. self.gaps_rect_radio = RadioSet([{'label': 'T/B', 'value': 'tb'},
  128. {'label': 'L/R', 'value': 'lr'},
  129. {'label': '4', 'value': '4'}])
  130. form_layout_3.addRow(gapslabel_rect, self.gaps_rect_radio)
  131. hlay2 = QtWidgets.QHBoxLayout()
  132. self.layout.addLayout(hlay2)
  133. hlay2.addStretch()
  134. self.rect_cutout_object_btn = QtWidgets.QPushButton("Rectangular Cutout Object")
  135. self.rect_cutout_object_btn.setToolTip(
  136. "Cutout the selected object.\n"
  137. "The resulting cutout shape is\n"
  138. "always of a rectangle form and it will be\n"
  139. "the bounding box of the Object."
  140. )
  141. hlay2.addWidget(self.rect_cutout_object_btn)
  142. self.layout.addStretch()
  143. ## Init GUI
  144. self.dia.set_value(1)
  145. self.margin.set_value(0)
  146. self.gapsize.set_value(1)
  147. self.gaps.set_value(4)
  148. self.gaps_rect_radio.set_value("4")
  149. ## Signals
  150. self.ff_cutout_object_btn.clicked.connect(self.on_freeform_cutout)
  151. self.rect_cutout_object_btn.clicked.connect(self.on_rectangular_cutout)
  152. self.type_obj_combo.currentIndexChanged.connect(self.on_type_obj_index_changed)
  153. def on_type_obj_index_changed(self, index):
  154. obj_type = self.type_obj_combo.currentIndex()
  155. self.obj_combo.setRootModelIndex(self.app.collection.index(obj_type, 0, QtCore.QModelIndex()))
  156. self.obj_combo.setCurrentIndex(0)
  157. def run(self):
  158. FlatCAMTool.run(self)
  159. self.app.ui.notebook.setTabText(2, "Cutout Tool")
  160. def install(self, icon=None, separator=None, **kwargs):
  161. FlatCAMTool.install(self, icon, separator, shortcut='ALT+U', **kwargs)
  162. def on_freeform_cutout(self):
  163. def subtract_rectangle(obj_, x0, y0, x1, y1):
  164. pts = [(x0, y0), (x1, y0), (x1, y1), (x0, y1)]
  165. obj_.subtract_polygon(pts)
  166. name = self.obj_combo.currentText()
  167. # Get source object.
  168. try:
  169. cutout_obj = self.app.collection.get_by_name(str(name))
  170. except:
  171. self.app.inform.emit("[error_notcl]Could not retrieve object: %s" % name)
  172. return "Could not retrieve object: %s" % name
  173. if cutout_obj is None:
  174. self.app.inform.emit("[error_notcl]Object not found: %s" % cutout_obj)
  175. try:
  176. dia = float(self.dia.get_value())
  177. except TypeError:
  178. self.app.inform.emit("[warning_notcl] Tool diameter value is missing. Add it and retry.")
  179. return
  180. try:
  181. margin = float(self.margin.get_value())
  182. except TypeError:
  183. self.app.inform.emit("[warning_notcl] Margin value is missing. Add it and retry.")
  184. return
  185. try:
  186. gapsize = float(self.gapsize.get_value())
  187. except TypeError:
  188. self.app.inform.emit("[warning_notcl] Gap size value is missing. Add it and retry.")
  189. return
  190. try:
  191. gaps = self.gaps.get_value()
  192. except TypeError:
  193. self.app.inform.emit("[warning_notcl] Number of gaps value is missing. Add it and retry.")
  194. return
  195. if 0 in {dia}:
  196. self.app.inform.emit("[warning_notcl]Tool Diameter is zero value. Change it to a positive integer.")
  197. return "Tool Diameter is zero value. Change it to a positive integer."
  198. if gaps not in ['lr', 'tb', '2lr', '2tb', '4', '8']:
  199. self.app.inform.emit("[warning_notcl] Gaps value can be only one of: 'lr', 'tb', '2lr', '2tb', 4 or 8. "
  200. "Fill in a correct value and retry. ")
  201. return
  202. # Get min and max data for each object as we just cut rectangles across X or Y
  203. xmin, ymin, xmax, ymax = cutout_obj.bounds()
  204. px = 0.5 * (xmin + xmax) + margin
  205. py = 0.5 * (ymin + ymax) + margin
  206. lenghtx = (xmax - xmin) + (margin * 2)
  207. lenghty = (ymax - ymin) + (margin * 2)
  208. gapsize = gapsize + (dia / 2)
  209. if isinstance(cutout_obj,FlatCAMGeometry):
  210. # rename the obj name so it can be identified as cutout
  211. cutout_obj.options["name"] += "_cutout"
  212. else:
  213. cutout_obj.isolate(dia=dia, passes=1, overlap=1, combine=False, outname="_temp")
  214. ext_obj = self.app.collection.get_by_name("_temp")
  215. def geo_init(geo_obj, app_obj):
  216. geo_obj.solid_geometry = obj_exteriors
  217. outname = cutout_obj.options["name"] + "_cutout"
  218. obj_exteriors = ext_obj.get_exteriors()
  219. self.app.new_object('geometry', outname, geo_init)
  220. self.app.collection.set_all_inactive()
  221. self.app.collection.set_active("_temp")
  222. self.app.on_delete()
  223. cutout_obj = self.app.collection.get_by_name(outname)
  224. if int(gaps) == 8 or gaps == '2lr':
  225. subtract_rectangle(cutout_obj,
  226. xmin - gapsize, # botleft_x
  227. py - gapsize + lenghty / 4, # botleft_y
  228. xmax + gapsize, # topright_x
  229. py + gapsize + lenghty / 4) # topright_y
  230. subtract_rectangle(cutout_obj,
  231. xmin - gapsize,
  232. py - gapsize - lenghty / 4,
  233. xmax + gapsize,
  234. py + gapsize - lenghty / 4)
  235. if int(gaps) == 8 or gaps == '2tb':
  236. subtract_rectangle(cutout_obj,
  237. px - gapsize + lenghtx / 4,
  238. ymin - gapsize,
  239. px + gapsize + lenghtx / 4,
  240. ymax + gapsize)
  241. subtract_rectangle(cutout_obj,
  242. px - gapsize - lenghtx / 4,
  243. ymin - gapsize,
  244. px + gapsize - lenghtx / 4,
  245. ymax + gapsize)
  246. if int(gaps) == 4 or gaps == 'lr':
  247. subtract_rectangle(cutout_obj,
  248. xmin - gapsize,
  249. py - gapsize,
  250. xmax + gapsize,
  251. py + gapsize)
  252. if int(gaps) == 4 or gaps == 'tb':
  253. subtract_rectangle(cutout_obj,
  254. px - gapsize,
  255. ymin - gapsize,
  256. px + gapsize,
  257. ymax + gapsize)
  258. cutout_obj.plot()
  259. self.app.inform.emit("[success] Any form CutOut operation finished.")
  260. self.app.ui.notebook.setCurrentWidget(self.app.ui.project_tab)
  261. def on_rectangular_cutout(self):
  262. name = self.obj_combo.currentText()
  263. # Get source object.
  264. try:
  265. cutout_obj = self.app.collection.get_by_name(str(name))
  266. except:
  267. self.app.inform.emit("[error_notcl]Could not retrieve object: %s" % name)
  268. return "Could not retrieve object: %s" % name
  269. if cutout_obj is None:
  270. self.app.inform.emit("[error_notcl]Object not found: %s" % cutout_obj)
  271. try:
  272. dia = float(self.dia.get_value())
  273. except TypeError:
  274. self.app.inform.emit("[warning_notcl] Tool diameter value is missing. Add it and retry.")
  275. return
  276. try:
  277. margin = float(self.margin.get_value())
  278. except TypeError:
  279. self.app.inform.emit("[warning_notcl] Margin value is missing. Add it and retry.")
  280. return
  281. try:
  282. gapsize = float(self.gapsize.get_value())
  283. except TypeError:
  284. self.app.inform.emit("[warning_notcl] Gap size value is missing. Add it and retry.")
  285. return
  286. try:
  287. gaps = self.gaps_rect_radio.get_value()
  288. except TypeError:
  289. self.app.inform.emit("[warning_notcl] Number of gaps value is missing. Add it and retry.")
  290. return
  291. if 0 in {dia}:
  292. self.app.inform.emit("[error_notcl]Tool Diameter is zero value. Change it to a positive integer.")
  293. return "Tool Diameter is zero value. Change it to a positive integer."
  294. def geo_init(geo_obj, app_obj):
  295. real_margin = margin + (dia / 2)
  296. real_gap_size = gapsize + dia
  297. minx, miny, maxx, maxy = cutout_obj.bounds()
  298. minx -= real_margin
  299. maxx += real_margin
  300. miny -= real_margin
  301. maxy += real_margin
  302. midx = 0.5 * (minx + maxx)
  303. midy = 0.5 * (miny + maxy)
  304. hgap = 0.5 * real_gap_size
  305. pts = [[midx - hgap, maxy],
  306. [minx, maxy],
  307. [minx, midy + hgap],
  308. [minx, midy - hgap],
  309. [minx, miny],
  310. [midx - hgap, miny],
  311. [midx + hgap, miny],
  312. [maxx, miny],
  313. [maxx, midy - hgap],
  314. [maxx, midy + hgap],
  315. [maxx, maxy],
  316. [midx + hgap, maxy]]
  317. cases = {"tb": [[pts[0], pts[1], pts[4], pts[5]],
  318. [pts[6], pts[7], pts[10], pts[11]]],
  319. "lr": [[pts[9], pts[10], pts[1], pts[2]],
  320. [pts[3], pts[4], pts[7], pts[8]]],
  321. "4": [[pts[0], pts[1], pts[2]],
  322. [pts[3], pts[4], pts[5]],
  323. [pts[6], pts[7], pts[8]],
  324. [pts[9], pts[10], pts[11]]]}
  325. cuts = cases[gaps]
  326. geo_obj.solid_geometry = cascaded_union([LineString(segment) for segment in cuts])
  327. # TODO: Check for None
  328. self.app.new_object("geometry", name + "_cutout", geo_init)
  329. self.app.inform.emit("[success] Rectangular CutOut operation finished.")
  330. self.app.ui.notebook.setCurrentWidget(self.app.ui.project_tab)
  331. def reset_fields(self):
  332. self.obj_combo.setRootModelIndex(self.app.collection.index(0, 0, QtCore.QModelIndex()))