FlatCAMCNCJob.py 93 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214
  1. # ##########################################################
  2. # FlatCAM: 2D Post-processing for Manufacturing #
  3. # http://flatcam.org #
  4. # Author: Juan Pablo Caram (c) #
  5. # Date: 2/5/2014 #
  6. # MIT Licence #
  7. # ##########################################################
  8. # ##########################################################
  9. # File modified by: Marius Stanciu #
  10. # ##########################################################
  11. from copy import deepcopy
  12. from io import StringIO
  13. from datetime import datetime
  14. from appEditors.AppTextEditor import AppTextEditor
  15. from appObjects.FlatCAMObj import *
  16. from matplotlib.backend_bases import KeyEvent as mpl_key_event
  17. from camlib import CNCjob
  18. from shapely.ops import unary_union
  19. from shapely.geometry import Point, MultiPoint
  20. try:
  21. from shapely.ops import voronoi_diagram
  22. except Exception:
  23. pass
  24. import os
  25. import sys
  26. import time
  27. import serial
  28. import glob
  29. import math
  30. import gettext
  31. import appTranslation as fcTranslate
  32. import builtins
  33. fcTranslate.apply_language('strings')
  34. if '_' not in builtins.__dict__:
  35. _ = gettext.gettext
  36. class CNCJobObject(FlatCAMObj, CNCjob):
  37. """
  38. Represents G-Code.
  39. """
  40. optionChanged = QtCore.pyqtSignal(str)
  41. build_al_table_sig = QtCore.pyqtSignal()
  42. ui_type = CNCObjectUI
  43. def __init__(self, name, units="in", kind="generic", z_move=0.1,
  44. feedrate=3.0, feedrate_rapid=3.0, z_cut=-0.002, tooldia=0.0,
  45. spindlespeed=None):
  46. log.debug("Creating CNCJob object...")
  47. self.decimals = self.app.decimals
  48. CNCjob.__init__(self, units=units, kind=kind, z_move=z_move,
  49. feedrate=feedrate, feedrate_rapid=feedrate_rapid, z_cut=z_cut, tooldia=tooldia,
  50. spindlespeed=spindlespeed, steps_per_circle=int(self.app.defaults["cncjob_steps_per_circle"]))
  51. FlatCAMObj.__init__(self, name)
  52. self.kind = "cncjob"
  53. self.options.update({
  54. "plot": True,
  55. "tooldia": 0.03937, # 0.4mm in inches
  56. "append": "",
  57. "prepend": "",
  58. "dwell": False,
  59. "dwelltime": 1,
  60. "type": 'Geometry',
  61. # "toolchange_macro": '',
  62. # "toolchange_macro_enable": False
  63. })
  64. '''
  65. This is a dict of dictionaries. Each dict is associated with a tool present in the file. The key is the
  66. diameter of the tools and the value is another dict that will hold the data under the following form:
  67. {tooldia: {
  68. 'tooluid': 1,
  69. 'offset': 'Path',
  70. 'type_item': 'Rough',
  71. 'tool_type': 'C1',
  72. 'data': {} # a dict to hold the parameters
  73. 'gcode': "" # a string with the actual GCODE
  74. 'gcode_parsed': {} # dictionary holding the CNCJob geometry and type of geometry
  75. (cut or move)
  76. 'solid_geometry': []
  77. },
  78. ...
  79. }
  80. It is populated in the GeometryObject.mtool_gen_cncjob()
  81. BEWARE: I rely on the ordered nature of the Python 3.7 dictionary. Things might change ...
  82. '''
  83. self.cnc_tools = {}
  84. '''
  85. This is a dict of dictionaries. Each dict is associated with a tool present in the file. The key is the
  86. diameter of the tools and the value is another dict that will hold the data under the following form:
  87. {tooldia: {
  88. 'tool': int,
  89. 'nr_drills': int,
  90. 'nr_slots': int,
  91. 'offset': float,
  92. 'data': {}, a dict to hold the parameters
  93. 'gcode': "", a string with the actual GCODE
  94. 'gcode_parsed': [], list of dicts holding the CNCJob geometry and
  95. type of geometry (cut or move)
  96. 'solid_geometry': [],
  97. },
  98. ...
  99. }
  100. It is populated in the ExcellonObject.on_create_cncjob_click() but actually
  101. it's done in camlib.CNCJob.generate_from_excellon_by_tool()
  102. BEWARE: I rely on the ordered nature of the Python 3.7 dictionary. Things might change ...
  103. '''
  104. self.exc_cnc_tools = {}
  105. # flag to store if the CNCJob is part of a special group of CNCJob objects that can't be processed by the
  106. # default engine of FlatCAM. They generated by some of tools and are special cases of CNCJob objects.
  107. self.special_group = None
  108. # for now it show if the plot will be done for multi-tool CNCJob (True) or for single tool
  109. # (like the one in the TCL Command), False
  110. self.multitool = False
  111. # determine if the GCode was generated out of a Excellon object or a Geometry object
  112. self.origin_kind = None
  113. self.coords_decimals = 4
  114. self.fr_decimals = 2
  115. # used for parsing the GCode lines to adjust the GCode when the GCode is offseted or scaled
  116. gcodex_re_string = r'(?=.*(X[-\+]?\d*\.\d*))'
  117. self.g_x_re = re.compile(gcodex_re_string)
  118. gcodey_re_string = r'(?=.*(Y[-\+]?\d*\.\d*))'
  119. self.g_y_re = re.compile(gcodey_re_string)
  120. gcodez_re_string = r'(?=.*(Z[-\+]?\d*\.\d*))'
  121. self.g_z_re = re.compile(gcodez_re_string)
  122. gcodef_re_string = r'(?=.*(F[-\+]?\d*\.\d*))'
  123. self.g_f_re = re.compile(gcodef_re_string)
  124. gcodet_re_string = r'(?=.*(\=\s*[-\+]?\d*\.\d*))'
  125. self.g_t_re = re.compile(gcodet_re_string)
  126. gcodenr_re_string = r'([+-]?\d*\.\d+)'
  127. self.g_nr_re = re.compile(gcodenr_re_string)
  128. if self.app.is_legacy is False:
  129. self.text_col = self.app.plotcanvas.new_text_collection()
  130. self.text_col.enabled = True
  131. self.annotation = self.app.plotcanvas.new_text_group(collection=self.text_col)
  132. self.gcode_editor_tab = None
  133. self.gcode_viewer_tab = None
  134. self.source_file = ''
  135. self.units_found = self.app.defaults['units']
  136. self.probing_gcode_text = ''
  137. # store the current selection shape status to be restored after manual adding test points
  138. self.old_selection_state = self.app.defaults['global_selection_shape']
  139. # if mouse is dragging set the object True
  140. self.mouse_is_dragging = False
  141. # if mouse events are bound to local methods
  142. self.mouse_events_connected = False
  143. # event handlers references
  144. self.kp = None
  145. self.mm = None
  146. self.mr = None
  147. self.append_snippet = ''
  148. self.prepend_snippet = ''
  149. self.gc_header = self.gcode_header()
  150. self.gc_start = ''
  151. self.gc_end = ''
  152. '''
  153. dictionary of dictionaries to store the informations for the autolevelling
  154. format:
  155. {
  156. id: {
  157. 'point': Shapely Point
  158. 'geo': Shapely Polygon from Voronoi diagram,
  159. 'height': float
  160. }
  161. }
  162. '''
  163. self.al_geometry_dict = {}
  164. self.solid_geo = None
  165. self.grbl_ser_port = None
  166. self.pressed_button = None
  167. # Attributes to be included in serialization
  168. # Always append to it because it carries contents
  169. # from predecessors.
  170. self.ser_attrs += [
  171. 'options', 'kind', 'origin_kind', 'cnc_tools', 'exc_cnc_tools', 'multitool', 'append_snippet',
  172. 'prepend_snippet', 'gc_header'
  173. ]
  174. def build_ui(self):
  175. self.ui_disconnect()
  176. FlatCAMObj.build_ui(self)
  177. self.units = self.app.defaults['units'].upper()
  178. # if the FlatCAM object is Excellon don't build the CNC Tools Table but hide it
  179. self.ui.cnc_tools_table.hide()
  180. if self.cnc_tools:
  181. self.ui.cnc_tools_table.show()
  182. self.build_cnc_tools_table()
  183. self.ui.exc_cnc_tools_table.hide()
  184. if self.exc_cnc_tools:
  185. self.ui.exc_cnc_tools_table.show()
  186. self.build_excellon_cnc_tools()
  187. if self.ui.sal_cb.get_value():
  188. self.build_al_table()
  189. self.ui_connect()
  190. def build_cnc_tools_table(self):
  191. tool_idx = 0
  192. n = len(self.cnc_tools)
  193. self.ui.cnc_tools_table.setRowCount(n)
  194. for dia_key, dia_value in self.cnc_tools.items():
  195. tool_idx += 1
  196. row_no = tool_idx - 1
  197. t_id = QtWidgets.QTableWidgetItem('%d' % int(tool_idx))
  198. # id.setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled)
  199. self.ui.cnc_tools_table.setItem(row_no, 0, t_id) # Tool name/id
  200. # Make sure that the tool diameter when in MM is with no more than 2 decimals.
  201. # There are no tool bits in MM with more than 2 decimals diameter.
  202. # For INCH the decimals should be no more than 4. There are no tools under 10mils.
  203. dia_item = QtWidgets.QTableWidgetItem('%.*f' % (self.decimals, float(dia_value['tooldia'])))
  204. offset_txt = list(str(dia_value['offset']))
  205. offset_txt[0] = offset_txt[0].upper()
  206. offset_item = QtWidgets.QTableWidgetItem(''.join(offset_txt))
  207. type_item = QtWidgets.QTableWidgetItem(str(dia_value['type']))
  208. tool_type_item = QtWidgets.QTableWidgetItem(str(dia_value['tool_type']))
  209. t_id.setFlags(QtCore.Qt.ItemIsEnabled)
  210. dia_item.setFlags(QtCore.Qt.ItemIsEnabled)
  211. offset_item.setFlags(QtCore.Qt.ItemIsEnabled)
  212. type_item.setFlags(QtCore.Qt.ItemIsEnabled)
  213. tool_type_item.setFlags(QtCore.Qt.ItemIsEnabled)
  214. # hack so the checkbox stay centered in the table cell
  215. # used this:
  216. # https://stackoverflow.com/questions/32458111/pyqt-allign-checkbox-and-put-it-in-every-row
  217. # plot_item = QtWidgets.QWidget()
  218. # checkbox = FCCheckBox()
  219. # checkbox.setCheckState(QtCore.Qt.Checked)
  220. # qhboxlayout = QtWidgets.QHBoxLayout(plot_item)
  221. # qhboxlayout.addWidget(checkbox)
  222. # qhboxlayout.setAlignment(QtCore.Qt.AlignCenter)
  223. # qhboxlayout.setContentsMargins(0, 0, 0, 0)
  224. plot_item = FCCheckBox()
  225. plot_item.setLayoutDirection(QtCore.Qt.RightToLeft)
  226. tool_uid_item = QtWidgets.QTableWidgetItem(str(dia_key))
  227. if self.ui.plot_cb.isChecked():
  228. plot_item.setChecked(True)
  229. self.ui.cnc_tools_table.setItem(row_no, 1, dia_item) # Diameter
  230. self.ui.cnc_tools_table.setItem(row_no, 2, offset_item) # Offset
  231. self.ui.cnc_tools_table.setItem(row_no, 3, type_item) # Toolpath Type
  232. self.ui.cnc_tools_table.setItem(row_no, 4, tool_type_item) # Tool Type
  233. # ## REMEMBER: THIS COLUMN IS HIDDEN IN OBJECTUI.PY # ##
  234. self.ui.cnc_tools_table.setItem(row_no, 5, tool_uid_item) # Tool unique ID)
  235. self.ui.cnc_tools_table.setCellWidget(row_no, 6, plot_item)
  236. # make the diameter column editable
  237. # for row in range(tool_idx):
  238. # self.ui.cnc_tools_table.item(row, 1).setFlags(QtCore.Qt.ItemIsSelectable |
  239. # QtCore.Qt.ItemIsEnabled)
  240. for row in range(tool_idx):
  241. self.ui.cnc_tools_table.item(row, 0).setFlags(
  242. self.ui.cnc_tools_table.item(row, 0).flags() ^ QtCore.Qt.ItemIsSelectable)
  243. self.ui.cnc_tools_table.resizeColumnsToContents()
  244. self.ui.cnc_tools_table.resizeRowsToContents()
  245. vertical_header = self.ui.cnc_tools_table.verticalHeader()
  246. # vertical_header.setSectionResizeMode(QtWidgets.QHeaderView.ResizeToContents)
  247. vertical_header.hide()
  248. self.ui.cnc_tools_table.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
  249. horizontal_header = self.ui.cnc_tools_table.horizontalHeader()
  250. horizontal_header.setMinimumSectionSize(10)
  251. horizontal_header.setDefaultSectionSize(70)
  252. horizontal_header.setSectionResizeMode(0, QtWidgets.QHeaderView.Fixed)
  253. horizontal_header.resizeSection(0, 20)
  254. horizontal_header.setSectionResizeMode(1, QtWidgets.QHeaderView.Stretch)
  255. horizontal_header.setSectionResizeMode(3, QtWidgets.QHeaderView.ResizeToContents)
  256. horizontal_header.setSectionResizeMode(4, QtWidgets.QHeaderView.Fixed)
  257. horizontal_header.resizeSection(4, 40)
  258. horizontal_header.setSectionResizeMode(6, QtWidgets.QHeaderView.Fixed)
  259. horizontal_header.resizeSection(4, 17)
  260. # horizontal_header.setStretchLastSection(True)
  261. self.ui.cnc_tools_table.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
  262. self.ui.cnc_tools_table.setColumnWidth(0, 20)
  263. self.ui.cnc_tools_table.setColumnWidth(4, 40)
  264. self.ui.cnc_tools_table.setColumnWidth(6, 17)
  265. # self.ui.geo_tools_table.setSortingEnabled(True)
  266. self.ui.cnc_tools_table.setMinimumHeight(self.ui.cnc_tools_table.getHeight())
  267. self.ui.cnc_tools_table.setMaximumHeight(self.ui.cnc_tools_table.getHeight())
  268. def build_excellon_cnc_tools(self):
  269. tool_idx = 0
  270. n = len(self.exc_cnc_tools)
  271. self.ui.exc_cnc_tools_table.setRowCount(n)
  272. for tooldia_key, dia_value in self.exc_cnc_tools.items():
  273. tool_idx += 1
  274. row_no = tool_idx - 1
  275. t_id = QtWidgets.QTableWidgetItem('%d' % int(tool_idx))
  276. dia_item = QtWidgets.QTableWidgetItem('%.*f' % (self.decimals, float(tooldia_key)))
  277. nr_drills_item = QtWidgets.QTableWidgetItem('%d' % int(dia_value['nr_drills']))
  278. nr_slots_item = QtWidgets.QTableWidgetItem('%d' % int(dia_value['nr_slots']))
  279. cutz_item = QtWidgets.QTableWidgetItem('%.*f' % (self.decimals, float(dia_value['offset']) + self.z_cut))
  280. t_id.setFlags(QtCore.Qt.ItemIsEnabled)
  281. dia_item.setFlags(QtCore.Qt.ItemIsEnabled)
  282. nr_drills_item.setFlags(QtCore.Qt.ItemIsEnabled)
  283. nr_slots_item.setFlags(QtCore.Qt.ItemIsEnabled)
  284. cutz_item.setFlags(QtCore.Qt.ItemIsEnabled)
  285. # hack so the checkbox stay centered in the table cell
  286. # used this:
  287. # https://stackoverflow.com/questions/32458111/pyqt-allign-checkbox-and-put-it-in-every-row
  288. # plot_item = QtWidgets.QWidget()
  289. # checkbox = FCCheckBox()
  290. # checkbox.setCheckState(QtCore.Qt.Checked)
  291. # qhboxlayout = QtWidgets.QHBoxLayout(plot_item)
  292. # qhboxlayout.addWidget(checkbox)
  293. # qhboxlayout.setAlignment(QtCore.Qt.AlignCenter)
  294. # qhboxlayout.setContentsMargins(0, 0, 0, 0)
  295. plot_item = FCCheckBox()
  296. plot_item.setLayoutDirection(QtCore.Qt.RightToLeft)
  297. tool_uid_item = QtWidgets.QTableWidgetItem(str(dia_value['tool']))
  298. if self.ui.plot_cb.isChecked():
  299. plot_item.setChecked(True)
  300. self.ui.exc_cnc_tools_table.setItem(row_no, 0, t_id) # Tool name/id
  301. self.ui.exc_cnc_tools_table.setItem(row_no, 1, dia_item) # Diameter
  302. self.ui.exc_cnc_tools_table.setItem(row_no, 2, nr_drills_item) # Nr of drills
  303. self.ui.exc_cnc_tools_table.setItem(row_no, 3, nr_slots_item) # Nr of slots
  304. # ## REMEMBER: THIS COLUMN IS HIDDEN IN OBJECTUI.PY # ##
  305. self.ui.exc_cnc_tools_table.setItem(row_no, 4, tool_uid_item) # Tool unique ID)
  306. self.ui.exc_cnc_tools_table.setItem(row_no, 5, cutz_item)
  307. self.ui.exc_cnc_tools_table.setCellWidget(row_no, 6, plot_item)
  308. for row in range(tool_idx):
  309. self.ui.exc_cnc_tools_table.item(row, 0).setFlags(
  310. self.ui.exc_cnc_tools_table.item(row, 0).flags() ^ QtCore.Qt.ItemIsSelectable)
  311. self.ui.exc_cnc_tools_table.resizeColumnsToContents()
  312. self.ui.exc_cnc_tools_table.resizeRowsToContents()
  313. vertical_header = self.ui.exc_cnc_tools_table.verticalHeader()
  314. vertical_header.hide()
  315. self.ui.exc_cnc_tools_table.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
  316. horizontal_header = self.ui.exc_cnc_tools_table.horizontalHeader()
  317. horizontal_header.setMinimumSectionSize(10)
  318. horizontal_header.setDefaultSectionSize(70)
  319. horizontal_header.setSectionResizeMode(0, QtWidgets.QHeaderView.Fixed)
  320. horizontal_header.resizeSection(0, 20)
  321. horizontal_header.setSectionResizeMode(1, QtWidgets.QHeaderView.Stretch)
  322. horizontal_header.setSectionResizeMode(2, QtWidgets.QHeaderView.ResizeToContents)
  323. horizontal_header.setSectionResizeMode(3, QtWidgets.QHeaderView.ResizeToContents)
  324. horizontal_header.setSectionResizeMode(5, QtWidgets.QHeaderView.ResizeToContents)
  325. horizontal_header.setSectionResizeMode(6, QtWidgets.QHeaderView.Fixed)
  326. # horizontal_header.setStretchLastSection(True)
  327. self.ui.exc_cnc_tools_table.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
  328. self.ui.exc_cnc_tools_table.setColumnWidth(0, 20)
  329. self.ui.exc_cnc_tools_table.setColumnWidth(6, 17)
  330. self.ui.exc_cnc_tools_table.setMinimumHeight(self.ui.exc_cnc_tools_table.getHeight())
  331. self.ui.exc_cnc_tools_table.setMaximumHeight(self.ui.exc_cnc_tools_table.getHeight())
  332. def build_al_table(self):
  333. tool_idx = 0
  334. n = len(self.al_geometry_dict)
  335. self.ui.al_probe_points_table.setRowCount(n)
  336. for id_key, value in self.al_geometry_dict.items():
  337. tool_idx += 1
  338. row_no = tool_idx - 1
  339. t_id = QtWidgets.QTableWidgetItem('%d' % int(tool_idx))
  340. x = value['point'].x
  341. y = value['point'].y
  342. xy_coords = self.app.dec_format(x, dec=self.app.decimals), self.app.dec_format(y, dec=self.app.decimals)
  343. coords_item = QtWidgets.QTableWidgetItem(str(xy_coords))
  344. height = self.app.dec_format(value['height'], dec=self.app.decimals)
  345. height_item = QtWidgets.QTableWidgetItem(str(height))
  346. t_id.setFlags(QtCore.Qt.ItemIsEnabled)
  347. coords_item.setFlags(QtCore.Qt.ItemIsEnabled)
  348. height_item.setFlags(QtCore.Qt.ItemIsEnabled)
  349. self.ui.al_probe_points_table.setItem(row_no, 0, t_id) # Tool name/id
  350. self.ui.al_probe_points_table.setItem(row_no, 1, coords_item) # X-Y coords
  351. self.ui.al_probe_points_table.setItem(row_no, 2, height_item) # Determined Height
  352. self.ui.al_probe_points_table.resizeColumnsToContents()
  353. self.ui.al_probe_points_table.resizeRowsToContents()
  354. h_header = self.ui.al_probe_points_table.horizontalHeader()
  355. h_header.setMinimumSectionSize(10)
  356. h_header.setDefaultSectionSize(70)
  357. h_header.setSectionResizeMode(0, QtWidgets.QHeaderView.Fixed)
  358. h_header.resizeSection(0, 20)
  359. h_header.setSectionResizeMode(1, QtWidgets.QHeaderView.Stretch)
  360. h_header.setSectionResizeMode(2, QtWidgets.QHeaderView.ResizeToContents)
  361. self.ui.al_probe_points_table.setMinimumHeight(self.ui.al_probe_points_table.getHeight())
  362. self.ui.al_probe_points_table.setMaximumHeight(self.ui.al_probe_points_table.getHeight())
  363. if self.ui.al_probe_points_table.model().rowCount():
  364. self.ui.voronoi_cb.setDisabled(False)
  365. self.ui.grbl_get_heightmap_button.setDisabled(False)
  366. self.ui.h_gcode_button.setDisabled(False)
  367. self.ui.view_h_gcode_button.setDisabled(False)
  368. else:
  369. self.ui.voronoi_cb.setDisabled(True)
  370. self.ui.grbl_get_heightmap_button.setDisabled(True)
  371. self.ui.h_gcode_button.setDisabled(True)
  372. self.ui.view_h_gcode_button.setDisabled(True)
  373. def set_ui(self, ui):
  374. FlatCAMObj.set_ui(self, ui)
  375. log.debug("FlatCAMCNCJob.set_ui()")
  376. assert isinstance(self.ui, CNCObjectUI), \
  377. "Expected a CNCObjectUI, got %s" % type(self.ui)
  378. self.units = self.app.defaults['units'].upper()
  379. self.units_found = self.app.defaults['units']
  380. # this signal has to be connected to it's slot before the defaults are populated
  381. # the decision done in the slot has to override the default value set below
  382. # self.ui.toolchange_cb.toggled.connect(self.on_toolchange_custom_clicked)
  383. self.form_fields.update({
  384. "plot": self.ui.plot_cb,
  385. "tooldia": self.ui.tooldia_entry,
  386. # "append": self.ui.append_text,
  387. # "prepend": self.ui.prepend_text,
  388. # "toolchange_macro": self.ui.toolchange_text,
  389. # "toolchange_macro_enable": self.ui.toolchange_cb,
  390. "al_travelz": self.ui.ptravelz_entry,
  391. "al_probe_depth": self.ui.pdepth_entry,
  392. "al_probe_fr": self.ui.feedrate_probe_entry,
  393. "al_controller": self.ui.al_controller_combo,
  394. "al_mode": self.ui.al_mode_radio,
  395. "al_rows": self.ui.al_rows_entry,
  396. "al_columns": self.ui.al_columns_entry,
  397. "al_grbl_jog_step": self.ui.jog_step_entry,
  398. "al_grbl_jog_fr": self.ui.jog_fr_entry,
  399. })
  400. self.append_snippet = self.app.defaults['cncjob_append']
  401. self.prepend_snippet = self.app.defaults['cncjob_prepend']
  402. if self.append_snippet != '' or self.prepend_snippet:
  403. self.ui.snippets_cb.set_value(True)
  404. # Fill form fields only on object create
  405. self.to_form()
  406. # this means that the object that created this CNCJob was an Excellon or Geometry
  407. try:
  408. if self.travel_distance:
  409. self.ui.t_distance_label.show()
  410. self.ui.t_distance_entry.setVisible(True)
  411. self.ui.t_distance_entry.setDisabled(True)
  412. self.ui.t_distance_entry.set_value('%.*f' % (self.decimals, float(self.travel_distance)))
  413. self.ui.units_label.setText(str(self.units).lower())
  414. self.ui.units_label.setDisabled(True)
  415. self.ui.t_time_label.show()
  416. self.ui.t_time_entry.setVisible(True)
  417. self.ui.t_time_entry.setDisabled(True)
  418. # if time is more than 1 then we have minutes, else we have seconds
  419. if self.routing_time > 1:
  420. self.ui.t_time_entry.set_value('%.*f' % (self.decimals, math.ceil(float(self.routing_time))))
  421. self.ui.units_time_label.setText('min')
  422. else:
  423. time_r = self.routing_time * 60
  424. self.ui.t_time_entry.set_value('%.*f' % (self.decimals, math.ceil(float(time_r))))
  425. self.ui.units_time_label.setText('sec')
  426. self.ui.units_time_label.setDisabled(True)
  427. except AttributeError:
  428. pass
  429. if self.multitool is False:
  430. self.ui.tooldia_entry.show()
  431. self.ui.updateplot_button.show()
  432. else:
  433. self.ui.tooldia_entry.hide()
  434. self.ui.updateplot_button.hide()
  435. # set the kind of geometries are plotted by default with plot2() from camlib.CNCJob
  436. self.ui.cncplot_method_combo.set_value(self.app.defaults["cncjob_plot_kind"])
  437. try:
  438. self.ui.annotation_cb.stateChanged.disconnect(self.on_annotation_change)
  439. except (TypeError, AttributeError):
  440. pass
  441. self.ui.annotation_cb.stateChanged.connect(self.on_annotation_change)
  442. # set if to display text annotations
  443. self.ui.annotation_cb.set_value(self.app.defaults["cncjob_annotation"])
  444. self.ui.updateplot_button.clicked.connect(self.on_updateplot_button_click)
  445. self.ui.export_gcode_button.clicked.connect(self.on_exportgcode_button_click)
  446. self.ui.review_gcode_button.clicked.connect(self.on_edit_code_click)
  447. self.ui.editor_button.clicked.connect(lambda: self.app.object2editor())
  448. # autolevelling signals
  449. self.ui.sal_cb.stateChanged.connect(self.on_autolevelling)
  450. self.ui.al_mode_radio.activated_custom.connect(self.on_mode_radio)
  451. self.ui.al_controller_combo.currentIndexChanged.connect(self.on_controller_change)
  452. self.ui.voronoi_cb.stateChanged.connect(self.show_voronoi_diagram)
  453. # GRBL
  454. self.ui.com_search_button.clicked.connect(self.on_search_ports)
  455. self.ui.add_bd_button.clicked.connect(self.on_add_baudrate_grbl)
  456. self.ui.del_bd_button.clicked.connect(self.on_delete_baudrate_grbl)
  457. self.ui.controller_reset_button.clicked.connect(self.on_grbl_reset)
  458. self.ui.com_connect_button.clicked.connect(self.on_connect_grbl)
  459. self.ui.grbl_send_button.clicked.connect(self.on_send_grbl_command)
  460. self.ui.grbl_command_entry.returnPressed.connect(self.on_send_grbl_command)
  461. # Jog
  462. self.ui.jog_wdg.jog_up_button.clicked.connect(lambda: self.on_jog(direction='yplus'))
  463. self.ui.jog_wdg.jog_down_button.clicked.connect(lambda: self.on_jog(direction='yminus'))
  464. self.ui.jog_wdg.jog_right_button.clicked.connect(lambda: self.on_jog(direction='xplus'))
  465. self.ui.jog_wdg.jog_left_button.clicked.connect(lambda: self.on_jog(direction='xminus'))
  466. self.ui.jog_wdg.jog_z_up_button.clicked.connect(lambda: self.on_jog(direction='zplus'))
  467. self.ui.jog_wdg.jog_z_down_button.clicked.connect(lambda: self.on_jog(direction='zminus'))
  468. self.ui.jog_wdg.jog_origin_button.clicked.connect(lambda: self.on_jog(direction='origin'))
  469. # Zero
  470. self.ui.zero_axs_wdg.grbl_zerox_button.clicked.connect(lambda: self.on_grbl_zero(axis='x'))
  471. self.ui.zero_axs_wdg.grbl_zeroy_button.clicked.connect(lambda: self.on_grbl_zero(axis='y'))
  472. self.ui.zero_axs_wdg.grbl_zeroz_button.clicked.connect(lambda: self.on_grbl_zero(axis='z'))
  473. self.ui.zero_axs_wdg.grbl_zero_all_button.clicked.connect(lambda: self.on_grbl_zero(axis='all'))
  474. self.ui.zero_axs_wdg.grbl_homing_button.clicked.connect(self.on_grbl_homing)
  475. # Sender
  476. self.ui.grbl_report_button.clicked.connect(lambda: self.send_grbl_command(command='?'))
  477. self.ui.grbl_get_param_button.clicked.connect(
  478. lambda: self.get_grbl_parameter(param=self.ui.grbl_parameter_entry.get_value()))
  479. self.ui.view_h_gcode_button.clicked.connect(self.on_edit_probing_gcode)
  480. self.ui.h_gcode_button.clicked.connect(self.on_save_probing_gcode)
  481. self.ui.import_heights_button.clicked.connect(self.on_import_height_map)
  482. self.ui.pause_resume_button.clicked.connect(self.on_grbl_pause_resume)
  483. self.build_al_table_sig.connect(self.build_al_table)
  484. # self.ui.tc_variable_combo.currentIndexChanged[str].connect(self.on_cnc_custom_parameters)
  485. self.ui.cncplot_method_combo.activated_custom.connect(self.on_plot_kind_change)
  486. # Show/Hide Advanced Options
  487. if self.app.defaults["global_app_level"] == 'b':
  488. self.ui.level.setText(_(
  489. '<span style="color:green;"><b>Basic</b></span>'
  490. ))
  491. self.ui.sal_cb.hide()
  492. self.ui.sal_cb.set_value(False)
  493. else:
  494. self.ui.level.setText(_(
  495. '<span style="color:red;"><b>Advanced</b></span>'
  496. ))
  497. if 'Roland' in self.pp_excellon_name or 'Roland' in self.pp_geometry_name or 'hpgl' in \
  498. self.pp_geometry_name:
  499. self.ui.sal_cb.hide()
  500. self.ui.sal_cb.set_value(False)
  501. else:
  502. self.ui.sal_cb.show()
  503. self.ui.sal_cb.set_value(self.app.defaults["cncjob_al_status"])
  504. preamble = self.append_snippet
  505. postamble = self.prepend_snippet
  506. gc = self.export_gcode(preamble=preamble, postamble=postamble, to_file=True)
  507. self.source_file = gc.getvalue()
  508. self.ui.al_mode_radio.set_value(self.options['al_mode'])
  509. self.on_controller_change()
  510. # def on_cnc_custom_parameters(self, signal_text):
  511. # if signal_text == 'Parameters':
  512. # return
  513. # else:
  514. # self.ui.toolchange_text.insertPlainText('%%%s%%' % signal_text)
  515. def ui_connect(self):
  516. for row in range(self.ui.cnc_tools_table.rowCount()):
  517. self.ui.cnc_tools_table.cellWidget(row, 6).clicked.connect(self.on_plot_cb_click_table)
  518. for row in range(self.ui.exc_cnc_tools_table.rowCount()):
  519. self.ui.exc_cnc_tools_table.cellWidget(row, 6).clicked.connect(self.on_plot_cb_click_table)
  520. self.ui.plot_cb.stateChanged.connect(self.on_plot_cb_click)
  521. self.ui.al_add_button.clicked.connect(self.on_add_al_probepoints)
  522. self.ui.show_al_table.stateChanged.connect(self.on_show_al_table)
  523. def ui_disconnect(self):
  524. for row in range(self.ui.cnc_tools_table.rowCount()):
  525. try:
  526. self.ui.cnc_tools_table.cellWidget(row, 6).clicked.disconnect(self.on_plot_cb_click_table)
  527. except (TypeError, AttributeError):
  528. pass
  529. for row in range(self.ui.exc_cnc_tools_table.rowCount()):
  530. try:
  531. self.ui.exc_cnc_tools_table.cellWidget(row, 6).clicked.disconnect(self.on_plot_cb_click_table)
  532. except (TypeError, AttributeError):
  533. pass
  534. try:
  535. self.ui.plot_cb.stateChanged.disconnect(self.on_plot_cb_click)
  536. except (TypeError, AttributeError):
  537. pass
  538. try:
  539. self.ui.al_add_button.clicked.disconnect()
  540. except (TypeError, AttributeError):
  541. pass
  542. try:
  543. self.ui.show_al_table.stateChanged.disconnect()
  544. except (TypeError, AttributeError):
  545. pass
  546. def on_add_al_probepoints(self):
  547. # create the solid_geo
  548. self.solid_geo = unary_union([geo['geom'] for geo in self.gcode_parsed if geo['kind'][0] == 'C'])
  549. # reset al table
  550. self.ui.al_probe_points_table.setRowCount(0)
  551. # reset the al dict
  552. self.al_geometry_dict.clear()
  553. xmin, ymin, xmax, ymax = self.solid_geo.bounds
  554. if self.ui.al_mode_radio.get_value() == 'grid':
  555. width = abs(xmax - xmin)
  556. height = abs(ymax - ymin)
  557. cols = self.ui.al_columns_entry.get_value()
  558. rows = self.ui.al_rows_entry.get_value()
  559. dx = width / (cols + 1)
  560. dy = height / (rows + 1)
  561. points = []
  562. new_y = ymin
  563. for x in range(rows):
  564. new_y += dy
  565. new_x = xmin
  566. for y in range(cols):
  567. new_x += dx
  568. formatted_point = (
  569. self.app.dec_format(new_x, self.app.decimals),
  570. self.app.dec_format(new_y, self.app.decimals)
  571. )
  572. points.append(formatted_point)
  573. pt_id = 0
  574. pts_list = []
  575. for point in points:
  576. pt_id += 1
  577. pt = Point(point)
  578. pts_list.append(pt)
  579. new_dict = {
  580. 'point': pt,
  581. 'geo': None,
  582. 'height': 0.0
  583. }
  584. self.al_geometry_dict[pt_id] = deepcopy(new_dict)
  585. self.calculate_voronoi_diagram(pts=pts_list)
  586. # generate Probing GCode
  587. self.probing_gcode_text = self.probing_gcode()
  588. else:
  589. self.app.inform.emit(_("Click on canvas to add a Probe Point..."))
  590. if self.app.is_legacy is False:
  591. self.app.plotcanvas.graph_event_disconnect('key_press', self.app.ui.keyPressEvent)
  592. self.app.plotcanvas.graph_event_disconnect('mouse_press', self.app.on_mouse_click_over_plot)
  593. self.app.plotcanvas.graph_event_disconnect('mouse_release', self.app.on_mouse_click_release_over_plot)
  594. else:
  595. self.app.plotcanvas.graph_event_disconnect(self.app.kp)
  596. self.app.plotcanvas.graph_event_disconnect(self.app.mp)
  597. self.app.plotcanvas.graph_event_disconnect(self.app.mr)
  598. self.kp = self.app.plotcanvas.graph_event_connect('key_press', self.on_key_press)
  599. self.mr = self.app.plotcanvas.graph_event_connect('mouse_release', self.on_mouse_click_release)
  600. self.mouse_events_connected = True
  601. self.build_al_table_sig.emit()
  602. def show_voronoi_diagram(self, state):
  603. if state:
  604. pass
  605. else:
  606. pass
  607. def calculate_voronoi_diagram(self, pts):
  608. pts_union = MultiPoint(pts)
  609. env = self.solid_geo.envelope
  610. print(pts_union.wkt)
  611. try:
  612. voronoi_union = voronoi_diagram(geom=pts_union, envelope=env)
  613. print(voronoi_union)
  614. except Exception as e:
  615. log.debug("CNCJobObject.calculate_voronoi_diagram() --> %s" % str(e))
  616. return
  617. for pt_key in list(self.al_geometry_dict.keys()):
  618. for poly in voronoi_union:
  619. if self.al_geometry_dict[pt_key]['point'].within(poly):
  620. self.al_geometry_dict[pt_key]['geo'] = poly
  621. # To be called after clicking on the plot.
  622. def on_mouse_click_release(self, event):
  623. if self.app.is_legacy is False:
  624. event_pos = event.pos
  625. # event_is_dragging = event.is_dragging
  626. right_button = 2
  627. else:
  628. event_pos = (event.xdata, event.ydata)
  629. # event_is_dragging = self.app.plotcanvas.is_dragging
  630. right_button = 3
  631. try:
  632. x = float(event_pos[0])
  633. y = float(event_pos[1])
  634. except TypeError:
  635. return
  636. event_pos = (x, y)
  637. # do paint single only for left mouse clicks
  638. if event.button == 1:
  639. pos = self.app.plotcanvas.translate_coords(event_pos)
  640. # use the snapped position as reference
  641. snapped_pos = self.app.geo_editor.snap(pos[0], pos[1])
  642. if not self.al_geometry_dict:
  643. new_dict = {
  644. 'point': Point(snapped_pos),
  645. 'geo': None,
  646. 'height': 0.0
  647. }
  648. self.al_geometry_dict[1] = deepcopy(new_dict)
  649. else:
  650. int_keys = [int(k) for k in self.al_geometry_dict.keys()]
  651. new_id = max(int_keys) + 1
  652. new_dict = {
  653. 'point': Point(snapped_pos),
  654. 'geo': None,
  655. 'height': 0.0
  656. }
  657. self.al_geometry_dict[new_id] = deepcopy(new_dict)
  658. # rebuild the al table
  659. self.build_al_table_sig.emit()
  660. self.app.inform.emit(_("Added a Probe Point... Click again to add another or right click to finish ..."))
  661. # if RMB then we exit
  662. elif event.button == right_button and self.mouse_is_dragging is False:
  663. if self.app.is_legacy is False:
  664. self.app.plotcanvas.graph_event_disconnect('key_press', self.on_key_press)
  665. self.app.plotcanvas.graph_event_disconnect('mouse_release', self.on_mouse_click_release)
  666. else:
  667. self.app.plotcanvas.graph_event_disconnect(self.kp)
  668. self.app.plotcanvas.graph_event_disconnect(self.mr)
  669. self.app.kp = self.app.plotcanvas.graph_event_connect('key_press', self.app.ui.keyPressEvent)
  670. self.app.mp = self.app.plotcanvas.graph_event_connect('mouse_press', self.app.on_mouse_click_over_plot)
  671. self.app.mr = self.app.plotcanvas.graph_event_connect('mouse_release',
  672. self.app.on_mouse_click_release_over_plot)
  673. # signal that the mouse events are disconnected from local methods
  674. self.mouse_events_connected = False
  675. # restore selection
  676. self.app.defaults['global_selection_shape'] = self.old_selection_state
  677. self.app.inform.emit(_("Finished adding Probe Points..."))
  678. pts_list = []
  679. for k in self.al_geometry_dict:
  680. pts_list.append(self.al_geometry_dict[k]['point'])
  681. self.calculate_voronoi_diagram(pts=pts_list)
  682. self.probing_gcode_text = self.probing_gcode()
  683. # rebuild the al table
  684. self.build_al_table_sig.emit()
  685. def on_key_press(self, event):
  686. # events out of the self.app.collection view (it's about Project Tab) are of type int
  687. if type(event) is int:
  688. key = event
  689. # events from the GUI are of type QKeyEvent
  690. elif type(event) == QtGui.QKeyEvent:
  691. key = event.key()
  692. elif isinstance(event, mpl_key_event): # MatPlotLib key events are trickier to interpret than the rest
  693. key = event.key
  694. key = QtGui.QKeySequence(key)
  695. # check for modifiers
  696. key_string = key.toString().lower()
  697. if '+' in key_string:
  698. mod, __, key_text = key_string.rpartition('+')
  699. if mod.lower() == 'ctrl':
  700. # modifiers = QtCore.Qt.ControlModifier
  701. pass
  702. elif mod.lower() == 'alt':
  703. # modifiers = QtCore.Qt.AltModifier
  704. pass
  705. elif mod.lower() == 'shift':
  706. # modifiers = QtCore.Qt.ShiftModifier
  707. pass
  708. else:
  709. # modifiers = QtCore.Qt.NoModifier
  710. pass
  711. key = QtGui.QKeySequence(key_text)
  712. # events from Vispy are of type KeyEvent
  713. else:
  714. key = event.key
  715. # Escape = Deselect All
  716. if key == QtCore.Qt.Key_Escape or key == 'Escape':
  717. if self.mouse_events_connected is True:
  718. self.mouse_events_connected = False
  719. if self.app.is_legacy is False:
  720. self.app.plotcanvas.graph_event_disconnect('key_press', self.on_key_press)
  721. self.app.plotcanvas.graph_event_disconnect('mouse_release', self.on_mouse_click_release)
  722. else:
  723. self.app.plotcanvas.graph_event_disconnect(self.kp)
  724. self.app.plotcanvas.graph_event_disconnect(self.mr)
  725. self.app.kp = self.app.plotcanvas.graph_event_connect('key_press', self.app.ui.keyPressEvent)
  726. self.app.mp = self.app.plotcanvas.graph_event_connect('mouse_press', self.app.on_mouse_click_over_plot)
  727. self.app.mr = self.app.plotcanvas.graph_event_connect('mouse_release',
  728. self.app.on_mouse_click_release_over_plot)
  729. if self.ui.big_cursor_cb.get_value():
  730. # restore cursor
  731. self.app.on_cursor_type(val=self.old_cursor_type)
  732. # restore selection
  733. self.app.defaults['global_selection_shape'] = self.old_selection_state
  734. # Grid toggle
  735. if key == QtCore.Qt.Key_G or key == 'G':
  736. self.app.ui.grid_snap_btn.trigger()
  737. # Jump to coords
  738. if key == QtCore.Qt.Key_J or key == 'J':
  739. l_x, l_y = self.app.on_jump_to()
  740. def on_autolevelling(self, state):
  741. self.ui.al_frame.show() if state else self.ui.al_frame.hide()
  742. self.app.defaults["cncjob_al_status"] = True if state else False
  743. def on_show_al_table(self, state):
  744. self.ui.al_probe_points_table.show() if state else self.ui.al_probe_points_table.hide()
  745. def on_mode_radio(self, val):
  746. # reset al table
  747. self.ui.al_probe_points_table.setRowCount(0)
  748. # reset the al dict
  749. self.al_geometry_dict.clear()
  750. # build AL table
  751. self.build_al_table()
  752. if val == "manual":
  753. self.ui.al_rows_entry.setDisabled(True)
  754. self.ui.al_rows_label.setDisabled(True)
  755. self.ui.al_columns_entry.setDisabled(True)
  756. self.ui.al_columns_label.setDisabled(True)
  757. else:
  758. self.ui.al_rows_entry.setDisabled(False)
  759. self.ui.al_rows_label.setDisabled(False)
  760. self.ui.al_columns_entry.setDisabled(False)
  761. self.ui.al_columns_label.setDisabled(False)
  762. def on_controller_change(self):
  763. if self.ui.al_controller_combo.get_value() == 'GRBL':
  764. self.ui.h_gcode_button.hide()
  765. self.ui.view_h_gcode_button.hide()
  766. self.ui.import_heights_button.hide()
  767. self.ui.grbl_frame.show()
  768. self.on_search_ports(muted=True)
  769. else:
  770. self.ui.h_gcode_button.show()
  771. self.ui.view_h_gcode_button.show()
  772. self.ui.import_heights_button.show()
  773. self.ui.grbl_frame.hide()
  774. # if the is empty then there is a chance that we've added probe points but the GRBL controller was selected
  775. # therefore no Probing GCode was genrated (it is different for GRBL on how it gets it's Probing GCode
  776. if not self.probing_gcode_text or self.probing_gcode_text == '':
  777. # generate Probing GCode
  778. self.probing_gcode_text = self.probing_gcode()
  779. def list_serial_ports(self):
  780. """
  781. Lists serial port names.
  782. From here: https://stackoverflow.com/questions/12090503/listing-available-com-ports-with-python
  783. :raises EnvironmentError: On unsupported or unknown platforms
  784. :returns: A list of the serial ports available on the system
  785. """
  786. if sys.platform.startswith('win'):
  787. ports = ['COM%s' % (i + 1) for i in range(256)]
  788. elif sys.platform.startswith('linux') or sys.platform.startswith('cygwin'):
  789. # this excludes your current terminal "/dev/tty"
  790. ports = glob.glob('/dev/tty[A-Za-z]*')
  791. elif sys.platform.startswith('darwin'):
  792. ports = glob.glob('/dev/tty.*')
  793. else:
  794. raise EnvironmentError('Unsupported platform')
  795. result = []
  796. s = serial.Serial()
  797. for port in ports:
  798. s.port = port
  799. try:
  800. s.open()
  801. s.close()
  802. result.append(port)
  803. except (OSError, serial.SerialException):
  804. # result.append(port + " (in use)")
  805. pass
  806. return result
  807. def on_search_ports(self, muted=None):
  808. port_list = self.list_serial_ports()
  809. self.ui.com_list_combo.clear()
  810. self.ui.com_list_combo.addItems(port_list)
  811. if muted is not True:
  812. self.app.inform.emit('[WARNING_NOTCL] %s' % _("COM list updated ..."))
  813. def on_connect_grbl(self):
  814. port_name = self.ui.com_list_combo.currentText()
  815. if " (" in port_name:
  816. port_name = port_name.rpartition(" (")[0]
  817. baudrate = int(self.ui.baudrates_list_combo.currentText())
  818. try:
  819. self.grbl_ser_port = serial.serial_for_url(port_name, baudrate,
  820. bytesize=serial.EIGHTBITS,
  821. parity=serial.PARITY_NONE,
  822. stopbits=serial.STOPBITS_ONE,
  823. timeout=0.1,
  824. xonxoff=False,
  825. rtscts=False)
  826. # Toggle DTR to reset the controller loaded with GRBL (Arduino, ESP32, etc)
  827. try:
  828. self.grbl_ser_port.dtr = False
  829. except IOError:
  830. pass
  831. self.grbl_ser_port.reset_input_buffer()
  832. try:
  833. self.grbl_ser_port.dtr = True
  834. except IOError:
  835. pass
  836. answer = self.wake_grbl()
  837. answer = ['ok'] # hack for development without a GRBL controller connected
  838. for line in answer:
  839. if 'ok' in line.lower():
  840. self.ui.com_connect_button.setStyleSheet("QPushButton {background-color: seagreen;}")
  841. self.ui.com_connect_button.setText(_("Connected"))
  842. self.ui.controller_reset_button.setDisabled(False)
  843. for idx in range(self.ui.al_toolbar.count()):
  844. if self.ui.al_toolbar.tabText(idx) == _("Connect"):
  845. self.ui.al_toolbar.tabBar.setTabTextColor(idx, QtGui.QColor('seagreen'))
  846. if self.ui.al_toolbar.tabText(idx) == _("Control"):
  847. self.ui.al_toolbar.tabBar.setTabEnabled(idx, True)
  848. if self.ui.al_toolbar.tabText(idx) == _("Sender"):
  849. self.ui.al_toolbar.tabBar.setTabEnabled(idx, True)
  850. self.app.inform.emit("%s: %s" % (_("Port connected"), port_name))
  851. return
  852. self.grbl_ser_port.close()
  853. self.app.inform.emit("[ERROR_NOTCL] %s: %s" % (_("Could not connect to GRBL on port"), port_name))
  854. except serial.SerialException:
  855. self.grbl_ser_port = serial.Serial()
  856. self.grbl_ser_port.port = port_name
  857. self.grbl_ser_port.close()
  858. self.ui.com_connect_button.setStyleSheet("QPushButton {background-color: red;}")
  859. self.ui.com_connect_button.setText(_("Disconnected"))
  860. self.ui.controller_reset_button.setDisabled(True)
  861. for idx in range(self.ui.al_toolbar.count()):
  862. if self.ui.al_toolbar.tabText(idx) == _("Connect"):
  863. self.ui.al_toolbar.tabBar.setTabTextColor(idx, QtGui.QColor('red'))
  864. if self.ui.al_toolbar.tabText(idx) == _("Control"):
  865. self.ui.al_toolbar.tabBar.setTabEnabled(idx, False)
  866. if self.ui.al_toolbar.tabText(idx) == _("Sender"):
  867. self.ui.al_toolbar.tabBar.setTabEnabled(idx, False)
  868. self.app.inform.emit("%s: %s" % (_("Port is connected. Disconnecting"), port_name))
  869. except Exception:
  870. self.app.inform.emit("[ERROR_NOTCL] %s: %s" % (_("Could not connect to port"), port_name))
  871. def on_add_baudrate_grbl(self):
  872. new_bd = str(self.ui.new_baudrate_entry.get_value())
  873. if int(new_bd) >= 40 and new_bd not in self.ui.baudrates_list_combo.model().stringList():
  874. self.ui.baudrates_list_combo.addItem(new_bd)
  875. self.ui.baudrates_list_combo.setCurrentText(new_bd)
  876. def on_delete_baudrate_grbl(self):
  877. current_idx = self.ui.baudrates_list_combo.currentIndex()
  878. self.ui.baudrates_list_combo.removeItem(current_idx)
  879. def wake_grbl(self):
  880. # Wake up grbl
  881. self.grbl_ser_port.write("\r\n\r\n".encode('utf-8'))
  882. # Wait for GRBL controller to initialize
  883. time.sleep(1)
  884. grbl_out = deepcopy(self.grbl_ser_port.readlines())
  885. self.grbl_ser_port.reset_input_buffer()
  886. return grbl_out
  887. def on_send_grbl_command(self):
  888. cmd = self.ui.grbl_command_entry.get_value()
  889. # show the Shell Dock
  890. self.app.ui.shell_dock.show()
  891. def worker_task():
  892. with self.app.proc_container.new(_("Sending GCode...")):
  893. self.send_grbl_command(command=cmd)
  894. self.app.worker_task.emit({'fcn': worker_task, 'params': []})
  895. def send_grbl_command(self, command, echo=True):
  896. stripped_cmd = command.strip()
  897. cmd = stripped_cmd.rpartition('\n')[0]
  898. if echo:
  899. self.app.inform_shell[str, bool].emit(cmd, False)
  900. # Send Gcode command to GRBL
  901. snd = cmd + '\n'
  902. self.grbl_ser_port.write(snd.encode('utf-8'))
  903. grbl_out = self.grbl_ser_port.readlines()
  904. result = False
  905. for line in grbl_out:
  906. if echo:
  907. try:
  908. self.app.inform_shell.emit(' : ' + line.decode('utf-8').strip().upper())
  909. except Exception as e:
  910. log.debug("CNCJobObject.send_grbl_command() --> %s" % str(e))
  911. if 'ok' in line:
  912. result = True
  913. return result
  914. def send_grbl_block(self, command, echo=True):
  915. stripped_cmd = command.strip()
  916. for l in stripped_cmd.split('\n'):
  917. if echo:
  918. self.app.inform_shell[str, bool].emit(l, False)
  919. # Send Gcode block to GRBL
  920. snd = l + '\n'
  921. self.grbl_ser_port.write(snd.encode('utf-8'))
  922. grbl_out = self.grbl_ser_port.readlines()
  923. for line in grbl_out:
  924. if echo:
  925. try:
  926. self.app.inform_shell.emit(' : ' + line.decode('utf-8').strip().upper())
  927. except Exception as e:
  928. log.debug("CNCJobObject.send_grbl_block() --> %s" % str(e))
  929. def get_grbl_parameter(self, param):
  930. if '$' in param:
  931. param = param.replace('$','')
  932. snd = '$$\n'
  933. self.grbl_ser_port.write(snd.encode('utf-8'))
  934. grbl_out = self.grbl_ser_port.readlines()
  935. for line in grbl_out:
  936. decoded_line = line.decode('utf-8')
  937. par = '$%s' % str(param)
  938. if par in decoded_line:
  939. result = float(decoded_line.rpartition('=')[2])
  940. self.app.shell_message("GRBL Parameter: %s = %s" % (str(param), str(result)), show=True)
  941. return result
  942. def on_jog(self, direction=None):
  943. if direction is None:
  944. return
  945. cmd = ''
  946. step = self.ui.jog_step_entry.get_value(),
  947. feedrate = self.ui.jog_fr_entry.get_value()
  948. travelz = float(self.app.defaults["cncjob_al_grbl_travelz"])
  949. if direction == 'xplus':
  950. cmd = "$J=G91 %s X%s F%s" % ({'IN': 'G20', 'MM': 'G21'}[self.units], str(step), str(feedrate))
  951. if direction == 'xminus':
  952. cmd = "$J=G91 %s X-%s F%s" % ({'IN': 'G20', 'MM': 'G21'}[self.units], str(step), str(feedrate))
  953. if direction == 'yplus':
  954. cmd = "$J=G91 %s Y%s F%s" % ({'IN': 'G20', 'MM': 'G21'}[self.units], str(step), str(feedrate))
  955. if direction == 'yminus':
  956. cmd = "$J=G91 %s Y-%s F%s" % ({'IN': 'G20', 'MM': 'G21'}[self.units], str(step), str(feedrate))
  957. if direction == 'zplus':
  958. cmd = "$J=G91 %s Z%s F%s" % ({'IN': 'G20', 'MM': 'G21'}[self.units], str(step), str(feedrate))
  959. if direction == 'zminus':
  960. cmd = "$J=G91 %s Z-%s F%s" % ({'IN': 'G20', 'MM': 'G21'}[self.units], str(step), str(feedrate))
  961. if direction == 'origin':
  962. cmd = "$J=G90 %s Z%s F%s" % ({'IN': 'G20', 'MM': 'G21'}[self.units], str(travelz), str(feedrate))
  963. self.send_grbl_command(command=cmd, echo=False)
  964. cmd = "$J=G90 %s X0.0 Y0.0 F%s" % ({'IN': 'G20', 'MM': 'G21'}[self.units], str(feedrate))
  965. self.send_grbl_command(command=cmd, echo=False)
  966. return
  967. self.send_grbl_command(command=cmd, echo=False)
  968. def on_grbl_zero(self, axis):
  969. current_mode = self.get_grbl_parameter('10')
  970. if current_mode is None:
  971. return
  972. cmd = '$10=0'
  973. self.send_grbl_command(command=cmd, echo=False)
  974. if axis == 'x':
  975. cmd = 'G10 L2 P1 X0'
  976. elif axis == 'y':
  977. cmd = 'G10 L2 P1 Y0'
  978. elif axis == 'z':
  979. cmd = 'G10 L2 P1 Z0'
  980. else:
  981. # all
  982. cmd = 'G10 L2 P1 X0 Y0 Z0'
  983. self.send_grbl_command(command=cmd, echo=False)
  984. # restore previous mode
  985. cmd = '$10=%d' % int(current_mode)
  986. self.send_grbl_command(command=cmd, echo=False)
  987. def on_grbl_homing(self):
  988. cmd = '$H'
  989. self.app.inform.emit("%s" % _("GRBL is doing a home cycle."))
  990. self.wake_grbl()
  991. self.send_grbl_command(command=cmd)
  992. def on_grbl_reset(self):
  993. cmd = '\x18'
  994. self.app.inform.emit("%s" % _("GRBL software reset was sent."))
  995. self.wake_grbl()
  996. self.send_grbl_command(command=cmd)
  997. def on_grbl_pause_resume(self, checked):
  998. if checked is False:
  999. cmd = '~'
  1000. self.send_grbl_command(command=cmd)
  1001. self.app.inform.emit("%s" % _("GRBL resumed."))
  1002. else:
  1003. cmd = '!'
  1004. self.send_grbl_command(command=cmd)
  1005. self.app.inform.emit("%s" % _("GRBL paused."))
  1006. def probing_gcode(self):
  1007. """
  1008. :return: Probing GCode
  1009. :rtype: str
  1010. """
  1011. p_gcode = ''
  1012. header = ''
  1013. time_str = "{:%A, %d %B %Y at %H:%M}".format(datetime.now())
  1014. coords = []
  1015. for id_key, value in self.al_geometry_dict.items():
  1016. x = value['point'].x
  1017. y = value['point'].y
  1018. coords.append(
  1019. (
  1020. self.app.dec_format(x, dec=self.app.decimals),
  1021. self.app.dec_format(y, dec=self.app.decimals)
  1022. )
  1023. )
  1024. pr_travel = self.ui.ptravelz_entry.get_value()
  1025. probe_fr = self.ui.feedrate_probe_entry.get_value()
  1026. pr_depth = self.ui.pdepth_entry.get_value()
  1027. controller = self.ui.al_controller_combo.get_value()
  1028. header += '(G-CODE GENERATED BY FLATCAM v%s - www.flatcam.org - Version Date: %s)\n' % \
  1029. (str(self.app.version), str(self.app.version_date)) + '\n'
  1030. header += '(This is a autolevelling probing GCode.)\n' \
  1031. '(Make sure that before you start the job you first do a zero for all axis.)\n\n'
  1032. header += '(Name: ' + str(self.options['name']) + ')\n'
  1033. header += '(Type: ' + "Autolevelling Probing GCode " + ')\n'
  1034. header += '(Units: ' + self.units.upper() + ')\n'
  1035. header += '(Created on ' + time_str + ')\n'
  1036. # commands
  1037. if controller == 'MACH3':
  1038. probing_command = 'G31'
  1039. # probing_var = '#2002'
  1040. openfile_command = 'M40'
  1041. closefile_command = 'M41'
  1042. elif controller == 'MACH4':
  1043. probing_command = 'G31'
  1044. # probing_var = '#5063'
  1045. openfile_command = 'M40'
  1046. closefile_command = 'M41'
  1047. elif controller == 'LinuxCNC':
  1048. probing_command = 'G38.2'
  1049. # probing_var = '#5422'
  1050. openfile_command = '(PROBEOPEN a_probing_points_file.txt)'
  1051. closefile_command = '(PROBECLOSE)'
  1052. elif controller == 'GRBL':
  1053. # do nothing here because the Probing GCode for GRBL is obtained differently
  1054. return
  1055. else:
  1056. log.debug("CNCJobObject.probing_gcode() -> controller not supported")
  1057. return
  1058. # #############################################################################################################
  1059. # ########################### GCODE construction ##############################################################
  1060. # #############################################################################################################
  1061. # header
  1062. p_gcode += header + '\n'
  1063. # supplementary message for LinuxCNC
  1064. if controller == 'LinuxCNC':
  1065. p_gcode += "The file with the stored probing points can be found\n" \
  1066. "in the configuration folder for LinuxCNC.\n" \
  1067. "The name of the file is: a_probing_points_file.txt.\n"
  1068. # units
  1069. p_gcode += 'G21\n' if self.units == 'MM' else 'G20\n'
  1070. # reference mode = absolute
  1071. p_gcode += 'G90\n'
  1072. # open a new file
  1073. p_gcode += openfile_command + '\n'
  1074. # move to safe height (probe travel Z)
  1075. p_gcode += 'G0 Z%s\n' % str(self.app.dec_format(pr_travel, self.coords_decimals))
  1076. # probing points
  1077. for idx, xy_tuple in enumerate(coords, 1): # index starts from 1
  1078. x = xy_tuple[0]
  1079. y = xy_tuple[1]
  1080. # move to probing point
  1081. p_gcode += "G0 X%sY%s\n" % (
  1082. str(self.app.dec_format(x, self.coords_decimals)),
  1083. str(self.app.dec_format(y, self.coords_decimals))
  1084. )
  1085. # do the probing
  1086. p_gcode += "%s Z%s F%s\n" % (
  1087. probing_command,
  1088. str(self.app.dec_format(pr_depth, self.coords_decimals)),
  1089. str(self.app.dec_format(probe_fr, self.fr_decimals)),
  1090. )
  1091. # store in a global numeric variable the value of the detected probe Z
  1092. # I offset the global numeric variable by 500 so it does not conflict with something else
  1093. # temp_var = int(idx + 500)
  1094. # p_gcode += "#%d = %s\n" % (temp_var, probing_var)
  1095. # move to safe height (probe travel Z)
  1096. p_gcode += 'G0 Z%s\n' % str(self.app.dec_format(pr_travel, self.coords_decimals))
  1097. # close the file
  1098. p_gcode += closefile_command + '\n'
  1099. # finish the GCode
  1100. p_gcode += 'M2'
  1101. return p_gcode
  1102. def on_save_probing_gcode(self):
  1103. lines = StringIO(self.probing_gcode_text)
  1104. _filter_ = self.app.defaults['cncjob_save_filters']
  1105. name = "probing_gcode"
  1106. try:
  1107. dir_file_to_save = self.app.get_last_save_folder() + '/' + str(name)
  1108. filename, _f = FCFileSaveDialog.get_saved_filename(
  1109. caption=_("Export Code ..."),
  1110. directory=dir_file_to_save,
  1111. ext_filter=_filter_
  1112. )
  1113. except TypeError:
  1114. filename, _f = FCFileSaveDialog.get_saved_filename(caption=_("Export Code ..."), ext_filter=_filter_)
  1115. if filename == '':
  1116. self.app.inform.emit('[WARNING_NOTCL] %s' % _("Export cancelled ..."))
  1117. return
  1118. else:
  1119. try:
  1120. force_windows_line_endings = self.app.defaults['cncjob_line_ending']
  1121. if force_windows_line_endings and sys.platform != 'win32':
  1122. with open(filename, 'w', newline='\r\n') as f:
  1123. for line in lines:
  1124. f.write(line)
  1125. else:
  1126. with open(filename, 'w') as f:
  1127. for line in lines:
  1128. f.write(line)
  1129. except FileNotFoundError:
  1130. self.app.inform.emit('[WARNING_NOTCL] %s' % _("No such file or directory"))
  1131. return
  1132. except PermissionError:
  1133. self.app.inform.emit(
  1134. '[WARNING] %s' % _("Permission denied, saving not possible.\n"
  1135. "Most likely another app is holding the file open and not accessible.")
  1136. )
  1137. return 'fail'
  1138. def on_edit_probing_gcode(self):
  1139. self.app.proc_container.view.set_busy(_("Loading..."))
  1140. gco = self.probing_gcode_text
  1141. if gco is None or gco == '':
  1142. self.app.inform.emit('[WARNING_NOTCL] %s...' % _('There is nothing to view'))
  1143. return
  1144. self.gcode_viewer_tab = AppTextEditor(app=self.app, plain_text=True)
  1145. # add the tab if it was closed
  1146. self.app.ui.plot_tab_area.addTab(self.gcode_viewer_tab, '%s' % _("Code Viewer"))
  1147. self.gcode_viewer_tab.setObjectName('code_viewer_tab')
  1148. # delete the absolute and relative position and messages in the infobar
  1149. self.app.ui.position_label.setText("")
  1150. self.app.ui.rel_position_label.setText("")
  1151. self.gcode_viewer_tab.code_editor.completer_enable = False
  1152. self.gcode_viewer_tab.buttonRun.hide()
  1153. # Switch plot_area to CNCJob tab
  1154. self.app.ui.plot_tab_area.setCurrentWidget(self.gcode_viewer_tab)
  1155. self.gcode_viewer_tab.t_frame.hide()
  1156. # then append the text from GCode to the text editor
  1157. try:
  1158. self.gcode_viewer_tab.load_text(gco, move_to_start=True, clear_text=True)
  1159. except Exception as e:
  1160. log.debug('FlatCAMCNCJob.on_edit_code_click() -->%s' % str(e))
  1161. return
  1162. self.gcode_viewer_tab.t_frame.show()
  1163. self.app.proc_container.view.set_idle()
  1164. self.gcode_viewer_tab.buttonSave.hide()
  1165. self.gcode_viewer_tab.buttonOpen.hide()
  1166. self.gcode_viewer_tab.buttonPrint.hide()
  1167. self.gcode_viewer_tab.buttonPreview.hide()
  1168. self.gcode_viewer_tab.buttonReplace.hide()
  1169. self.gcode_viewer_tab.sel_all_cb.hide()
  1170. self.gcode_viewer_tab.entryReplace.hide()
  1171. self.gcode_viewer_tab.button_update_code.show()
  1172. # self.gcode_viewer_tab.code_editor.setReadOnly(True)
  1173. self.gcode_viewer_tab.button_update_code.clicked.connect(self.on_update_probing_gcode)
  1174. self.app.inform.emit('[success] %s...' % _('Loaded Machine Code into Code Viewer'))
  1175. def on_update_probing_gcode(self):
  1176. self.probing_gcode_text = self.gcode_viewer_tab.code_editor.toPlainText()
  1177. def on_import_height_map(self):
  1178. """
  1179. Import the height map file into the app
  1180. :return:
  1181. :rtype:
  1182. """
  1183. _filter_ = "Text File .txt (*.txt);;All Files (*.*)"
  1184. try:
  1185. filename, _f = QtWidgets.QFileDialog.getOpenFileName(caption=_("Import Height Map"),
  1186. directory=self.app.get_last_folder(),
  1187. filter=_filter_)
  1188. except TypeError:
  1189. filename, _f = QtWidgets.QFileDialog.getOpenFileName(caption=_("Import Height Map"),
  1190. filter=_filter_)
  1191. filename = str(filename)
  1192. if filename == '':
  1193. self.app.inform.emit('[WARNING_NOTCL] %s' % _("Cancelled."))
  1194. else:
  1195. self.app.worker_task.emit({'fcn': self.import_height_map, 'params': [filename]})
  1196. def import_height_map(self, filename):
  1197. """
  1198. :param filename:
  1199. :type filename:
  1200. :return:
  1201. :rtype:
  1202. """
  1203. stream = ''
  1204. try:
  1205. if filename:
  1206. with open(filename, 'r') as f:
  1207. stream = f.readlines()
  1208. else:
  1209. return
  1210. except IOError:
  1211. log.error("Failed to open height map file: %s" % filename)
  1212. self.inform.emit('[ERROR_NOTCL] %s: %s' % (_("Failed to open height map file"), filename))
  1213. return
  1214. idx = 0
  1215. for line in stream:
  1216. if line != '':
  1217. idx += 1
  1218. line = line.replace(' ', ',').replace('\n', '').split(',')
  1219. if idx not in self.al_geometry_dict:
  1220. self.al_geometry_dict[idx] = {}
  1221. self.al_geometry_dict[idx]['height'] = float(line[2])
  1222. if 'point' not in self.al_geometry_dict[idx]:
  1223. x = float(line[0])
  1224. y = float(line[1])
  1225. self.al_geometry_dict[idx]['point'] = Point((x, y))
  1226. self.build_al_table_sig.emit()
  1227. def on_updateplot_button_click(self, *args):
  1228. """
  1229. Callback for the "Updata Plot" button. Reads the form for updates
  1230. and plots the object.
  1231. """
  1232. self.read_form()
  1233. self.on_plot_kind_change()
  1234. def on_plot_kind_change(self):
  1235. kind = self.ui.cncplot_method_combo.get_value()
  1236. def worker_task():
  1237. with self.app.proc_container.new(_("Plotting...")):
  1238. self.plot(kind=kind)
  1239. self.app.worker_task.emit({'fcn': worker_task, 'params': []})
  1240. def on_exportgcode_button_click(self):
  1241. """
  1242. Handler activated by a button clicked when exporting GCode.
  1243. :param args:
  1244. :return:
  1245. """
  1246. self.app.defaults.report_usage("cncjob_on_exportgcode_button")
  1247. self.read_form()
  1248. name = self.app.collection.get_active().options['name']
  1249. save_gcode = False
  1250. if 'Roland' in self.pp_excellon_name or 'Roland' in self.pp_geometry_name:
  1251. _filter_ = "RML1 Files .rol (*.rol);;All Files (*.*)"
  1252. elif 'hpgl' in self.pp_geometry_name:
  1253. _filter_ = "HPGL Files .plt (*.plt);;All Files (*.*)"
  1254. else:
  1255. save_gcode = True
  1256. _filter_ = self.app.defaults['cncjob_save_filters']
  1257. try:
  1258. dir_file_to_save = self.app.get_last_save_folder() + '/' + str(name)
  1259. filename, _f = FCFileSaveDialog.get_saved_filename(
  1260. caption=_("Export Code ..."),
  1261. directory=dir_file_to_save,
  1262. ext_filter=_filter_
  1263. )
  1264. except TypeError:
  1265. filename, _f = FCFileSaveDialog.get_saved_filename(caption=_("Export Code ..."), ext_filter=_filter_)
  1266. self.export_gcode_handler(filename, is_gcode=save_gcode)
  1267. def export_gcode_handler(self, filename, is_gcode=True):
  1268. preamble = ''
  1269. postamble = ''
  1270. filename = str(filename)
  1271. if filename == '':
  1272. self.app.inform.emit('[WARNING_NOTCL] %s' % _("Export cancelled ..."))
  1273. return
  1274. else:
  1275. if is_gcode is True:
  1276. used_extension = filename.rpartition('.')[2]
  1277. self.update_filters(last_ext=used_extension, filter_string='cncjob_save_filters')
  1278. new_name = os.path.split(str(filename))[1].rpartition('.')[0]
  1279. self.ui.name_entry.set_value(new_name)
  1280. self.on_name_activate(silent=True)
  1281. try:
  1282. if self.ui.snippets_cb.get_value():
  1283. preamble = self.append_snippet
  1284. postamble = self.prepend_snippet
  1285. gc = self.export_gcode(filename, preamble=preamble, postamble=postamble)
  1286. except Exception as err:
  1287. log.debug("CNCJobObject.export_gcode_handler() --> %s" % str(err))
  1288. gc = self.export_gcode(filename)
  1289. if gc == 'fail':
  1290. return
  1291. if self.app.defaults["global_open_style"] is False:
  1292. self.app.file_opened.emit("gcode", filename)
  1293. self.app.file_saved.emit("gcode", filename)
  1294. self.app.inform.emit('[success] %s: %s' % (_("File saved to"), filename))
  1295. def on_edit_code_click(self, *args):
  1296. """
  1297. Handler activated by a button clicked when reviewing GCode.
  1298. :param args:
  1299. :return:
  1300. """
  1301. self.app.proc_container.view.set_busy(_("Loading..."))
  1302. preamble = self.append_snippet
  1303. postamble = self.prepend_snippet
  1304. gco = self.export_gcode(preamble=preamble, postamble=postamble, to_file=True)
  1305. if gco == 'fail':
  1306. return
  1307. else:
  1308. self.app.gcode_edited = gco
  1309. self.gcode_editor_tab = AppTextEditor(app=self.app, plain_text=True)
  1310. # add the tab if it was closed
  1311. self.app.ui.plot_tab_area.addTab(self.gcode_editor_tab, '%s' % _("Code Review"))
  1312. self.gcode_editor_tab.setObjectName('code_editor_tab')
  1313. # delete the absolute and relative position and messages in the infobar
  1314. self.app.ui.position_label.setText("")
  1315. self.app.ui.rel_position_label.setText("")
  1316. self.gcode_editor_tab.code_editor.completer_enable = False
  1317. self.gcode_editor_tab.buttonRun.hide()
  1318. # Switch plot_area to CNCJob tab
  1319. self.app.ui.plot_tab_area.setCurrentWidget(self.gcode_editor_tab)
  1320. self.gcode_editor_tab.t_frame.hide()
  1321. # then append the text from GCode to the text editor
  1322. try:
  1323. self.gcode_editor_tab.load_text(self.app.gcode_edited.getvalue(), move_to_start=True, clear_text=True)
  1324. except Exception as e:
  1325. log.debug('FlatCAMCNCJob.on_edit_code_click() -->%s' % str(e))
  1326. return
  1327. self.gcode_editor_tab.t_frame.show()
  1328. self.app.proc_container.view.set_idle()
  1329. self.gcode_editor_tab.buttonSave.hide()
  1330. self.gcode_editor_tab.buttonOpen.hide()
  1331. self.gcode_editor_tab.buttonPrint.hide()
  1332. self.gcode_editor_tab.buttonPreview.hide()
  1333. self.gcode_editor_tab.buttonReplace.hide()
  1334. self.gcode_editor_tab.sel_all_cb.hide()
  1335. self.gcode_editor_tab.entryReplace.hide()
  1336. self.gcode_editor_tab.code_editor.setReadOnly(True)
  1337. self.app.inform.emit('[success] %s...' % _('Loaded Machine Code into Code Editor'))
  1338. def on_update_source_file(self):
  1339. self.source_file = self.gcode_editor_tab.code_editor.toPlainText()
  1340. def gcode_header(self, comment_start_symbol=None, comment_stop_symbol=None):
  1341. """
  1342. Will create a header to be added to all GCode files generated by FlatCAM
  1343. :param comment_start_symbol: A symbol to be used as the first symbol in a comment
  1344. :param comment_stop_symbol: A symbol to be used as the last symbol in a comment
  1345. :return: A string with a GCode header
  1346. """
  1347. log.debug("FlatCAMCNCJob.gcode_header()")
  1348. time_str = "{:%A, %d %B %Y at %H:%M}".format(datetime.now())
  1349. marlin = False
  1350. hpgl = False
  1351. probe_pp = False
  1352. gcode = ''
  1353. start_comment = comment_start_symbol if comment_start_symbol is not None else '('
  1354. stop_comment = comment_stop_symbol if comment_stop_symbol is not None else ')'
  1355. try:
  1356. for key in self.cnc_tools:
  1357. ppg = self.cnc_tools[key]['data']['ppname_g']
  1358. if 'marlin' in ppg.lower() or 'repetier' in ppg.lower():
  1359. marlin = True
  1360. break
  1361. if ppg == 'hpgl':
  1362. hpgl = True
  1363. break
  1364. if "toolchange_probe" in ppg.lower():
  1365. probe_pp = True
  1366. break
  1367. except KeyError:
  1368. # log.debug("FlatCAMCNCJob.gcode_header() error: --> %s" % str(e))
  1369. pass
  1370. try:
  1371. if 'marlin' in self.options['ppname_e'].lower() or 'repetier' in self.options['ppname_e'].lower():
  1372. marlin = True
  1373. except KeyError:
  1374. # log.debug("FlatCAMCNCJob.gcode_header(): --> There is no such self.option: %s" % str(e))
  1375. pass
  1376. try:
  1377. if "toolchange_probe" in self.options['ppname_e'].lower():
  1378. probe_pp = True
  1379. except KeyError:
  1380. # log.debug("FlatCAMCNCJob.gcode_header(): --> There is no such self.option: %s" % str(e))
  1381. pass
  1382. if marlin is True:
  1383. gcode += ';Marlin(Repetier) G-CODE GENERATED BY FLATCAM v%s - www.flatcam.org - Version Date: %s\n' % \
  1384. (str(self.app.version), str(self.app.version_date)) + '\n'
  1385. gcode += ';Name: ' + str(self.options['name']) + '\n'
  1386. gcode += ';Type: ' + "G-code from " + str(self.options['type']) + '\n'
  1387. # if str(p['options']['type']) == 'Excellon' or str(p['options']['type']) == 'Excellon Geometry':
  1388. # gcode += '(Tools in use: ' + str(p['options']['Tools_in_use']) + ')\n'
  1389. gcode += ';Units: ' + self.units.upper() + '\n' + "\n"
  1390. gcode += ';Created on ' + time_str + '\n' + '\n'
  1391. elif hpgl is True:
  1392. gcode += 'CO "HPGL CODE GENERATED BY FLATCAM v%s - www.flatcam.org - Version Date: %s' % \
  1393. (str(self.app.version), str(self.app.version_date)) + '";\n'
  1394. gcode += 'CO "Name: ' + str(self.options['name']) + '";\n'
  1395. gcode += 'CO "Type: ' + "HPGL code from " + str(self.options['type']) + '";\n'
  1396. # if str(p['options']['type']) == 'Excellon' or str(p['options']['type']) == 'Excellon Geometry':
  1397. # gcode += '(Tools in use: ' + str(p['options']['Tools_in_use']) + ')\n'
  1398. gcode += 'CO "Units: ' + self.units.upper() + '";\n'
  1399. gcode += 'CO "Created on ' + time_str + '";\n'
  1400. elif probe_pp is True:
  1401. gcode += '(G-CODE GENERATED BY FLATCAM v%s - www.flatcam.org - Version Date: %s)\n' % \
  1402. (str(self.app.version), str(self.app.version_date)) + '\n'
  1403. gcode += '(This GCode tool change is done by using a Probe.)\n' \
  1404. '(Make sure that before you start the job you first do a rough zero for Z axis.)\n' \
  1405. '(This means that you need to zero the CNC axis and then jog to the toolchange X, Y location,)\n' \
  1406. '(mount the probe and adjust the Z so more or less the probe tip touch the plate. ' \
  1407. 'Then zero the Z axis.)\n' + '\n'
  1408. gcode += '(Name: ' + str(self.options['name']) + ')\n'
  1409. gcode += '(Type: ' + "G-code from " + str(self.options['type']) + ')\n'
  1410. # if str(p['options']['type']) == 'Excellon' or str(p['options']['type']) == 'Excellon Geometry':
  1411. # gcode += '(Tools in use: ' + str(p['options']['Tools_in_use']) + ')\n'
  1412. gcode += '(Units: ' + self.units.upper() + ')\n' + "\n"
  1413. gcode += '(Created on ' + time_str + ')\n' + '\n'
  1414. else:
  1415. gcode += '%sG-CODE GENERATED BY FLATCAM v%s - www.flatcam.org - Version Date: %s%s\n' % \
  1416. (start_comment, str(self.app.version), str(self.app.version_date), stop_comment) + '\n'
  1417. gcode += '%sName: ' % start_comment + str(self.options['name']) + '%s\n' % stop_comment
  1418. gcode += '%sType: ' % start_comment + "G-code from " + str(self.options['type']) + '%s\n' % stop_comment
  1419. # if str(p['options']['type']) == 'Excellon' or str(p['options']['type']) == 'Excellon Geometry':
  1420. # gcode += '(Tools in use: ' + str(p['options']['Tools_in_use']) + ')\n'
  1421. gcode += '%sUnits: ' % start_comment + self.units.upper() + '%s\n' % stop_comment + "\n"
  1422. gcode += '%sCreated on ' % start_comment + time_str + '%s\n' % stop_comment + '\n'
  1423. return gcode
  1424. @staticmethod
  1425. def gcode_footer(end_command=None):
  1426. """
  1427. Will add the M02 to the end of GCode, if requested.
  1428. :param end_command: 'M02' or 'M30' - String
  1429. :return:
  1430. """
  1431. if end_command:
  1432. return end_command
  1433. else:
  1434. return 'M02'
  1435. def export_gcode(self, filename=None, preamble='', postamble='', to_file=False, from_tcl=False):
  1436. """
  1437. This will save the GCode from the Gcode object to a file on the OS filesystem
  1438. :param filename: filename for the GCode file
  1439. :param preamble: a custom Gcode block to be added at the beginning of the Gcode file
  1440. :param postamble: a custom Gcode block to be added at the end of the Gcode file
  1441. :param to_file: if False then no actual file is saved but the app will know that a file was created
  1442. :param from_tcl: True if run from Tcl Shell
  1443. :return: None
  1444. """
  1445. # gcode = ''
  1446. # roland = False
  1447. # hpgl = False
  1448. # isel_icp = False
  1449. include_header = True
  1450. if preamble == '':
  1451. preamble = self.app.defaults["cncjob_prepend"]
  1452. if postamble == '':
  1453. preamble = self.app.defaults["cncjob_append"]
  1454. try:
  1455. if self.special_group:
  1456. self.app.inform.emit('[WARNING_NOTCL] %s %s %s.' %
  1457. (_("This CNCJob object can't be processed because it is a"),
  1458. str(self.special_group),
  1459. _("CNCJob object")))
  1460. return 'fail'
  1461. except AttributeError:
  1462. pass
  1463. # if this dict is not empty then the object is a Geometry object
  1464. if self.cnc_tools:
  1465. first_key = next(iter(self.cnc_tools))
  1466. include_header = self.app.preprocessors[self.cnc_tools[first_key]['data']['ppname_g']].include_header
  1467. # if this dict is not empty then the object is an Excellon object
  1468. if self.exc_cnc_tools:
  1469. first_key = next(iter(self.exc_cnc_tools))
  1470. include_header = self.app.preprocessors[
  1471. self.exc_cnc_tools[first_key]['data']['tools_drill_ppname_e']
  1472. ].include_header
  1473. gcode = ''
  1474. if include_header is False:
  1475. g = preamble
  1476. # detect if using multi-tool and make the Gcode summation correctly for each case
  1477. if self.multitool is True:
  1478. for tooluid_key in self.cnc_tools:
  1479. for key, value in self.cnc_tools[tooluid_key].items():
  1480. if key == 'gcode':
  1481. gcode += value
  1482. break
  1483. else:
  1484. gcode += self.gcode
  1485. g = g + gcode + postamble
  1486. else:
  1487. # search for the GCode beginning which is usually a G20 or G21
  1488. # fix so the preamble gets inserted in between the comments header and the actual start of GCODE
  1489. # g_idx = gcode.rfind('G20')
  1490. #
  1491. # # if it did not find 'G20' then search for 'G21'
  1492. # if g_idx == -1:
  1493. # g_idx = gcode.rfind('G21')
  1494. #
  1495. # # if it did not find 'G20' and it did not find 'G21' then there is an error and return
  1496. # if g_idx == -1:
  1497. # self.app.inform.emit('[ERROR_NOTCL] %s' % _("G-code does not have a units code: either G20 or G21"))
  1498. # return
  1499. # detect if using multi-tool and make the Gcode summation correctly for each case
  1500. if self.multitool is True:
  1501. if self.origin_kind == 'excellon':
  1502. for tooluid_key in self.exc_cnc_tools:
  1503. for key, value in self.exc_cnc_tools[tooluid_key].items():
  1504. if key == 'gcode' and value:
  1505. gcode += value
  1506. break
  1507. else:
  1508. for tooluid_key in self.cnc_tools:
  1509. for key, value in self.cnc_tools[tooluid_key].items():
  1510. if key == 'gcode' and value:
  1511. gcode += value
  1512. break
  1513. else:
  1514. gcode += self.gcode
  1515. end_gcode = self.gcode_footer() if self.app.defaults['cncjob_footer'] is True else ''
  1516. # detect if using a HPGL preprocessor
  1517. hpgl = False
  1518. if self.cnc_tools:
  1519. for key in self.cnc_tools:
  1520. if 'ppname_g' in self.cnc_tools[key]['data']:
  1521. if 'hpgl' in self.cnc_tools[key]['data']['ppname_g']:
  1522. hpgl = True
  1523. break
  1524. elif self.exc_cnc_tools:
  1525. for key in self.cnc_tools:
  1526. if 'ppname_e' in self.cnc_tools[key]['data']:
  1527. if 'hpgl' in self.cnc_tools[key]['data']['ppname_e']:
  1528. hpgl = True
  1529. break
  1530. if hpgl:
  1531. processed_gcode = ''
  1532. pa_re = re.compile(r"^PA\s*(-?\d+\.\d*),?\s*(-?\d+\.\d*)*;?$")
  1533. for gline in gcode.splitlines():
  1534. match = pa_re.search(gline)
  1535. if match:
  1536. x_int = int(float(match.group(1)))
  1537. y_int = int(float(match.group(2)))
  1538. new_line = 'PA%d,%d;\n' % (x_int, y_int)
  1539. processed_gcode += new_line
  1540. else:
  1541. processed_gcode += gline + '\n'
  1542. gcode = processed_gcode
  1543. g = self.gc_header + '\n' + preamble + '\n' + gcode + postamble + end_gcode
  1544. else:
  1545. try:
  1546. g_idx = gcode.index('G94')
  1547. if preamble != '' and postamble != '':
  1548. g = self.gc_header + gcode[:g_idx + 3] + '\n' + preamble + '\n' + \
  1549. gcode[(g_idx + 3):] + postamble + end_gcode
  1550. elif preamble == '':
  1551. g = self.gc_header + gcode[:g_idx + 3] + '\n' + \
  1552. gcode[(g_idx + 3):] + postamble + end_gcode
  1553. elif postamble == '':
  1554. g = self.gc_header + gcode[:g_idx + 3] + '\n' + preamble + '\n' + \
  1555. gcode[(g_idx + 3):] + end_gcode
  1556. else:
  1557. g = self.gc_header + gcode[:g_idx + 3] + gcode[(g_idx + 3):] + end_gcode
  1558. except ValueError:
  1559. self.app.inform.emit('[ERROR_NOTCL] %s' %
  1560. _("G-code does not have a G94 code.\n"
  1561. "Append Code snippet will not be used.."))
  1562. g = self.gc_header + '\n' + gcode + postamble + end_gcode
  1563. # if toolchange custom is used, replace M6 code with the code from the Toolchange Custom Text box
  1564. # if self.ui.toolchange_cb.get_value() is True:
  1565. # # match = self.re_toolchange.search(g)
  1566. # if 'M6' in g:
  1567. # m6_code = self.parse_custom_toolchange_code(self.ui.toolchange_text.get_value())
  1568. # if m6_code is None or m6_code == '':
  1569. # self.app.inform.emit(
  1570. # '[ERROR_NOTCL] %s' % _("Cancelled. The Toolchange Custom code is enabled but it's empty.")
  1571. # )
  1572. # return 'fail'
  1573. #
  1574. # g = g.replace('M6', m6_code)
  1575. # self.app.inform.emit('[success] %s' % _("Toolchange G-code was replaced by a custom code."))
  1576. lines = StringIO(g)
  1577. # Write
  1578. if filename is not None:
  1579. try:
  1580. force_windows_line_endings = self.app.defaults['cncjob_line_ending']
  1581. if force_windows_line_endings and sys.platform != 'win32':
  1582. with open(filename, 'w', newline='\r\n') as f:
  1583. for line in lines:
  1584. f.write(line)
  1585. else:
  1586. with open(filename, 'w') as f:
  1587. for line in lines:
  1588. f.write(line)
  1589. except FileNotFoundError:
  1590. self.app.inform.emit('[WARNING_NOTCL] %s' % _("No such file or directory"))
  1591. return
  1592. except PermissionError:
  1593. self.app.inform.emit(
  1594. '[WARNING] %s' % _("Permission denied, saving not possible.\n"
  1595. "Most likely another app is holding the file open and not accessible.")
  1596. )
  1597. return 'fail'
  1598. elif to_file is False:
  1599. # Just for adding it to the recent files list.
  1600. if self.app.defaults["global_open_style"] is False:
  1601. self.app.file_opened.emit("cncjob", filename)
  1602. self.app.file_saved.emit("cncjob", filename)
  1603. self.app.inform.emit('[success] %s: %s' % (_("Saved to"), filename))
  1604. else:
  1605. return lines
  1606. # def on_toolchange_custom_clicked(self, signal):
  1607. # """
  1608. # Handler for clicking toolchange custom.
  1609. #
  1610. # :param signal:
  1611. # :return:
  1612. # """
  1613. #
  1614. # try:
  1615. # if 'toolchange_custom' not in str(self.options['ppname_e']).lower():
  1616. # if self.ui.toolchange_cb.get_value():
  1617. # self.ui.toolchange_cb.set_value(False)
  1618. # self.app.inform.emit('[WARNING_NOTCL] %s' %
  1619. # _("The used preprocessor file has to have in it's name: 'toolchange_custom'"))
  1620. # except KeyError:
  1621. # try:
  1622. # for key in self.cnc_tools:
  1623. # ppg = self.cnc_tools[key]['data']['ppname_g']
  1624. # if 'toolchange_custom' not in str(ppg).lower():
  1625. # if self.ui.toolchange_cb.get_value():
  1626. # self.ui.toolchange_cb.set_value(False)
  1627. # self.app.inform.emit('[WARNING_NOTCL] %s' %
  1628. # _("The used preprocessor file has to have in it's name: "
  1629. # "'toolchange_custom'"))
  1630. # except KeyError:
  1631. # self.app.inform.emit('[ERROR] %s' % _("There is no preprocessor file."))
  1632. def get_gcode(self, preamble='', postamble=''):
  1633. """
  1634. We need this to be able to get_gcode separately for shell command export_gcode
  1635. :param preamble: Extra GCode added to the beginning of the GCode
  1636. :param postamble: Extra GCode added at the end of the GCode
  1637. :return: The modified GCode
  1638. """
  1639. return preamble + '\n' + self.gcode + "\n" + postamble
  1640. def get_svg(self):
  1641. # we need this to be able get_svg separately for shell command export_svg
  1642. pass
  1643. def on_plot_cb_click(self, *args):
  1644. """
  1645. Handler for clicking on the Plot checkbox.
  1646. :param args:
  1647. :return:
  1648. """
  1649. if self.muted_ui:
  1650. return
  1651. kind = self.ui.cncplot_method_combo.get_value()
  1652. self.plot(kind=kind)
  1653. self.read_form_item('plot')
  1654. self.ui_disconnect()
  1655. cb_flag = self.ui.plot_cb.isChecked()
  1656. for row in range(self.ui.cnc_tools_table.rowCount()):
  1657. table_cb = self.ui.cnc_tools_table.cellWidget(row, 6)
  1658. if cb_flag:
  1659. table_cb.setChecked(True)
  1660. else:
  1661. table_cb.setChecked(False)
  1662. self.ui_connect()
  1663. def on_plot_cb_click_table(self):
  1664. """
  1665. Handler for clicking the plot checkboxes added into a Table on each row. Purpose: toggle visibility for the
  1666. tool/aperture found on that row.
  1667. :return:
  1668. """
  1669. # self.ui.cnc_tools_table.cellWidget(row, 2).widget().setCheckState(QtCore.Qt.Unchecked)
  1670. self.ui_disconnect()
  1671. # cw = self.sender()
  1672. # cw_index = self.ui.cnc_tools_table.indexAt(cw.pos())
  1673. # cw_row = cw_index.row()
  1674. kind = self.ui.cncplot_method_combo.get_value()
  1675. self.shapes.clear(update=True)
  1676. if self.origin_kind == "excellon":
  1677. for r in range(self.ui.exc_cnc_tools_table.rowCount()):
  1678. row_dia = float('%.*f' % (self.decimals, float(self.ui.exc_cnc_tools_table.item(r, 1).text())))
  1679. for tooluid_key in self.exc_cnc_tools:
  1680. tooldia = float('%.*f' % (self.decimals, float(tooluid_key)))
  1681. if row_dia == tooldia:
  1682. gcode_parsed = self.exc_cnc_tools[tooluid_key]['gcode_parsed']
  1683. if self.ui.exc_cnc_tools_table.cellWidget(r, 6).isChecked():
  1684. self.plot2(tooldia=tooldia, obj=self, visible=True, gcode_parsed=gcode_parsed, kind=kind)
  1685. else:
  1686. for tooluid_key in self.cnc_tools:
  1687. tooldia = float('%.*f' % (self.decimals, float(self.cnc_tools[tooluid_key]['tooldia'])))
  1688. gcode_parsed = self.cnc_tools[tooluid_key]['gcode_parsed']
  1689. # tool_uid = int(self.ui.cnc_tools_table.item(cw_row, 3).text())
  1690. for r in range(self.ui.cnc_tools_table.rowCount()):
  1691. if int(self.ui.cnc_tools_table.item(r, 5).text()) == int(tooluid_key):
  1692. if self.ui.cnc_tools_table.cellWidget(r, 6).isChecked():
  1693. self.plot2(tooldia=tooldia, obj=self, visible=True, gcode_parsed=gcode_parsed, kind=kind)
  1694. self.shapes.redraw()
  1695. # make sure that the general plot is disabled if one of the row plot's are disabled and
  1696. # if all the row plot's are enabled also enable the general plot checkbox
  1697. cb_cnt = 0
  1698. total_row = self.ui.cnc_tools_table.rowCount()
  1699. for row in range(total_row):
  1700. if self.ui.cnc_tools_table.cellWidget(row, 6).isChecked():
  1701. cb_cnt += 1
  1702. else:
  1703. cb_cnt -= 1
  1704. if cb_cnt < total_row:
  1705. self.ui.plot_cb.setChecked(False)
  1706. else:
  1707. self.ui.plot_cb.setChecked(True)
  1708. self.ui_connect()
  1709. def plot(self, visible=None, kind='all'):
  1710. """
  1711. # Does all the required setup and returns False
  1712. # if the 'ptint' option is set to False.
  1713. :param visible: Boolean to decide if the object will be plotted as visible or disabled on canvas
  1714. :param kind: String. Can be "all" or "travel" or "cut". For CNCJob plotting
  1715. :return: None
  1716. """
  1717. if not FlatCAMObj.plot(self):
  1718. return
  1719. visible = visible if visible else self.options['plot']
  1720. if self.app.is_legacy is False:
  1721. if self.ui.annotation_cb.get_value() and self.ui.plot_cb.get_value():
  1722. self.text_col.enabled = True
  1723. else:
  1724. self.text_col.enabled = False
  1725. self.annotation.redraw()
  1726. try:
  1727. if self.multitool is False: # single tool usage
  1728. try:
  1729. dia_plot = float(self.options["tooldia"])
  1730. except ValueError:
  1731. # we may have a tuple with only one element and a comma
  1732. dia_plot = [float(el) for el in self.options["tooldia"].split(',') if el != ''][0]
  1733. self.plot2(tooldia=dia_plot, obj=self, visible=visible, kind=kind)
  1734. else:
  1735. # I do this so the travel lines thickness will reflect the tool diameter
  1736. # may work only for objects created within the app and not Gcode imported from elsewhere for which we
  1737. # don't know the origin
  1738. if self.origin_kind == "excellon":
  1739. if self.exc_cnc_tools:
  1740. for tooldia_key in self.exc_cnc_tools:
  1741. tooldia = float('%.*f' % (self.decimals, float(tooldia_key)))
  1742. gcode_parsed = self.exc_cnc_tools[tooldia_key]['gcode_parsed']
  1743. if not gcode_parsed:
  1744. continue
  1745. # gcode_parsed = self.gcode_parsed
  1746. self.plot2(tooldia=tooldia, obj=self, visible=visible, gcode_parsed=gcode_parsed, kind=kind)
  1747. else:
  1748. # multiple tools usage
  1749. if self.cnc_tools:
  1750. for tooluid_key in self.cnc_tools:
  1751. tooldia = float('%.*f' % (self.decimals, float(self.cnc_tools[tooluid_key]['tooldia'])))
  1752. gcode_parsed = self.cnc_tools[tooluid_key]['gcode_parsed']
  1753. self.plot2(tooldia=tooldia, obj=self, visible=visible, gcode_parsed=gcode_parsed, kind=kind)
  1754. self.shapes.redraw()
  1755. except (ObjectDeleted, AttributeError):
  1756. self.shapes.clear(update=True)
  1757. if self.app.is_legacy is False:
  1758. self.annotation.clear(update=True)
  1759. def on_annotation_change(self):
  1760. """
  1761. Handler for toggling the annotation display by clicking a checkbox.
  1762. :return:
  1763. """
  1764. if self.app.is_legacy is False:
  1765. if self.ui.annotation_cb.get_value():
  1766. self.text_col.enabled = True
  1767. else:
  1768. self.text_col.enabled = False
  1769. # kind = self.ui.cncplot_method_combo.get_value()
  1770. # self.plot(kind=kind)
  1771. self.annotation.redraw()
  1772. else:
  1773. kind = self.ui.cncplot_method_combo.get_value()
  1774. self.plot(kind=kind)
  1775. def convert_units(self, units):
  1776. """
  1777. Units conversion used by the CNCJob objects.
  1778. :param units: Can be "MM" or "IN"
  1779. :return:
  1780. """
  1781. log.debug("FlatCAMObj.FlatCAMECNCjob.convert_units()")
  1782. factor = CNCjob.convert_units(self, units)
  1783. self.options["tooldia"] = float(self.options["tooldia"]) * factor
  1784. param_list = ['cutz', 'depthperpass', 'travelz', 'feedrate', 'feedrate_z', 'feedrate_rapid',
  1785. 'endz', 'toolchangez']
  1786. temp_tools_dict = {}
  1787. tool_dia_copy = {}
  1788. data_copy = {}
  1789. for tooluid_key, tooluid_value in self.cnc_tools.items():
  1790. for dia_key, dia_value in tooluid_value.items():
  1791. if dia_key == 'tooldia':
  1792. dia_value *= factor
  1793. dia_value = float('%.*f' % (self.decimals, dia_value))
  1794. tool_dia_copy[dia_key] = dia_value
  1795. if dia_key == 'offset':
  1796. tool_dia_copy[dia_key] = dia_value
  1797. if dia_key == 'offset_value':
  1798. dia_value *= factor
  1799. tool_dia_copy[dia_key] = dia_value
  1800. if dia_key == 'type':
  1801. tool_dia_copy[dia_key] = dia_value
  1802. if dia_key == 'tool_type':
  1803. tool_dia_copy[dia_key] = dia_value
  1804. if dia_key == 'data':
  1805. for data_key, data_value in dia_value.items():
  1806. # convert the form fields that are convertible
  1807. for param in param_list:
  1808. if data_key == param and data_value is not None:
  1809. data_copy[data_key] = data_value * factor
  1810. # copy the other dict entries that are not convertible
  1811. if data_key not in param_list:
  1812. data_copy[data_key] = data_value
  1813. tool_dia_copy[dia_key] = deepcopy(data_copy)
  1814. data_copy.clear()
  1815. if dia_key == 'gcode':
  1816. tool_dia_copy[dia_key] = dia_value
  1817. if dia_key == 'gcode_parsed':
  1818. tool_dia_copy[dia_key] = dia_value
  1819. if dia_key == 'solid_geometry':
  1820. tool_dia_copy[dia_key] = dia_value
  1821. # if dia_key == 'solid_geometry':
  1822. # tool_dia_copy[dia_key] = affinity.scale(dia_value, xfact=factor, origin=(0, 0))
  1823. # if dia_key == 'gcode_parsed':
  1824. # for g in dia_value:
  1825. # g['geom'] = affinity.scale(g['geom'], factor, factor, origin=(0, 0))
  1826. #
  1827. # tool_dia_copy['gcode_parsed'] = deepcopy(dia_value)
  1828. # tool_dia_copy['solid_geometry'] = cascaded_union([geo['geom'] for geo in dia_value])
  1829. temp_tools_dict.update({
  1830. tooluid_key: deepcopy(tool_dia_copy)
  1831. })
  1832. tool_dia_copy.clear()
  1833. self.cnc_tools.clear()
  1834. self.cnc_tools = deepcopy(temp_tools_dict)