FlatCAMExcellon.py 82 KB

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