FlatCAMExcellon.py 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876
  1. # ##########################################################
  2. # FlatCAM: 2D Post-processing for Manufacturing #
  3. # http://flatcam.org #
  4. # Author: Juan Pablo Caram (c) #
  5. # Date: 2/5/2014 #
  6. # MIT Licence #
  7. # ##########################################################
  8. # ##########################################################
  9. # File modified by: Marius Stanciu #
  10. # ##########################################################
  11. from shapely.geometry import Point, LineString
  12. from copy import deepcopy
  13. from appParsers.ParseExcellon import Excellon
  14. from appObjects.FlatCAMObj import *
  15. import itertools
  16. import numpy as np
  17. from collections import defaultdict
  18. import gettext
  19. import appTranslation as fcTranslate
  20. import builtins
  21. fcTranslate.apply_language('strings')
  22. if '_' not in builtins.__dict__:
  23. _ = gettext.gettext
  24. class ExcellonObject(FlatCAMObj, Excellon):
  25. """
  26. Represents Excellon/Drill code. An object stored in the FlatCAM objects collection (a dict)
  27. """
  28. ui_type = ExcellonObjectUI
  29. optionChanged = QtCore.pyqtSignal(str)
  30. def __init__(self, name):
  31. self.decimals = self.app.decimals
  32. self.circle_steps = int(self.app.defaults["geometry_circle_steps"])
  33. Excellon.__init__(self, geo_steps_per_circle=self.circle_steps)
  34. FlatCAMObj.__init__(self, name)
  35. self.kind = "excellon"
  36. self.options.update({
  37. "plot": True,
  38. "solid": False,
  39. "multicolored": False,
  40. "operation": "drill",
  41. "milling_type": "drills",
  42. "milling_dia": 0.04,
  43. "cutz": -0.1,
  44. "multidepth": False,
  45. "depthperpass": 0.7,
  46. "travelz": 0.1,
  47. "feedrate": self.app.defaults["geometry_feedrate"],
  48. "feedrate_z": 5.0,
  49. "feedrate_rapid": 5.0,
  50. "tooldia": 0.1,
  51. "slot_tooldia": 0.1,
  52. "toolchange": False,
  53. "toolchangez": 1.0,
  54. "toolchangexy": "0.0, 0.0",
  55. "extracut": self.app.defaults["geometry_extracut"],
  56. "extracut_length": self.app.defaults["geometry_extracut_length"],
  57. "endz": 2.0,
  58. "endxy": '',
  59. "startz": None,
  60. "offset": 0.0,
  61. "spindlespeed": 0,
  62. "dwell": True,
  63. "dwelltime": 1000,
  64. "ppname_e": 'default',
  65. "ppname_g": self.app.defaults["geometry_ppname_g"],
  66. "z_pdepth": -0.02,
  67. "feedrate_probe": 3.0,
  68. "optimization_type": "B",
  69. })
  70. # TODO: Document this.
  71. self.tool_cbs = {}
  72. # dict that holds the object names and the option name
  73. # the key is the object name (defines in ObjectUI) for each UI element that is a parameter
  74. # particular for a tool and the value is the actual name of the option that the UI element is changing
  75. self.name2option = {}
  76. # default set of data to be added to each tool in self.tools as self.tools[tool]['data'] = self.default_data
  77. self.default_data = {}
  78. # fill in self.default_data values from self.options
  79. for opt_key, opt_val in self.app.options.items():
  80. if opt_key.find('excellon_') == 0:
  81. self.default_data[opt_key] = deepcopy(opt_val)
  82. for opt_key, opt_val in self.app.options.items():
  83. if opt_key.find('geometry_') == 0:
  84. self.default_data[opt_key] = deepcopy(opt_val)
  85. # variable to store the total amount of drills per job
  86. self.tot_drill_cnt = 0
  87. self.tool_row = 0
  88. # variable to store the total amount of slots per job
  89. self.tot_slot_cnt = 0
  90. self.tool_row_slots = 0
  91. # variable to store the distance travelled
  92. self.travel_distance = 0.0
  93. # store the source file here
  94. self.source_file = ""
  95. self.multigeo = False
  96. self.units_found = self.app.defaults['units']
  97. self.fill_color = self.app.defaults['excellon_plot_fill']
  98. self.outline_color = self.app.defaults['excellon_plot_line']
  99. self.alpha_level = 'bf'
  100. # store here the state of the exclusion checkbox state to be restored after building the UI
  101. # TODO add this in the sel.app.defaults dict and in Preferences
  102. self.exclusion_area_cb_is_checked = False
  103. # Attributes to be included in serialization
  104. # Always append to it because it carries contents
  105. # from predecessors.
  106. self.ser_attrs += ['options', 'kind']
  107. @staticmethod
  108. def merge(exc_list, exc_final, decimals=None, fuse_tools=True):
  109. """
  110. Merge Excellon objects found in exc_list parameter into exc_final object.
  111. Options are always copied from source .
  112. Tools are disregarded, what is taken in consideration is the unique drill diameters found as values in the
  113. exc_list tools dict's. In the reconstruction section for each unique tool diameter it will be created a
  114. tool_name to be used in the final Excellon object, exc_final.
  115. If only one object is in exc_list parameter then this function will copy that object in the exc_final
  116. :param exc_list: List or one object of ExcellonObject Objects to join.
  117. :type exc_list: list
  118. :param exc_final: Destination ExcellonObject object.
  119. :type exc_final: class
  120. :param decimals: The number of decimals to be used for diameters
  121. :type decimals: int
  122. :param fuse_tools: If True will try to fuse tools of the same diameter for the Excellon objects
  123. :type fuse_tools: bool
  124. :return: None
  125. """
  126. if exc_final.tools is None:
  127. exc_final.tools = {}
  128. if decimals is None:
  129. decimals = 4
  130. decimals_exc = decimals
  131. try:
  132. flattened_list = list(itertools.chain(*exc_list))
  133. except TypeError:
  134. flattened_list = exc_list
  135. new_tools = {}
  136. total_geo = []
  137. toolid = 0
  138. for exc in flattened_list:
  139. # copy options of the current excellon obj to the final excellon obj
  140. # only the last object options will survive
  141. for option in exc.options:
  142. if option != 'name':
  143. try:
  144. exc_final.options[option] = exc.options[option]
  145. except Exception:
  146. exc.app.log.warning("Failed to copy option.", option)
  147. for tool in exc.tools:
  148. toolid += 1
  149. new_tools[toolid] = exc.tools[tool]
  150. exc_final.tools = deepcopy(new_tools)
  151. # add the zeros and units to the exc_final object
  152. exc_final.zeros = exc.zeros
  153. exc_final.units = exc.units
  154. total_geo += exc.solid_geometry
  155. exc_final.solid_geometry = total_geo
  156. fused_tools_dict = {}
  157. if exc_final.tools and fuse_tools:
  158. toolid = 0
  159. for tool, tool_dict in exc_final.tools.items():
  160. current_tooldia = float('%.*f' % (decimals_exc, tool_dict['tooldia']))
  161. toolid += 1
  162. # calculate all diameters in fused_tools_dict
  163. all_dia = []
  164. if fused_tools_dict:
  165. for f_tool in fused_tools_dict:
  166. all_dia.append(float('%.*f' % (decimals_exc, fused_tools_dict[f_tool]['tooldia'])))
  167. if current_tooldia in all_dia:
  168. # find tool for current_tooldia in fuse_tools
  169. t = None
  170. for f_tool in fused_tools_dict:
  171. if fused_tools_dict[f_tool]['tooldia'] == current_tooldia:
  172. t = f_tool
  173. break
  174. if t:
  175. fused_tools_dict[t]['drills'] += tool_dict['drills']
  176. fused_tools_dict[t]['slots'] += tool_dict['slots']
  177. fused_tools_dict[t]['solid_geometry'] += tool_dict['solid_geometry']
  178. else:
  179. fused_tools_dict[toolid] = tool_dict
  180. fused_tools_dict[toolid]['tooldia'] = current_tooldia
  181. exc_final.tools = fused_tools_dict
  182. # create the geometry for the exc_final object
  183. exc_final.create_geometry()
  184. def build_ui(self):
  185. """
  186. Will (re)build the Excellon UI updating it (the tool table)
  187. :return: None
  188. :rtype:
  189. """
  190. FlatCAMObj.build_ui(self)
  191. # Area Exception - exclusion shape added signal
  192. # first disconnect it from any other object
  193. try:
  194. self.app.exc_areas.e_shape_modified.disconnect()
  195. except (TypeError, AttributeError):
  196. pass
  197. # then connect it to the current build_ui() method
  198. self.app.exc_areas.e_shape_modified.connect(self.update_exclusion_table)
  199. self.units = self.app.defaults['units'].upper()
  200. for row in range(self.ui.tools_table.rowCount()):
  201. try:
  202. # if connected, disconnect the signal from the slot on item_changed as it creates issues
  203. offset_spin_widget = self.ui.tools_table.cellWidget(row, 4)
  204. offset_spin_widget.valueChanged.disconnect()
  205. except (TypeError, AttributeError):
  206. pass
  207. n = len(self.tools)
  208. # we have (n+2) rows because there are 'n' tools, each a row, plus the last 2 rows for totals.
  209. self.ui.tools_table.setRowCount(n + 2)
  210. self.tot_drill_cnt = 0
  211. self.tot_slot_cnt = 0
  212. self.tool_row = 0
  213. sort = []
  214. for k, v in list(self.tools.items()):
  215. sort.append((k, v['tooldia']))
  216. sorted_tools = sorted(sort, key=lambda t1: t1[1])
  217. tools = [i[0] for i in sorted_tools]
  218. new_options = {}
  219. for opt in self.options:
  220. new_options[opt] = self.options[opt]
  221. for tool_no in tools:
  222. # add the data dictionary for each tool with the default values
  223. self.tools[tool_no]['data'] = deepcopy(new_options)
  224. # self.tools[tool_no]['data']["tooldia"] = self.tools[tool_no]["C"]
  225. # self.tools[tool_no]['data']["slot_tooldia"] = self.tools[tool_no]["C"]
  226. drill_cnt = 0 # variable to store the nr of drills per tool
  227. slot_cnt = 0 # variable to store the nr of slots per tool
  228. # Find no of drills for the current tool
  229. try:
  230. drill_cnt = len(self.tools[tool_no]['drills'])
  231. except KeyError:
  232. drill_cnt = 0
  233. self.tot_drill_cnt += drill_cnt
  234. # Find no of slots for the current tool
  235. try:
  236. slot_cnt = len(self.tools[tool_no]['slots'])
  237. except KeyError:
  238. slot_cnt = 0
  239. self.tot_slot_cnt += slot_cnt
  240. exc_id_item = QtWidgets.QTableWidgetItem('%d' % int(tool_no))
  241. exc_id_item.setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled)
  242. dia_item = QtWidgets.QTableWidgetItem('%.*f' % (self.decimals, self.tools[tool_no]['tooldia']))
  243. dia_item.setFlags(QtCore.Qt.ItemIsEnabled)
  244. drill_count_item = QtWidgets.QTableWidgetItem('%d' % drill_cnt)
  245. drill_count_item.setFlags(QtCore.Qt.ItemIsEnabled)
  246. # if the slot number is zero is better to not clutter the GUI with zero's so we print a space
  247. slot_count_str = '%d' % slot_cnt if slot_cnt > 0 else ''
  248. slot_count_item = QtWidgets.QTableWidgetItem(slot_count_str)
  249. slot_count_item.setFlags(QtCore.Qt.ItemIsEnabled)
  250. plot_item = FCCheckBox()
  251. plot_item.setLayoutDirection(QtCore.Qt.RightToLeft)
  252. if self.ui.plot_cb.isChecked():
  253. plot_item.setChecked(True)
  254. self.ui.tools_table.setItem(self.tool_row, 0, exc_id_item) # Tool name/id
  255. self.ui.tools_table.setItem(self.tool_row, 1, dia_item) # Diameter
  256. self.ui.tools_table.setItem(self.tool_row, 2, drill_count_item) # Number of drills per tool
  257. self.ui.tools_table.setItem(self.tool_row, 3, slot_count_item) # Number of drills per tool
  258. empty_plot_item = QtWidgets.QTableWidgetItem('')
  259. empty_plot_item.setFlags(~QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled)
  260. self.ui.tools_table.setItem(self.tool_row, 5, empty_plot_item)
  261. self.ui.tools_table.setCellWidget(self.tool_row, 5, plot_item)
  262. self.tool_row += 1
  263. # add a last row with the Total number of drills
  264. empty_1 = QtWidgets.QTableWidgetItem('')
  265. empty_1.setFlags(~QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled)
  266. empty_1_1 = QtWidgets.QTableWidgetItem('')
  267. empty_1_1.setFlags(~QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled)
  268. empty_1_2 = QtWidgets.QTableWidgetItem('')
  269. empty_1_2.setFlags(~QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled)
  270. empty_1_3 = QtWidgets.QTableWidgetItem('')
  271. empty_1_3.setFlags(~QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled)
  272. label_tot_drill_count = QtWidgets.QTableWidgetItem(_('Total Drills'))
  273. tot_drill_count = QtWidgets.QTableWidgetItem('%d' % self.tot_drill_cnt)
  274. label_tot_drill_count.setFlags(QtCore.Qt.ItemIsEnabled)
  275. tot_drill_count.setFlags(QtCore.Qt.ItemIsEnabled)
  276. self.ui.tools_table.setItem(self.tool_row, 0, empty_1)
  277. self.ui.tools_table.setItem(self.tool_row, 1, label_tot_drill_count)
  278. self.ui.tools_table.setItem(self.tool_row, 2, tot_drill_count) # Total number of drills
  279. self.ui.tools_table.setItem(self.tool_row, 3, empty_1_1)
  280. self.ui.tools_table.setItem(self.tool_row, 5, empty_1_3)
  281. font = QtGui.QFont()
  282. font.setBold(True)
  283. font.setWeight(75)
  284. for k in [1, 2]:
  285. self.ui.tools_table.item(self.tool_row, k).setForeground(QtGui.QColor(127, 0, 255))
  286. self.ui.tools_table.item(self.tool_row, k).setFont(font)
  287. self.tool_row += 1
  288. # add a last row with the Total number of slots
  289. empty_2 = QtWidgets.QTableWidgetItem('')
  290. empty_2.setFlags(~QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled)
  291. empty_2_1 = QtWidgets.QTableWidgetItem('')
  292. empty_2_1.setFlags(~QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled)
  293. empty_2_2 = QtWidgets.QTableWidgetItem('')
  294. empty_2_2.setFlags(~QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled)
  295. empty_2_3 = QtWidgets.QTableWidgetItem('')
  296. empty_2_3.setFlags(~QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled)
  297. label_tot_slot_count = QtWidgets.QTableWidgetItem(_('Total Slots'))
  298. tot_slot_count = QtWidgets.QTableWidgetItem('%d' % self.tot_slot_cnt)
  299. label_tot_slot_count.setFlags(QtCore.Qt.ItemIsEnabled)
  300. tot_slot_count.setFlags(QtCore.Qt.ItemIsEnabled)
  301. self.ui.tools_table.setItem(self.tool_row, 0, empty_2)
  302. self.ui.tools_table.setItem(self.tool_row, 1, label_tot_slot_count)
  303. self.ui.tools_table.setItem(self.tool_row, 2, empty_2_1)
  304. self.ui.tools_table.setItem(self.tool_row, 3, tot_slot_count) # Total number of slots
  305. self.ui.tools_table.setItem(self.tool_row, 5, empty_2_3)
  306. for kl in [1, 2, 3]:
  307. self.ui.tools_table.item(self.tool_row, kl).setFont(font)
  308. self.ui.tools_table.item(self.tool_row, kl).setForeground(QtGui.QColor(0, 70, 255))
  309. # sort the tool diameter column
  310. # self.ui.tools_table.sortItems(1)
  311. # all the tools are selected by default
  312. self.ui.tools_table.selectColumn(0)
  313. self.ui.tools_table.resizeColumnsToContents()
  314. self.ui.tools_table.resizeRowsToContents()
  315. vertical_header = self.ui.tools_table.verticalHeader()
  316. # vertical_header.setSectionResizeMode(QtWidgets.QHeaderView.ResizeToContents)
  317. vertical_header.hide()
  318. self.ui.tools_table.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
  319. horizontal_header = self.ui.tools_table.horizontalHeader()
  320. horizontal_header.setMinimumSectionSize(10)
  321. horizontal_header.setDefaultSectionSize(70)
  322. horizontal_header.setSectionResizeMode(0, QtWidgets.QHeaderView.Fixed)
  323. horizontal_header.resizeSection(0, 20)
  324. horizontal_header.setSectionResizeMode(1, QtWidgets.QHeaderView.Stretch)
  325. horizontal_header.setSectionResizeMode(2, QtWidgets.QHeaderView.ResizeToContents)
  326. horizontal_header.setSectionResizeMode(3, QtWidgets.QHeaderView.ResizeToContents)
  327. horizontal_header.setSectionResizeMode(5, QtWidgets.QHeaderView.Fixed)
  328. horizontal_header.resizeSection(5, 17)
  329. self.ui.tools_table.setColumnWidth(5, 17)
  330. # horizontal_header.setStretchLastSection(True)
  331. # horizontal_header.setColumnWidth(2, QtWidgets.QHeaderView.ResizeToContents)
  332. # horizontal_header.setStretchLastSection(True)
  333. self.ui.tools_table.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
  334. self.ui.tools_table.setSortingEnabled(False)
  335. self.ui.tools_table.setMinimumHeight(self.ui.tools_table.getHeight())
  336. self.ui.tools_table.setMaximumHeight(self.ui.tools_table.getHeight())
  337. # find if we have drills:
  338. has_drills = None
  339. for tt in self.tools:
  340. if 'drills' in self.tools[tt] and self.tools[tt]['drills']:
  341. has_drills = True
  342. break
  343. if has_drills is None:
  344. self.ui.tooldia_entry.hide()
  345. self.ui.generate_milling_button.hide()
  346. else:
  347. self.ui.tooldia_entry.show()
  348. self.ui.generate_milling_button.show()
  349. # find if we have slots
  350. has_slots = None
  351. for tt in self.tools:
  352. if 'slots' in self.tools[tt] and self.tools[tt]['slots']:
  353. has_slots = True
  354. break
  355. if has_slots is None:
  356. self.ui.slot_tooldia_entry.hide()
  357. self.ui.generate_milling_slots_button.hide()
  358. else:
  359. self.ui.slot_tooldia_entry.show()
  360. self.ui.generate_milling_slots_button.show()
  361. # set the text on tool_data_label after loading the object
  362. sel_items = self.ui.tools_table.selectedItems()
  363. sel_rows = [it.row() for it in sel_items]
  364. if len(sel_rows) > 1:
  365. self.ui.tool_data_label.setText(
  366. "<b>%s: <font color='#0000FF'>%s</font></b>" % (_('Parameters for'), _("Multiple Tools"))
  367. )
  368. # Build Exclusion Areas section
  369. e_len = len(self.app.exc_areas.exclusion_areas_storage)
  370. self.ui.exclusion_table.setRowCount(e_len)
  371. area_id = 0
  372. for area in range(e_len):
  373. area_id += 1
  374. area_dict = self.app.exc_areas.exclusion_areas_storage[area]
  375. area_id_item = QtWidgets.QTableWidgetItem('%d' % int(area_id))
  376. area_id_item.setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled)
  377. self.ui.exclusion_table.setItem(area, 0, area_id_item) # Area id
  378. object_item = QtWidgets.QTableWidgetItem('%s' % area_dict["obj_type"])
  379. object_item.setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled)
  380. self.ui.exclusion_table.setItem(area, 1, object_item) # Origin Object
  381. strategy_item = QtWidgets.QTableWidgetItem('%s' % area_dict["strategy"])
  382. strategy_item.setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled)
  383. self.ui.exclusion_table.setItem(area, 2, strategy_item) # Strategy
  384. overz_item = QtWidgets.QTableWidgetItem('%s' % area_dict["overz"])
  385. overz_item.setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled)
  386. self.ui.exclusion_table.setItem(area, 3, overz_item) # Over Z
  387. self.ui.exclusion_table.resizeColumnsToContents()
  388. self.ui.exclusion_table.resizeRowsToContents()
  389. area_vheader = self.ui.exclusion_table.verticalHeader()
  390. area_vheader.hide()
  391. self.ui.exclusion_table.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
  392. area_hheader = self.ui.exclusion_table.horizontalHeader()
  393. area_hheader.setMinimumSectionSize(10)
  394. area_hheader.setDefaultSectionSize(70)
  395. area_hheader.setSectionResizeMode(0, QtWidgets.QHeaderView.Fixed)
  396. area_hheader.resizeSection(0, 20)
  397. area_hheader.setSectionResizeMode(1, QtWidgets.QHeaderView.Stretch)
  398. area_hheader.setSectionResizeMode(2, QtWidgets.QHeaderView.ResizeToContents)
  399. area_hheader.setSectionResizeMode(3, QtWidgets.QHeaderView.ResizeToContents)
  400. # area_hheader.setStretchLastSection(True)
  401. self.ui.exclusion_table.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
  402. self.ui.exclusion_table.setColumnWidth(0, 20)
  403. self.ui.exclusion_table.setMinimumHeight(self.ui.exclusion_table.getHeight())
  404. self.ui.exclusion_table.setMaximumHeight(self.ui.exclusion_table.getHeight())
  405. self.ui_connect()
  406. def set_ui(self, ui):
  407. """
  408. Configures the user interface for this object.
  409. Connects options to form fields.
  410. :param ui: User interface object.
  411. :type ui: ExcellonObjectUI
  412. :return: None
  413. """
  414. FlatCAMObj.set_ui(self, ui)
  415. log.debug("ExcellonObject.set_ui()")
  416. self.units = self.app.defaults['units'].upper()
  417. self.form_fields.update({
  418. "plot": self.ui.plot_cb,
  419. "solid": self.ui.solid_cb,
  420. "multicolored": self.ui.multicolored_cb,
  421. "operation": self.ui.operation_radio,
  422. "milling_type": self.ui.milling_type_radio,
  423. "milling_dia": self.ui.mill_dia_entry,
  424. "cutz": self.ui.cutz_entry,
  425. "multidepth": self.ui.mpass_cb,
  426. "depthperpass": self.ui.maxdepth_entry,
  427. "travelz": self.ui.travelz_entry,
  428. "feedrate_z": self.ui.feedrate_z_entry,
  429. "feedrate": self.ui.xyfeedrate_entry,
  430. "feedrate_rapid": self.ui.feedrate_rapid_entry,
  431. "tooldia": self.ui.tooldia_entry,
  432. "slot_tooldia": self.ui.slot_tooldia_entry,
  433. "toolchange": self.ui.toolchange_cb,
  434. "toolchangez": self.ui.toolchangez_entry,
  435. "extracut": self.ui.extracut_cb,
  436. "extracut_length": self.ui.e_cut_entry,
  437. "spindlespeed": self.ui.spindlespeed_entry,
  438. "dwell": self.ui.dwell_cb,
  439. "dwelltime": self.ui.dwelltime_entry,
  440. "startz": self.ui.estartz_entry,
  441. "endz": self.ui.endz_entry,
  442. "endxy": self.ui.endxy_entry,
  443. "offset": self.ui.offset_entry,
  444. "ppname_e": self.ui.pp_excellon_name_cb,
  445. "ppname_g": self.ui.pp_geo_name_cb,
  446. "z_pdepth": self.ui.pdepth_entry,
  447. "feedrate_probe": self.ui.feedrate_probe_entry,
  448. # "gcode_type": self.ui.excellon_gcode_type_radio,
  449. "area_exclusion": self.ui.exclusion_cb,
  450. "area_shape": self.ui.area_shape_radio,
  451. "area_strategy": self.ui.strategy_radio,
  452. "area_overz": self.ui.over_z_entry,
  453. })
  454. self.name2option = {
  455. "e_operation": "operation",
  456. "e_milling_type": "milling_type",
  457. "e_milling_dia": "milling_dia",
  458. "e_cutz": "cutz",
  459. "e_multidepth": "multidepth",
  460. "e_depthperpass": "depthperpass",
  461. "e_travelz": "travelz",
  462. "e_feedratexy": "feedrate",
  463. "e_feedratez": "feedrate_z",
  464. "e_fr_rapid": "feedrate_rapid",
  465. "e_extracut": "extracut",
  466. "e_extracut_length": "extracut_length",
  467. "e_spindlespeed": "spindlespeed",
  468. "e_dwell": "dwell",
  469. "e_dwelltime": "dwelltime",
  470. "e_offset": "offset",
  471. }
  472. # populate Excellon preprocessor combobox list
  473. for name in list(self.app.preprocessors.keys()):
  474. # the HPGL preprocessor is only for Geometry not for Excellon job therefore don't add it
  475. if name == 'hpgl':
  476. continue
  477. self.ui.pp_excellon_name_cb.addItem(name)
  478. # populate Geometry (milling) preprocessor combobox list
  479. for name in list(self.app.preprocessors.keys()):
  480. self.ui.pp_geo_name_cb.addItem(name)
  481. # Fill form fields
  482. self.to_form()
  483. # update the changes in UI depending on the selected preprocessor in Preferences
  484. # after this moment all the changes in the Posprocessor combo will be handled by the activated signal of the
  485. # self.ui.pp_excellon_name_cb combobox
  486. self.on_pp_changed()
  487. # Show/Hide Advanced Options
  488. if self.app.defaults["global_app_level"] == 'b':
  489. self.ui.level.setText('<span style="color:green;"><b>%s</b></span>' % _('Basic'))
  490. self.ui.tools_table.setColumnHidden(4, True)
  491. self.ui.tools_table.setColumnHidden(5, True)
  492. self.ui.estartz_label.hide()
  493. self.ui.estartz_entry.hide()
  494. self.ui.feedrate_rapid_label.hide()
  495. self.ui.feedrate_rapid_entry.hide()
  496. self.ui.pdepth_label.hide()
  497. self.ui.pdepth_entry.hide()
  498. self.ui.feedrate_probe_label.hide()
  499. self.ui.feedrate_probe_entry.hide()
  500. else:
  501. self.ui.level.setText('<span style="color:red;"><b>%s</b></span>' % _('Advanced'))
  502. assert isinstance(self.ui, ExcellonObjectUI), \
  503. "Expected a ExcellonObjectUI, got %s" % type(self.ui)
  504. self.ui.plot_cb.stateChanged.connect(self.on_plot_cb_click)
  505. self.ui.solid_cb.stateChanged.connect(self.on_solid_cb_click)
  506. self.ui.multicolored_cb.stateChanged.connect(self.on_multicolored_cb_click)
  507. self.ui.generate_cnc_button.clicked.connect(self.on_create_cncjob_button_click)
  508. self.ui.generate_milling_button.clicked.connect(self.on_generate_milling_button_click)
  509. self.ui.generate_milling_slots_button.clicked.connect(self.on_generate_milling_slots_button_click)
  510. # Exclusion areas signals
  511. self.ui.exclusion_table.horizontalHeader().sectionClicked.connect(self.exclusion_table_toggle_all)
  512. self.ui.exclusion_table.lost_focus.connect(self.clear_selection)
  513. self.ui.exclusion_table.itemClicked.connect(self.draw_sel_shape)
  514. self.ui.add_area_button.clicked.connect(self.on_add_area_click)
  515. self.ui.delete_area_button.clicked.connect(self.on_clear_area_click)
  516. self.ui.delete_sel_area_button.clicked.connect(self.on_delete_sel_areas)
  517. self.ui.strategy_radio.activated_custom.connect(self.on_strategy)
  518. self.on_operation_type(val='drill')
  519. self.ui.operation_radio.activated_custom.connect(self.on_operation_type)
  520. self.ui.pp_excellon_name_cb.activated.connect(self.on_pp_changed)
  521. self.ui.apply_param_to_all.clicked.connect(self.on_apply_param_to_all_clicked)
  522. self.units_found = self.app.defaults['units']
  523. # ########################################
  524. # #######3 TEMP SETTINGS #################
  525. # ########################################
  526. self.ui.operation_radio.set_value("drill")
  527. self.ui.operation_radio.setEnabled(False)
  528. def ui_connect(self):
  529. """
  530. Will connect all signals in the Excellon UI that needs to be connected
  531. :return: None
  532. :rtype:
  533. """
  534. # selective plotting
  535. for row in range(self.ui.tools_table.rowCount() - 2):
  536. self.ui.tools_table.cellWidget(row, 5).clicked.connect(self.on_plot_cb_click_table)
  537. self.ui.plot_cb.stateChanged.connect(self.on_plot_cb_click)
  538. # rows selected
  539. self.ui.tools_table.clicked.connect(self.on_row_selection_change)
  540. self.ui.tools_table.horizontalHeader().sectionClicked.connect(self.on_row_selection_change)
  541. # value changed in the particular parameters of a tool
  542. for key, option in self.name2option.items():
  543. current_widget = self.form_fields[option]
  544. if isinstance(current_widget, FCCheckBox):
  545. current_widget.stateChanged.connect(self.form_to_storage)
  546. if isinstance(current_widget, RadioSet):
  547. current_widget.activated_custom.connect(self.form_to_storage)
  548. elif isinstance(current_widget, FCDoubleSpinner) or isinstance(current_widget, FCSpinner):
  549. current_widget.returnPressed.connect(self.form_to_storage)
  550. def ui_disconnect(self):
  551. """
  552. Will disconnect all signals in the Excellon UI that needs to be disconnected
  553. :return: None
  554. :rtype:
  555. """
  556. # selective plotting
  557. for row in range(self.ui.tools_table.rowCount()):
  558. try:
  559. self.ui.tools_table.cellWidget(row, 5).clicked.disconnect()
  560. except (TypeError, AttributeError):
  561. pass
  562. try:
  563. self.ui.plot_cb.stateChanged.disconnect()
  564. except (TypeError, AttributeError):
  565. pass
  566. # rows selected
  567. try:
  568. self.ui.tools_table.clicked.disconnect()
  569. except (TypeError, AttributeError):
  570. pass
  571. try:
  572. self.ui.tools_table.horizontalHeader().sectionClicked.disconnect()
  573. except (TypeError, AttributeError):
  574. pass
  575. # value changed in the particular parameters of a tool
  576. for key, option in self.name2option.items():
  577. current_widget = self.form_fields[option]
  578. if isinstance(current_widget, FCCheckBox):
  579. try:
  580. current_widget.stateChanged.disconnect(self.form_to_storage)
  581. except (TypeError, ValueError):
  582. pass
  583. if isinstance(current_widget, RadioSet):
  584. try:
  585. current_widget.activated_custom.disconnect(self.form_to_storage)
  586. except (TypeError, ValueError):
  587. pass
  588. elif isinstance(current_widget, FCDoubleSpinner) or isinstance(current_widget, FCSpinner):
  589. try:
  590. current_widget.returnPressed.disconnect(self.form_to_storage)
  591. except (TypeError, ValueError):
  592. pass
  593. def on_row_selection_change(self):
  594. """
  595. Called when the user clicks on a row in Tools Table
  596. :return: None
  597. :rtype:
  598. """
  599. self.ui_disconnect()
  600. sel_rows = []
  601. sel_items = self.ui.tools_table.selectedItems()
  602. for it in sel_items:
  603. sel_rows.append(it.row())
  604. if not sel_rows:
  605. self.ui.tool_data_label.setText(
  606. "<b>%s: <font color='#0000FF'>%s</font></b>" % (_('Parameters for'), _("No Tool Selected"))
  607. )
  608. self.ui.generate_cnc_button.setDisabled(True)
  609. self.ui.generate_milling_button.setDisabled(True)
  610. self.ui.generate_milling_slots_button.setDisabled(True)
  611. self.ui_connect()
  612. return
  613. else:
  614. self.ui.generate_cnc_button.setDisabled(False)
  615. self.ui.generate_milling_button.setDisabled(False)
  616. self.ui.generate_milling_slots_button.setDisabled(False)
  617. if len(sel_rows) == 1:
  618. # update the QLabel that shows for which Tool we have the parameters in the UI form
  619. tooluid = int(self.ui.tools_table.item(sel_rows[0], 0).text())
  620. self.ui.tool_data_label.setText(
  621. "<b>%s: <font color='#0000FF'>%s %d</font></b>" % (_('Parameters for'), _("Tool"), tooluid)
  622. )
  623. else:
  624. self.ui.tool_data_label.setText(
  625. "<b>%s: <font color='#0000FF'>%s</font></b>" % (_('Parameters for'), _("Multiple Tools"))
  626. )
  627. for c_row in sel_rows:
  628. # populate the form with the data from the tool associated with the row parameter
  629. try:
  630. item = self.ui.tools_table.item(c_row, 0)
  631. if type(item) is not None:
  632. tooluid = item.text()
  633. self.storage_to_form(self.tools[str(tooluid)]['data'])
  634. else:
  635. self.ui_connect()
  636. return
  637. except Exception as e:
  638. log.debug("Tool missing. Add a tool in Geo Tool Table. %s" % str(e))
  639. self.ui_connect()
  640. return
  641. self.ui_connect()
  642. def storage_to_form(self, dict_storage):
  643. """
  644. Will update the GUI with data from the "storage" in this case the dict self.tools
  645. :param dict_storage: A dictionary holding the data relevant for gnerating Gcode from Excellon
  646. :type dict_storage: dict
  647. :return: None
  648. :rtype:
  649. """
  650. for form_key in self.form_fields:
  651. for storage_key in dict_storage:
  652. if form_key == storage_key and form_key not in \
  653. ["toolchange", "toolchangez", "startz", "endz", "ppname_e", "ppname_g"]:
  654. try:
  655. self.form_fields[form_key].set_value(dict_storage[form_key])
  656. except Exception as e:
  657. log.debug("ExcellonObject.storage_to_form() --> %s" % str(e))
  658. pass
  659. def form_to_storage(self):
  660. """
  661. Will update the 'storage' attribute which is the dict self.tools with data collected from GUI
  662. :return: None
  663. :rtype:
  664. """
  665. if self.ui.tools_table.rowCount() == 0:
  666. # there is no tool in tool table so we can't save the GUI elements values to storage
  667. return
  668. self.ui_disconnect()
  669. widget_changed = self.sender()
  670. wdg_objname = widget_changed.objectName()
  671. option_changed = self.name2option[wdg_objname]
  672. # row = self.ui.tools_table.currentRow()
  673. rows = sorted(set(index.row() for index in self.ui.tools_table.selectedIndexes()))
  674. for row in rows:
  675. if row < 0:
  676. row = 0
  677. tooluid_item = int(self.ui.tools_table.item(row, 0).text())
  678. for tooluid_key, tooluid_val in self.tools.items():
  679. if int(tooluid_key) == tooluid_item:
  680. new_option_value = self.form_fields[option_changed].get_value()
  681. if option_changed in tooluid_val:
  682. tooluid_val[option_changed] = new_option_value
  683. if option_changed in tooluid_val['data']:
  684. tooluid_val['data'][option_changed] = new_option_value
  685. self.ui_connect()
  686. def on_operation_type(self, val):
  687. """
  688. Called by a RadioSet activated_custom signal
  689. :param val: Parameter passes by the signal that called this method
  690. :type val: str
  691. :return: None
  692. :rtype:
  693. """
  694. if val == 'mill':
  695. self.ui.mill_type_label.show()
  696. self.ui.milling_type_radio.show()
  697. self.ui.mill_dia_label.show()
  698. self.ui.mill_dia_entry.show()
  699. self.ui.frxylabel.show()
  700. self.ui.xyfeedrate_entry.show()
  701. self.ui.extracut_cb.show()
  702. self.ui.e_cut_entry.show()
  703. # if 'laser' not in self.ui.pp_excellon_name_cb.get_value().lower():
  704. # self.ui.mpass_cb.show()
  705. # self.ui.maxdepth_entry.show()
  706. else:
  707. self.ui.mill_type_label.hide()
  708. self.ui.milling_type_radio.hide()
  709. self.ui.mill_dia_label.hide()
  710. self.ui.mill_dia_entry.hide()
  711. # self.ui.mpass_cb.hide()
  712. # self.ui.maxdepth_entry.hide()
  713. self.ui.frxylabel.hide()
  714. self.ui.xyfeedrate_entry.hide()
  715. self.ui.extracut_cb.hide()
  716. self.ui.e_cut_entry.hide()
  717. def get_selected_tools_list(self):
  718. """
  719. Returns the keys to the self.tools dictionary corresponding
  720. to the selections on the tool list in the appGUI.
  721. :return: List of tools.
  722. :rtype: list
  723. """
  724. return [x.text() for x in self.ui.tools_table.selectedItems()]
  725. def get_selected_tools_table_items(self):
  726. """
  727. Returns a list of lists, each list in the list is made out of row elements
  728. :return: List of table_tools items.
  729. :rtype: list
  730. """
  731. table_tools_items = []
  732. for x in self.ui.tools_table.selectedItems():
  733. # from the columnCount we subtract a value of 1 which represent the last column (plot column)
  734. # which does not have text
  735. txt = ''
  736. elem = []
  737. for column in range(0, self.ui.tools_table.columnCount() - 1):
  738. try:
  739. txt = self.ui.tools_table.item(x.row(), column).text()
  740. except AttributeError:
  741. try:
  742. txt = self.ui.tools_table.cellWidget(x.row(), column).currentText()
  743. except AttributeError:
  744. pass
  745. elem.append(txt)
  746. table_tools_items.append(deepcopy(elem))
  747. # table_tools_items.append([self.ui.tools_table.item(x.row(), column).text()
  748. # for column in range(0, self.ui.tools_table.columnCount() - 1)])
  749. for item in table_tools_items:
  750. item[0] = str(item[0])
  751. return table_tools_items
  752. def export_excellon(self, whole, fract, e_zeros=None, form='dec', factor=1, slot_type='routing'):
  753. """
  754. Returns two values, first is a boolean , if 1 then the file has slots and second contain the Excellon code
  755. :param whole: Integer part digits
  756. :type whole: int
  757. :param fract: Fractional part digits
  758. :type fract: int
  759. :param e_zeros: Excellon zeros suppression: LZ or TZ
  760. :type e_zeros: str
  761. :param form: Excellon format: 'dec',
  762. :type form: str
  763. :param factor: Conversion factor
  764. :type factor: float
  765. :param slot_type: How to treat slots: "routing" or "drilling"
  766. :type slot_type: str
  767. :return: A tuple: (has_slots, Excellon_code) -> (bool, str)
  768. :rtype: tuple
  769. """
  770. excellon_code = ''
  771. # store here if the file has slots, return 1 if any slots, 0 if only drills
  772. slots_in_file = 0
  773. # find if we have drills:
  774. has_drills = None
  775. for tt in self.tools:
  776. if 'drills' in self.tools[tt] and self.tools[tt]['drills']:
  777. has_drills = True
  778. break
  779. # find if we have slots:
  780. has_slots = None
  781. for tt in self.tools:
  782. if 'slots' in self.tools[tt] and self.tools[tt]['slots']:
  783. has_slots = True
  784. slots_in_file = 1
  785. break
  786. # drills processing
  787. try:
  788. if has_drills:
  789. length = whole + fract
  790. for tool in self.tools:
  791. excellon_code += 'T0%s\n' % str(tool) if int(tool) < 10 else 'T%s\n' % str(tool)
  792. for drill in self.tools[tool]['drills']:
  793. if form == 'dec':
  794. drill_x = drill.x * factor
  795. drill_y = drill.y * factor
  796. excellon_code += "X{:.{dec}f}Y{:.{dec}f}\n".format(drill_x, drill_y, dec=fract)
  797. elif e_zeros == 'LZ':
  798. drill_x = drill.x * factor
  799. drill_y = drill.y * factor
  800. exc_x_formatted = "{:.{dec}f}".format(drill_x, dec=fract)
  801. exc_y_formatted = "{:.{dec}f}".format(drill_y, dec=fract)
  802. # extract whole part and decimal part
  803. exc_x_formatted = exc_x_formatted.partition('.')
  804. exc_y_formatted = exc_y_formatted.partition('.')
  805. # left padd the 'whole' part with zeros
  806. x_whole = exc_x_formatted[0].rjust(whole, '0')
  807. y_whole = exc_y_formatted[0].rjust(whole, '0')
  808. # restore the coordinate padded in the left with 0 and added the decimal part
  809. # without the decinal dot
  810. exc_x_formatted = x_whole + exc_x_formatted[2]
  811. exc_y_formatted = y_whole + exc_y_formatted[2]
  812. excellon_code += "X{xform}Y{yform}\n".format(xform=exc_x_formatted,
  813. yform=exc_y_formatted)
  814. else:
  815. drill_x = drill.x * factor
  816. drill_y = drill.y * factor
  817. exc_x_formatted = "{:.{dec}f}".format(drill_x, dec=fract).replace('.', '')
  818. exc_y_formatted = "{:.{dec}f}".format(drill_y, dec=fract).replace('.', '')
  819. # pad with rear zeros
  820. exc_x_formatted.ljust(length, '0')
  821. exc_y_formatted.ljust(length, '0')
  822. excellon_code += "X{xform}Y{yform}\n".format(xform=exc_x_formatted,
  823. yform=exc_y_formatted)
  824. except Exception as e:
  825. log.debug(str(e))
  826. # slots processing
  827. try:
  828. if has_slots:
  829. for tool in self.tools:
  830. excellon_code += 'G05\n'
  831. if int(tool) < 10:
  832. excellon_code += 'T0' + str(tool) + '\n'
  833. else:
  834. excellon_code += 'T' + str(tool) + '\n'
  835. for slot in self.tools[tool]['slots']:
  836. if form == 'dec':
  837. start_slot_x = slot.x * factor
  838. start_slot_y = slot.y * factor
  839. stop_slot_x = slot.x * factor
  840. stop_slot_y = slot.y * factor
  841. if slot_type == 'routing':
  842. excellon_code += "G00X{:.{dec}f}Y{:.{dec}f}\nM15\n".format(start_slot_x,
  843. start_slot_y,
  844. dec=fract)
  845. excellon_code += "G01X{:.{dec}f}Y{:.{dec}f}\nM16\n".format(stop_slot_x,
  846. stop_slot_y,
  847. dec=fract)
  848. elif slot_type == 'drilling':
  849. excellon_code += "X{:.{dec}f}Y{:.{dec}f}G85X{:.{dec}f}Y{:.{dec}f}\nG05\n".format(
  850. start_slot_x, start_slot_y, stop_slot_x, stop_slot_y, dec=fract
  851. )
  852. elif e_zeros == 'LZ':
  853. start_slot_x = slot.x * factor
  854. start_slot_y = slot.y * factor
  855. stop_slot_x = slot.x * factor
  856. stop_slot_y = slot.y * factor
  857. start_slot_x_formatted = "{:.{dec}f}".format(start_slot_x, dec=fract).replace('.', '')
  858. start_slot_y_formatted = "{:.{dec}f}".format(start_slot_y, dec=fract).replace('.', '')
  859. stop_slot_x_formatted = "{:.{dec}f}".format(stop_slot_x, dec=fract).replace('.', '')
  860. stop_slot_y_formatted = "{:.{dec}f}".format(stop_slot_y, dec=fract).replace('.', '')
  861. # extract whole part and decimal part
  862. start_slot_x_formatted = start_slot_x_formatted.partition('.')
  863. start_slot_y_formatted = start_slot_y_formatted.partition('.')
  864. stop_slot_x_formatted = stop_slot_x_formatted.partition('.')
  865. stop_slot_y_formatted = stop_slot_y_formatted.partition('.')
  866. # left padd the 'whole' part with zeros
  867. start_x_whole = start_slot_x_formatted[0].rjust(whole, '0')
  868. start_y_whole = start_slot_y_formatted[0].rjust(whole, '0')
  869. stop_x_whole = stop_slot_x_formatted[0].rjust(whole, '0')
  870. stop_y_whole = stop_slot_y_formatted[0].rjust(whole, '0')
  871. # restore the coordinate padded in the left with 0 and added the decimal part
  872. # without the decinal dot
  873. start_slot_x_formatted = start_x_whole + start_slot_x_formatted[2]
  874. start_slot_y_formatted = start_y_whole + start_slot_y_formatted[2]
  875. stop_slot_x_formatted = stop_x_whole + stop_slot_x_formatted[2]
  876. stop_slot_y_formatted = stop_y_whole + stop_slot_y_formatted[2]
  877. if slot_type == 'routing':
  878. excellon_code += "G00X{xstart}Y{ystart}\nM15\n".format(xstart=start_slot_x_formatted,
  879. ystart=start_slot_y_formatted)
  880. excellon_code += "G01X{xstop}Y{ystop}\nM16\n".format(xstop=stop_slot_x_formatted,
  881. ystop=stop_slot_y_formatted)
  882. elif slot_type == 'drilling':
  883. excellon_code += "{xstart}Y{ystart}G85X{xstop}Y{ystop}\nG05\n".format(
  884. xstart=start_slot_x_formatted, ystart=start_slot_y_formatted,
  885. xstop=stop_slot_x_formatted, ystop=stop_slot_y_formatted
  886. )
  887. else:
  888. start_slot_x = slot.x * factor
  889. start_slot_y = slot.y * factor
  890. stop_slot_x = slot.x * factor
  891. stop_slot_y = slot.y * factor
  892. length = whole + fract
  893. start_slot_x_formatted = "{:.{dec}f}".format(start_slot_x, dec=fract).replace('.', '')
  894. start_slot_y_formatted = "{:.{dec}f}".format(start_slot_y, dec=fract).replace('.', '')
  895. stop_slot_x_formatted = "{:.{dec}f}".format(stop_slot_x, dec=fract).replace('.', '')
  896. stop_slot_y_formatted = "{:.{dec}f}".format(stop_slot_y, dec=fract).replace('.', '')
  897. # pad with rear zeros
  898. start_slot_x_formatted.ljust(length, '0')
  899. start_slot_y_formatted.ljust(length, '0')
  900. stop_slot_x_formatted.ljust(length, '0')
  901. stop_slot_y_formatted.ljust(length, '0')
  902. if slot_type == 'routing':
  903. excellon_code += "G00X{xstart}Y{ystart}\nM15\n".format(xstart=start_slot_x_formatted,
  904. ystart=start_slot_y_formatted)
  905. excellon_code += "G01X{xstop}Y{ystop}\nM16\n".format(xstop=stop_slot_x_formatted,
  906. ystop=stop_slot_y_formatted)
  907. elif slot_type == 'drilling':
  908. excellon_code += "{xstart}Y{ystart}G85X{xstop}Y{ystop}\nG05\n".format(
  909. xstart=start_slot_x_formatted, ystart=start_slot_y_formatted,
  910. xstop=stop_slot_x_formatted, ystop=stop_slot_y_formatted
  911. )
  912. except Exception as e:
  913. log.debug(str(e))
  914. if not has_drills and not has_slots:
  915. log.debug("FlatCAMObj.ExcellonObject.export_excellon() --> Excellon Object is empty: no drills, no slots.")
  916. return 'fail'
  917. return slots_in_file, excellon_code
  918. def generate_milling_drills(self, tools=None, outname=None, tooldia=None, plot=False, use_thread=False):
  919. """
  920. Will generate an Geometry Object allowing to cut a drill hole instead of drilling it.
  921. Note: This method is a good template for generic operations as
  922. it takes it's options from parameters or otherwise from the
  923. object's options and returns a (success, msg) tuple as feedback
  924. for shell operations.
  925. :param tools: A list of tools where the drills are to be milled or a string: "all"
  926. :type tools:
  927. :param outname: the name of the resulting Geometry object
  928. :type outname: str
  929. :param tooldia: the tool diameter to be used in creation of the milling path (Geometry Object)
  930. :type tooldia: float
  931. :param plot: if to plot the resulting object
  932. :type plot: bool
  933. :param use_thread: if to use threading for creation of the Geometry object
  934. :type use_thread: bool
  935. :return: Success/failure condition tuple (bool, str).
  936. :rtype: tuple
  937. """
  938. # Get the tools from the list. These are keys
  939. # to self.tools
  940. if tools is None:
  941. tools = self.get_selected_tools_list()
  942. if outname is None:
  943. outname = self.options["name"] + "_mill"
  944. if tooldia is None:
  945. tooldia = float(self.options["tooldia"])
  946. # Sort tools by diameter. items() -> [('name', diameter), ...]
  947. # sorted_tools = sorted(list(self.tools.items()), key=lambda tl: tl[1]) # no longer works in Python3
  948. sort = []
  949. for k, v in self.tools.items():
  950. sort.append((k, v['tooldia']))
  951. sorted_tools = sorted(sort, key=lambda t1: t1[1])
  952. if tools == "all":
  953. tools = [i[0] for i in sorted_tools] # List if ordered tool names.
  954. log.debug("Tools 'all' and sorted are: %s" % str(tools))
  955. if len(tools) == 0:
  956. self.app.inform.emit('[ERROR_NOTCL] %s' % _("Please select one or more tools from the list and try again."))
  957. return False, "Error: No tools."
  958. for tool in tools:
  959. if tooldia > self.tools[tool]["tooldia"]:
  960. mseg = '[ERROR_NOTCL] %s %s: %s' % (_("Milling tool for DRILLS is larger than hole size. Cancelled."),
  961. _("Tool"),
  962. str(tool))
  963. self.app.inform.emit(mseg)
  964. return False, "Error: Milling tool is larger than hole."
  965. def geo_init(geo_obj, app_obj):
  966. """
  967. :param geo_obj: New object
  968. :type geo_obj: GeometryObject
  969. :param app_obj: App
  970. :type app_obj: FlatCAMApp.App
  971. :return:
  972. :rtype:
  973. """
  974. assert geo_obj.kind == 'geometry', "Initializer expected a GeometryObject, got %s" % type(geo_obj)
  975. # ## Add properties to the object
  976. # get the tool_table items in a list of row items
  977. tool_table_items = self.get_selected_tools_table_items()
  978. # insert an information only element in the front
  979. tool_table_items.insert(0, [_("Tool_nr"), _("Diameter"), _("Drills_Nr"), _("Slots_Nr")])
  980. geo_obj.options['Tools_in_use'] = tool_table_items
  981. geo_obj.options['type'] = 'Excellon Geometry'
  982. geo_obj.options["cnctooldia"] = str(tooldia)
  983. geo_obj.options["multidepth"] = self.options["multidepth"]
  984. geo_obj.solid_geometry = []
  985. # in case that the tool used has the same diameter with the hole, and since the maximum resolution
  986. # for FlatCAM is 6 decimals,
  987. # we add a tenth of the minimum value, meaning 0.0000001, which from our point of view is "almost zero"
  988. for tool in tools:
  989. for drill in self.tools[tool]['drills']:
  990. buffer_value = self.tools[tool]['tooldia'] / 2 - tooldia / 2
  991. if buffer_value == 0:
  992. geo_obj.solid_geometry.append(drill.buffer(0.0000001).exterior)
  993. else:
  994. geo_obj.solid_geometry.append(drill.buffer(buffer_value).exterior)
  995. if use_thread:
  996. def geo_thread(a_obj):
  997. a_obj.app_obj.new_object("geometry", outname, geo_init, plot=plot)
  998. # Create a promise with the new name
  999. self.app.collection.promise(outname)
  1000. # Send to worker
  1001. self.app.worker_task.emit({'fcn': geo_thread, 'params': [self.app]})
  1002. else:
  1003. self.app.app_obj.new_object("geometry", outname, geo_init, plot=plot)
  1004. return True, ""
  1005. def generate_milling_slots(self, tools=None, outname=None, tooldia=None, plot=False, use_thread=False):
  1006. """
  1007. Will generate an Geometry Object allowing to cut/mill a slot hole.
  1008. Note: This method is a good template for generic operations as
  1009. it takes it's options from parameters or otherwise from the
  1010. object's options and returns a (success, msg) tuple as feedback
  1011. for shell operations.
  1012. :param tools: A list of tools where the drills are to be milled or a string: "all"
  1013. :type tools:
  1014. :param outname: the name of the resulting Geometry object
  1015. :type outname: str
  1016. :param tooldia: the tool diameter to be used in creation of the milling path (Geometry Object)
  1017. :type tooldia: float
  1018. :param plot: if to plot the resulting object
  1019. :type plot: bool
  1020. :param use_thread: if to use threading for creation of the Geometry object
  1021. :type use_thread: bool
  1022. :return: Success/failure condition tuple (bool, str).
  1023. :rtype: tuple
  1024. """
  1025. # Get the tools from the list. These are keys
  1026. # to self.tools
  1027. if tools is None:
  1028. tools = self.get_selected_tools_list()
  1029. if outname is None:
  1030. outname = self.options["name"] + "_mill"
  1031. if tooldia is None:
  1032. tooldia = float(self.options["slot_tooldia"])
  1033. # Sort tools by diameter. items() -> [('name', diameter), ...]
  1034. # sorted_tools = sorted(list(self.tools.items()), key=lambda tl: tl[1]) # no longer works in Python3
  1035. sort = []
  1036. for k, v in self.tools.items():
  1037. sort.append((k, v['tooldia']))
  1038. sorted_tools = sorted(sort, key=lambda t1: t1[1])
  1039. if tools == "all":
  1040. tools = [i[0] for i in sorted_tools] # List if ordered tool names.
  1041. log.debug("Tools 'all' and sorted are: %s" % str(tools))
  1042. if len(tools) == 0:
  1043. self.app.inform.emit('[ERROR_NOTCL] %s' % _("Please select one or more tools from the list and try again."))
  1044. return False, "Error: No tools."
  1045. for tool in tools:
  1046. # I add the 0.0001 value to account for the rounding error in converting from IN to MM and reverse
  1047. adj_toolstable_tooldia = float('%.*f' % (self.decimals, float(tooldia)))
  1048. adj_file_tooldia = float('%.*f' % (self.decimals, float(self.tools[tool]["tooldia"])))
  1049. if adj_toolstable_tooldia > adj_file_tooldia + 0.0001:
  1050. self.app.inform.emit('[ERROR_NOTCL] %s' %
  1051. _("Milling tool for SLOTS is larger than hole size. Cancelled."))
  1052. return False, "Error: Milling tool is larger than hole."
  1053. def geo_init(geo_obj, app_obj):
  1054. assert geo_obj.kind == 'geometry', "Initializer expected a GeometryObject, got %s" % type(geo_obj)
  1055. # ## Add properties to the object
  1056. # get the tool_table items in a list of row items
  1057. tool_table_items = self.get_selected_tools_table_items()
  1058. # insert an information only element in the front
  1059. tool_table_items.insert(0, [_("Tool_nr"), _("Diameter"), _("Drills_Nr"), _("Slots_Nr")])
  1060. geo_obj.options['Tools_in_use'] = tool_table_items
  1061. geo_obj.options['type'] = 'Excellon Geometry'
  1062. geo_obj.options["cnctooldia"] = str(tooldia)
  1063. geo_obj.options["multidepth"] = self.options["multidepth"]
  1064. geo_obj.solid_geometry = []
  1065. # in case that the tool used has the same diameter with the hole, and since the maximum resolution
  1066. # for FlatCAM is 6 decimals,
  1067. # we add a tenth of the minimum value, meaning 0.0000001, which from our point of view is "almost zero"
  1068. for tool in tools:
  1069. for slot in self.tools[tool]['slots']:
  1070. toolstable_tool = float('%.*f' % (self.decimals, float(tooldia)))
  1071. file_tool = float('%.*f' % (self.decimals, float(self.tools[tool]["tooldia"])))
  1072. # I add the 0.0001 value to account for the rounding error in converting from IN to MM and reverse
  1073. # for the file_tool (tooldia actually)
  1074. buffer_value = float(file_tool / 2) - float(toolstable_tool / 2) + 0.0001
  1075. if buffer_value == 0:
  1076. start = slot[0]
  1077. stop = slot[1]
  1078. lines_string = LineString([start, stop])
  1079. poly = lines_string.buffer(0.0000001, int(self.geo_steps_per_circle)).exterior
  1080. geo_obj.solid_geometry.append(poly)
  1081. else:
  1082. start = slot[0]
  1083. stop = slot[1]
  1084. lines_string = LineString([start, stop])
  1085. poly = lines_string.buffer(buffer_value, int(self.geo_steps_per_circle)).exterior
  1086. geo_obj.solid_geometry.append(poly)
  1087. if use_thread:
  1088. def geo_thread(a_obj):
  1089. a_obj.app_obj.new_object("geometry", outname + '_slot', geo_init, plot=plot)
  1090. # Create a promise with the new name
  1091. self.app.collection.promise(outname)
  1092. # Send to worker
  1093. self.app.worker_task.emit({'fcn': geo_thread, 'params': [self.app]})
  1094. else:
  1095. self.app.app_obj.new_object("geometry", outname + '_slot', geo_init, plot=plot)
  1096. return True, ""
  1097. def on_generate_milling_button_click(self, *args):
  1098. self.app.defaults.report_usage("excellon_on_create_milling_drills button")
  1099. self.read_form()
  1100. self.generate_milling_drills(use_thread=False, plot=True)
  1101. def on_generate_milling_slots_button_click(self, *args):
  1102. self.app.defaults.report_usage("excellon_on_create_milling_slots_button")
  1103. self.read_form()
  1104. self.generate_milling_slots(use_thread=False, plot=True)
  1105. def on_pp_changed(self):
  1106. current_pp = self.ui.pp_excellon_name_cb.get_value()
  1107. if "toolchange_probe" in current_pp.lower():
  1108. self.ui.pdepth_entry.setVisible(True)
  1109. self.ui.pdepth_label.show()
  1110. self.ui.feedrate_probe_entry.setVisible(True)
  1111. self.ui.feedrate_probe_label.show()
  1112. else:
  1113. self.ui.pdepth_entry.setVisible(False)
  1114. self.ui.pdepth_label.hide()
  1115. self.ui.feedrate_probe_entry.setVisible(False)
  1116. self.ui.feedrate_probe_label.hide()
  1117. if 'marlin' in current_pp.lower() or 'custom' in current_pp.lower():
  1118. self.ui.feedrate_rapid_label.show()
  1119. self.ui.feedrate_rapid_entry.show()
  1120. else:
  1121. self.ui.feedrate_rapid_label.hide()
  1122. self.ui.feedrate_rapid_entry.hide()
  1123. if 'laser' in current_pp.lower():
  1124. self.ui.cutzlabel.hide()
  1125. self.ui.cutz_entry.hide()
  1126. try:
  1127. self.ui.mpass_cb.hide()
  1128. self.ui.maxdepth_entry.hide()
  1129. except AttributeError:
  1130. pass
  1131. if 'marlin' in current_pp.lower():
  1132. self.ui.travelzlabel.setText('%s:' % _("Focus Z"))
  1133. self.ui.endz_label.show()
  1134. self.ui.endz_entry.show()
  1135. else:
  1136. self.ui.travelzlabel.hide()
  1137. self.ui.travelz_entry.hide()
  1138. self.ui.endz_label.hide()
  1139. self.ui.endz_entry.hide()
  1140. try:
  1141. self.ui.frzlabel.hide()
  1142. self.ui.feedrate_z_entry.hide()
  1143. except AttributeError:
  1144. pass
  1145. self.ui.dwell_cb.hide()
  1146. self.ui.dwelltime_entry.hide()
  1147. self.ui.spindle_label.setText('%s:' % _("Laser Power"))
  1148. try:
  1149. self.ui.tool_offset_label.hide()
  1150. self.ui.offset_entry.hide()
  1151. except AttributeError:
  1152. pass
  1153. else:
  1154. self.ui.cutzlabel.show()
  1155. self.ui.cutz_entry.show()
  1156. try:
  1157. self.ui.mpass_cb.show()
  1158. self.ui.maxdepth_entry.show()
  1159. except AttributeError:
  1160. pass
  1161. self.ui.travelzlabel.setText('%s:' % _('Travel Z'))
  1162. self.ui.travelzlabel.show()
  1163. self.ui.travelz_entry.show()
  1164. self.ui.endz_label.show()
  1165. self.ui.endz_entry.show()
  1166. try:
  1167. self.ui.frzlabel.show()
  1168. self.ui.feedrate_z_entry.show()
  1169. except AttributeError:
  1170. pass
  1171. self.ui.dwell_cb.show()
  1172. self.ui.dwelltime_entry.show()
  1173. self.ui.spindle_label.setText('%s:' % _('Spindle speed'))
  1174. try:
  1175. self.ui.tool_offset_lbl.show()
  1176. self.ui.offset_entry.show()
  1177. except AttributeError:
  1178. pass
  1179. def on_create_cncjob_button_click(self, *args):
  1180. self.app.defaults.report_usage("excellon_on_create_cncjob_button")
  1181. self.read_form()
  1182. # Get the tools from the list
  1183. tools = self.get_selected_tools_list()
  1184. if len(tools) == 0:
  1185. # if there is a single tool in the table (remember that the last 2 rows are for totals and do not count in
  1186. # tool number) it means that there are 3 rows (1 tool and 2 totals).
  1187. # in this case regardless of the selection status of that tool, use it.
  1188. if self.ui.tools_table.rowCount() == 3:
  1189. tools.append(int(self.ui.tools_table.item(0, 0).text()))
  1190. else:
  1191. self.app.inform.emit('[ERROR_NOTCL] %s' %
  1192. _("Please select one or more tools from the list and try again."))
  1193. return
  1194. xmin = self.options['xmin']
  1195. ymin = self.options['ymin']
  1196. xmax = self.options['xmax']
  1197. ymax = self.options['ymax']
  1198. job_name = self.options["name"] + "_cnc"
  1199. pp_excellon_name = self.options["ppname_e"]
  1200. # Object initialization function for app.app_obj.new_object()
  1201. def job_init(job_obj, app_obj):
  1202. assert job_obj.kind == 'cncjob', "Initializer expected a CNCJobObject, got %s" % type(job_obj)
  1203. # get the tool_table items in a list of row items
  1204. tool_table_items = self.get_selected_tools_table_items()
  1205. # insert an information only element in the front
  1206. tool_table_items.insert(0, [_("Tool_nr"), _("Diameter"), _("Drills_Nr"), _("Slots_Nr")])
  1207. # ## Add properties to the object
  1208. job_obj.origin_kind = 'excellon'
  1209. job_obj.options['Tools_in_use'] = tool_table_items
  1210. job_obj.options['type'] = 'Excellon'
  1211. job_obj.options['ppname_e'] = pp_excellon_name
  1212. job_obj.multidepth = self.options["multidepth"]
  1213. job_obj.z_depthpercut = self.options["depthperpass"]
  1214. job_obj.z_move = float(self.options["travelz"])
  1215. job_obj.feedrate = float(self.options["feedrate_z"])
  1216. job_obj.z_feedrate = float(self.options["feedrate_z"])
  1217. job_obj.feedrate_rapid = float(self.options["feedrate_rapid"])
  1218. job_obj.spindlespeed = float(self.options["spindlespeed"]) if self.options["spindlespeed"] != 0 else None
  1219. job_obj.spindledir = self.app.defaults['excellon_spindledir']
  1220. job_obj.dwell = self.options["dwell"]
  1221. job_obj.dwelltime = float(self.options["dwelltime"])
  1222. job_obj.pp_excellon_name = pp_excellon_name
  1223. job_obj.toolchange_xy_type = "excellon"
  1224. job_obj.coords_decimals = int(self.app.defaults["cncjob_coords_decimals"])
  1225. job_obj.fr_decimals = int(self.app.defaults["cncjob_fr_decimals"])
  1226. job_obj.options['xmin'] = xmin
  1227. job_obj.options['ymin'] = ymin
  1228. job_obj.options['xmax'] = xmax
  1229. job_obj.options['ymax'] = ymax
  1230. job_obj.z_pdepth = float(self.options["z_pdepth"])
  1231. job_obj.feedrate_probe = float(self.options["feedrate_probe"])
  1232. job_obj.z_cut = float(self.options['cutz'])
  1233. job_obj.toolchange = self.options["toolchange"]
  1234. job_obj.xy_toolchange = self.app.defaults["excellon_toolchangexy"]
  1235. job_obj.z_toolchange = float(self.options["toolchangez"])
  1236. job_obj.startz = float(self.options["startz"]) if self.options["startz"] else None
  1237. job_obj.endz = float(self.options["endz"])
  1238. job_obj.xy_end = self.options["endxy"]
  1239. job_obj.excellon_optimization_type = self.app.defaults["excellon_optimization_type"]
  1240. tools_csv = ','.join(tools)
  1241. ret_val = job_obj.generate_from_excellon_by_tool(self, tools_csv, use_ui=True)
  1242. if ret_val == 'fail':
  1243. return 'fail'
  1244. job_obj.gcode_parse()
  1245. job_obj.create_geometry()
  1246. # To be run in separate thread
  1247. def job_thread(a_obj):
  1248. with self.app.proc_container.new(_("Generating CNC Code")):
  1249. a_obj.app_obj.new_object("cncjob", job_name, job_init)
  1250. # Create promise for the new name.
  1251. self.app.collection.promise(job_name)
  1252. # Send to worker
  1253. # self.app.worker.add_task(job_thread, [self.app])
  1254. self.app.worker_task.emit({'fcn': job_thread, 'params': [self.app]})
  1255. def convert_units(self, units):
  1256. log.debug("FlatCAMObj.ExcellonObject.convert_units()")
  1257. Excellon.convert_units(self, units)
  1258. # factor = Excellon.convert_units(self, units)
  1259. # self.options['drillz'] = float(self.options['drillz']) * factor
  1260. # self.options['travelz'] = float(self.options['travelz']) * factor
  1261. # self.options['feedrate'] = float(self.options['feedrate']) * factor
  1262. # self.options['feedrate_rapid'] = float(self.options['feedrate_rapid']) * factor
  1263. # self.options['toolchangez'] = float(self.options['toolchangez']) * factor
  1264. #
  1265. # if self.app.defaults["excellon_toolchangexy"] == '':
  1266. # self.options['toolchangexy'] = "0.0, 0.0"
  1267. # else:
  1268. # coords_xy = [float(eval(coord)) for coord in self.app.defaults["excellon_toolchangexy"].split(",")]
  1269. # if len(coords_xy) < 2:
  1270. # self.app.inform.emit('[ERROR] %s' % _("The Toolchange X,Y field in Edit -> Preferences has to be "
  1271. # "in the format (x, y) \n"
  1272. # "but now there is only one value, not two. "))
  1273. # return 'fail'
  1274. # coords_xy[0] *= factor
  1275. # coords_xy[1] *= factor
  1276. # self.options['toolchangexy'] = "%f, %f" % (coords_xy[0], coords_xy[1])
  1277. #
  1278. # if self.options['startz'] is not None:
  1279. # self.options['startz'] = float(self.options['startz']) * factor
  1280. # self.options['endz'] = float(self.options['endz']) * factor
  1281. def on_add_area_click(self):
  1282. shape_button = self.ui.area_shape_radio
  1283. overz_button = self.ui.over_z_entry
  1284. strategy_radio = self.ui.strategy_radio
  1285. cnc_button = self.ui.generate_cnc_button
  1286. solid_geo = self.solid_geometry
  1287. obj_type = self.kind
  1288. self.app.exc_areas.on_add_area_click(
  1289. shape_button=shape_button, overz_button=overz_button, cnc_button=cnc_button, strategy_radio=strategy_radio,
  1290. solid_geo=solid_geo, obj_type=obj_type)
  1291. def on_clear_area_click(self):
  1292. if not self.app.exc_areas.exclusion_areas_storage:
  1293. self.app.inform.emit("[WARNING_NOTCL] %s" % _("Delete failed. There are no exclusion areas to delete."))
  1294. return
  1295. self.app.exc_areas.on_clear_area_click()
  1296. self.app.exc_areas.e_shape_modified.emit()
  1297. def on_delete_sel_areas(self):
  1298. sel_model = self.ui.exclusion_table.selectionModel()
  1299. sel_indexes = sel_model.selectedIndexes()
  1300. # it will iterate over all indexes which means all items in all columns too but I'm interested only on rows
  1301. # so the duplicate rows will not be added
  1302. sel_rows = set()
  1303. for idx in sel_indexes:
  1304. sel_rows.add(idx.row())
  1305. if not sel_rows:
  1306. self.app.inform.emit("[WARNING_NOTCL] %s" % _("Delete failed. Nothing is selected."))
  1307. return
  1308. self.app.exc_areas.delete_sel_shapes(idxs=list(sel_rows))
  1309. self.app.exc_areas.e_shape_modified.emit()
  1310. def draw_sel_shape(self):
  1311. sel_model = self.ui.exclusion_table.selectionModel()
  1312. sel_indexes = sel_model.selectedIndexes()
  1313. # it will iterate over all indexes which means all items in all columns too but I'm interested only on rows
  1314. sel_rows = set()
  1315. for idx in sel_indexes:
  1316. sel_rows.add(idx.row())
  1317. self.delete_sel_shape()
  1318. if self.app.is_legacy is False:
  1319. face = self.app.defaults['global_sel_fill'][:-2] + str(hex(int(0.2 * 255)))[2:]
  1320. outline = self.app.defaults['global_sel_line'][:-2] + str(hex(int(0.8 * 255)))[2:]
  1321. else:
  1322. face = self.app.defaults['global_sel_fill'][:-2] + str(hex(int(0.4 * 255)))[2:]
  1323. outline = self.app.defaults['global_sel_line'][:-2] + str(hex(int(1.0 * 255)))[2:]
  1324. for row in sel_rows:
  1325. sel_rect = self.app.exc_areas.exclusion_areas_storage[row]['shape']
  1326. self.app.move_tool.sel_shapes.add(sel_rect, color=outline, face_color=face, update=True, layer=0,
  1327. tolerance=None)
  1328. if self.app.is_legacy is True:
  1329. self.app.move_tool.sel_shapes.redraw()
  1330. def clear_selection(self):
  1331. self.app.delete_selection_shape()
  1332. # self.ui.exclusion_table.clearSelection()
  1333. def delete_sel_shape(self):
  1334. self.app.delete_selection_shape()
  1335. def update_exclusion_table(self):
  1336. self.exclusion_area_cb_is_checked = True if self.ui.exclusion_cb.isChecked() else False
  1337. self.build_ui()
  1338. self.ui.exclusion_cb.set_value(self.exclusion_area_cb_is_checked)
  1339. def on_strategy(self, val):
  1340. if val == 'around':
  1341. self.ui.over_z_label.setDisabled(True)
  1342. self.ui.over_z_entry.setDisabled(True)
  1343. else:
  1344. self.ui.over_z_label.setDisabled(False)
  1345. self.ui.over_z_entry.setDisabled(False)
  1346. def exclusion_table_toggle_all(self):
  1347. """
  1348. will toggle the selection of all rows in Exclusion Areas table
  1349. :return:
  1350. """
  1351. sel_model = self.ui.exclusion_table.selectionModel()
  1352. sel_indexes = sel_model.selectedIndexes()
  1353. # it will iterate over all indexes which means all items in all columns too but I'm interested only on rows
  1354. sel_rows = set()
  1355. for idx in sel_indexes:
  1356. sel_rows.add(idx.row())
  1357. if sel_rows:
  1358. self.ui.exclusion_table.clearSelection()
  1359. self.delete_sel_shape()
  1360. else:
  1361. self.ui.exclusion_table.selectAll()
  1362. self.draw_sel_shape()
  1363. def on_solid_cb_click(self, *args):
  1364. if self.muted_ui:
  1365. return
  1366. self.read_form_item('solid')
  1367. self.plot()
  1368. def on_multicolored_cb_click(self, *args):
  1369. if self.muted_ui:
  1370. return
  1371. self.read_form_item('multicolored')
  1372. self.plot()
  1373. def on_plot_cb_click(self, *args):
  1374. if self.muted_ui:
  1375. return
  1376. self.plot()
  1377. self.read_form_item('plot')
  1378. self.ui_disconnect()
  1379. cb_flag = self.ui.plot_cb.isChecked()
  1380. for row in range(self.ui.tools_table.rowCount() - 2):
  1381. table_cb = self.ui.tools_table.cellWidget(row, 5)
  1382. if cb_flag:
  1383. table_cb.setChecked(True)
  1384. else:
  1385. table_cb.setChecked(False)
  1386. self.ui_connect()
  1387. def on_plot_cb_click_table(self):
  1388. # self.ui.cnc_tools_table.cellWidget(row, 2).widget().setCheckState(QtCore.Qt.Unchecked)
  1389. self.ui_disconnect()
  1390. # cw = self.sender()
  1391. # cw_index = self.ui.tools_table.indexAt(cw.pos())
  1392. # cw_row = cw_index.row()
  1393. check_row = 0
  1394. self.shapes.clear(update=True)
  1395. for tool_key in self.tools:
  1396. solid_geometry = self.tools[tool_key]['solid_geometry']
  1397. # find the geo_tool_table row associated with the tool_key
  1398. for row in range(self.ui.tools_table.rowCount()):
  1399. tool_item = int(self.ui.tools_table.item(row, 0).text())
  1400. if tool_item == int(tool_key):
  1401. check_row = row
  1402. break
  1403. if self.ui.tools_table.cellWidget(check_row, 5).isChecked():
  1404. self.options['plot'] = True
  1405. # self.plot_element(element=solid_geometry, visible=True)
  1406. # Plot excellon (All polygons?)
  1407. if self.options["solid"]:
  1408. for geo in solid_geometry:
  1409. self.add_shape(shape=geo, color='#750000BF', face_color='#C40000BF',
  1410. visible=self.options['plot'],
  1411. layer=2)
  1412. else:
  1413. for geo in solid_geometry:
  1414. self.add_shape(shape=geo.exterior, color='red', visible=self.options['plot'])
  1415. for ints in geo.interiors:
  1416. self.add_shape(shape=ints, color='green', visible=self.options['plot'])
  1417. self.shapes.redraw()
  1418. # make sure that the general plot is disabled if one of the row plot's are disabled and
  1419. # if all the row plot's are enabled also enable the general plot checkbox
  1420. cb_cnt = 0
  1421. total_row = self.ui.tools_table.rowCount()
  1422. for row in range(total_row - 2):
  1423. if self.ui.tools_table.cellWidget(row, 5).isChecked():
  1424. cb_cnt += 1
  1425. else:
  1426. cb_cnt -= 1
  1427. if cb_cnt < total_row - 2:
  1428. self.ui.plot_cb.setChecked(False)
  1429. else:
  1430. self.ui.plot_cb.setChecked(True)
  1431. self.ui_connect()
  1432. def plot(self, visible=None, kind=None):
  1433. # Does all the required setup and returns False
  1434. # if the 'ptint' option is set to False.
  1435. if not FlatCAMObj.plot(self):
  1436. return
  1437. if self.app.is_legacy is False:
  1438. def random_color():
  1439. r_color = np.random.rand(4)
  1440. r_color[3] = 1
  1441. return r_color
  1442. else:
  1443. def random_color():
  1444. while True:
  1445. r_color = np.random.rand(4)
  1446. r_color[3] = 1
  1447. new_color = '#'
  1448. for idx in range(len(r_color)):
  1449. new_color += '%x' % int(r_color[idx] * 255)
  1450. # do it until a valid color is generated
  1451. # a valid color has the # symbol, another 6 chars for the color and the last 2 chars for alpha
  1452. # for a total of 9 chars
  1453. if len(new_color) == 9:
  1454. break
  1455. return new_color
  1456. # try:
  1457. # # Plot Excellon (All polygons?)
  1458. # if self.options["solid"]:
  1459. # for tool in self.tools:
  1460. # for geo in self.tools[tool]['solid_geometry']:
  1461. # self.add_shape(shape=geo, color='#750000BF', face_color='#C40000BF',
  1462. # visible=self.options['plot'],
  1463. # layer=2)
  1464. # else:
  1465. # for tool in self.tools:
  1466. # for geo in self.tools[tool]['solid_geometry']:
  1467. # self.add_shape(shape=geo.exterior, color='red', visible=self.options['plot'])
  1468. # for ints in geo.interiors:
  1469. # self.add_shape(shape=ints, color='orange', visible=self.options['plot'])
  1470. #
  1471. # self.shapes.redraw()
  1472. # return
  1473. # except (ObjectDeleted, AttributeError, KeyError):
  1474. # self.shapes.clear(update=True)
  1475. # this stays for compatibility reasons, in case we try to open old projects
  1476. try:
  1477. __ = iter(self.solid_geometry)
  1478. except TypeError:
  1479. self.solid_geometry = [self.solid_geometry]
  1480. visible = visible if visible else self.options['plot']
  1481. try:
  1482. # Plot Excellon (All polygons?)
  1483. if self.options["solid"]:
  1484. # for geo in self.solid_geometry:
  1485. # self.add_shape(shape=geo,
  1486. # color=self.outline_color,
  1487. # face_color=random_color() if self.options['multicolored'] else self.fill_color,
  1488. # visible=visible,
  1489. # layer=2)
  1490. # plot polygons for each tool separately
  1491. for tool in self.tools:
  1492. # set the color here so we have one color for each tool
  1493. geo_color = random_color()
  1494. # tool is a dict also
  1495. for geo in self.tools[tool]["solid_geometry"]:
  1496. self.add_shape(shape=geo,
  1497. color=geo_color if self.options['multicolored'] else self.outline_color,
  1498. face_color=geo_color if self.options['multicolored'] else self.fill_color,
  1499. visible=visible,
  1500. layer=2)
  1501. else:
  1502. for geo in self.solid_geometry:
  1503. self.add_shape(shape=geo.exterior, color='red', visible=visible)
  1504. for ints in geo.interiors:
  1505. self.add_shape(shape=ints, color='orange', visible=visible)
  1506. self.shapes.redraw()
  1507. except (ObjectDeleted, AttributeError):
  1508. self.shapes.clear(update=True)
  1509. def on_apply_param_to_all_clicked(self):
  1510. if self.ui.tools_table.rowCount() == 0:
  1511. # there is no tool in tool table so we can't save the GUI elements values to storage
  1512. log.debug("ExcellonObject.on_apply_param_to_all_clicked() --> no tool in Tools Table, aborting.")
  1513. return
  1514. self.ui_disconnect()
  1515. row = self.ui.tools_table.currentRow()
  1516. if row < 0:
  1517. row = 0
  1518. tooluid_item = int(self.ui.tools_table.item(row, 0).text())
  1519. temp_tool_data = {}
  1520. for tooluid_key, tooluid_val in self.tools.items():
  1521. if int(tooluid_key) == tooluid_item:
  1522. # this will hold the 'data' key of the self.tools[tool] dictionary that corresponds to
  1523. # the current row in the tool table
  1524. temp_tool_data = tooluid_val['data']
  1525. break
  1526. for tooluid_key, tooluid_val in self.tools.items():
  1527. tooluid_val['data'] = deepcopy(temp_tool_data)
  1528. self.app.inform.emit('[success] %s' % _("Current Tool parameters were applied to all tools."))
  1529. self.ui_connect()