ToolPanelize.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. ############################################################
  2. # FlatCAM: 2D Post-processing for Manufacturing #
  3. # http://flatcam.org #
  4. # File Author: Marius Adrian Stanciu (c) #
  5. # Date: 3/10/2019 #
  6. # MIT Licence #
  7. ############################################################
  8. from FlatCAMTool import FlatCAMTool
  9. from copy import copy, deepcopy
  10. from ObjectCollection import *
  11. import time
  12. import gettext
  13. import FlatCAMTranslation as fcTranslate
  14. fcTranslate.apply_language('ToolPanelize')
  15. import builtins
  16. if '_' not in builtins.__dict__:
  17. _ = gettext.gettext
  18. class Panelize(FlatCAMTool):
  19. toolName = _("Panelize PCB")
  20. def __init__(self, app):
  21. super(Panelize, self).__init__(self)
  22. self.app = app
  23. ## Title
  24. title_label = QtWidgets.QLabel("%s" % self.toolName)
  25. title_label.setStyleSheet("""
  26. QLabel
  27. {
  28. font-size: 16px;
  29. font-weight: bold;
  30. }
  31. """)
  32. self.layout.addWidget(title_label)
  33. ## Form Layout
  34. form_layout = QtWidgets.QFormLayout()
  35. self.layout.addLayout(form_layout)
  36. ## Type of object to be panelized
  37. self.type_obj_combo = QtWidgets.QComboBox()
  38. self.type_obj_combo.addItem("Gerber")
  39. self.type_obj_combo.addItem("Excellon")
  40. self.type_obj_combo.addItem("Geometry")
  41. self.type_obj_combo.setItemIcon(0, QtGui.QIcon("share/flatcam_icon16.png"))
  42. self.type_obj_combo.setItemIcon(1, QtGui.QIcon("share/drill16.png"))
  43. self.type_obj_combo.setItemIcon(2, QtGui.QIcon("share/geometry16.png"))
  44. self.type_obj_combo_label = QtWidgets.QLabel(_("Object Type:"))
  45. self.type_obj_combo_label.setToolTip(
  46. _("Specify the type of object to be panelized\n"
  47. "It can be of type: Gerber, Excellon or Geometry.\n"
  48. "The selection here decide the type of objects that will be\n"
  49. "in the Object combobox.")
  50. )
  51. form_layout.addRow(self.type_obj_combo_label, self.type_obj_combo)
  52. ## Object to be panelized
  53. self.object_combo = QtWidgets.QComboBox()
  54. self.object_combo.setModel(self.app.collection)
  55. self.object_combo.setRootModelIndex(self.app.collection.index(0, 0, QtCore.QModelIndex()))
  56. self.object_combo.setCurrentIndex(1)
  57. self.object_label = QtWidgets.QLabel(_("Object:"))
  58. self.object_label.setToolTip(
  59. _("Object to be panelized. This means that it will\n"
  60. "be duplicated in an array of rows and columns.")
  61. )
  62. form_layout.addRow(self.object_label, self.object_combo)
  63. ## Type of Box Object to be used as an envelope for panelization
  64. self.type_box_combo = QtWidgets.QComboBox()
  65. self.type_box_combo.addItem("Gerber")
  66. self.type_box_combo.addItem("Excellon")
  67. self.type_box_combo.addItem("Geometry")
  68. # we get rid of item1 ("Excellon") as it is not suitable for use as a "box" for panelizing
  69. self.type_box_combo.view().setRowHidden(1, True)
  70. self.type_box_combo.setItemIcon(0, QtGui.QIcon("share/flatcam_icon16.png"))
  71. self.type_box_combo.setItemIcon(2, QtGui.QIcon("share/geometry16.png"))
  72. self.type_box_combo_label = QtWidgets.QLabel(_("Box Type:"))
  73. self.type_box_combo_label.setToolTip(
  74. _("Specify the type of object to be used as an container for\n"
  75. "panelization. It can be: Gerber or Geometry type.\n"
  76. "The selection here decide the type of objects that will be\n"
  77. "in the Box Object combobox.")
  78. )
  79. form_layout.addRow(self.type_box_combo_label, self.type_box_combo)
  80. ## Box
  81. self.box_combo = QtWidgets.QComboBox()
  82. self.box_combo.setModel(self.app.collection)
  83. self.box_combo.setRootModelIndex(self.app.collection.index(0, 0, QtCore.QModelIndex()))
  84. self.box_combo.setCurrentIndex(1)
  85. self.box_combo_label = QtWidgets.QLabel(_("Box Object:"))
  86. self.box_combo_label.setToolTip(
  87. _("The actual object that is used a container for the\n "
  88. "selected object that is to be panelized.")
  89. )
  90. form_layout.addRow(self.box_combo_label, self.box_combo)
  91. ## Spacing Columns
  92. self.spacing_columns = FCEntry()
  93. self.spacing_columns_label = QtWidgets.QLabel(_("Spacing cols:"))
  94. self.spacing_columns_label.setToolTip(
  95. _("Spacing between columns of the desired panel.\n"
  96. "In current units.")
  97. )
  98. form_layout.addRow(self.spacing_columns_label, self.spacing_columns)
  99. ## Spacing Rows
  100. self.spacing_rows = FCEntry()
  101. self.spacing_rows_label = QtWidgets.QLabel(_("Spacing rows:"))
  102. self.spacing_rows_label.setToolTip(
  103. _("Spacing between rows of the desired panel.\n"
  104. "In current units.")
  105. )
  106. form_layout.addRow(self.spacing_rows_label, self.spacing_rows)
  107. ## Columns
  108. self.columns = FCEntry()
  109. self.columns_label = QtWidgets.QLabel(_("Columns:"))
  110. self.columns_label.setToolTip(
  111. _("Number of columns of the desired panel")
  112. )
  113. form_layout.addRow(self.columns_label, self.columns)
  114. ## Rows
  115. self.rows = FCEntry()
  116. self.rows_label = QtWidgets.QLabel(_("Rows:"))
  117. self.rows_label.setToolTip(
  118. _("Number of rows of the desired panel")
  119. )
  120. form_layout.addRow(self.rows_label, self.rows)
  121. ## Type of resulting Panel object
  122. self.panel_type_radio = RadioSet([{'label': 'Gerber', 'value': 'gerber'},
  123. {'label': 'Geometry', 'value': 'geometry'}])
  124. self.panel_type_label = QtWidgets.QLabel(_("Panel Type:"))
  125. self.panel_type_label.setToolTip(
  126. _("Choose the type of object for the panel object:\n"
  127. "- Geometry\n"
  128. "- Gerber")
  129. )
  130. form_layout.addRow(self.panel_type_label)
  131. form_layout.addRow(self.panel_type_radio)
  132. ## Constrains
  133. self.constrain_cb = FCCheckBox(_("Constrain panel within:"))
  134. self.constrain_cb.setToolTip(
  135. _("Area define by DX and DY within to constrain the panel.\n"
  136. "DX and DY values are in current units.\n"
  137. "Regardless of how many columns and rows are desired,\n"
  138. "the final panel will have as many columns and rows as\n"
  139. "they fit completely within selected area.")
  140. )
  141. form_layout.addRow(self.constrain_cb)
  142. self.x_width_entry = FCEntry()
  143. self.x_width_lbl = QtWidgets.QLabel(_("Width (DX):"))
  144. self.x_width_lbl.setToolTip(
  145. _("The width (DX) within which the panel must fit.\n"
  146. "In current units.")
  147. )
  148. form_layout.addRow(self.x_width_lbl, self.x_width_entry)
  149. self.y_height_entry = FCEntry()
  150. self.y_height_lbl = QtWidgets.QLabel(_("Height (DY):"))
  151. self.y_height_lbl.setToolTip(
  152. _("The height (DY)within which the panel must fit.\n"
  153. "In current units.")
  154. )
  155. form_layout.addRow(self.y_height_lbl, self.y_height_entry)
  156. self.constrain_sel = OptionalInputSection(
  157. self.constrain_cb, [self.x_width_lbl, self.x_width_entry, self.y_height_lbl, self.y_height_entry])
  158. ## Buttons
  159. hlay_2 = QtWidgets.QHBoxLayout()
  160. self.layout.addLayout(hlay_2)
  161. hlay_2.addStretch()
  162. self.panelize_object_button = QtWidgets.QPushButton(_("Panelize Object"))
  163. self.panelize_object_button.setToolTip(
  164. _("Panelize the specified object around the specified box.\n"
  165. "In other words it creates multiple copies of the source object,\n"
  166. "arranged in a 2D array of rows and columns.")
  167. )
  168. hlay_2.addWidget(self.panelize_object_button)
  169. self.layout.addStretch()
  170. ## Signals
  171. self.panelize_object_button.clicked.connect(self.on_panelize)
  172. self.type_obj_combo.currentIndexChanged.connect(self.on_type_obj_index_changed)
  173. self.type_box_combo.currentIndexChanged.connect(self.on_type_box_index_changed)
  174. # list to hold the temporary objects
  175. self.objs = []
  176. # final name for the panel object
  177. self.outname = ""
  178. # flag to signal the constrain was activated
  179. self.constrain_flag = False
  180. def run(self, toggle=True):
  181. self.app.report_usage("ToolPanelize()")
  182. if toggle:
  183. # if the splitter is hidden, display it, else hide it but only if the current widget is the same
  184. if self.app.ui.splitter.sizes()[0] == 0:
  185. self.app.ui.splitter.setSizes([1, 1])
  186. else:
  187. try:
  188. if self.app.ui.tool_scroll_area.widget().objectName() == self.toolName:
  189. self.app.ui.splitter.setSizes([0, 1])
  190. except AttributeError:
  191. pass
  192. FlatCAMTool.run(self)
  193. self.set_tool_ui()
  194. self.app.ui.notebook.setTabText(2, "Panel. Tool")
  195. def install(self, icon=None, separator=None, **kwargs):
  196. FlatCAMTool.install(self, icon, separator, shortcut='ALT+Z', **kwargs)
  197. def set_tool_ui(self):
  198. self.reset_fields()
  199. sp_c = self.app.defaults["tools_panelize_spacing_columns"] if \
  200. self.app.defaults["tools_panelize_spacing_columns"] else 0.0
  201. self.spacing_columns.set_value(float(sp_c))
  202. sp_r = self.app.defaults["tools_panelize_spacing_rows"] if \
  203. self.app.defaults["tools_panelize_spacing_rows"] else 0.0
  204. self.spacing_rows.set_value(float(sp_r))
  205. rr = self.app.defaults["tools_panelize_rows"] if \
  206. self.app.defaults["tools_panelize_rows"] else 0.0
  207. self.rows.set_value(int(rr))
  208. cc = self.app.defaults["tools_panelize_columns"] if \
  209. self.app.defaults["tools_panelize_columns"] else 0.0
  210. self.columns.set_value(int(cc))
  211. c_cb = self.app.defaults["tools_panelize_constrain"] if \
  212. self.app.defaults["tools_panelize_constrain"] else False
  213. self.constrain_cb.set_value(c_cb)
  214. x_w = self.app.defaults["tools_panelize_constrainx"] if \
  215. self.app.defaults["tools_panelize_constrainx"] else 0.0
  216. self.x_width_entry.set_value(float(x_w))
  217. y_w = self.app.defaults["tools_panelize_constrainy"] if \
  218. self.app.defaults["tools_panelize_constrainy"] else 0.0
  219. self.y_height_entry.set_value(float(y_w))
  220. panel_type = self.app.defaults["tools_panelize_panel_type"] if \
  221. self.app.defaults["tools_panelize_panel_type"] else 'gerber'
  222. self.panel_type_radio.set_value(panel_type)
  223. def on_type_obj_index_changed(self):
  224. obj_type = self.type_obj_combo.currentIndex()
  225. self.object_combo.setRootModelIndex(self.app.collection.index(obj_type, 0, QtCore.QModelIndex()))
  226. self.object_combo.setCurrentIndex(0)
  227. def on_type_box_index_changed(self):
  228. obj_type = self.type_box_combo.currentIndex()
  229. self.box_combo.setRootModelIndex(self.app.collection.index(obj_type, 0, QtCore.QModelIndex()))
  230. self.box_combo.setCurrentIndex(0)
  231. def on_panelize(self):
  232. name = self.object_combo.currentText()
  233. # Get source object.
  234. try:
  235. obj = self.app.collection.get_by_name(str(name))
  236. except:
  237. self.app.inform.emit(_("[ERROR_NOTCL]Could not retrieve object: %s") % name)
  238. return "Could not retrieve object: %s" % name
  239. panel_obj = obj
  240. if panel_obj is None:
  241. self.app.inform.emit(_("[ERROR_NOTCL]Object not found: %s") % panel_obj)
  242. return "Object not found: %s" % panel_obj
  243. boxname = self.box_combo.currentText()
  244. try:
  245. box = self.app.collection.get_by_name(boxname)
  246. except:
  247. self.app.inform.emit(_("[ERROR_NOTCL]Could not retrieve object: %s") % boxname)
  248. return "Could not retrieve object: %s" % boxname
  249. if box is None:
  250. self.app.inform.emit(_("[WARNING]No object Box. Using instead %s") % panel_obj)
  251. box = panel_obj
  252. self.outname = name + '_panelized'
  253. try:
  254. spacing_columns = float(self.spacing_columns.get_value())
  255. except ValueError:
  256. # try to convert comma to decimal point. if it's still not working error message and return
  257. try:
  258. spacing_columns = float(self.spacing_columns.get_value().replace(',', '.'))
  259. except ValueError:
  260. self.app.inform.emit(_("[ERROR_NOTCL]Wrong value format entered, "
  261. "use a number."))
  262. return
  263. spacing_columns = spacing_columns if spacing_columns is not None else 0
  264. try:
  265. spacing_rows = float(self.spacing_rows.get_value())
  266. except ValueError:
  267. # try to convert comma to decimal point. if it's still not working error message and return
  268. try:
  269. spacing_rows = float(self.spacing_rows.get_value().replace(',', '.'))
  270. except ValueError:
  271. self.app.inform.emit(_("[ERROR_NOTCL]Wrong value format entered, "
  272. "use a number."))
  273. return
  274. spacing_rows = spacing_rows if spacing_rows is not None else 0
  275. try:
  276. rows = int(self.rows.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. rows = float(self.rows.get_value().replace(',', '.'))
  281. rows = int(rows)
  282. except ValueError:
  283. self.app.inform.emit(_("[ERROR_NOTCL]Wrong value format entered, "
  284. "use a number."))
  285. return
  286. rows = rows if rows is not None else 1
  287. try:
  288. columns = int(self.columns.get_value())
  289. except ValueError:
  290. # try to convert comma to decimal point. if it's still not working error message and return
  291. try:
  292. columns = float(self.columns.get_value().replace(',', '.'))
  293. columns = int(columns)
  294. except ValueError:
  295. self.app.inform.emit(_("[ERROR_NOTCL]Wrong value format entered, "
  296. "use a number."))
  297. return
  298. columns = columns if columns is not None else 1
  299. try:
  300. constrain_dx = float(self.x_width_entry.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. constrain_dx = float(self.x_width_entry.get_value().replace(',', '.'))
  305. except ValueError:
  306. self.app.inform.emit(_("[ERROR_NOTCL]Wrong value format entered, "
  307. "use a number."))
  308. return
  309. try:
  310. constrain_dy = float(self.y_height_entry.get_value())
  311. except ValueError:
  312. # try to convert comma to decimal point. if it's still not working error message and return
  313. try:
  314. constrain_dy = float(self.y_height_entry.get_value().replace(',', '.'))
  315. except ValueError:
  316. self.app.inform.emit(_("[ERROR_NOTCL]Wrong value format entered, "
  317. "use a number."))
  318. return
  319. panel_type = str(self.panel_type_radio.get_value())
  320. if 0 in {columns, rows}:
  321. self.app.inform.emit(_("[ERROR_NOTCL]Columns or Rows are zero value. Change them to a positive integer."))
  322. return "Columns or Rows are zero value. Change them to a positive integer."
  323. xmin, ymin, xmax, ymax = box.bounds()
  324. lenghtx = xmax - xmin + spacing_columns
  325. lenghty = ymax - ymin + spacing_rows
  326. # check if constrain within an area is desired
  327. if self.constrain_cb.isChecked():
  328. panel_lengthx = ((xmax - xmin) * columns) + (spacing_columns * (columns - 1))
  329. panel_lengthy = ((ymax - ymin) * rows) + (spacing_rows * (rows - 1))
  330. # adjust the number of columns and/or rows so the panel will fit within the panel constraint area
  331. if (panel_lengthx > constrain_dx) or (panel_lengthy > constrain_dy):
  332. self.constrain_flag = True
  333. while panel_lengthx > constrain_dx:
  334. columns -= 1
  335. panel_lengthx = ((xmax - xmin) * columns) + (spacing_columns * (columns - 1))
  336. while panel_lengthy > constrain_dy:
  337. rows -= 1
  338. panel_lengthy = ((ymax - ymin) * rows) + (spacing_rows * (rows - 1))
  339. def panelize_2():
  340. if panel_obj is not None:
  341. self.app.inform.emit(_("Generating panel ... Please wait."))
  342. self.app.progress.emit(0)
  343. def job_init_excellon(obj_fin, app_obj):
  344. currenty = 0.0
  345. self.app.progress.emit(10)
  346. obj_fin.tools = panel_obj.tools.copy()
  347. obj_fin.drills = []
  348. obj_fin.slots = []
  349. obj_fin.solid_geometry = []
  350. for option in panel_obj.options:
  351. if option is not 'name':
  352. try:
  353. obj_fin.options[option] = panel_obj.options[option]
  354. except:
  355. log.warning("Failed to copy option.", option)
  356. for row in range(rows):
  357. currentx = 0.0
  358. for col in range(columns):
  359. if panel_obj.drills:
  360. for tool_dict in panel_obj.drills:
  361. point_offseted = affinity.translate(tool_dict['point'], currentx, currenty)
  362. obj_fin.drills.append(
  363. {
  364. "point": point_offseted,
  365. "tool": tool_dict['tool']
  366. }
  367. )
  368. if panel_obj.slots:
  369. for tool_dict in panel_obj.slots:
  370. start_offseted = affinity.translate(tool_dict['start'], currentx, currenty)
  371. stop_offseted = affinity.translate(tool_dict['stop'], currentx, currenty)
  372. obj_fin.slots.append(
  373. {
  374. "start": start_offseted,
  375. "stop": stop_offseted,
  376. "tool": tool_dict['tool']
  377. }
  378. )
  379. currentx += lenghtx
  380. currenty += lenghty
  381. obj_fin.create_geometry()
  382. obj_fin.zeros = panel_obj.zeros
  383. obj_fin.units = panel_obj.units
  384. def job_init_geometry(obj_fin, app_obj):
  385. currentx = 0.0
  386. currenty = 0.0
  387. def translate_recursion(geom):
  388. if type(geom) == list:
  389. geoms = list()
  390. for local_geom in geom:
  391. geoms.append(translate_recursion(local_geom))
  392. return geoms
  393. else:
  394. return affinity.translate(geom, xoff=currentx, yoff=currenty)
  395. obj_fin.solid_geometry = []
  396. if isinstance(panel_obj, FlatCAMGeometry):
  397. obj_fin.multigeo = panel_obj.multigeo
  398. obj_fin.tools = deepcopy(panel_obj.tools)
  399. if panel_obj.multigeo is True:
  400. for tool in panel_obj.tools:
  401. obj_fin.tools[tool]['solid_geometry'][:] = []
  402. self.app.progress.emit(0)
  403. for row in range(rows):
  404. currentx = 0.0
  405. for col in range(columns):
  406. if isinstance(panel_obj, FlatCAMGeometry):
  407. if panel_obj.multigeo is True:
  408. for tool in panel_obj.tools:
  409. obj_fin.tools[tool]['solid_geometry'].append(translate_recursion(
  410. panel_obj.tools[tool]['solid_geometry'])
  411. )
  412. else:
  413. obj_fin.solid_geometry.append(
  414. translate_recursion(panel_obj.solid_geometry)
  415. )
  416. else:
  417. obj_fin.solid_geometry.append(
  418. translate_recursion(panel_obj.solid_geometry)
  419. )
  420. currentx += lenghtx
  421. currenty += lenghty
  422. if isinstance(panel_obj, FlatCAMExcellon):
  423. self.app.progress.emit(50)
  424. self.app.new_object("excellon", self.outname, job_init_excellon, plot=True, autoselected=True)
  425. else:
  426. self.app.progress.emit(50)
  427. self.app.new_object(panel_type, self.outname, job_init_geometry,
  428. plot=True, autoselected=True)
  429. if self.constrain_flag is False:
  430. self.app.inform.emit(_("[success]Panel done..."))
  431. else:
  432. self.constrain_flag = False
  433. self.app.inform.emit(_("[WARNING] Too big for the constrain area. Final panel has %s columns and %s rows") %
  434. (columns, rows))
  435. proc = self.app.proc_container.new(_("Generating panel ... Please wait."))
  436. def job_thread(app_obj):
  437. try:
  438. panelize_2()
  439. self.app.inform.emit(_("[success]Panel created successfully."))
  440. except Exception as e:
  441. proc.done()
  442. log.debug(str(e))
  443. return
  444. proc.done()
  445. self.app.collection.promise(self.outname)
  446. self.app.worker_task.emit({'fcn': job_thread, 'params': [self.app]})
  447. def reset_fields(self):
  448. self.object_combo.setRootModelIndex(self.app.collection.index(0, 0, QtCore.QModelIndex()))
  449. self.box_combo.setRootModelIndex(self.app.collection.index(0, 0, QtCore.QModelIndex()))