ToolDrilling.py 144 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284
  1. # ##########################################################
  2. # FlatCAM: 2D Post-processing for Manufacturing #
  3. # File by: Marius Adrian Stanciu (c) #
  4. # Date: 6/15/2020 #
  5. # License: MIT Licence #
  6. # ##########################################################
  7. from PyQt5 import QtWidgets, QtCore, QtGui
  8. from appTool import AppTool
  9. from appGUI.GUIElements import FCCheckBox, FCDoubleSpinner, RadioSet, FCTable, FCButton, \
  10. FCComboBox, OptionalInputSection, FCSpinner, NumericalEvalEntry, OptionalHideInputSection, FCLabel
  11. from appParsers.ParseExcellon import Excellon
  12. from copy import deepcopy
  13. import numpy as np
  14. import math
  15. from shapely.ops import unary_union
  16. from shapely.geometry import Point, LineString
  17. from matplotlib.backend_bases import KeyEvent as mpl_key_event
  18. import logging
  19. import gettext
  20. import appTranslation as fcTranslate
  21. import builtins
  22. import platform
  23. import re
  24. import traceback
  25. from Common import GracefulException as grace
  26. fcTranslate.apply_language('strings')
  27. if '_' not in builtins.__dict__:
  28. _ = gettext.gettext
  29. if platform.architecture()[0] == '64bit':
  30. from ortools.constraint_solver import pywrapcp
  31. from ortools.constraint_solver import routing_enums_pb2
  32. log = logging.getLogger('base')
  33. settings = QtCore.QSettings("Open Source", "FlatCAM")
  34. if settings.contains("machinist"):
  35. machinist_setting = settings.value('machinist', type=int)
  36. else:
  37. machinist_setting = 0
  38. class ToolDrilling(AppTool, Excellon):
  39. def __init__(self, app):
  40. self.app = app
  41. self.decimals = self.app.decimals
  42. AppTool.__init__(self, app)
  43. Excellon.__init__(self, geo_steps_per_circle=self.app.defaults["geometry_circle_steps"])
  44. # #############################################################################
  45. # ######################### Tool GUI ##########################################
  46. # #############################################################################
  47. self.t_ui = DrillingUI(layout=self.layout, app=self.app)
  48. self.toolName = self.t_ui.toolName
  49. # #############################################################################
  50. # ########################## VARIABLES ########################################
  51. # #############################################################################
  52. self.units = ''
  53. self.excellon_tools = {}
  54. self.tooluid = 0
  55. self.kind = "excellon"
  56. # dict that holds the object names and the option name
  57. # the key is the object name (defines in ObjectUI) for each UI element that is a parameter
  58. # particular for a tool and the value is the actual name of the option that the UI element is changing
  59. self.name2option = {}
  60. # store here the default data for Geometry Data
  61. self.default_data = {}
  62. self.obj_name = ""
  63. self.excellon_obj = None
  64. self.first_click = False
  65. self.cursor_pos = None
  66. self.mouse_is_dragging = False
  67. # store here the points for the "Polygon" area selection shape
  68. self.points = []
  69. self.mm = None
  70. self.mr = None
  71. self.kp = None
  72. # variable to store the total amount of drills per job
  73. self.tot_drill_cnt = 0
  74. self.tool_row = 0
  75. # variable to store the total amount of slots per job
  76. self.tot_slot_cnt = 0
  77. self.tool_row_slots = 0
  78. # variable to store the distance travelled
  79. self.travel_distance = 0.0
  80. self.grid_status_memory = self.app.ui.grid_snap_btn.isChecked()
  81. # store here the state of the exclusion checkbox state to be restored after building the UI
  82. # TODO add this in the sel.app.defaults dict and in Preferences
  83. self.exclusion_area_cb_is_checked = False
  84. # store here solid_geometry when there are tool with isolation job
  85. self.solid_geometry = []
  86. self.circle_steps = int(self.app.defaults["geometry_circle_steps"])
  87. self.tooldia = None
  88. # multiprocessing
  89. self.pool = self.app.pool
  90. self.results = []
  91. # disconnect flags
  92. self.area_sel_disconnect_flag = False
  93. self.poly_sel_disconnect_flag = False
  94. self.form_fields = {
  95. "cutz": self.t_ui.cutz_entry,
  96. "multidepth": self.t_ui.mpass_cb,
  97. "depthperpass": self.t_ui.maxdepth_entry,
  98. "travelz": self.t_ui.travelz_entry,
  99. "feedrate_z": self.t_ui.feedrate_z_entry,
  100. "feedrate_rapid": self.t_ui.feedrate_rapid_entry,
  101. "spindlespeed": self.t_ui.spindlespeed_entry,
  102. "dwell": self.t_ui.dwell_cb,
  103. "dwelltime": self.t_ui.dwelltime_entry,
  104. "offset": self.t_ui.offset_entry,
  105. "drill_slots": self.t_ui.drill_slots_cb,
  106. "drill_overlap": self.t_ui.drill_overlap_entry,
  107. "last_drill": self.t_ui.last_drill_cb
  108. }
  109. self.name2option = {
  110. "e_cutz": "cutz",
  111. "e_multidepth": "multidepth",
  112. "e_depthperpass": "depthperpass",
  113. "e_travelz": "travelz",
  114. "e_feedratez": "feedrate_z",
  115. "e_fr_rapid": "feedrate_rapid",
  116. "e_spindlespeed": "spindlespeed",
  117. "e_dwell": "dwell",
  118. "e_dwelltime": "dwelltime",
  119. "e_offset": "offset",
  120. "e_drill_slots": "drill_slots",
  121. "e_drill_slots_overlap": "drill_overlap",
  122. "e_drill_last_drill": "last_drill",
  123. }
  124. self.old_tool_dia = None
  125. self.poly_drawn = False
  126. self.connect_signals_at_init()
  127. def install(self, icon=None, separator=None, **kwargs):
  128. AppTool.install(self, icon, separator, shortcut='Alt+D', **kwargs)
  129. def run(self, toggle=True):
  130. self.app.defaults.report_usage("ToolDrilling()")
  131. log.debug("ToolDrilling().run() was launched ...")
  132. if toggle:
  133. # if the splitter is hidden, display it, else hide it but only if the current widget is the same
  134. if self.app.ui.splitter.sizes()[0] == 0:
  135. self.app.ui.splitter.setSizes([1, 1])
  136. else:
  137. try:
  138. if self.app.ui.tool_scroll_area.widget().objectName() == self.toolName:
  139. # if tab is populated with the tool but it does not have the focus, focus on it
  140. if not self.app.ui.notebook.currentWidget() is self.app.ui.tool_tab:
  141. # focus on Tool Tab
  142. self.app.ui.notebook.setCurrentWidget(self.app.ui.tool_tab)
  143. else:
  144. self.app.ui.splitter.setSizes([0, 1])
  145. except AttributeError:
  146. pass
  147. else:
  148. if self.app.ui.splitter.sizes()[0] == 0:
  149. self.app.ui.splitter.setSizes([1, 1])
  150. AppTool.run(self)
  151. self.set_tool_ui()
  152. self.on_object_changed()
  153. # self.build_tool_ui()
  154. # all the tools are selected by default
  155. self.t_ui.tools_table.selectAll()
  156. self.app.ui.notebook.setTabText(2, _("Drilling Tool"))
  157. def connect_signals_at_init(self):
  158. # #############################################################################
  159. # ############################ SIGNALS ########################################
  160. # #############################################################################
  161. self.t_ui.apply_param_to_all.clicked.connect(self.on_apply_param_to_all_clicked)
  162. self.t_ui.generate_cnc_button.clicked.connect(self.on_cnc_button_click)
  163. self.t_ui.tools_table.drag_drop_sig.connect(self.rebuild_ui)
  164. # Exclusion areas signals
  165. self.t_ui.exclusion_table.horizontalHeader().sectionClicked.connect(self.exclusion_table_toggle_all)
  166. self.t_ui.exclusion_table.lost_focus.connect(self.clear_selection)
  167. self.t_ui.exclusion_table.itemClicked.connect(self.draw_sel_shape)
  168. self.t_ui.add_area_button.clicked.connect(self.on_add_area_click)
  169. self.t_ui.delete_area_button.clicked.connect(self.on_clear_area_click)
  170. self.t_ui.delete_sel_area_button.clicked.connect(self.on_delete_sel_areas)
  171. self.t_ui.strategy_radio.activated_custom.connect(self.on_strategy)
  172. self.t_ui.pp_excellon_name_cb.activated.connect(self.on_pp_changed)
  173. self.t_ui.reset_button.clicked.connect(self.set_tool_ui)
  174. # Cleanup on Graceful exit (CTRL+ALT+X combo key)
  175. self.app.cleanup.connect(self.set_tool_ui)
  176. def set_tool_ui(self):
  177. self.units = self.app.defaults['units'].upper()
  178. self.old_tool_dia = self.app.defaults["tools_iso_newdia"]
  179. # try to select in the Excellon combobox the active object
  180. try:
  181. selected_obj = self.app.collection.get_active()
  182. if selected_obj.kind == 'excellon':
  183. current_name = selected_obj.options['name']
  184. self.t_ui.object_combo.set_value(current_name)
  185. except Exception:
  186. pass
  187. # reset the Excellon preprocessor combo
  188. self.t_ui.pp_excellon_name_cb.clear()
  189. # populate Excellon preprocessor combobox list
  190. for name in list(self.app.preprocessors.keys()):
  191. # the HPGL preprocessor is only for Geometry not for Excellon job therefore don't add it
  192. if name == 'hpgl':
  193. continue
  194. self.t_ui.pp_excellon_name_cb.addItem(name)
  195. # add tooltips
  196. for it in range(self.t_ui.pp_excellon_name_cb.count()):
  197. self.t_ui.pp_excellon_name_cb.setItemData(
  198. it, self.t_ui.pp_excellon_name_cb.itemText(it), QtCore.Qt.ToolTipRole)
  199. # update the changes in UI depending on the selected preprocessor in Preferences
  200. # after this moment all the changes in the Posprocessor combo will be handled by the activated signal of the
  201. # self.t_ui.pp_excellon_name_cb combobox
  202. self.on_pp_changed()
  203. app_mode = self.app.defaults["global_app_level"]
  204. # Show/Hide Advanced Options
  205. if app_mode == 'b':
  206. self.t_ui.level.setText('<span style="color:green;"><b>%s</b></span>' % _('Basic'))
  207. self.t_ui.estartz_label.hide()
  208. self.t_ui.estartz_entry.hide()
  209. self.t_ui.feedrate_rapid_label.hide()
  210. self.t_ui.feedrate_rapid_entry.hide()
  211. self.t_ui.pdepth_label.hide()
  212. self.t_ui.pdepth_entry.hide()
  213. self.t_ui.feedrate_probe_label.hide()
  214. self.t_ui.feedrate_probe_entry.hide()
  215. else:
  216. self.t_ui.level.setText('<span style="color:red;"><b>%s</b></span>' % _('Advanced'))
  217. self.t_ui.tools_frame.show()
  218. self.t_ui.order_radio.set_value(self.app.defaults["excellon_tool_order"])
  219. loaded_obj = self.app.collection.get_by_name(self.t_ui.object_combo.get_value())
  220. if loaded_obj:
  221. outname = loaded_obj.options['name']
  222. else:
  223. outname = ''
  224. # init the working variables
  225. self.default_data.clear()
  226. self.default_data = {
  227. "name": outname + '_drill',
  228. "plot": self.app.defaults["excellon_plot"],
  229. "solid": self.app.defaults["excellon_solid"],
  230. "multicolored": self.app.defaults["excellon_multicolored"],
  231. "merge_fuse_tools": self.app.defaults["excellon_merge_fuse_tools"],
  232. "format_upper_in": self.app.defaults["excellon_format_upper_in"],
  233. "format_lower_in": self.app.defaults["excellon_format_lower_in"],
  234. "format_upper_mm": self.app.defaults["excellon_format_upper_mm"],
  235. "lower_mm": self.app.defaults["excellon_format_lower_mm"],
  236. "zeros": self.app.defaults["excellon_zeros"],
  237. "excellon_units": self.app.defaults["excellon_units"],
  238. "excellon_update": self.app.defaults["excellon_update"],
  239. "excellon_optimization_type": self.app.defaults["excellon_optimization_type"],
  240. "excellon_search_time": self.app.defaults["excellon_search_time"],
  241. "excellon_plot_fill": self.app.defaults["excellon_plot_fill"],
  242. "excellon_plot_line": self.app.defaults["excellon_plot_line"],
  243. # Excellon Options
  244. "excellon_tool_order": self.app.defaults["excellon_tool_order"],
  245. "cutz": self.app.defaults["excellon_cutz"],
  246. "multidepth": self.app.defaults["excellon_multidepth"],
  247. "depthperpass": self.app.defaults["excellon_depthperpass"],
  248. "travelz": self.app.defaults["excellon_travelz"],
  249. "endz": self.app.defaults["excellon_endz"],
  250. "endxy": self.app.defaults["excellon_endxy"],
  251. "feedrate_z": self.app.defaults["excellon_feedrate_z"],
  252. "spindlespeed": self.app.defaults["excellon_spindlespeed"],
  253. "dwell": self.app.defaults["excellon_dwell"],
  254. "dwelltime": self.app.defaults["excellon_dwelltime"],
  255. "toolchange": self.app.defaults["excellon_toolchange"],
  256. "toolchangez": self.app.defaults["excellon_toolchangez"],
  257. "ppname_e": self.app.defaults["excellon_ppname_e"],
  258. "tooldia": self.app.defaults["excellon_tooldia"],
  259. "slot_tooldia": self.app.defaults["excellon_slot_tooldia"],
  260. "gcode_type": self.app.defaults["excellon_gcode_type"],
  261. "excellon_area_exclusion": self.app.defaults["excellon_area_exclusion"],
  262. "excellon_area_shape": self.app.defaults["excellon_area_shape"],
  263. "excellon_area_strategy": self.app.defaults["excellon_area_strategy"],
  264. "excellon_area_overz": self.app.defaults["excellon_area_overz"],
  265. # Excellon Advanced Options
  266. "offset": self.app.defaults["excellon_offset"],
  267. "toolchangexy": self.app.defaults["excellon_toolchangexy"],
  268. "startz": self.app.defaults["excellon_startz"],
  269. "feedrate_rapid": self.app.defaults["excellon_feedrate_rapid"],
  270. "z_pdepth": self.app.defaults["excellon_z_pdepth"],
  271. "feedrate_probe": self.app.defaults["excellon_feedrate_probe"],
  272. "spindledir": self.app.defaults["excellon_spindledir"],
  273. "f_plunge": self.app.defaults["excellon_f_plunge"],
  274. "f_retract": self.app.defaults["excellon_f_retract"],
  275. # Drill Slots
  276. "drill_slots": self.app.defaults["excellon_drill_slots"],
  277. "drill_overlap": self.app.defaults["excellon_drill_overlap"],
  278. "last_drill": self.app.defaults["excellon_last_drill"],
  279. "gcode": '',
  280. "gcode_parsed": '',
  281. "geometry": [],
  282. }
  283. # fill in self.default_data values from self.options
  284. for opt_key, opt_val in self.app.options.items():
  285. if opt_key.find('excellon_') == 0:
  286. self.default_data[opt_key] = deepcopy(opt_val)
  287. self.first_click = False
  288. self.cursor_pos = None
  289. self.mouse_is_dragging = False
  290. self.units = self.app.defaults['units'].upper()
  291. # ########################################
  292. # #######3 TEMP SETTINGS #################
  293. # ########################################
  294. self.t_ui.tools_table.setRowCount(2)
  295. self.t_ui.tools_table.setMinimumHeight(self.t_ui.tools_table.getHeight())
  296. self.t_ui.tools_table.setMaximumHeight(self.t_ui.tools_table.getHeight())
  297. # make sure to update the UI on init
  298. try:
  299. self.excellon_tools = self.excellon_obj.tools
  300. except AttributeError:
  301. # no object loaded
  302. pass
  303. self.build_tool_ui()
  304. # ########################################
  305. # ########################################
  306. # ####### Fill in the parameters #########
  307. # ########################################
  308. # ########################################
  309. self.t_ui.cutz_entry.set_value(self.app.defaults["excellon_cutz"])
  310. self.t_ui.mpass_cb.set_value(self.app.defaults["excellon_multidepth"])
  311. self.t_ui.maxdepth_entry.set_value(self.app.defaults["excellon_depthperpass"])
  312. self.t_ui.travelz_entry.set_value(self.app.defaults["excellon_travelz"])
  313. self.t_ui.feedrate_z_entry.set_value(self.app.defaults["excellon_feedrate_z"])
  314. self.t_ui.feedrate_rapid_entry.set_value(self.app.defaults["excellon_feedrate_rapid"])
  315. self.t_ui.spindlespeed_entry.set_value(self.app.defaults["excellon_spindlespeed"])
  316. self.t_ui.dwell_cb.set_value(self.app.defaults["excellon_dwell"])
  317. self.t_ui.dwelltime_entry.set_value(self.app.defaults["excellon_dwelltime"])
  318. self.t_ui.offset_entry.set_value(self.app.defaults["excellon_offset"])
  319. self.t_ui.toolchange_cb.set_value(self.app.defaults["excellon_toolchange"])
  320. self.t_ui.toolchangez_entry.set_value(self.app.defaults["excellon_toolchangez"])
  321. self.t_ui.estartz_entry.set_value(self.app.defaults["excellon_startz"])
  322. self.t_ui.endz_entry.set_value(self.app.defaults["excellon_endz"])
  323. self.t_ui.endxy_entry.set_value(self.app.defaults["excellon_endxy"])
  324. self.t_ui.pdepth_entry.set_value(self.app.defaults["excellon_z_pdepth"])
  325. self.t_ui.feedrate_probe_entry.set_value(self.app.defaults["excellon_feedrate_probe"])
  326. self.t_ui.exclusion_cb.set_value(self.app.defaults["excellon_area_exclusion"])
  327. self.t_ui.strategy_radio.set_value(self.app.defaults["excellon_area_strategy"])
  328. self.t_ui.over_z_entry.set_value(self.app.defaults["excellon_area_overz"])
  329. self.t_ui.area_shape_radio.set_value(self.app.defaults["excellon_area_shape"])
  330. # Drill slots - part of the Advanced Excellon params
  331. self.t_ui.drill_slots_cb.set_value(self.app.defaults["excellon_drill_slots"])
  332. self.t_ui.drill_overlap_entry.set_value(self.app.defaults["excellon_drill_overlap"])
  333. self.t_ui.last_drill_cb.set_value(self.app.defaults["excellon_last_drill"])
  334. # if the app mode is Basic then disable this feature
  335. if app_mode == 'b':
  336. self.t_ui.drill_slots_cb.set_value(False)
  337. self.t_ui.drill_slots_cb.hide()
  338. self.t_ui.drill_overlap_label.hide()
  339. self.t_ui.drill_overlap_entry.hide()
  340. self.t_ui.last_drill_cb.hide()
  341. try:
  342. self.t_ui.object_combo.currentTextChanged.disconnect()
  343. except (AttributeError, TypeError):
  344. pass
  345. self.t_ui.object_combo.currentTextChanged.connect(self.on_object_changed)
  346. def rebuild_ui(self):
  347. # read the table tools uid
  348. current_uid_list = []
  349. for row in range(self.t_ui.tools_table.rowCount()):
  350. try:
  351. uid = int(self.t_ui.tools_table.item(row, 3).text())
  352. current_uid_list.append(uid)
  353. except AttributeError:
  354. continue
  355. new_tools = {}
  356. new_uid = 1
  357. for current_uid in current_uid_list:
  358. new_tools[new_uid] = deepcopy(self.excellon_tools[current_uid])
  359. new_uid += 1
  360. self.excellon_tools = new_tools
  361. # the tools table changed therefore we need to rebuild it
  362. QtCore.QTimer.singleShot(20, self.build_tool_ui)
  363. def build_tool_ui(self):
  364. log.debug("ToolDrilling.build_tool_ui()")
  365. self.ui_disconnect()
  366. # order the tools by tool diameter if it's the case
  367. sorted_tools = []
  368. for k, v in self.excellon_tools.items():
  369. sorted_tools.append(float('%.*f' % (self.decimals, float(v['tooldia']))))
  370. order = self.t_ui.order_radio.get_value()
  371. if order == 'fwd':
  372. sorted_tools.sort(reverse=False)
  373. elif order == 'rev':
  374. sorted_tools.sort(reverse=True)
  375. else:
  376. pass
  377. # remake the excellon_tools dict in the order above
  378. new_id = 1
  379. new_tools = {}
  380. for tooldia in sorted_tools:
  381. for old_tool in self.excellon_tools:
  382. if float('%.*f' % (self.decimals, float(self.excellon_tools[old_tool]['tooldia']))) == tooldia:
  383. new_tools[new_id] = deepcopy(self.excellon_tools[old_tool])
  384. new_id += 1
  385. self.excellon_tools = new_tools
  386. if self.excellon_obj and self.excellon_tools:
  387. self.t_ui.exc_param_frame.setDisabled(False)
  388. tools = [k for k in self.excellon_tools]
  389. else:
  390. self.t_ui.exc_param_frame.setDisabled(True)
  391. self.t_ui.tools_table.setRowCount(2)
  392. tools = []
  393. n = len(tools)
  394. # we have (n+2) rows because there are 'n' tools, each a row, plus the last 2 rows for totals.
  395. self.t_ui.tools_table.setRowCount(n + 2)
  396. self.tool_row = 0
  397. tot_drill_cnt = 0
  398. tot_slot_cnt = 0
  399. for tool_no in tools:
  400. # Find no of drills for the current tool
  401. try:
  402. drill_cnt = len(self.excellon_tools[tool_no]["drills"]) # variable to store the nr of drills per tool
  403. except KeyError:
  404. drill_cnt = 0
  405. tot_drill_cnt += drill_cnt
  406. # Find no of slots for the current tool
  407. try:
  408. slot_cnt = len(self.excellon_tools[tool_no]["slots"]) # variable to store the nr of slots per tool
  409. except KeyError:
  410. slot_cnt = 0
  411. tot_slot_cnt += slot_cnt
  412. # Tool name/id
  413. exc_id_item = QtWidgets.QTableWidgetItem('%d' % int(tool_no))
  414. exc_id_item.setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsDragEnabled)
  415. self.t_ui.tools_table.setItem(self.tool_row, 0, exc_id_item)
  416. # Tool Diameter
  417. dia_item = QtWidgets.QTableWidgetItem('%.*f' % (self.decimals, self.excellon_tools[tool_no]['tooldia']))
  418. dia_item.setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsDragEnabled)
  419. self.t_ui.tools_table.setItem(self.tool_row, 1, dia_item)
  420. # Number of drills per tool
  421. drill_count_item = QtWidgets.QTableWidgetItem('%d' % drill_cnt)
  422. drill_count_item.setFlags(QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsDragEnabled)
  423. self.t_ui.tools_table.setItem(self.tool_row, 2, drill_count_item)
  424. # Tool unique ID
  425. tool_uid_item = QtWidgets.QTableWidgetItem(str(int(tool_no)))
  426. # ## REMEMBER: THIS COLUMN IS HIDDEN in UI
  427. self.t_ui.tools_table.setItem(self.tool_row, 3, tool_uid_item)
  428. # Number of slots per tool
  429. # if the slot number is zero is better to not clutter the GUI with zero's so we print a space
  430. slot_count_str = '%d' % slot_cnt if slot_cnt > 0 else ''
  431. slot_count_item = QtWidgets.QTableWidgetItem(slot_count_str)
  432. slot_count_item.setFlags(QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsDragEnabled)
  433. self.t_ui.tools_table.setItem(self.tool_row, 4, slot_count_item)
  434. self.tool_row += 1
  435. # add a last row with the Total number of drills
  436. empty_1 = QtWidgets.QTableWidgetItem('')
  437. empty_1.setFlags(~QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled)
  438. empty_1_1 = QtWidgets.QTableWidgetItem('')
  439. empty_1_1.setFlags(~QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled)
  440. label_tot_drill_count = QtWidgets.QTableWidgetItem(_('Total Drills'))
  441. label_tot_drill_count.setFlags(QtCore.Qt.ItemIsEnabled)
  442. tot_drill_count = QtWidgets.QTableWidgetItem('%d' % tot_drill_cnt)
  443. tot_drill_count.setFlags(QtCore.Qt.ItemIsEnabled)
  444. self.t_ui.tools_table.setItem(self.tool_row, 0, empty_1)
  445. self.t_ui.tools_table.setItem(self.tool_row, 1, label_tot_drill_count)
  446. self.t_ui.tools_table.setItem(self.tool_row, 2, tot_drill_count) # Total number of drills
  447. self.t_ui.tools_table.setItem(self.tool_row, 4, empty_1_1)
  448. font = QtGui.QFont()
  449. font.setBold(True)
  450. font.setWeight(75)
  451. for k in [1, 2]:
  452. self.t_ui.tools_table.item(self.tool_row, k).setForeground(QtGui.QColor(127, 0, 255))
  453. self.t_ui.tools_table.item(self.tool_row, k).setFont(font)
  454. self.tool_row += 1
  455. # add a last row with the Total number of slots
  456. empty_2 = QtWidgets.QTableWidgetItem('')
  457. empty_2.setFlags(~QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled)
  458. empty_2_1 = QtWidgets.QTableWidgetItem('')
  459. empty_2_1.setFlags(~QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled)
  460. label_tot_slot_count = QtWidgets.QTableWidgetItem(_('Total Slots'))
  461. tot_slot_count = QtWidgets.QTableWidgetItem('%d' % tot_slot_cnt)
  462. label_tot_slot_count.setFlags(QtCore.Qt.ItemIsEnabled)
  463. tot_slot_count.setFlags(QtCore.Qt.ItemIsEnabled)
  464. self.t_ui.tools_table.setItem(self.tool_row, 0, empty_2)
  465. self.t_ui.tools_table.setItem(self.tool_row, 1, label_tot_slot_count)
  466. self.t_ui.tools_table.setItem(self.tool_row, 2, empty_2_1)
  467. self.t_ui.tools_table.setItem(self.tool_row, 4, tot_slot_count) # Total number of slots
  468. for kl in [1, 2, 4]:
  469. self.t_ui.tools_table.item(self.tool_row, kl).setFont(font)
  470. self.t_ui.tools_table.item(self.tool_row, kl).setForeground(QtGui.QColor(0, 70, 255))
  471. # make the diameter column editable
  472. # for row in range(self.t_ui.tools_table.rowCount() - 2):
  473. # self.t_ui.tools_table.item(row, 1).setFlags(
  474. # QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled)
  475. self.t_ui.tools_table.resizeColumnsToContents()
  476. self.t_ui.tools_table.resizeRowsToContents()
  477. vertical_header = self.t_ui.tools_table.verticalHeader()
  478. vertical_header.hide()
  479. self.t_ui.tools_table.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
  480. horizontal_header = self.t_ui.tools_table.horizontalHeader()
  481. self.t_ui.tools_table.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
  482. horizontal_header.setMinimumSectionSize(10)
  483. horizontal_header.setDefaultSectionSize(70)
  484. horizontal_header.setSectionResizeMode(0, QtWidgets.QHeaderView.Fixed)
  485. horizontal_header.resizeSection(0, 20)
  486. horizontal_header.setSectionResizeMode(1, QtWidgets.QHeaderView.Stretch)
  487. horizontal_header.setSectionResizeMode(2, QtWidgets.QHeaderView.ResizeToContents)
  488. horizontal_header.setSectionResizeMode(4, QtWidgets.QHeaderView.ResizeToContents)
  489. self.t_ui.tools_table.setSortingEnabled(False)
  490. self.t_ui.tools_table.setMinimumHeight(self.t_ui.tools_table.getHeight())
  491. self.t_ui.tools_table.setMaximumHeight(self.t_ui.tools_table.getHeight())
  492. # all the tools are selected by default
  493. self.t_ui.tools_table.selectAll()
  494. # Build Exclusion Areas section
  495. e_len = len(self.app.exc_areas.exclusion_areas_storage)
  496. self.t_ui.exclusion_table.setRowCount(e_len)
  497. area_id = 0
  498. for area in range(e_len):
  499. area_id += 1
  500. area_dict = self.app.exc_areas.exclusion_areas_storage[area]
  501. area_id_item = QtWidgets.QTableWidgetItem('%d' % int(area_id))
  502. area_id_item.setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled)
  503. self.t_ui.exclusion_table.setItem(area, 0, area_id_item) # Area id
  504. object_item = QtWidgets.QTableWidgetItem('%s' % area_dict["obj_type"])
  505. object_item.setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled)
  506. self.t_ui.exclusion_table.setItem(area, 1, object_item) # Origin Object
  507. strategy_item = QtWidgets.QTableWidgetItem('%s' % area_dict["strategy"])
  508. strategy_item.setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled)
  509. self.t_ui.exclusion_table.setItem(area, 2, strategy_item) # Strategy
  510. overz_item = QtWidgets.QTableWidgetItem('%s' % area_dict["overz"])
  511. overz_item.setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled)
  512. self.t_ui.exclusion_table.setItem(area, 3, overz_item) # Over Z
  513. self.t_ui.exclusion_table.resizeColumnsToContents()
  514. self.t_ui.exclusion_table.resizeRowsToContents()
  515. area_vheader = self.t_ui.exclusion_table.verticalHeader()
  516. area_vheader.hide()
  517. self.t_ui.exclusion_table.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
  518. area_hheader = self.t_ui.exclusion_table.horizontalHeader()
  519. area_hheader.setMinimumSectionSize(10)
  520. area_hheader.setDefaultSectionSize(70)
  521. area_hheader.setSectionResizeMode(0, QtWidgets.QHeaderView.Fixed)
  522. area_hheader.resizeSection(0, 20)
  523. area_hheader.setSectionResizeMode(1, QtWidgets.QHeaderView.Stretch)
  524. area_hheader.setSectionResizeMode(2, QtWidgets.QHeaderView.ResizeToContents)
  525. area_hheader.setSectionResizeMode(3, QtWidgets.QHeaderView.ResizeToContents)
  526. # area_hheader.setStretchLastSection(True)
  527. self.t_ui.exclusion_table.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
  528. self.t_ui.exclusion_table.setColumnWidth(0, 20)
  529. self.t_ui.exclusion_table.setMinimumHeight(self.t_ui.exclusion_table.getHeight())
  530. self.t_ui.exclusion_table.setMaximumHeight(self.t_ui.exclusion_table.getHeight())
  531. self.ui_connect()
  532. # set the text on tool_data_label after loading the object
  533. sel_rows = set()
  534. sel_items = self.t_ui.tools_table.selectedItems()
  535. for it in sel_items:
  536. sel_rows.add(it.row())
  537. if len(sel_rows) > 1:
  538. self.t_ui.tool_data_label.setText(
  539. "<b>%s: <font color='#0000FF'>%s</font></b>" % (_('Parameters for'), _("Multiple Tools"))
  540. )
  541. elif len(sel_rows) == 1:
  542. # update the QLabel that shows for which Tool we have the parameters in the UI form
  543. toolnr = int(self.t_ui.tools_table.item(list(sel_rows)[0], 0).text())
  544. self.t_ui.tool_data_label.setText(
  545. "<b>%s: <font color='#0000FF'>%s %d</font></b>" % (_('Parameters for'), _("Tool"), toolnr)
  546. )
  547. def on_object_changed(self):
  548. log.debug("ToolDrilling.on_object_changed()")
  549. # updated units
  550. self.units = self.app.defaults['units'].upper()
  551. # load the Excellon object
  552. self.obj_name = self.t_ui.object_combo.currentText()
  553. # Get source object.
  554. try:
  555. self.excellon_obj = self.app.collection.get_by_name(self.obj_name)
  556. except Exception:
  557. self.app.inform.emit('[ERROR_NOTCL] %s: %s' % (_("Could not retrieve object"), str(self.obj_name)))
  558. return
  559. if self.excellon_obj is None:
  560. self.excellon_tools = {}
  561. self.t_ui.exc_param_frame.setDisabled(True)
  562. self.set_tool_ui()
  563. else:
  564. self.excellon_tools = self.excellon_obj.tools
  565. self.app.collection.set_active(self.obj_name)
  566. self.t_ui.exc_param_frame.setDisabled(False)
  567. self.excellon_tools = self.excellon_obj.tools
  568. self.build_tool_ui()
  569. sel_rows = set()
  570. table_items = self.t_ui.tools_table.selectedItems()
  571. if table_items:
  572. for it in table_items:
  573. sel_rows.add(it.row())
  574. if not sel_rows or len(sel_rows) == 0:
  575. self.t_ui.generate_cnc_button.setDisabled(True)
  576. self.t_ui.tool_data_label.setText(
  577. "<b>%s: <font color='#0000FF'>%s</font></b>" % (_('Parameters for'), _("No Tool Selected"))
  578. )
  579. else:
  580. self.t_ui.generate_cnc_button.setDisabled(False)
  581. def ui_connect(self):
  582. # Area Exception - exclusion shape added signal
  583. # first disconnect it from any other object
  584. try:
  585. self.app.exc_areas.e_shape_modified.disconnect()
  586. except (TypeError, AttributeError):
  587. pass
  588. # then connect it to the current build_tool_ui() method
  589. self.app.exc_areas.e_shape_modified.connect(self.update_exclusion_table)
  590. # rows selected
  591. self.t_ui.tools_table.clicked.connect(self.on_row_selection_change)
  592. self.t_ui.tools_table.horizontalHeader().sectionClicked.connect(self.on_toggle_all_rows)
  593. # Tool Parameters
  594. for opt in self.form_fields:
  595. current_widget = self.form_fields[opt]
  596. if isinstance(current_widget, FCCheckBox):
  597. current_widget.stateChanged.connect(self.form_to_storage)
  598. if isinstance(current_widget, RadioSet):
  599. current_widget.activated_custom.connect(self.form_to_storage)
  600. elif isinstance(current_widget, FCDoubleSpinner) or isinstance(current_widget, FCSpinner):
  601. current_widget.returnPressed.connect(self.form_to_storage)
  602. elif isinstance(current_widget, FCComboBox):
  603. current_widget.currentIndexChanged.connect(self.form_to_storage)
  604. self.t_ui.order_radio.activated_custom[str].connect(self.on_order_changed)
  605. def ui_disconnect(self):
  606. # rows selected
  607. try:
  608. self.t_ui.tools_table.clicked.disconnect(self.on_row_selection_change)
  609. except (TypeError, AttributeError):
  610. pass
  611. try:
  612. self.t_ui.tools_table.horizontalHeader().sectionClicked.disconnect(self.on_toggle_all_rows)
  613. except (TypeError, AttributeError):
  614. pass
  615. # tool table widgets
  616. for row in range(self.t_ui.tools_table.rowCount()):
  617. try:
  618. self.t_ui.tools_table.cellWidget(row, 2).currentIndexChanged.disconnect()
  619. except (TypeError, AttributeError):
  620. pass
  621. # Tool Parameters
  622. for opt in self.form_fields:
  623. current_widget = self.form_fields[opt]
  624. if isinstance(current_widget, FCCheckBox):
  625. try:
  626. current_widget.stateChanged.disconnect(self.form_to_storage)
  627. except (TypeError, ValueError):
  628. pass
  629. if isinstance(current_widget, RadioSet):
  630. try:
  631. current_widget.activated_custom.disconnect(self.form_to_storage)
  632. except (TypeError, ValueError):
  633. pass
  634. elif isinstance(current_widget, FCDoubleSpinner) or isinstance(current_widget, FCSpinner):
  635. try:
  636. current_widget.returnPressed.disconnect(self.form_to_storage)
  637. except (TypeError, ValueError):
  638. pass
  639. elif isinstance(current_widget, FCComboBox):
  640. try:
  641. current_widget.currentIndexChanged.disconnect(self.form_to_storage)
  642. except (TypeError, ValueError):
  643. pass
  644. try:
  645. self.t_ui.order_radio.activated_custom[str].disconnect()
  646. except (TypeError, ValueError):
  647. pass
  648. def on_toggle_all_rows(self):
  649. """
  650. will toggle the selection of all rows in Tools table
  651. :return:
  652. """
  653. sel_model = self.t_ui.tools_table.selectionModel()
  654. sel_indexes = sel_model.selectedIndexes()
  655. # it will iterate over all indexes which means all items in all columns too but I'm interested only on rows
  656. sel_rows = set()
  657. for idx in sel_indexes:
  658. sel_rows.add(idx.row())
  659. if len(sel_rows) == self.t_ui.tools_table.rowCount():
  660. self.t_ui.tools_table.clearSelection()
  661. self.t_ui.exc_param_frame.setDisabled(True)
  662. else:
  663. self.t_ui.tools_table.selectAll()
  664. self.t_ui.exc_param_frame.setDisabled(False)
  665. self.update_ui()
  666. def on_row_selection_change(self):
  667. self.update_ui()
  668. def update_ui(self):
  669. self.blockSignals(True)
  670. sel_rows = set()
  671. table_items = self.t_ui.tools_table.selectedItems()
  672. if table_items:
  673. for it in table_items:
  674. sel_rows.add(it.row())
  675. # sel_rows = sorted(set(index.row() for index in self.t_ui.tools_table.selectedIndexes()))
  676. if not sel_rows or len(sel_rows) == 0:
  677. self.t_ui.generate_cnc_button.setDisabled(True)
  678. self.t_ui.exc_param_frame.setDisabled(True)
  679. self.t_ui.tool_data_label.setText(
  680. "<b>%s: <font color='#0000FF'>%s</font></b>" % (_('Parameters for'), _("No Tool Selected"))
  681. )
  682. self.blockSignals(False)
  683. return
  684. else:
  685. self.t_ui.generate_cnc_button.setDisabled(False)
  686. self.t_ui.exc_param_frame.setDisabled(False)
  687. if len(sel_rows) == 1:
  688. # update the QLabel that shows for which Tool we have the parameters in the UI form
  689. tooluid = int(self.t_ui.tools_table.item(list(sel_rows)[0], 0).text())
  690. self.t_ui.tool_data_label.setText(
  691. "<b>%s: <font color='#0000FF'>%s %d</font></b>" % (_('Parameters for'), _("Tool"), tooluid)
  692. )
  693. else:
  694. self.t_ui.tool_data_label.setText(
  695. "<b>%s: <font color='#0000FF'>%s</font></b>" % (_('Parameters for'), _("Multiple Tools"))
  696. )
  697. for c_row in sel_rows:
  698. # populate the form with the data from the tool associated with the row parameter
  699. try:
  700. item = self.t_ui.tools_table.item(c_row, 3)
  701. if type(item) is not None:
  702. tooluid = int(item.text())
  703. self.storage_to_form(self.excellon_tools[tooluid]['data'])
  704. else:
  705. self.blockSignals(False)
  706. return
  707. except Exception as e:
  708. log.debug("Tool missing. Add a tool in the Tool Table. %s" % str(e))
  709. self.blockSignals(False)
  710. return
  711. self.blockSignals(False)
  712. def storage_to_form(self, dict_storage):
  713. """
  714. Will update the GUI with data from the "storage" in this case the dict self.tools
  715. :param dict_storage: A dictionary holding the data relevant for gnerating Gcode from Excellon
  716. :type dict_storage: dict
  717. :return: None
  718. :rtype:
  719. """
  720. for form_key in self.form_fields:
  721. for storage_key in dict_storage:
  722. if form_key == storage_key and form_key not in \
  723. ["toolchange", "toolchangez", "startz", "endz", "ppname_e", "ppname_g"]:
  724. try:
  725. self.form_fields[form_key].set_value(dict_storage[form_key])
  726. except Exception as e:
  727. log.debug("ToolDrilling.storage_to_form() --> %s" % str(e))
  728. pass
  729. def form_to_storage(self):
  730. """
  731. Will update the 'storage' attribute which is the dict self.tools with data collected from GUI
  732. :return: None
  733. :rtype:
  734. """
  735. if self.t_ui.tools_table.rowCount() == 2:
  736. # there is no tool in tool table so we can't save the GUI elements values to storage
  737. # Excellon Tool Table has 2 rows by default
  738. return
  739. self.blockSignals(True)
  740. widget_changed = self.sender()
  741. wdg_objname = widget_changed.objectName()
  742. option_changed = self.name2option[wdg_objname]
  743. # row = self.t_ui.tools_table.currentRow()
  744. rows = sorted(list(set(index.row() for index in self.t_ui.tools_table.selectedIndexes())))
  745. for row in rows:
  746. if row < 0:
  747. row = 0
  748. tooluid_item = int(self.t_ui.tools_table.item(row, 3).text())
  749. for tooluid_key, tooluid_val in self.excellon_tools.items():
  750. if int(tooluid_key) == tooluid_item:
  751. new_option_value = self.form_fields[option_changed].get_value()
  752. if option_changed in tooluid_val:
  753. tooluid_val[option_changed] = new_option_value
  754. if option_changed in tooluid_val['data']:
  755. tooluid_val['data'][option_changed] = new_option_value
  756. self.blockSignals(False)
  757. def get_selected_tools_list(self):
  758. """
  759. Returns the keys to the self.tools dictionary corresponding
  760. to the selections on the tool list in the appGUI.
  761. :return: List of tools.
  762. :rtype: list
  763. """
  764. return [str(x.text()) for x in self.t_ui.tools_table.selectedItems()]
  765. def get_selected_tools_table_items(self):
  766. """
  767. Returns a list of lists, each list in the list is made out of row elements
  768. :return: List of table_tools items.
  769. :rtype: list
  770. """
  771. table_tools_items = []
  772. for x in self.t_ui.tools_table.selectedItems():
  773. # from the columnCount we subtract a value of 1 which represent the last column (plot column)
  774. # which does not have text
  775. txt = ''
  776. elem = []
  777. for column in range(0, self.t_ui.tools_table.columnCount() - 1):
  778. try:
  779. txt = self.t_ui.tools_table.item(x.row(), column).text()
  780. except AttributeError:
  781. try:
  782. txt = self.t_ui.tools_table.cellWidget(x.row(), column).currentText()
  783. except AttributeError:
  784. pass
  785. elem.append(txt)
  786. table_tools_items.append(deepcopy(elem))
  787. # table_tools_items.append([self.t_ui.tools_table.item(x.row(), column).text()
  788. # for column in range(0, self.t_ui.tools_table.columnCount() - 1)])
  789. for item in table_tools_items:
  790. item[0] = str(item[0])
  791. return table_tools_items
  792. def on_apply_param_to_all_clicked(self):
  793. if self.t_ui.tools_table.rowCount() == 0:
  794. # there is no tool in tool table so we can't save the GUI elements values to storage
  795. log.debug("ToolDrilling.on_apply_param_to_all_clicked() --> no tool in Tools Table, aborting.")
  796. return
  797. self.blockSignals(True)
  798. row = self.t_ui.tools_table.currentRow()
  799. if row < 0:
  800. row = 0
  801. tooluid_item = int(self.t_ui.tools_table.item(row, 3).text())
  802. temp_tool_data = {}
  803. for tooluid_key, tooluid_val in self.excellon_tools.items():
  804. if int(tooluid_key) == tooluid_item:
  805. # this will hold the 'data' key of the self.tools[tool] dictionary that corresponds to
  806. # the current row in the tool table
  807. temp_tool_data = tooluid_val['data']
  808. break
  809. for tooluid_key, tooluid_val in self.excellon_tools.items():
  810. tooluid_val['data'] = deepcopy(temp_tool_data)
  811. self.app.inform.emit('[success] %s' % _("Current Tool parameters were applied to all tools."))
  812. self.blockSignals(False)
  813. def on_order_changed(self, order):
  814. if order != 'no':
  815. self.build_tool_ui()
  816. def on_tooltable_cellwidget_change(self):
  817. cw = self.sender()
  818. assert isinstance(cw, QtWidgets.QComboBox), \
  819. "Expected a QtWidgets.QComboBox, got %s" % isinstance(cw, QtWidgets.QComboBox)
  820. cw_index = self.t_ui.tools_table.indexAt(cw.pos())
  821. cw_row = cw_index.row()
  822. cw_col = cw_index.column()
  823. current_uid = int(self.t_ui.tools_table.item(cw_row, 3).text())
  824. # if the sender is in the column with index 2 then we update the tool_type key
  825. if cw_col == 2:
  826. tt = cw.currentText()
  827. typ = 'Iso' if tt == 'V' else "Rough"
  828. self.excellon_tools[current_uid].update({
  829. 'type': typ,
  830. 'tool_type': tt,
  831. })
  832. def generate_milling_drills(self, tools=None, outname=None, tooldia=None, plot=False, use_thread=False):
  833. """
  834. Will generate an Geometry Object allowing to cut a drill hole instead of drilling it.
  835. Note: This method is a good template for generic operations as
  836. it takes it's options from parameters or otherwise from the
  837. object's options and returns a (success, msg) tuple as feedback
  838. for shell operations.
  839. :param tools: A list of tools where the drills are to be milled or a string: "all"
  840. :type tools:
  841. :param outname: the name of the resulting Geometry object
  842. :type outname: str
  843. :param tooldia: the tool diameter to be used in creation of the milling path (Geometry Object)
  844. :type tooldia: float
  845. :param plot: if to plot the resulting object
  846. :type plot: bool
  847. :param use_thread: if to use threading for creation of the Geometry object
  848. :type use_thread: bool
  849. :return: Success/failure condition tuple (bool, str).
  850. :rtype: tuple
  851. """
  852. # Get the tools from the list. These are keys
  853. # to self.tools
  854. if tools is None:
  855. tools = self.get_selected_tools_list()
  856. if outname is None:
  857. outname = self.options["name"] + "_mill"
  858. if tooldia is None:
  859. tooldia = float(self.options["tooldia"])
  860. # Sort tools by diameter. items() -> [('name', diameter), ...]
  861. # sorted_tools = sorted(list(self.tools.items()), key=lambda tl: tl[1]) # no longer works in Python3
  862. sort = []
  863. for k, v in self.tools.items():
  864. sort.append((k, v.get('tooldia')))
  865. sorted_tools = sorted(sort, key=lambda t1: t1[1])
  866. if tools == "all":
  867. tools = [i[0] for i in sorted_tools] # List if ordered tool names.
  868. log.debug("Tools 'all' and sorted are: %s" % str(tools))
  869. if len(tools) == 0:
  870. self.app.inform.emit('[ERROR_NOTCL] %s' %
  871. _("Please select one or more tools from the list and try again."))
  872. return False, "Error: No tools."
  873. for tool in tools:
  874. if tooldia > self.tools[tool]["C"]:
  875. self.app.inform.emit(
  876. '[ERROR_NOTCL] %s %s: %s' % (
  877. _("Milling tool for DRILLS is larger than hole size. Cancelled."),
  878. _("Tool"),
  879. str(tool)
  880. )
  881. )
  882. return False, "Error: Milling tool is larger than hole."
  883. def geo_init(geo_obj, app_obj):
  884. """
  885. :param geo_obj: New object
  886. :type geo_obj: GeometryObject
  887. :param app_obj: App
  888. :type app_obj: FlatCAMApp.App
  889. :return:
  890. :rtype:
  891. """
  892. assert geo_obj.kind == 'geometry', "Initializer expected a GeometryObject, got %s" % type(geo_obj)
  893. app_obj.inform.emit(_("Generating drills milling geometry..."))
  894. # ## Add properties to the object
  895. # get the tool_table items in a list of row items
  896. tool_table_items = self.get_selected_tools_table_items()
  897. # insert an information only element in the front
  898. tool_table_items.insert(0, [_("Tool_nr"), _("Diameter"), _("Drills_Nr"), _("Slots_Nr")])
  899. geo_obj.options['Tools_in_use'] = tool_table_items
  900. geo_obj.options['type'] = 'Excellon Geometry'
  901. geo_obj.options["cnctooldia"] = str(tooldia)
  902. geo_obj.options["multidepth"] = self.options["multidepth"]
  903. geo_obj.solid_geometry = []
  904. # in case that the tool used has the same diameter with the hole, and since the maximum resolution
  905. # for FlatCAM is 6 decimals,
  906. # we add a tenth of the minimum value, meaning 0.0000001, which from our point of view is "almost zero"
  907. for hole in self.drills:
  908. if hole['tool'] in tools:
  909. buffer_value = self.tools[hole['tool']]["C"] / 2 - tooldia / 2
  910. if buffer_value == 0:
  911. geo_obj.solid_geometry.append(
  912. Point(hole['point']).buffer(0.0000001).exterior)
  913. else:
  914. geo_obj.solid_geometry.append(
  915. Point(hole['point']).buffer(buffer_value).exterior)
  916. if use_thread:
  917. def geo_thread(a_obj):
  918. a_obj.app_obj.new_object("geometry", outname, geo_init, plot=plot)
  919. # Create a promise with the new name
  920. self.app.collection.promise(outname)
  921. # Send to worker
  922. self.app.worker_task.emit({'fcn': geo_thread, 'params': [self.app]})
  923. else:
  924. self.app.app_obj.new_object("geometry", outname, geo_init, plot=plot)
  925. return True, ""
  926. def generate_milling_slots(self, tools=None, outname=None, tooldia=None, plot=False, use_thread=False):
  927. """
  928. Will generate an Geometry Object allowing to cut/mill a slot hole.
  929. Note: This method is a good template for generic operations as
  930. it takes it's options from parameters or otherwise from the
  931. object's options and returns a (success, msg) tuple as feedback
  932. for shell operations.
  933. :param tools: A list of tools where the drills are to be milled or a string: "all"
  934. :type tools:
  935. :param outname: the name of the resulting Geometry object
  936. :type outname: str
  937. :param tooldia: the tool diameter to be used in creation of the milling path (Geometry Object)
  938. :type tooldia: float
  939. :param plot: if to plot the resulting object
  940. :type plot: bool
  941. :param use_thread: if to use threading for creation of the Geometry object
  942. :type use_thread: bool
  943. :return: Success/failure condition tuple (bool, str).
  944. :rtype: tuple
  945. """
  946. # Get the tools from the list. These are keys
  947. # to self.tools
  948. if tools is None:
  949. tools = self.get_selected_tools_list()
  950. if outname is None:
  951. outname = self.options["name"] + "_mill"
  952. if tooldia is None:
  953. tooldia = float(self.options["slot_tooldia"])
  954. # Sort tools by diameter. items() -> [('name', diameter), ...]
  955. # sorted_tools = sorted(list(self.tools.items()), key=lambda tl: tl[1]) # no longer works in Python3
  956. sort = []
  957. for k, v in self.tools.items():
  958. sort.append((k, v.get('tooldia')))
  959. sorted_tools = sorted(sort, key=lambda t1: t1[1])
  960. if tools == "all":
  961. tools = [i[0] for i in sorted_tools] # List if ordered tool names.
  962. log.debug("Tools 'all' and sorted are: %s" % str(tools))
  963. if len(tools) == 0:
  964. self.app.inform.emit('[ERROR_NOTCL] %s' %
  965. _("Please select one or more tools from the list and try again."))
  966. return False, "Error: No tools."
  967. for tool in tools:
  968. # I add the 0.0001 value to account for the rounding error in converting from IN to MM and reverse
  969. adj_toolstable_tooldia = float('%.*f' % (self.decimals, float(tooldia)))
  970. adj_file_tooldia = float('%.*f' % (self.decimals, float(self.tools[tool]["C"])))
  971. if adj_toolstable_tooldia > adj_file_tooldia + 0.0001:
  972. self.app.inform.emit('[ERROR_NOTCL] %s' %
  973. _("Milling tool for SLOTS is larger than hole size. Cancelled."))
  974. return False, "Error: Milling tool is larger than hole."
  975. def geo_init(geo_obj, app_obj):
  976. assert geo_obj.kind == 'geometry', "Initializer expected a GeometryObject, got %s" % type(geo_obj)
  977. app_obj.inform.emit(_("Generating slot milling geometry..."))
  978. # ## Add properties to the object
  979. # get the tool_table items in a list of row items
  980. tool_table_items = self.get_selected_tools_table_items()
  981. # insert an information only element in the front
  982. tool_table_items.insert(0, [_("Tool_nr"), _("Diameter"), _("Drills_Nr"), _("Slots_Nr")])
  983. geo_obj.options['Tools_in_use'] = tool_table_items
  984. geo_obj.options['type'] = 'Excellon Geometry'
  985. geo_obj.options["cnctooldia"] = str(tooldia)
  986. geo_obj.options["multidepth"] = self.options["multidepth"]
  987. geo_obj.solid_geometry = []
  988. # in case that the tool used has the same diameter with the hole, and since the maximum resolution
  989. # for FlatCAM is 6 decimals,
  990. # we add a tenth of the minimum value, meaning 0.0000001, which from our point of view is "almost zero"
  991. for slot in self.slots:
  992. if slot['tool'] in tools:
  993. toolstable_tool = float('%.*f' % (self.decimals, float(tooldia)))
  994. file_tool = float('%.*f' % (self.decimals, float(self.tools[tool]["C"])))
  995. # I add the 0.0001 value to account for the rounding error in converting from IN to MM and reverse
  996. # for the file_tool (tooldia actually)
  997. buffer_value = float(file_tool / 2) - float(toolstable_tool / 2) + 0.0001
  998. if buffer_value == 0:
  999. start = slot['start']
  1000. stop = slot['stop']
  1001. lines_string = LineString([start, stop])
  1002. poly = lines_string.buffer(0.0000001, int(self.geo_steps_per_circle)).exterior
  1003. geo_obj.solid_geometry.append(poly)
  1004. else:
  1005. start = slot['start']
  1006. stop = slot['stop']
  1007. lines_string = LineString([start, stop])
  1008. poly = lines_string.buffer(buffer_value, int(self.geo_steps_per_circle)).exterior
  1009. geo_obj.solid_geometry.append(poly)
  1010. if use_thread:
  1011. def geo_thread(a_obj):
  1012. a_obj.app_obj.new_object("geometry", outname + '_slot', geo_init, plot=plot)
  1013. # Create a promise with the new name
  1014. self.app.collection.promise(outname)
  1015. # Send to worker
  1016. self.app.worker_task.emit({'fcn': geo_thread, 'params': [self.app]})
  1017. else:
  1018. self.app.app_obj.new_object("geometry", outname + '_slot', geo_init, plot=plot)
  1019. return True, ""
  1020. def on_pp_changed(self):
  1021. current_pp = self.t_ui.pp_excellon_name_cb.get_value()
  1022. if "toolchange_probe" in current_pp.lower():
  1023. self.t_ui.pdepth_entry.setVisible(True)
  1024. self.t_ui.pdepth_label.show()
  1025. self.t_ui.feedrate_probe_entry.setVisible(True)
  1026. self.t_ui.feedrate_probe_label.show()
  1027. else:
  1028. self.t_ui.pdepth_entry.setVisible(False)
  1029. self.t_ui.pdepth_label.hide()
  1030. self.t_ui.feedrate_probe_entry.setVisible(False)
  1031. self.t_ui.feedrate_probe_label.hide()
  1032. if 'marlin' in current_pp.lower() or 'custom' in current_pp.lower():
  1033. self.t_ui.feedrate_rapid_label.show()
  1034. self.t_ui.feedrate_rapid_entry.show()
  1035. else:
  1036. self.t_ui.feedrate_rapid_label.hide()
  1037. self.t_ui.feedrate_rapid_entry.hide()
  1038. if 'laser' in current_pp.lower():
  1039. self.t_ui.cutzlabel.hide()
  1040. self.t_ui.cutz_entry.hide()
  1041. try:
  1042. self.t_ui.mpass_cb.hide()
  1043. self.t_ui.maxdepth_entry.hide()
  1044. except AttributeError:
  1045. pass
  1046. if 'marlin' in current_pp.lower():
  1047. self.t_ui.travelzlabel.setText('%s:' % _("Focus Z"))
  1048. self.t_ui.endz_label.show()
  1049. self.t_ui.endz_entry.show()
  1050. else:
  1051. self.t_ui.travelzlabel.hide()
  1052. self.t_ui.travelz_entry.hide()
  1053. self.t_ui.endz_label.hide()
  1054. self.t_ui.endz_entry.hide()
  1055. try:
  1056. self.t_ui.frzlabel.hide()
  1057. self.t_ui.feedrate_z_entry.hide()
  1058. except AttributeError:
  1059. pass
  1060. self.t_ui.dwell_cb.hide()
  1061. self.t_ui.dwelltime_entry.hide()
  1062. self.t_ui.spindle_label.setText('%s:' % _("Laser Power"))
  1063. try:
  1064. self.t_ui.tool_offset_label.hide()
  1065. self.t_ui.offset_entry.hide()
  1066. except AttributeError:
  1067. pass
  1068. else:
  1069. self.t_ui.cutzlabel.show()
  1070. self.t_ui.cutz_entry.show()
  1071. try:
  1072. self.t_ui.mpass_cb.show()
  1073. self.t_ui.maxdepth_entry.show()
  1074. except AttributeError:
  1075. pass
  1076. self.t_ui.travelzlabel.setText('%s:' % _('Travel Z'))
  1077. self.t_ui.travelzlabel.show()
  1078. self.t_ui.travelz_entry.show()
  1079. self.t_ui.endz_label.show()
  1080. self.t_ui.endz_entry.show()
  1081. try:
  1082. self.t_ui.frzlabel.show()
  1083. self.t_ui.feedrate_z_entry.show()
  1084. except AttributeError:
  1085. pass
  1086. self.t_ui.dwell_cb.show()
  1087. self.t_ui.dwelltime_entry.show()
  1088. self.t_ui.spindle_label.setText('%s:' % _('Spindle speed'))
  1089. try:
  1090. # self.t_ui.tool_offset_lbl.show()
  1091. self.t_ui.offset_entry.show()
  1092. except AttributeError:
  1093. pass
  1094. def on_key_press(self, event):
  1095. # modifiers = QtWidgets.QApplication.keyboardModifiers()
  1096. # matplotlib_key_flag = False
  1097. # events out of the self.app.collection view (it's about Project Tab) are of type int
  1098. if type(event) is int:
  1099. key = event
  1100. # events from the GUI are of type QKeyEvent
  1101. elif type(event) == QtGui.QKeyEvent:
  1102. key = event.key()
  1103. elif isinstance(event, mpl_key_event): # MatPlotLib key events are trickier to interpret than the rest
  1104. # matplotlib_key_flag = True
  1105. key = event.key
  1106. key = QtGui.QKeySequence(key)
  1107. # check for modifiers
  1108. key_string = key.toString().lower()
  1109. if '+' in key_string:
  1110. mod, __, key_text = key_string.rpartition('+')
  1111. if mod.lower() == 'ctrl':
  1112. # modifiers = QtCore.Qt.ControlModifier
  1113. pass
  1114. elif mod.lower() == 'alt':
  1115. # modifiers = QtCore.Qt.AltModifier
  1116. pass
  1117. elif mod.lower() == 'shift':
  1118. # modifiers = QtCore.Qt.ShiftModifier
  1119. pass
  1120. else:
  1121. # modifiers = QtCore.Qt.NoModifier
  1122. pass
  1123. key = QtGui.QKeySequence(key_text)
  1124. # events from Vispy are of type KeyEvent
  1125. else:
  1126. key = event.key
  1127. if key == QtCore.Qt.Key_Escape or key == 'Escape':
  1128. self.points = []
  1129. self.poly_drawn = False
  1130. self.delete_moving_selection_shape()
  1131. self.delete_tool_selection_shape()
  1132. def on_add_area_click(self):
  1133. shape_button = self.t_ui.area_shape_radio
  1134. overz_button = self.t_ui.over_z_entry
  1135. strategy_radio = self.t_ui.strategy_radio
  1136. cnc_button = self.t_ui.generate_cnc_button
  1137. solid_geo = self.excellon_obj.solid_geometry
  1138. obj_type = self.excellon_obj.kind
  1139. self.app.exc_areas.on_add_area_click(
  1140. shape_button=shape_button, overz_button=overz_button, cnc_button=cnc_button, strategy_radio=strategy_radio,
  1141. solid_geo=solid_geo, obj_type=obj_type)
  1142. def on_clear_area_click(self):
  1143. if not self.app.exc_areas.exclusion_areas_storage:
  1144. self.app.inform.emit("[WARNING_NOTCL] %s" % _("Delete failed. There are no exclusion areas to delete."))
  1145. return
  1146. self.app.exc_areas.on_clear_area_click()
  1147. self.app.exc_areas.e_shape_modified.emit()
  1148. def on_delete_sel_areas(self):
  1149. sel_model = self.t_ui.exclusion_table.selectionModel()
  1150. sel_indexes = sel_model.selectedIndexes()
  1151. # it will iterate over all indexes which means all items in all columns too but I'm interested only on rows
  1152. # so the duplicate rows will not be added
  1153. sel_rows = set()
  1154. for idx in sel_indexes:
  1155. sel_rows.add(idx.row())
  1156. if not sel_rows:
  1157. self.app.inform.emit("[WARNING_NOTCL] %s" % _("Delete failed. Nothing is selected."))
  1158. return
  1159. self.app.exc_areas.delete_sel_shapes(idxs=list(sel_rows))
  1160. self.app.exc_areas.e_shape_modified.emit()
  1161. def draw_sel_shape(self):
  1162. sel_model = self.t_ui.exclusion_table.selectionModel()
  1163. sel_indexes = sel_model.selectedIndexes()
  1164. # it will iterate over all indexes which means all items in all columns too but I'm interested only on rows
  1165. sel_rows = set()
  1166. for idx in sel_indexes:
  1167. sel_rows.add(idx.row())
  1168. self.delete_sel_shape()
  1169. if self.app.is_legacy is False:
  1170. face = self.app.defaults['global_sel_fill'][:-2] + str(hex(int(0.2 * 255)))[2:]
  1171. outline = self.app.defaults['global_sel_line'][:-2] + str(hex(int(0.8 * 255)))[2:]
  1172. else:
  1173. face = self.app.defaults['global_sel_fill'][:-2] + str(hex(int(0.4 * 255)))[2:]
  1174. outline = self.app.defaults['global_sel_line'][:-2] + str(hex(int(1.0 * 255)))[2:]
  1175. for row in sel_rows:
  1176. sel_rect = self.app.exc_areas.exclusion_areas_storage[row]['shape']
  1177. self.app.move_tool.sel_shapes.add(sel_rect, color=outline, face_color=face, update=True, layer=0,
  1178. tolerance=None)
  1179. if self.app.is_legacy is True:
  1180. self.app.move_tool.sel_shapes.redraw()
  1181. def clear_selection(self):
  1182. self.app.delete_selection_shape()
  1183. # self.t_ui.exclusion_table.clearSelection()
  1184. def delete_sel_shape(self):
  1185. self.app.delete_selection_shape()
  1186. def update_exclusion_table(self):
  1187. self.exclusion_area_cb_is_checked = True if self.t_ui.exclusion_cb.isChecked() else False
  1188. self.build_tool_ui()
  1189. self.t_ui.exclusion_cb.set_value(self.exclusion_area_cb_is_checked)
  1190. def on_strategy(self, val):
  1191. if val == 'around':
  1192. self.t_ui.over_z_label.setDisabled(True)
  1193. self.t_ui.over_z_entry.setDisabled(True)
  1194. else:
  1195. self.t_ui.over_z_label.setDisabled(False)
  1196. self.t_ui.over_z_entry.setDisabled(False)
  1197. def exclusion_table_toggle_all(self):
  1198. """
  1199. will toggle the selection of all rows in Exclusion Areas table
  1200. :return:
  1201. """
  1202. sel_model = self.t_ui.exclusion_table.selectionModel()
  1203. sel_indexes = sel_model.selectedIndexes()
  1204. # it will iterate over all indexes which means all items in all columns too but I'm interested only on rows
  1205. sel_rows = set()
  1206. for idx in sel_indexes:
  1207. sel_rows.add(idx.row())
  1208. if sel_rows:
  1209. self.t_ui.exclusion_table.clearSelection()
  1210. self.delete_sel_shape()
  1211. else:
  1212. self.t_ui.exclusion_table.selectAll()
  1213. self.draw_sel_shape()
  1214. def on_cnc_button_click(self):
  1215. obj_name = self.t_ui.object_combo.currentText()
  1216. # Get source object.
  1217. try:
  1218. self.excellon_obj = self.app.collection.get_by_name(obj_name)
  1219. except Exception:
  1220. self.app.inform.emit('[ERROR_NOTCL] %s: %s' % (_("Could not retrieve object"), str(obj_name)))
  1221. return
  1222. if self.excellon_obj is None:
  1223. self.app.inform.emit('[ERROR_NOTCL] %s.' % _("Object not found"))
  1224. return
  1225. # Get the tools from the Tool Table
  1226. selected_uid = set()
  1227. for it in self.t_ui.tools_table.selectedItems():
  1228. uid = self.t_ui.tools_table.item(it.row(), 3).text()
  1229. selected_uid.add(uid)
  1230. tools = list(selected_uid)
  1231. if len(tools) == 0:
  1232. # if there is a single tool in the table (remember that the last 2 rows are for totals and do not count in
  1233. # tool number) it means that there are 3 rows (1 tool and 2 totals).
  1234. # in this case regardless of the selection status of that tool, use it.
  1235. if self.t_ui.tools_table.rowCount() == 3:
  1236. tools.append(self.t_ui.tools_table.item(0, 0).text())
  1237. else:
  1238. self.app.inform.emit('[ERROR_NOTCL] %s' %
  1239. _("Please select one or more tools from the list and try again."))
  1240. return
  1241. xmin = self.excellon_obj.options['xmin']
  1242. ymin = self.excellon_obj.options['ymin']
  1243. xmax = self.excellon_obj.options['xmax']
  1244. ymax = self.excellon_obj.options['ymax']
  1245. job_name = self.excellon_obj.options["name"] + "_cnc"
  1246. pp_excellon_name = self.t_ui.pp_excellon_name_cb.get_value()
  1247. # z_pdepth = self.t_ui.pdepth_entry.get_value()
  1248. # feedrate_probe = self.t_ui.feedrate_probe_entry.get_value()
  1249. # toolchange = self.t_ui.toolchange_cb.get_value()
  1250. # z_toolchange = self.t_ui.toolchangez_entry.get_value()
  1251. # startz = self.t_ui.estartz_entry.get_value()
  1252. # endz = self.t_ui.endz_entry.get_value()
  1253. # xy_end = self.t_ui.endxy_entry.get_value()
  1254. # Object initialization function for app.app_obj.new_object()
  1255. def job_init(job_obj, app_obj):
  1256. assert job_obj.kind == 'cncjob', "Initializer expected a CNCJobObject, got %s" % type(job_obj)
  1257. app_obj.inform.emit(_("Generating Excellon CNCJob..."))
  1258. # get the tool_table items in a list of row items
  1259. tool_table_items = self.get_selected_tools_table_items()
  1260. # insert an information only element in the front
  1261. tool_table_items.insert(0, [_("Tool_nr"), _("Diameter"), _("Drills_Nr"), _("Slots_Nr")])
  1262. # ## Add properties to the object
  1263. job_obj.origin_kind = 'excellon'
  1264. job_obj.options['Tools_in_use'] = tool_table_items
  1265. job_obj.options['type'] = 'Excellon'
  1266. job_obj.options['ppname_e'] = pp_excellon_name
  1267. job_obj.pp_excellon_name = pp_excellon_name
  1268. job_obj.toolchange_xy_type = "excellon"
  1269. job_obj.coords_decimals = int(self.app.defaults["cncjob_coords_decimals"])
  1270. job_obj.fr_decimals = int(self.app.defaults["cncjob_fr_decimals"])
  1271. job_obj.options['xmin'] = xmin
  1272. job_obj.options['ymin'] = ymin
  1273. job_obj.options['xmax'] = xmax
  1274. job_obj.options['ymax'] = ymax
  1275. # job_obj.z_pdepth = z_pdepth
  1276. # job_obj.feedrate_probe = feedrate_probe
  1277. #
  1278. # job_obj.toolchange = toolchange
  1279. # job_obj.xy_toolchange = self.app.defaults["excellon_toolchangexy"]
  1280. # job_obj.z_toolchange = z_toolchange
  1281. # job_obj.startz = startz
  1282. # job_obj.endz = endz
  1283. # job_obj.xy_end = xy_end
  1284. # job_obj.excellon_optimization_type = self.app.defaults["excellon_optimization_type"]
  1285. tools_csv = ','.join(tools)
  1286. job_obj.gcode = self.generate_from_excellon_by_tool(tools_csv, self.tools)
  1287. print(job_obj.gcode)
  1288. if job_obj.gcode == 'fail':
  1289. return 'fail'
  1290. job_obj.gcode_parse()
  1291. job_obj.create_geometry()
  1292. # To be run in separate thread
  1293. def job_thread(a_obj):
  1294. with self.app.proc_container.new(_("Generating CNC Code")):
  1295. a_obj.app_obj.new_object("cncjob", job_name, job_init)
  1296. # Create promise for the new name.
  1297. self.app.collection.promise(job_name)
  1298. # Send to worker
  1299. # self.app.worker.add_task(job_thread, [self.app])
  1300. self.app.worker_task.emit({'fcn': job_thread, 'params': [self.app]})
  1301. def drilling_handler(self, obj):
  1302. pass
  1303. # Distance callback
  1304. class CreateDistanceCallback(object):
  1305. """Create callback to calculate distances between points."""
  1306. def __init__(self, locs, manager):
  1307. self.manager = manager
  1308. self.matrix = {}
  1309. if locs:
  1310. size = len(locs)
  1311. for from_node in range(size):
  1312. self.matrix[from_node] = {}
  1313. for to_node in range(size):
  1314. if from_node == to_node:
  1315. self.matrix[from_node][to_node] = 0
  1316. else:
  1317. x1 = locs[from_node][0]
  1318. y1 = locs[from_node][1]
  1319. x2 = locs[to_node][0]
  1320. y2 = locs[to_node][1]
  1321. self.matrix[from_node][to_node] = distance_euclidian(x1, y1, x2, y2)
  1322. # def Distance(self, from_node, to_node):
  1323. # return int(self.matrix[from_node][to_node])
  1324. def Distance(self, from_index, to_index):
  1325. # Convert from routing variable Index to distance matrix NodeIndex.
  1326. from_node = self.manager.IndexToNode(from_index)
  1327. to_node = self.manager.IndexToNode(to_index)
  1328. return self.matrix[from_node][to_node]
  1329. @staticmethod
  1330. def create_tool_data_array(points):
  1331. # Create the data.
  1332. loc_list = []
  1333. for pt in points:
  1334. loc_list.append((pt.coords.xy[0][0], pt.coords.xy[1][0]))
  1335. return loc_list
  1336. def optimized_ortools_meta(self, locations, start=None):
  1337. optimized_path = []
  1338. tsp_size = len(locations)
  1339. num_routes = 1 # The number of routes, which is 1 in the TSP.
  1340. # Nodes are indexed from 0 to tsp_size - 1. The depot is the starting node of the route.
  1341. depot = 0 if start is None else start
  1342. # Create routing model.
  1343. if tsp_size > 0:
  1344. manager = pywrapcp.RoutingIndexManager(tsp_size, num_routes, depot)
  1345. routing = pywrapcp.RoutingModel(manager)
  1346. search_parameters = pywrapcp.DefaultRoutingSearchParameters()
  1347. search_parameters.local_search_metaheuristic = (
  1348. routing_enums_pb2.LocalSearchMetaheuristic.GUIDED_LOCAL_SEARCH)
  1349. # Set search time limit in milliseconds.
  1350. if float(self.app.defaults["excellon_search_time"]) != 0:
  1351. search_parameters.time_limit.seconds = int(
  1352. float(self.app.defaults["excellon_search_time"]))
  1353. else:
  1354. search_parameters.time_limit.seconds = 3
  1355. # Callback to the distance function. The callback takes two
  1356. # arguments (the from and to node indices) and returns the distance between them.
  1357. dist_between_locations = self.CreateDistanceCallback(locs=locations, manager=manager)
  1358. # if there are no distances then go to the next tool
  1359. if not dist_between_locations:
  1360. return
  1361. dist_callback = dist_between_locations.Distance
  1362. transit_callback_index = routing.RegisterTransitCallback(dist_callback)
  1363. routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index)
  1364. # Solve, returns a solution if any.
  1365. assignment = routing.SolveWithParameters(search_parameters)
  1366. if assignment:
  1367. # Solution cost.
  1368. log.info("OR-tools metaheuristics - Total distance: " + str(assignment.ObjectiveValue()))
  1369. # Inspect solution.
  1370. # Only one route here; otherwise iterate from 0 to routing.vehicles() - 1.
  1371. route_number = 0
  1372. node = routing.Start(route_number)
  1373. start_node = node
  1374. while not routing.IsEnd(node):
  1375. if self.app.abort_flag:
  1376. # graceful abort requested by the user
  1377. raise grace
  1378. optimized_path.append(node)
  1379. node = assignment.Value(routing.NextVar(node))
  1380. else:
  1381. log.warning('OR-tools metaheuristics - No solution found.')
  1382. else:
  1383. log.warning('OR-tools metaheuristics - Specify an instance greater than 0.')
  1384. return optimized_path
  1385. # ############################################# ##
  1386. def optimized_ortools_basic(self, locations, start=None):
  1387. optimized_path = []
  1388. tsp_size = len(locations)
  1389. num_routes = 1 # The number of routes, which is 1 in the TSP.
  1390. # Nodes are indexed from 0 to tsp_size - 1. The depot is the starting node of the route.
  1391. depot = 0 if start is None else start
  1392. # Create routing model.
  1393. if tsp_size > 0:
  1394. manager = pywrapcp.RoutingIndexManager(tsp_size, num_routes, depot)
  1395. routing = pywrapcp.RoutingModel(manager)
  1396. search_parameters = pywrapcp.DefaultRoutingSearchParameters()
  1397. # Callback to the distance function. The callback takes two
  1398. # arguments (the from and to node indices) and returns the distance between them.
  1399. dist_between_locations = self.CreateDistanceCallback(locs=locations, manager=manager)
  1400. # if there are no distances then go to the next tool
  1401. if not dist_between_locations:
  1402. return
  1403. dist_callback = dist_between_locations.Distance
  1404. transit_callback_index = routing.RegisterTransitCallback(dist_callback)
  1405. routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index)
  1406. # Solve, returns a solution if any.
  1407. assignment = routing.SolveWithParameters(search_parameters)
  1408. if assignment:
  1409. # Solution cost.
  1410. log.info("Total distance: " + str(assignment.ObjectiveValue()))
  1411. # Inspect solution.
  1412. # Only one route here; otherwise iterate from 0 to routing.vehicles() - 1.
  1413. route_number = 0
  1414. node = routing.Start(route_number)
  1415. start_node = node
  1416. while not routing.IsEnd(node):
  1417. optimized_path.append(node)
  1418. node = assignment.Value(routing.NextVar(node))
  1419. else:
  1420. log.warning('No solution found.')
  1421. else:
  1422. log.warning('Specify an instance greater than 0.')
  1423. return optimized_path
  1424. # ############################################# ##
  1425. @staticmethod
  1426. def optimized_travelling_salesman(points, start=None):
  1427. """
  1428. As solving the problem in the brute force way is too slow,
  1429. this function implements a simple heuristic: always
  1430. go to the nearest city.
  1431. Even if this algorithm is extremely simple, it works pretty well
  1432. giving a solution only about 25%% longer than the optimal one (cit. Wikipedia),
  1433. and runs very fast in O(N^2) time complexity.
  1434. >>> optimized_travelling_salesman([[i,j] for i in range(5) for j in range(5)])
  1435. [[0, 0], [0, 1], [0, 2], [0, 3], [0, 4], [1, 4], [1, 3], [1, 2], [1, 1], [1, 0], [2, 0], [2, 1], [2, 2],
  1436. [2, 3], [2, 4], [3, 4], [3, 3], [3, 2], [3, 1], [3, 0], [4, 0], [4, 1], [4, 2], [4, 3], [4, 4]]
  1437. >>> optimized_travelling_salesman([[0,0],[10,0],[6,0]])
  1438. [[0, 0], [6, 0], [10, 0]]
  1439. :param points: List of tuples with x, y coordinates
  1440. :type points: list
  1441. :param start: a tuple with a x,y coordinates of the start point
  1442. :type start: tuple
  1443. :return: List of points ordered in a optimized way
  1444. :rtype: list
  1445. """
  1446. if start is None:
  1447. start = points[0]
  1448. must_visit = points
  1449. path = [start]
  1450. # must_visit.remove(start)
  1451. while must_visit:
  1452. nearest = min(must_visit, key=lambda x: distance(path[-1], x))
  1453. path.append(nearest)
  1454. must_visit.remove(nearest)
  1455. return path
  1456. def check_zcut(self, zcut):
  1457. if zcut > 0:
  1458. self.app.inform.emit('[WARNING] %s' %
  1459. _("The Cut Z parameter has positive value. "
  1460. "It is the depth value to drill into material.\n"
  1461. "The Cut Z parameter needs to have a negative value, assuming it is a typo "
  1462. "therefore the app will convert the value to negative. "
  1463. "Check the resulting CNC code (Gcode etc)."))
  1464. return -zcut
  1465. elif zcut == 0:
  1466. self.app.inform.emit('[WARNING] %s.' % _("The Cut Z parameter is zero. There will be no cut, aborting"))
  1467. return 'fail'
  1468. def generate_from_excellon_by_tool(self, sel_tools, tools, order='fwd'):
  1469. """
  1470. Creates Gcode for this object from an Excellon object
  1471. for the specified tools.
  1472. :return: Tool GCode
  1473. :rtype: str
  1474. """
  1475. log.debug("Creating CNC Job from Excellon...")
  1476. settings = QtCore.QSettings("Open Source", "FlatCAM")
  1477. if settings.contains("machinist"):
  1478. machinist_setting = settings.value('machinist', type=int)
  1479. else:
  1480. machinist_setting = 0
  1481. # #############################################################################################################
  1482. # #############################################################################################################
  1483. # TOOLS
  1484. # sort the tools list by the second item in tuple (here we have a dict with diameter of the tool)
  1485. # so we actually are sorting the tools by diameter
  1486. # #############################################################################################################
  1487. # #############################################################################################################
  1488. all_tools = []
  1489. for tool_as_key, v in list(tools.items()):
  1490. all_tools.append((int(tool_as_key), float(v['tooldia'])))
  1491. if order == 'fwd':
  1492. sorted_tools = sorted(all_tools, key=lambda t1: t1[1])
  1493. elif order == 'rev':
  1494. sorted_tools = sorted(all_tools, key=lambda t1: t1[1], reverse=True)
  1495. else:
  1496. sorted_tools = all_tools
  1497. if sel_tools == "all":
  1498. selected_tools = [i[0] for i in all_tools] # we get a array of ordered sel_tools
  1499. else:
  1500. selected_tools = eval(sel_tools)
  1501. # Create a sorted list of selected sel_tools from the sorted_tools list
  1502. sel_tools = [i for i, j in sorted_tools for k in selected_tools if i == k]
  1503. log.debug("Tools sorted are: %s" % str(sel_tools))
  1504. # #############################################################################################################
  1505. # #############################################################################################################
  1506. # #############################################################################################################
  1507. # #############################################################################################################
  1508. # build a self.options['Tools_in_use'] list from scratch if we don't have one like in the case of
  1509. # running this method from a Tcl Command
  1510. # #############################################################################################################
  1511. # #############################################################################################################
  1512. build_tools_in_use_list = False
  1513. if 'Tools_in_use' not in self.excellon_obj.options:
  1514. self.excellon_obj.options['Tools_in_use'] = []
  1515. # if the list is empty (either we just added the key or it was already there but empty) signal to build it
  1516. if not self.excellon_obj.options['Tools_in_use']:
  1517. build_tools_in_use_list = True
  1518. # #############################################################################################################
  1519. # #############################################################################################################
  1520. # fill the data into the self.exc_cnc_tools dictionary
  1521. # #############################################################################################################
  1522. # #############################################################################################################
  1523. for it in all_tools:
  1524. for to_ol in sel_tools:
  1525. if to_ol == it[0]:
  1526. sol_geo = []
  1527. drill_no = 0
  1528. if 'drills' in tools[to_ol]:
  1529. drill_no = len(tools[to_ol]['drills'])
  1530. for drill in tools[to_ol]['drills']:
  1531. sol_geo.append(drill.buffer((it[1] / 2.0), resolution=self.geo_steps_per_circle))
  1532. slot_no = 0
  1533. if 'slots' in tools[to_ol]:
  1534. slot_no = len(tools[to_ol]['slots'])
  1535. for slot in tools[to_ol]['slots']:
  1536. start = (slot[0].x, slot[0].y)
  1537. stop = (slot[1].x, slot[1].y)
  1538. sol_geo.append(
  1539. LineString([start, stop]).buffer((it[1] / 2.0), resolution=self.geo_steps_per_circle)
  1540. )
  1541. if self.use_ui:
  1542. try:
  1543. z_off = float(tools[it[0]]['data']['offset']) * (-1)
  1544. except KeyError:
  1545. z_off = 0
  1546. else:
  1547. z_off = 0
  1548. default_data = {}
  1549. for k, v in list(self.options.items()):
  1550. default_data[k] = deepcopy(v)
  1551. self.exc_cnc_tools[it[1]] = {}
  1552. self.exc_cnc_tools[it[1]]['tool'] = it[0]
  1553. self.exc_cnc_tools[it[1]]['nr_drills'] = drill_no
  1554. self.exc_cnc_tools[it[1]]['nr_slots'] = slot_no
  1555. self.exc_cnc_tools[it[1]]['offset_z'] = z_off
  1556. self.exc_cnc_tools[it[1]]['data'] = default_data
  1557. self.exc_cnc_tools[it[1]]['solid_geometry'] = deepcopy(sol_geo)
  1558. # build a self.options['Tools_in_use'] list from scratch if we don't have one like in the case of
  1559. # running this method from a Tcl Command
  1560. if build_tools_in_use_list is True:
  1561. self.options['Tools_in_use'].append(
  1562. [it[0], it[1], drill_no, slot_no]
  1563. )
  1564. # #############################################################################################################
  1565. # #############################################################################################################
  1566. # Points (Group by tool): a dictionary of shapely Point geo elements grouped by tool number
  1567. # #############################################################################################################
  1568. # #############################################################################################################
  1569. self.app.inform.emit(_("Creating a list of points to drill..."))
  1570. points = {}
  1571. for tool, tl_dict in tools.items():
  1572. if tool in sel_tools:
  1573. if self.app.abort_flag:
  1574. # graceful abort requested by the user
  1575. raise grace
  1576. if 'drills' in tl_dict and tl_dict['drills']:
  1577. for drill_pt in tl_dict['drills']:
  1578. try:
  1579. points[tool].append(drill_pt)
  1580. except KeyError:
  1581. points[tool] = [drill_pt]
  1582. log.debug("Found %d TOOLS with drills." % len(points))
  1583. # check if there are drill points in the exclusion areas.
  1584. # If we find any within the exclusion areas return 'fail'
  1585. for tool in points:
  1586. for pt in points[tool]:
  1587. for area in self.app.exc_areas.exclusion_areas_storage:
  1588. pt_buf = pt.buffer(self.exc_tools[tool]['tooldia'] / 2.0)
  1589. if pt_buf.within(area['shape']) or pt_buf.intersects(area['shape']):
  1590. self.app.inform.emit("[ERROR_NOTCL] %s" % _("Failed. Drill points inside the exclusion zones."))
  1591. return 'fail'
  1592. # #############################################################################################################
  1593. # General Parameters
  1594. # #############################################################################################################
  1595. used_excellon_optimization_type = self.app.defaults["excellon_optimization_type"]
  1596. self.f_plunge = self.app.defaults["excellon_f_plunge"]
  1597. self.f_retract = self.app.defaults["excellon_f_retract"]
  1598. # Prepprocessor
  1599. self.pp_excellon_name = self.default_data["excellon_ppname_e"]
  1600. self.pp_excellon = self.app.preprocessors[self.pp_excellon_name]
  1601. p = self.pp_excellon
  1602. # this holds the resulting GCode
  1603. gcode = ''
  1604. # #############################################################################################################
  1605. # #############################################################################################################
  1606. # Initialization
  1607. # #############################################################################################################
  1608. # #############################################################################################################
  1609. gcode += self.doformat(p.start_code)
  1610. if self.toolchange is False:
  1611. if self.xy_toolchange is not None:
  1612. gcode += self.doformat(p.lift_code, x=self.xy_toolchange[0], y=self.xy_toolchange[1])
  1613. gcode += self.doformat(p.startz_code, x=self.xy_toolchange[0], y=self.xy_toolchange[1])
  1614. else:
  1615. gcode += self.doformat(p.lift_code, x=0.0, y=0.0)
  1616. gcode += self.doformat(p.startz_code, x=0.0, y=0.0)
  1617. if self.xy_toolchange is not None:
  1618. self.oldx = self.xy_toolchange[0]
  1619. self.oldy = self.xy_toolchange[1]
  1620. else:
  1621. self.oldx = 0.0
  1622. self.oldy = 0.0
  1623. measured_distance = 0.0
  1624. measured_down_distance = 0.0
  1625. measured_up_to_zero_distance = 0.0
  1626. measured_lift_distance = 0.0
  1627. # #############################################################################################################
  1628. # #############################################################################################################
  1629. # GCODE creation
  1630. # #############################################################################################################
  1631. # #############################################################################################################
  1632. self.app.inform.emit('%s...' % _("Starting G-Code"))
  1633. has_drills = None
  1634. for tool, tool_dict in self.exc_tools.items():
  1635. if 'drills' in tool_dict and tool_dict['drills']:
  1636. has_drills = True
  1637. break
  1638. if not has_drills:
  1639. log.debug("camlib.CNCJob.generate_from_excellon_by_tool() --> "
  1640. "The loaded Excellon file has no drills ...")
  1641. self.app.inform.emit('[ERROR_NOTCL] %s...' % _('The loaded Excellon file has no drills'))
  1642. return 'fail'
  1643. current_platform = platform.architecture()[0]
  1644. if current_platform != '64bit':
  1645. used_excellon_optimization_type = 'T'
  1646. # #############################################################################################################
  1647. # #############################################################################################################
  1648. # ################################## DRILLING !!! #########################################################
  1649. # #############################################################################################################
  1650. # #############################################################################################################
  1651. if used_excellon_optimization_type == 'M':
  1652. log.debug("Using OR-Tools Metaheuristic Guided Local Search drill path optimization.")
  1653. elif used_excellon_optimization_type == 'B':
  1654. log.debug("Using OR-Tools Basic drill path optimization.")
  1655. elif used_excellon_optimization_type == 'T':
  1656. log.debug("Using Travelling Salesman drill path optimization.")
  1657. else:
  1658. log.debug("Using no path optimization.")
  1659. if self.toolchange is True:
  1660. for tool in sel_tools:
  1661. tool_dict = tools[tool]['data']
  1662. # check if it has drills
  1663. if not tools[tool]['drills']:
  1664. continue
  1665. if self.app.abort_flag:
  1666. # graceful abort requested by the user
  1667. raise grace
  1668. self.tool = tool
  1669. self.tooldia = tools[tool]["tooldia"]
  1670. self.postdata['toolC'] = self.tooldia
  1671. self.z_feedrate = tool_dict['feedrate_z']
  1672. self.feedrate = tool_dict['feedrate']
  1673. self.z_cut = tool_dict['cutz']
  1674. gcode += self.doformat(p.z_feedrate_code)
  1675. # Z_cut parameter
  1676. if machinist_setting == 0:
  1677. self.z_cut = self.check_zcut(zcut=tool_dict["excellon_cutz"])
  1678. if self.z_cut == 'fail':
  1679. return 'fail'
  1680. # multidepth use this
  1681. old_zcut = tool_dict["excellon_cutz"]
  1682. self.z_move = tool_dict['travelz']
  1683. self.spindlespeed = tool_dict['spindlespeed']
  1684. self.dwell = tool_dict['dwell']
  1685. self.dwelltime = tool_dict['dwelltime']
  1686. self.multidepth = tool_dict['multidepth']
  1687. self.z_depthpercut = tool_dict['depthperpass']
  1688. # XY_toolchange parameter
  1689. self.xy_toolchange = tool_dict["excellon_toolchangexy"]
  1690. try:
  1691. if self.xy_toolchange == '':
  1692. self.xy_toolchange = None
  1693. else:
  1694. self.xy_toolchange = re.sub('[()\[\]]', '',
  1695. str(self.xy_toolchange)) if self.xy_toolchange else None
  1696. if self.xy_toolchange:
  1697. self.xy_toolchange = [float(eval(a)) for a in self.xy_toolchange.split(",")]
  1698. if self.xy_toolchange and len(self.xy_toolchange) != 2:
  1699. self.app.inform.emit('[ERROR]%s' %
  1700. _("The Toolchange X,Y field in Edit -> Preferences has to be "
  1701. "in the format (x, y) \nbut now there is only one value, not two. "))
  1702. return 'fail'
  1703. except Exception as e:
  1704. log.debug("camlib.CNCJob.generate_from_excellon_by_tool() --> %s" % str(e))
  1705. pass
  1706. # XY_end parameter
  1707. self.xy_end = tool_dict["excellon_endxy"]
  1708. self.xy_end = re.sub('[()\[\]]', '', str(self.xy_end)) if self.xy_end else None
  1709. if self.xy_end and self.xy_end != '':
  1710. self.xy_end = [float(eval(a)) for a in self.xy_end.split(",")]
  1711. if self.xy_end and len(self.xy_end) < 2:
  1712. self.app.inform.emit(
  1713. '[ERROR] %s' % _("The End Move X,Y field in Edit -> Preferences has to be "
  1714. "in the format (x, y) but now there is only one value, not two."))
  1715. return 'fail'
  1716. # #########################################################################################################
  1717. # ############ Create the data. #################
  1718. # #########################################################################################################
  1719. locations = []
  1720. altPoints = []
  1721. optimized_path = []
  1722. if used_excellon_optimization_type == 'M':
  1723. if tool in points:
  1724. locations = self.create_tool_data_array(points=points[tool])
  1725. # if there are no locations then go to the next tool
  1726. if not locations:
  1727. continue
  1728. optimized_path = self.optimized_ortools_meta(locations=locations)
  1729. elif used_excellon_optimization_type == 'B':
  1730. if tool in points:
  1731. locations = self.create_tool_data_array(points=points[tool])
  1732. # if there are no locations then go to the next tool
  1733. if not locations:
  1734. continue
  1735. optimized_path = self.optimized_ortools_basic(locations=locations)
  1736. elif used_excellon_optimization_type == 'T':
  1737. for point in points[tool]:
  1738. altPoints.append((point.coords.xy[0][0], point.coords.xy[1][0]))
  1739. optimized_path = self.optimized_travelling_salesman(altPoints)
  1740. else:
  1741. # it's actually not optimized path but here we build a list of (x,y) coordinates
  1742. # out of the tool's drills
  1743. for drill in self.exc_tools[tool]['drills']:
  1744. unoptimized_coords = (
  1745. drill.x,
  1746. drill.y
  1747. )
  1748. optimized_path.append(unoptimized_coords)
  1749. # #########################################################################################################
  1750. # #########################################################################################################
  1751. # Only if there are locations to drill
  1752. if not optimized_path:
  1753. continue
  1754. if self.app.abort_flag:
  1755. # graceful abort requested by the user
  1756. raise grace
  1757. # Tool change sequence (optional)
  1758. if self.toolchange:
  1759. gcode += self.doformat(p.toolchange_code, toolchangexy=(self.oldx, self.oldy))
  1760. # Spindle start
  1761. gcode += self.doformat(p.spindle_code)
  1762. # Dwell time
  1763. if self.dwell is True:
  1764. gcode += self.doformat(p.dwell_code)
  1765. current_tooldia = float('%.*f' % (self.decimals, float(self.exc_tools[tool]["tooldia"])))
  1766. self.app.inform.emit(
  1767. '%s: %s%s.' % (_("Starting G-Code for tool with diameter"),
  1768. str(current_tooldia),
  1769. str(self.units))
  1770. )
  1771. # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  1772. # APPLY Offset only when using the appGUI, for TclCommand this will create an error
  1773. # because the values for Z offset are created in build_tool_ui()
  1774. # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  1775. try:
  1776. z_offset = float(tool_dict['offset']) * (-1)
  1777. except KeyError:
  1778. z_offset = 0
  1779. self.z_cut = z_offset + old_zcut
  1780. self.coordinates_type = self.app.defaults["cncjob_coords_type"]
  1781. if self.coordinates_type == "G90":
  1782. # Drillling! for Absolute coordinates type G90
  1783. # variables to display the percentage of work done
  1784. geo_len = len(optimized_path)
  1785. old_disp_number = 0
  1786. log.warning("Number of drills for which to generate GCode: %s" % str(geo_len))
  1787. loc_nr = 0
  1788. for point in optimized_path:
  1789. if self.app.abort_flag:
  1790. # graceful abort requested by the user
  1791. raise grace
  1792. if used_excellon_optimization_type == 'T':
  1793. locx = point[0]
  1794. locy = point[1]
  1795. else:
  1796. locx = locations[point][0]
  1797. locy = locations[point][1]
  1798. travels = self.app.exc_areas.travel_coordinates(start_point=(self.oldx, self.oldy),
  1799. end_point=(locx, locy),
  1800. tooldia=current_tooldia)
  1801. prev_z = None
  1802. for travel in travels:
  1803. locx = travel[1][0]
  1804. locy = travel[1][1]
  1805. if travel[0] is not None:
  1806. # move to next point
  1807. gcode += self.doformat(p.rapid_code, x=locx, y=locy)
  1808. # raise to safe Z (travel[0]) each time because safe Z may be different
  1809. self.z_move = travel[0]
  1810. gcode += self.doformat(p.lift_code, x=locx, y=locy)
  1811. # restore z_move
  1812. self.z_move = tool_dict['travelz']
  1813. else:
  1814. if prev_z is not None:
  1815. # move to next point
  1816. gcode += self.doformat(p.rapid_code, x=locx, y=locy)
  1817. # we assume that previously the z_move was altered therefore raise to
  1818. # the travel_z (z_move)
  1819. self.z_move = tool_dict['travelz']
  1820. gcode += self.doformat(p.lift_code, x=locx, y=locy)
  1821. else:
  1822. # move to next point
  1823. gcode += self.doformat(p.rapid_code, x=locx, y=locy)
  1824. # store prev_z
  1825. prev_z = travel[0]
  1826. # gcode += self.doformat(p.rapid_code, x=locx, y=locy)
  1827. if self.multidepth and abs(self.z_cut) > abs(self.z_depthpercut):
  1828. doc = deepcopy(self.z_cut)
  1829. self.z_cut = 0.0
  1830. while abs(self.z_cut) < abs(doc):
  1831. self.z_cut -= self.z_depthpercut
  1832. if abs(doc) < abs(self.z_cut) < (abs(doc) + self.z_depthpercut):
  1833. self.z_cut = doc
  1834. gcode += self.doformat(p.down_code, x=locx, y=locy)
  1835. measured_down_distance += abs(self.z_cut) + abs(self.z_move)
  1836. if self.f_retract is False:
  1837. gcode += self.doformat(p.up_to_zero_code, x=locx, y=locy)
  1838. measured_up_to_zero_distance += abs(self.z_cut)
  1839. measured_lift_distance += abs(self.z_move)
  1840. else:
  1841. measured_lift_distance += abs(self.z_cut) + abs(self.z_move)
  1842. gcode += self.doformat(p.lift_code, x=locx, y=locy)
  1843. else:
  1844. gcode += self.doformat(p.down_code, x=locx, y=locy)
  1845. measured_down_distance += abs(self.z_cut) + abs(self.z_move)
  1846. if self.f_retract is False:
  1847. gcode += self.doformat(p.up_to_zero_code, x=locx, y=locy)
  1848. measured_up_to_zero_distance += abs(self.z_cut)
  1849. measured_lift_distance += abs(self.z_move)
  1850. else:
  1851. measured_lift_distance += abs(self.z_cut) + abs(self.z_move)
  1852. gcode += self.doformat(p.lift_code, x=locx, y=locy)
  1853. measured_distance += abs(distance_euclidian(locx, locy, self.oldx, self.oldy))
  1854. self.oldx = locx
  1855. self.oldy = locy
  1856. loc_nr += 1
  1857. disp_number = int(np.interp(loc_nr, [0, geo_len], [0, 100]))
  1858. if old_disp_number < disp_number <= 100:
  1859. self.app.proc_container.update_view_text(' %d%%' % disp_number)
  1860. old_disp_number = disp_number
  1861. else:
  1862. self.app.inform.emit('[ERROR_NOTCL] %s...' % _('G91 coordinates not implemented'))
  1863. return 'fail'
  1864. self.z_cut = deepcopy(old_zcut)
  1865. else:
  1866. # We are not using Toolchange therefore we need to decide which tool properties to use
  1867. one_tool = 1
  1868. all_points = []
  1869. for tool in points:
  1870. # check if it has drills
  1871. if not points[tool]:
  1872. continue
  1873. all_points += points[tool]
  1874. if self.app.abort_flag:
  1875. # graceful abort requested by the user
  1876. raise grace
  1877. self.tool = one_tool
  1878. self.tooldia = self.exc_tools[one_tool]["tooldia"]
  1879. self.postdata['toolC'] = self.tooldia
  1880. if self.use_ui:
  1881. self.z_feedrate = self.exc_tools[one_tool]['data']['feedrate_z']
  1882. self.feedrate = self.exc_tools[one_tool]['data']['feedrate']
  1883. self.z_cut = self.exc_tools[one_tool]['data']['cutz']
  1884. gcode += self.doformat(p.z_feedrate_code)
  1885. if self.machinist_setting == 0:
  1886. if self.z_cut > 0:
  1887. self.app.inform.emit('[WARNING] %s' %
  1888. _("The Cut Z parameter has positive value. "
  1889. "It is the depth value to drill into material.\n"
  1890. "The Cut Z parameter needs to have a negative value, "
  1891. "assuming it is a typo "
  1892. "therefore the app will convert the value to negative. "
  1893. "Check the resulting CNC code (Gcode etc)."))
  1894. self.z_cut = -self.z_cut
  1895. elif self.z_cut == 0:
  1896. self.app.inform.emit('[WARNING] %s.' %
  1897. _("The Cut Z parameter is zero. There will be no cut, skipping file"))
  1898. return 'fail'
  1899. old_zcut = deepcopy(self.z_cut)
  1900. self.z_move = self.exc_tools[one_tool]['data']['travelz']
  1901. self.spindlespeed = self.exc_tools[one_tool]['data']['spindlespeed']
  1902. self.dwell = self.exc_tools[one_tool]['data']['dwell']
  1903. self.dwelltime = self.exc_tools[one_tool]['data']['dwelltime']
  1904. self.multidepth = self.exc_tools[one_tool]['data']['multidepth']
  1905. self.z_depthpercut = self.exc_tools[one_tool]['data']['depthperpass']
  1906. else:
  1907. old_zcut = deepcopy(self.z_cut)
  1908. # #########################################################################################################
  1909. # ############ Create the data. #################
  1910. # #########################################################################################################
  1911. locations = []
  1912. altPoints = []
  1913. optimized_path = []
  1914. if used_excellon_optimization_type == 'M':
  1915. if all_points:
  1916. locations = self.create_tool_data_array(points=all_points)
  1917. # if there are no locations then go to the next tool
  1918. if not locations:
  1919. return 'fail'
  1920. optimized_path = self.optimized_ortools_meta(locations=locations)
  1921. elif used_excellon_optimization_type == 'B':
  1922. if all_points:
  1923. locations = self.create_tool_data_array(points=all_points)
  1924. # if there are no locations then go to the next tool
  1925. if not locations:
  1926. return 'fail'
  1927. optimized_path = self.optimized_ortools_basic(locations=locations)
  1928. elif used_excellon_optimization_type == 'T':
  1929. for point in all_points:
  1930. altPoints.append((point.coords.xy[0][0], point.coords.xy[1][0]))
  1931. optimized_path = self.optimized_travelling_salesman(altPoints)
  1932. else:
  1933. # it's actually not optimized path but here we build a list of (x,y) coordinates
  1934. # out of the tool's drills
  1935. for pt in all_points:
  1936. unoptimized_coords = (
  1937. pt.x,
  1938. pt.y
  1939. )
  1940. optimized_path.append(unoptimized_coords)
  1941. # #########################################################################################################
  1942. # #########################################################################################################
  1943. # Only if there are locations to drill
  1944. if not optimized_path:
  1945. return 'fail'
  1946. if self.app.abort_flag:
  1947. # graceful abort requested by the user
  1948. raise grace
  1949. # Spindle start
  1950. gcode += self.doformat(p.spindle_code)
  1951. # Dwell time
  1952. if self.dwell is True:
  1953. gcode += self.doformat(p.dwell_code)
  1954. current_tooldia = float('%.*f' % (self.decimals, float(self.exc_tools[one_tool]["tooldia"])))
  1955. self.app.inform.emit(
  1956. '%s: %s%s.' % (_("Starting G-Code for tool with diameter"),
  1957. str(current_tooldia),
  1958. str(self.units))
  1959. )
  1960. # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  1961. # APPLY Offset only when using the appGUI, for TclCommand this will create an error
  1962. # because the values for Z offset are created in build_tool_ui()
  1963. # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  1964. try:
  1965. z_offset = float(self.exc_tools[one_tool]['data']['offset']) * (-1)
  1966. except KeyError:
  1967. z_offset = 0
  1968. self.z_cut = z_offset + old_zcut
  1969. self.coordinates_type = self.app.defaults["cncjob_coords_type"]
  1970. if self.coordinates_type == "G90":
  1971. # Drillling! for Absolute coordinates type G90
  1972. # variables to display the percentage of work done
  1973. geo_len = len(optimized_path)
  1974. old_disp_number = 0
  1975. log.warning("Number of drills for which to generate GCode: %s" % str(geo_len))
  1976. loc_nr = 0
  1977. for point in optimized_path:
  1978. if self.app.abort_flag:
  1979. # graceful abort requested by the user
  1980. raise grace
  1981. if used_excellon_optimization_type == 'T':
  1982. locx = point[0]
  1983. locy = point[1]
  1984. else:
  1985. locx = locations[point][0]
  1986. locy = locations[point][1]
  1987. travels = self.app.exc_areas.travel_coordinates(start_point=(self.oldx, self.oldy),
  1988. end_point=(locx, locy),
  1989. tooldia=current_tooldia)
  1990. prev_z = None
  1991. for travel in travels:
  1992. locx = travel[1][0]
  1993. locy = travel[1][1]
  1994. if travel[0] is not None:
  1995. # move to next point
  1996. gcode += self.doformat(p.rapid_code, x=locx, y=locy)
  1997. # raise to safe Z (travel[0]) each time because safe Z may be different
  1998. self.z_move = travel[0]
  1999. gcode += self.doformat(p.lift_code, x=locx, y=locy)
  2000. # restore z_move
  2001. self.z_move = self.exc_tools[one_tool]['data']['travelz']
  2002. else:
  2003. if prev_z is not None:
  2004. # move to next point
  2005. gcode += self.doformat(p.rapid_code, x=locx, y=locy)
  2006. # we assume that previously the z_move was altered therefore raise to
  2007. # the travel_z (z_move)
  2008. self.z_move = self.exc_tools[one_tool]['data']['travelz']
  2009. gcode += self.doformat(p.lift_code, x=locx, y=locy)
  2010. else:
  2011. # move to next point
  2012. gcode += self.doformat(p.rapid_code, x=locx, y=locy)
  2013. # store prev_z
  2014. prev_z = travel[0]
  2015. # gcode += self.doformat(p.rapid_code, x=locx, y=locy)
  2016. if self.multidepth and abs(self.z_cut) > abs(self.z_depthpercut):
  2017. doc = deepcopy(self.z_cut)
  2018. self.z_cut = 0.0
  2019. while abs(self.z_cut) < abs(doc):
  2020. self.z_cut -= self.z_depthpercut
  2021. if abs(doc) < abs(self.z_cut) < (abs(doc) + self.z_depthpercut):
  2022. self.z_cut = doc
  2023. gcode += self.doformat(p.down_code, x=locx, y=locy)
  2024. measured_down_distance += abs(self.z_cut) + abs(self.z_move)
  2025. if self.f_retract is False:
  2026. gcode += self.doformat(p.up_to_zero_code, x=locx, y=locy)
  2027. measured_up_to_zero_distance += abs(self.z_cut)
  2028. measured_lift_distance += abs(self.z_move)
  2029. else:
  2030. measured_lift_distance += abs(self.z_cut) + abs(self.z_move)
  2031. gcode += self.doformat(p.lift_code, x=locx, y=locy)
  2032. else:
  2033. gcode += self.doformat(p.down_code, x=locx, y=locy)
  2034. measured_down_distance += abs(self.z_cut) + abs(self.z_move)
  2035. if self.f_retract is False:
  2036. gcode += self.doformat(p.up_to_zero_code, x=locx, y=locy)
  2037. measured_up_to_zero_distance += abs(self.z_cut)
  2038. measured_lift_distance += abs(self.z_move)
  2039. else:
  2040. measured_lift_distance += abs(self.z_cut) + abs(self.z_move)
  2041. gcode += self.doformat(p.lift_code, x=locx, y=locy)
  2042. measured_distance += abs(distance_euclidian(locx, locy, self.oldx, self.oldy))
  2043. self.oldx = locx
  2044. self.oldy = locy
  2045. loc_nr += 1
  2046. disp_number = int(np.interp(loc_nr, [0, geo_len], [0, 100]))
  2047. if old_disp_number < disp_number <= 100:
  2048. self.app.proc_container.update_view_text(' %d%%' % disp_number)
  2049. old_disp_number = disp_number
  2050. else:
  2051. self.app.inform.emit('[ERROR_NOTCL] %s...' % _('G91 coordinates not implemented'))
  2052. return 'fail'
  2053. self.z_cut = deepcopy(old_zcut)
  2054. if used_excellon_optimization_type == 'M':
  2055. log.debug("The total travel distance with OR-TOOLS Metaheuristics is: %s" % str(measured_distance))
  2056. elif used_excellon_optimization_type == 'B':
  2057. log.debug("The total travel distance with OR-TOOLS Basic Algorithm is: %s" % str(measured_distance))
  2058. elif used_excellon_optimization_type == 'T':
  2059. log.debug("The total travel distance with Travelling Salesman Algorithm is: %s" % str(measured_distance))
  2060. else:
  2061. log.debug("The total travel distance with with no optimization is: %s" % str(measured_distance))
  2062. gcode += self.doformat(p.spindle_stop_code)
  2063. # Move to End position
  2064. gcode += self.doformat(p.end_code, x=0, y=0)
  2065. # #############################################################################################################
  2066. # ############################# Calculate DISTANCE and ESTIMATED TIME #########################################
  2067. # #############################################################################################################
  2068. measured_distance += abs(distance_euclidian(self.oldx, self.oldy, 0, 0))
  2069. log.debug("The total travel distance including travel to end position is: %s" %
  2070. str(measured_distance) + '\n')
  2071. self.travel_distance = measured_distance
  2072. # I use the value of self.feedrate_rapid for the feadrate in case of the measure_lift_distance and for
  2073. # traveled_time because it is not always possible to determine the feedrate that the CNC machine uses
  2074. # for G0 move (the fastest speed available to the CNC router). Although self.feedrate_rapids is used only with
  2075. # Marlin preprocessor and derivatives.
  2076. self.routing_time = (measured_down_distance + measured_up_to_zero_distance) / self.feedrate
  2077. lift_time = measured_lift_distance / self.feedrate_rapid
  2078. traveled_time = measured_distance / self.feedrate_rapid
  2079. self.routing_time += lift_time + traveled_time
  2080. self.app.inform.emit(_("Finished G-Code generation..."))
  2081. return gcode
  2082. def reset_fields(self):
  2083. self.object_combo.setRootModelIndex(self.app.collection.index(0, 0, QtCore.QModelIndex()))
  2084. def doformat(self, fun, **kwargs):
  2085. return self.doformat2(fun, **kwargs) + "\n"
  2086. def doformat2(self, fun, **kwargs):
  2087. """
  2088. This method will call one of the current preprocessor methods having as parameters all the attributes of
  2089. current class to which will add the kwargs parameters
  2090. :param fun: One of the methods inside the preprocessor classes which get loaded here in the 'p' object
  2091. :type fun: class 'function'
  2092. :param kwargs: keyword args which will update attributes of the current class
  2093. :type kwargs: dict
  2094. :return: Gcode line
  2095. :rtype: str
  2096. """
  2097. attributes = AttrDict()
  2098. attributes.update(self.postdata)
  2099. attributes.update(kwargs)
  2100. try:
  2101. returnvalue = fun(attributes)
  2102. return returnvalue
  2103. except Exception:
  2104. self.app.log.error('Exception occurred within a preprocessor: ' + traceback.format_exc())
  2105. return ''
  2106. @property
  2107. def postdata(self):
  2108. """
  2109. This will return all the attributes of the class in the form of a dictionary
  2110. :return: Class attributes
  2111. :rtype: dict
  2112. """
  2113. return self.__dict__
  2114. class DrillingUI:
  2115. toolName = _("Drilling Tool")
  2116. def __init__(self, layout, app):
  2117. self.app = app
  2118. self.decimals = self.app.decimals
  2119. self.layout = layout
  2120. self.tools_frame = QtWidgets.QFrame()
  2121. self.tools_frame.setContentsMargins(0, 0, 0, 0)
  2122. self.layout.addWidget(self.tools_frame)
  2123. self.tools_box = QtWidgets.QVBoxLayout()
  2124. self.tools_box.setContentsMargins(0, 0, 0, 0)
  2125. self.tools_frame.setLayout(self.tools_box)
  2126. self.title_box = QtWidgets.QHBoxLayout()
  2127. self.tools_box.addLayout(self.title_box)
  2128. # ## Title
  2129. title_label = QtWidgets.QLabel("%s" % self.toolName)
  2130. title_label.setStyleSheet("""
  2131. QLabel
  2132. {
  2133. font-size: 16px;
  2134. font-weight: bold;
  2135. }
  2136. """)
  2137. title_label.setToolTip(
  2138. _("Create CNCJob with toolpaths for drilling or milling holes.")
  2139. )
  2140. self.title_box.addWidget(title_label)
  2141. # App Level label
  2142. self.level = QtWidgets.QLabel("")
  2143. self.level.setToolTip(
  2144. _(
  2145. "BASIC is suitable for a beginner. Many parameters\n"
  2146. "are hidden from the user in this mode.\n"
  2147. "ADVANCED mode will make available all parameters.\n\n"
  2148. "To change the application LEVEL, go to:\n"
  2149. "Edit -> Preferences -> General and check:\n"
  2150. "'APP. LEVEL' radio button."
  2151. )
  2152. )
  2153. self.level.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
  2154. self.title_box.addWidget(self.level)
  2155. # Grid Layout
  2156. grid0 = QtWidgets.QGridLayout()
  2157. grid0.setColumnStretch(0, 0)
  2158. grid0.setColumnStretch(1, 1)
  2159. self.tools_box.addLayout(grid0)
  2160. self.obj_combo_label = QtWidgets.QLabel('<b>%s</b>:' % _("EXCELLON"))
  2161. self.obj_combo_label.setToolTip(
  2162. _("Excellon object for drilling/milling operation.")
  2163. )
  2164. grid0.addWidget(self.obj_combo_label, 0, 0, 1, 2)
  2165. # ################################################
  2166. # ##### The object to be drilled #################
  2167. # ################################################
  2168. self.object_combo = FCComboBox()
  2169. self.object_combo.setModel(self.app.collection)
  2170. self.object_combo.setRootModelIndex(self.app.collection.index(1, 0, QtCore.QModelIndex()))
  2171. # self.object_combo.setCurrentIndex(1)
  2172. self.object_combo.is_last = True
  2173. grid0.addWidget(self.object_combo, 1, 0, 1, 2)
  2174. separator_line = QtWidgets.QFrame()
  2175. separator_line.setFrameShape(QtWidgets.QFrame.HLine)
  2176. separator_line.setFrameShadow(QtWidgets.QFrame.Sunken)
  2177. grid0.addWidget(separator_line, 2, 0, 1, 2)
  2178. # ################################################
  2179. # ########## Excellon Tool Table #################
  2180. # ################################################
  2181. self.tools_table = FCTable(drag_drop=True)
  2182. grid0.addWidget(self.tools_table, 3, 0, 1, 2)
  2183. self.tools_table.setColumnCount(5)
  2184. self.tools_table.setColumnHidden(3, True)
  2185. self.tools_table.setSortingEnabled(False)
  2186. self.tools_table.setHorizontalHeaderLabels(['#', _('Diameter'), _('Drills'), '', _('Slots')])
  2187. self.tools_table.horizontalHeaderItem(0).setToolTip(
  2188. _("This is the Tool Number.\n"
  2189. "When ToolChange is checked, on toolchange event this value\n"
  2190. "will be showed as a T1, T2 ... Tn in the Machine Code.\n\n"
  2191. "Here the tools are selected for G-code generation."))
  2192. self.tools_table.horizontalHeaderItem(1).setToolTip(
  2193. _("Tool Diameter. It's value (in current FlatCAM units) \n"
  2194. "is the cut width into the material."))
  2195. self.tools_table.horizontalHeaderItem(2).setToolTip(
  2196. _("The number of Drill holes. Holes that are drilled with\n"
  2197. "a drill bit."))
  2198. self.tools_table.horizontalHeaderItem(4).setToolTip(
  2199. _("The number of Slot holes. Holes that are created by\n"
  2200. "milling them with an endmill bit."))
  2201. # Tool order
  2202. self.order_label = QtWidgets.QLabel('%s:' % _('Tool order'))
  2203. self.order_label.setToolTip(_("This set the way that the tools in the tools table are used.\n"
  2204. "'No' --> means that the used order is the one in the tool table\n"
  2205. "'Forward' --> means that the tools will be ordered from small to big\n"
  2206. "'Reverse' --> means that the tools will ordered from big to small\n\n"
  2207. "WARNING: using rest machining will automatically set the order\n"
  2208. "in reverse and disable this control."))
  2209. self.order_radio = RadioSet([{'label': _('No'), 'value': 'no'},
  2210. {'label': _('Forward'), 'value': 'fwd'},
  2211. {'label': _('Reverse'), 'value': 'rev'}])
  2212. grid0.addWidget(self.order_label, 4, 0)
  2213. grid0.addWidget(self.order_radio, 4, 1)
  2214. separator_line = QtWidgets.QFrame()
  2215. separator_line.setFrameShape(QtWidgets.QFrame.HLine)
  2216. separator_line.setFrameShadow(QtWidgets.QFrame.Sunken)
  2217. grid0.addWidget(separator_line, 5, 0, 1, 2)
  2218. # ###########################################################
  2219. # ############# Create CNC Job ##############################
  2220. # ###########################################################
  2221. self.tool_data_label = QtWidgets.QLabel(
  2222. "<b>%s: <font color='#0000FF'>%s %d</font></b>" % (_('Parameters for'), _("Tool"), int(1)))
  2223. self.tool_data_label.setToolTip(
  2224. _(
  2225. "The data used for creating GCode.\n"
  2226. "Each tool store it's own set of such data."
  2227. )
  2228. )
  2229. grid0.addWidget(self.tool_data_label, 6, 0, 1, 2)
  2230. self.exc_param_frame = QtWidgets.QFrame()
  2231. self.exc_param_frame.setContentsMargins(0, 0, 0, 0)
  2232. grid0.addWidget(self.exc_param_frame, 7, 0, 1, 2)
  2233. self.exc_tools_box = QtWidgets.QVBoxLayout()
  2234. self.exc_tools_box.setContentsMargins(0, 0, 0, 0)
  2235. self.exc_param_frame.setLayout(self.exc_tools_box)
  2236. # #################################################################
  2237. # ################# GRID LAYOUT 3 ###############################
  2238. # #################################################################
  2239. self.grid1 = QtWidgets.QGridLayout()
  2240. self.grid1.setColumnStretch(0, 0)
  2241. self.grid1.setColumnStretch(1, 1)
  2242. self.exc_tools_box.addLayout(self.grid1)
  2243. # Cut Z
  2244. self.cutzlabel = QtWidgets.QLabel('%s:' % _('Cut Z'))
  2245. self.cutzlabel.setToolTip(
  2246. _("Drill depth (negative)\n"
  2247. "below the copper surface.")
  2248. )
  2249. self.cutz_entry = FCDoubleSpinner(callback=self.confirmation_message)
  2250. self.cutz_entry.set_precision(self.decimals)
  2251. if machinist_setting == 0:
  2252. self.cutz_entry.set_range(-9999.9999, 0.0000)
  2253. else:
  2254. self.cutz_entry.set_range(-9999.9999, 9999.9999)
  2255. self.cutz_entry.setSingleStep(0.1)
  2256. self.cutz_entry.setObjectName("e_cutz")
  2257. self.grid1.addWidget(self.cutzlabel, 4, 0)
  2258. self.grid1.addWidget(self.cutz_entry, 4, 1)
  2259. # Multi-Depth
  2260. self.mpass_cb = FCCheckBox('%s:' % _("Multi-Depth"))
  2261. self.mpass_cb.setToolTip(
  2262. _(
  2263. "Use multiple passes to limit\n"
  2264. "the cut depth in each pass. Will\n"
  2265. "cut multiple times until Cut Z is\n"
  2266. "reached."
  2267. )
  2268. )
  2269. self.mpass_cb.setObjectName("e_multidepth")
  2270. self.maxdepth_entry = FCDoubleSpinner(callback=self.confirmation_message)
  2271. self.maxdepth_entry.set_precision(self.decimals)
  2272. self.maxdepth_entry.set_range(0, 9999.9999)
  2273. self.maxdepth_entry.setSingleStep(0.1)
  2274. self.maxdepth_entry.setToolTip(_("Depth of each pass (positive)."))
  2275. self.maxdepth_entry.setObjectName("e_depthperpass")
  2276. self.mis_mpass_geo = OptionalInputSection(self.mpass_cb, [self.maxdepth_entry])
  2277. self.grid1.addWidget(self.mpass_cb, 5, 0)
  2278. self.grid1.addWidget(self.maxdepth_entry, 5, 1)
  2279. # Travel Z (z_move)
  2280. self.travelzlabel = QtWidgets.QLabel('%s:' % _('Travel Z'))
  2281. self.travelzlabel.setToolTip(
  2282. _("Tool height when travelling\n"
  2283. "across the XY plane.")
  2284. )
  2285. self.travelz_entry = FCDoubleSpinner(callback=self.confirmation_message)
  2286. self.travelz_entry.set_precision(self.decimals)
  2287. if machinist_setting == 0:
  2288. self.travelz_entry.set_range(0.00001, 9999.9999)
  2289. else:
  2290. self.travelz_entry.set_range(-9999.9999, 9999.9999)
  2291. self.travelz_entry.setSingleStep(0.1)
  2292. self.travelz_entry.setObjectName("e_travelz")
  2293. self.grid1.addWidget(self.travelzlabel, 6, 0)
  2294. self.grid1.addWidget(self.travelz_entry, 6, 1)
  2295. # Excellon Feedrate Z
  2296. self.frzlabel = QtWidgets.QLabel('%s:' % _('Feedrate Z'))
  2297. self.frzlabel.setToolTip(
  2298. _("Tool speed while drilling\n"
  2299. "(in units per minute).\n"
  2300. "So called 'Plunge' feedrate.\n"
  2301. "This is for linear move G01.")
  2302. )
  2303. self.feedrate_z_entry = FCDoubleSpinner(callback=self.confirmation_message)
  2304. self.feedrate_z_entry.set_precision(self.decimals)
  2305. self.feedrate_z_entry.set_range(0.0, 99999.9999)
  2306. self.feedrate_z_entry.setSingleStep(0.1)
  2307. self.feedrate_z_entry.setObjectName("e_feedratez")
  2308. self.grid1.addWidget(self.frzlabel, 14, 0)
  2309. self.grid1.addWidget(self.feedrate_z_entry, 14, 1)
  2310. # Excellon Rapid Feedrate
  2311. self.feedrate_rapid_label = QtWidgets.QLabel('%s:' % _('Feedrate Rapids'))
  2312. self.feedrate_rapid_label.setToolTip(
  2313. _("Tool speed while drilling\n"
  2314. "(in units per minute).\n"
  2315. "This is for the rapid move G00.\n"
  2316. "It is useful only for Marlin,\n"
  2317. "ignore for any other cases.")
  2318. )
  2319. self.feedrate_rapid_entry = FCDoubleSpinner(callback=self.confirmation_message)
  2320. self.feedrate_rapid_entry.set_precision(self.decimals)
  2321. self.feedrate_rapid_entry.set_range(0.0, 99999.9999)
  2322. self.feedrate_rapid_entry.setSingleStep(0.1)
  2323. self.feedrate_rapid_entry.setObjectName("e_fr_rapid")
  2324. self.grid1.addWidget(self.feedrate_rapid_label, 16, 0)
  2325. self.grid1.addWidget(self.feedrate_rapid_entry, 16, 1)
  2326. # default values is to hide
  2327. self.feedrate_rapid_label.hide()
  2328. self.feedrate_rapid_entry.hide()
  2329. # Spindlespeed
  2330. self.spindle_label = QtWidgets.QLabel('%s:' % _('Spindle speed'))
  2331. self.spindle_label.setToolTip(
  2332. _("Speed of the spindle\n"
  2333. "in RPM (optional)")
  2334. )
  2335. self.spindlespeed_entry = FCSpinner(callback=self.confirmation_message_int)
  2336. self.spindlespeed_entry.set_range(0, 1000000)
  2337. self.spindlespeed_entry.set_step(100)
  2338. self.spindlespeed_entry.setObjectName("e_spindlespeed")
  2339. self.grid1.addWidget(self.spindle_label, 19, 0)
  2340. self.grid1.addWidget(self.spindlespeed_entry, 19, 1)
  2341. # Dwell
  2342. self.dwell_cb = FCCheckBox('%s:' % _('Dwell'))
  2343. self.dwell_cb.setToolTip(
  2344. _("Pause to allow the spindle to reach its\n"
  2345. "speed before cutting.")
  2346. )
  2347. self.dwell_cb.setObjectName("e_dwell")
  2348. # Dwelltime
  2349. self.dwelltime_entry = FCDoubleSpinner(callback=self.confirmation_message)
  2350. self.dwelltime_entry.set_precision(self.decimals)
  2351. self.dwelltime_entry.set_range(0.0, 9999.9999)
  2352. self.dwelltime_entry.setSingleStep(0.1)
  2353. self.dwelltime_entry.setToolTip(
  2354. _("Number of time units for spindle to dwell.")
  2355. )
  2356. self.dwelltime_entry.setObjectName("e_dwelltime")
  2357. self.grid1.addWidget(self.dwell_cb, 20, 0)
  2358. self.grid1.addWidget(self.dwelltime_entry, 20, 1)
  2359. self.ois_dwell = OptionalInputSection(self.dwell_cb, [self.dwelltime_entry])
  2360. # Tool Offset
  2361. self.tool_offset_label = QtWidgets.QLabel('%s:' % _('Offset Z'))
  2362. self.tool_offset_label.setToolTip(
  2363. _("Some drill bits (the larger ones) need to drill deeper\n"
  2364. "to create the desired exit hole diameter due of the tip shape.\n"
  2365. "The value here can compensate the Cut Z parameter.")
  2366. )
  2367. self.offset_entry = FCDoubleSpinner(callback=self.confirmation_message)
  2368. self.offset_entry.set_precision(self.decimals)
  2369. self.offset_entry.set_range(-9999.9999, 9999.9999)
  2370. self.offset_entry.setObjectName("e_offset")
  2371. self.grid1.addWidget(self.tool_offset_label, 25, 0)
  2372. self.grid1.addWidget(self.offset_entry, 25, 1)
  2373. # Drill slots
  2374. self.drill_slots_cb = FCCheckBox('%s' % _('Drill slots'))
  2375. self.drill_slots_cb.setToolTip(
  2376. _("If the selected tool has slots then they will be drilled.")
  2377. )
  2378. self.drill_slots_cb.setObjectName("e_drill_slots")
  2379. self.grid1.addWidget(self.drill_slots_cb, 27, 0, 1, 2)
  2380. # Drill Overlap
  2381. self.drill_overlap_label = QtWidgets.QLabel('%s:' % _('Overlap'))
  2382. self.drill_overlap_label.setToolTip(
  2383. _("How much (percentage) of the tool diameter to overlap previous drill hole.")
  2384. )
  2385. self.drill_overlap_entry = FCDoubleSpinner(callback=self.confirmation_message)
  2386. self.drill_overlap_entry.set_precision(self.decimals)
  2387. self.drill_overlap_entry.set_range(0.0, 9999.9999)
  2388. self.drill_overlap_entry.setSingleStep(0.1)
  2389. self.drill_overlap_entry.setObjectName("e_drill_slots_overlap")
  2390. self.grid1.addWidget(self.drill_overlap_label, 28, 0)
  2391. self.grid1.addWidget(self.drill_overlap_entry, 28, 1)
  2392. # Last drill in slot
  2393. self.last_drill_cb = FCCheckBox('%s' % _('Last drill'))
  2394. self.last_drill_cb.setToolTip(
  2395. _("If the slot length is not completely covered by drill holes,\n"
  2396. "add a drill hole on the slot end point.")
  2397. )
  2398. self.last_drill_cb.setObjectName("e_drill_last_drill")
  2399. self.grid1.addWidget(self.last_drill_cb, 30, 0, 1, 2)
  2400. self.ois_drill_overlap = OptionalInputSection(
  2401. self.drill_slots_cb,
  2402. [
  2403. self.drill_overlap_label,
  2404. self.drill_overlap_entry,
  2405. self.last_drill_cb
  2406. ]
  2407. )
  2408. # #################################################################
  2409. # ################# GRID LAYOUT 5 ###############################
  2410. # #################################################################
  2411. # ################# COMMON PARAMETERS #############################
  2412. self.grid3 = QtWidgets.QGridLayout()
  2413. self.grid3.setColumnStretch(0, 0)
  2414. self.grid3.setColumnStretch(1, 1)
  2415. self.exc_tools_box.addLayout(self.grid3)
  2416. separator_line2 = QtWidgets.QFrame()
  2417. separator_line2.setFrameShape(QtWidgets.QFrame.HLine)
  2418. separator_line2.setFrameShadow(QtWidgets.QFrame.Sunken)
  2419. self.grid3.addWidget(separator_line2, 0, 0, 1, 2)
  2420. self.apply_param_to_all = FCButton(_("Apply parameters to all tools"))
  2421. self.apply_param_to_all.setToolTip(
  2422. _("The parameters in the current form will be applied\n"
  2423. "on all the tools from the Tool Table.")
  2424. )
  2425. self.grid3.addWidget(self.apply_param_to_all, 1, 0, 1, 2)
  2426. separator_line2 = QtWidgets.QFrame()
  2427. separator_line2.setFrameShape(QtWidgets.QFrame.HLine)
  2428. separator_line2.setFrameShadow(QtWidgets.QFrame.Sunken)
  2429. self.grid3.addWidget(separator_line2, 2, 0, 1, 2)
  2430. # General Parameters
  2431. self.gen_param_label = QtWidgets.QLabel('<b>%s</b>' % _("Common Parameters"))
  2432. self.gen_param_label.setToolTip(
  2433. _("Parameters that are common for all tools.")
  2434. )
  2435. self.grid3.addWidget(self.gen_param_label, 3, 0, 1, 2)
  2436. # Tool change Z:
  2437. self.toolchange_cb = FCCheckBox('%s:' % _("Tool change Z"))
  2438. self.toolchange_cb.setToolTip(
  2439. _("Include tool-change sequence\n"
  2440. "in G-Code (Pause for tool change).")
  2441. )
  2442. self.toolchangez_entry = FCDoubleSpinner(callback=self.confirmation_message)
  2443. self.toolchangez_entry.set_precision(self.decimals)
  2444. self.toolchangez_entry.setToolTip(
  2445. _("Z-axis position (height) for\n"
  2446. "tool change.")
  2447. )
  2448. if machinist_setting == 0:
  2449. self.toolchangez_entry.set_range(0.0, 9999.9999)
  2450. else:
  2451. self.toolchangez_entry.set_range(-9999.9999, 9999.9999)
  2452. self.toolchangez_entry.setSingleStep(0.1)
  2453. self.ois_tcz_e = OptionalInputSection(self.toolchange_cb, [self.toolchangez_entry])
  2454. self.grid3.addWidget(self.toolchange_cb, 8, 0)
  2455. self.grid3.addWidget(self.toolchangez_entry, 8, 1)
  2456. # Start move Z:
  2457. self.estartz_label = QtWidgets.QLabel('%s:' % _("Start Z"))
  2458. self.estartz_label.setToolTip(
  2459. _("Height of the tool just after start.\n"
  2460. "Delete the value if you don't need this feature.")
  2461. )
  2462. self.estartz_entry = NumericalEvalEntry(border_color='#0069A9')
  2463. self.grid3.addWidget(self.estartz_label, 9, 0)
  2464. self.grid3.addWidget(self.estartz_entry, 9, 1)
  2465. # End move Z:
  2466. self.endz_label = QtWidgets.QLabel('%s:' % _("End move Z"))
  2467. self.endz_label.setToolTip(
  2468. _("Height of the tool after\n"
  2469. "the last move at the end of the job.")
  2470. )
  2471. self.endz_entry = FCDoubleSpinner(callback=self.confirmation_message)
  2472. self.endz_entry.set_precision(self.decimals)
  2473. if machinist_setting == 0:
  2474. self.endz_entry.set_range(0.0, 9999.9999)
  2475. else:
  2476. self.endz_entry.set_range(-9999.9999, 9999.9999)
  2477. self.endz_entry.setSingleStep(0.1)
  2478. self.grid3.addWidget(self.endz_label, 11, 0)
  2479. self.grid3.addWidget(self.endz_entry, 11, 1)
  2480. # End Move X,Y
  2481. endmove_xy_label = QtWidgets.QLabel('%s:' % _('End move X,Y'))
  2482. endmove_xy_label.setToolTip(
  2483. _("End move X,Y position. In format (x,y).\n"
  2484. "If no value is entered then there is no move\n"
  2485. "on X,Y plane at the end of the job.")
  2486. )
  2487. self.endxy_entry = NumericalEvalEntry(border_color='#0069A9')
  2488. self.endxy_entry.setPlaceholderText(_("X,Y coordinates"))
  2489. self.grid3.addWidget(endmove_xy_label, 12, 0)
  2490. self.grid3.addWidget(self.endxy_entry, 12, 1)
  2491. # Probe depth
  2492. self.pdepth_label = QtWidgets.QLabel('%s:' % _("Probe Z depth"))
  2493. self.pdepth_label.setToolTip(
  2494. _("The maximum depth that the probe is allowed\n"
  2495. "to probe. Negative value, in current units.")
  2496. )
  2497. self.pdepth_entry = FCDoubleSpinner(callback=self.confirmation_message)
  2498. self.pdepth_entry.set_precision(self.decimals)
  2499. self.pdepth_entry.set_range(-9999.9999, 9999.9999)
  2500. self.pdepth_entry.setSingleStep(0.1)
  2501. self.pdepth_entry.setObjectName("e_depth_probe")
  2502. self.grid3.addWidget(self.pdepth_label, 13, 0)
  2503. self.grid3.addWidget(self.pdepth_entry, 13, 1)
  2504. self.pdepth_label.hide()
  2505. self.pdepth_entry.setVisible(False)
  2506. # Probe feedrate
  2507. self.feedrate_probe_label = QtWidgets.QLabel('%s:' % _("Feedrate Probe"))
  2508. self.feedrate_probe_label.setToolTip(
  2509. _("The feedrate used while the probe is probing.")
  2510. )
  2511. self.feedrate_probe_entry = FCDoubleSpinner(callback=self.confirmation_message)
  2512. self.feedrate_probe_entry.set_precision(self.decimals)
  2513. self.feedrate_probe_entry.set_range(0.0, 9999.9999)
  2514. self.feedrate_probe_entry.setSingleStep(0.1)
  2515. self.feedrate_probe_entry.setObjectName("e_fr_probe")
  2516. self.grid3.addWidget(self.feedrate_probe_label, 14, 0)
  2517. self.grid3.addWidget(self.feedrate_probe_entry, 14, 1)
  2518. self.feedrate_probe_label.hide()
  2519. self.feedrate_probe_entry.setVisible(False)
  2520. # Preprocessor Excellon selection
  2521. pp_excellon_label = QtWidgets.QLabel('%s:' % _("Preprocessor"))
  2522. pp_excellon_label.setToolTip(
  2523. _("The preprocessor JSON file that dictates\n"
  2524. "Gcode output for Excellon Objects.")
  2525. )
  2526. self.pp_excellon_name_cb = FCComboBox()
  2527. self.pp_excellon_name_cb.setFocusPolicy(QtCore.Qt.StrongFocus)
  2528. self.grid3.addWidget(pp_excellon_label, 15, 0)
  2529. self.grid3.addWidget(self.pp_excellon_name_cb, 15, 1)
  2530. # ------------------------------------------------------------------------------------------------------------
  2531. # ------------------------- EXCLUSION AREAS ------------------------------------------------------------------
  2532. # ------------------------------------------------------------------------------------------------------------
  2533. # Exclusion Areas
  2534. self.exclusion_cb = FCCheckBox('%s' % _("Add exclusion areas"))
  2535. self.exclusion_cb.setToolTip(
  2536. _(
  2537. "Include exclusion areas.\n"
  2538. "In those areas the travel of the tools\n"
  2539. "is forbidden."
  2540. )
  2541. )
  2542. self.grid3.addWidget(self.exclusion_cb, 20, 0, 1, 2)
  2543. self.exclusion_frame = QtWidgets.QFrame()
  2544. self.exclusion_frame.setContentsMargins(0, 0, 0, 0)
  2545. self.grid3.addWidget(self.exclusion_frame, 22, 0, 1, 2)
  2546. self.exclusion_box = QtWidgets.QVBoxLayout()
  2547. self.exclusion_box.setContentsMargins(0, 0, 0, 0)
  2548. self.exclusion_frame.setLayout(self.exclusion_box)
  2549. self.exclusion_table = FCTable()
  2550. self.exclusion_box.addWidget(self.exclusion_table)
  2551. self.exclusion_table.setSizeAdjustPolicy(QtWidgets.QAbstractScrollArea.AdjustToContents)
  2552. self.exclusion_table.setColumnCount(4)
  2553. self.exclusion_table.setColumnWidth(0, 20)
  2554. self.exclusion_table.setHorizontalHeaderLabels(['#', _('Object'), _('Strategy'), _('Over Z')])
  2555. self.exclusion_table.horizontalHeaderItem(0).setToolTip(_("This is the Area ID."))
  2556. self.exclusion_table.horizontalHeaderItem(1).setToolTip(
  2557. _("Type of the object where the exclusion area was added."))
  2558. self.exclusion_table.horizontalHeaderItem(2).setToolTip(
  2559. _("The strategy used for exclusion area. Go around the exclusion areas or over it."))
  2560. self.exclusion_table.horizontalHeaderItem(3).setToolTip(
  2561. _("If the strategy is to go over the area then this is the height at which the tool will go to avoid the "
  2562. "exclusion area."))
  2563. self.exclusion_table.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectRows)
  2564. grid_a1 = QtWidgets.QGridLayout()
  2565. grid_a1.setColumnStretch(0, 0)
  2566. grid_a1.setColumnStretch(1, 1)
  2567. self.exclusion_box.addLayout(grid_a1)
  2568. # Chose Strategy
  2569. self.strategy_label = FCLabel('%s:' % _("Strategy"))
  2570. self.strategy_label.setToolTip(_("The strategy followed when encountering an exclusion area.\n"
  2571. "Can be:\n"
  2572. "- Over -> when encountering the area, the tool will go to a set height\n"
  2573. "- Around -> will avoid the exclusion area by going around the area"))
  2574. self.strategy_radio = RadioSet([{'label': _('Over'), 'value': 'over'},
  2575. {'label': _('Around'), 'value': 'around'}])
  2576. grid_a1.addWidget(self.strategy_label, 1, 0)
  2577. grid_a1.addWidget(self.strategy_radio, 1, 1)
  2578. # Over Z
  2579. self.over_z_label = FCLabel('%s:' % _("Over Z"))
  2580. self.over_z_label.setToolTip(_("The height Z to which the tool will rise in order to avoid\n"
  2581. "an interdiction area."))
  2582. self.over_z_entry = FCDoubleSpinner()
  2583. self.over_z_entry.set_range(0.000, 9999.9999)
  2584. self.over_z_entry.set_precision(self.decimals)
  2585. grid_a1.addWidget(self.over_z_label, 2, 0)
  2586. grid_a1.addWidget(self.over_z_entry, 2, 1)
  2587. # Button Add Area
  2588. self.add_area_button = QtWidgets.QPushButton(_('Add area:'))
  2589. self.add_area_button.setToolTip(_("Add an Exclusion Area."))
  2590. # Area Selection shape
  2591. self.area_shape_radio = RadioSet([{'label': _("Square"), 'value': 'square'},
  2592. {'label': _("Polygon"), 'value': 'polygon'}])
  2593. self.area_shape_radio.setToolTip(
  2594. _("The kind of selection shape used for area selection.")
  2595. )
  2596. grid_a1.addWidget(self.add_area_button, 4, 0)
  2597. grid_a1.addWidget(self.area_shape_radio, 4, 1)
  2598. h_lay_1 = QtWidgets.QHBoxLayout()
  2599. self.exclusion_box.addLayout(h_lay_1)
  2600. # Button Delete All Areas
  2601. self.delete_area_button = QtWidgets.QPushButton(_('Delete All'))
  2602. self.delete_area_button.setToolTip(_("Delete all exclusion areas."))
  2603. # Button Delete Selected Areas
  2604. self.delete_sel_area_button = QtWidgets.QPushButton(_('Delete Selected'))
  2605. self.delete_sel_area_button.setToolTip(_("Delete all exclusion areas that are selected in the table."))
  2606. h_lay_1.addWidget(self.delete_area_button)
  2607. h_lay_1.addWidget(self.delete_sel_area_button)
  2608. self.ois_exclusion_exc = OptionalHideInputSection(self.exclusion_cb, [self.exclusion_frame])
  2609. # -------------------------- EXCLUSION AREAS END -------------------------------------------------------------
  2610. # ------------------------------------------------------------------------------------------------------------
  2611. separator_line = QtWidgets.QFrame()
  2612. separator_line.setFrameShape(QtWidgets.QFrame.HLine)
  2613. separator_line.setFrameShadow(QtWidgets.QFrame.Sunken)
  2614. self.grid3.addWidget(separator_line, 25, 0, 1, 2)
  2615. # #################################################################
  2616. # ################# GRID LAYOUT 6 ###############################
  2617. # #################################################################
  2618. self.grid4 = QtWidgets.QGridLayout()
  2619. self.grid4.setColumnStretch(0, 0)
  2620. self.grid4.setColumnStretch(1, 1)
  2621. self.tools_box.addLayout(self.grid4)
  2622. self.generate_cnc_button = QtWidgets.QPushButton(_('Generate CNCJob object'))
  2623. self.generate_cnc_button.setToolTip(
  2624. _("Generate the CNC Job.\n"
  2625. "If milling then an additional Geometry object will be created.\n"
  2626. "Add / Select at least one tool in the tool-table.\n"
  2627. "Click the # header to select all, or Ctrl + LMB\n"
  2628. "for custom selection of tools.")
  2629. )
  2630. self.generate_cnc_button.setStyleSheet("""
  2631. QPushButton
  2632. {
  2633. font-weight: bold;
  2634. }
  2635. """)
  2636. self.grid4.addWidget(self.generate_cnc_button, 3, 0, 1, 3)
  2637. self.tools_box.addStretch()
  2638. # ## Reset Tool
  2639. self.reset_button = QtWidgets.QPushButton(_("Reset Tool"))
  2640. self.reset_button.setToolTip(
  2641. _("Will reset the tool parameters.")
  2642. )
  2643. self.reset_button.setStyleSheet("""
  2644. QPushButton
  2645. {
  2646. font-weight: bold;
  2647. }
  2648. """)
  2649. self.tools_box.addWidget(self.reset_button)
  2650. # ############################ FINSIHED GUI ###################################
  2651. # #############################################################################
  2652. def confirmation_message(self, accepted, minval, maxval):
  2653. if accepted is False:
  2654. self.app.inform[str, bool].emit('[WARNING_NOTCL] %s: [%.*f, %.*f]' % (_("Edited value is out of range"),
  2655. self.decimals,
  2656. minval,
  2657. self.decimals,
  2658. maxval), False)
  2659. else:
  2660. self.app.inform[str, bool].emit('[success] %s' % _("Edited value is within limits."), False)
  2661. def confirmation_message_int(self, accepted, minval, maxval):
  2662. if accepted is False:
  2663. self.app.inform[str, bool].emit('[WARNING_NOTCL] %s: [%d, %d]' %
  2664. (_("Edited value is out of range"), minval, maxval), False)
  2665. else:
  2666. self.app.inform[str, bool].emit('[success] %s' % _("Edited value is within limits."), False)
  2667. def distance(pt1, pt2):
  2668. return np.sqrt((pt1[0] - pt2[0]) ** 2 + (pt1[1] - pt2[1]) ** 2)
  2669. def distance_euclidian(x1, y1, x2, y2):
  2670. return np.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2)
  2671. class AttrDict(dict):
  2672. def __init__(self, *args, **kwargs):
  2673. super(AttrDict, self).__init__(*args, **kwargs)
  2674. self.__dict__ = self