FlatCAMExcellon.py 82 KB

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