TclCommandDrillcncjob.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. from tclCommands.TclCommand import TclCommandSignaled
  2. from FlatCAMObj import FlatCAMExcellon
  3. import collections
  4. import math
  5. class TclCommandDrillcncjob(TclCommandSignaled):
  6. """
  7. Tcl shell command to Generates a Drill CNC Job from a Excellon Object.
  8. """
  9. # array of all command aliases, to be able use old names for backward compatibility (add_poly, add_polygon)
  10. aliases = ['drillcncjob']
  11. description = '%s %s' % ("--", "Generates a Drill CNC Job object from a Excellon Object.")
  12. # dictionary of types from Tcl command, needs to be ordered
  13. arg_names = collections.OrderedDict([
  14. ('name', str)
  15. ])
  16. # dictionary of types from Tcl command, needs to be ordered , this is for options like -optionname value
  17. option_types = collections.OrderedDict([
  18. ('drilled_dias', str),
  19. ('drillz', float),
  20. ('travelz', float),
  21. ('feedrate', float),
  22. ('feedrate_rapid', float),
  23. ('spindlespeed', int),
  24. ('toolchangez', float),
  25. ('toolchangexy', tuple),
  26. ('endz', float),
  27. ('dwelltime', float),
  28. ('pp', str),
  29. ('opt_type', str),
  30. ('diatol', float),
  31. ('muted', str),
  32. ('outname', str)
  33. ])
  34. # array of mandatory options for current Tcl command: required = {'name','outname'}
  35. required = ['name']
  36. # structured help for current command, args needs to be ordered
  37. help = {
  38. 'main': "Generates a Drill CNC Job from a Excellon Object.",
  39. 'args': collections.OrderedDict([
  40. ('name', 'Name of the source object.'),
  41. ('drilled_dias',
  42. 'Comma separated tool diameters of the drills to be drilled (example: 0.6,1.0 or 3.125). '
  43. 'No space allowed'),
  44. ('drillz', 'Drill depth into material (example: -2.0).'),
  45. ('travelz', 'Travel distance above material (example: 2.0).'),
  46. ('feedrate', 'Drilling feed rate.'),
  47. ('feedrate_rapid', 'Rapid drilling feed rate.'),
  48. ('spindlespeed', 'Speed of the spindle in rpm (example: 4000).'),
  49. ('toolchangez', 'Z distance for toolchange (example: 30.0).\n'
  50. 'If used in the command then a toolchange event will be included in gcode'),
  51. ('toolchangexy', 'X, Y coordonates for toolchange in format (x, y) (example: (2.0, 3.1) ).'),
  52. ('endz', 'Z distance at job end (example: 30.0).'),
  53. ('dwelltime', 'Time to pause to allow the spindle to reach the full speed.\n'
  54. 'If it is not used in command then it will not be included'),
  55. ('pp', 'This is the Excellon preprocessor name: case_sensitive, no_quotes'),
  56. ('opt_type', 'Name of move optimization type. B by default for Basic OR-Tools, M for Metaheuristic OR-Tools'
  57. 'T from Travelling Salesman Algorithm. B and M works only for 64bit version of FlatCAM and '
  58. 'T works only for 32bit version of FlatCAM'),
  59. ('diatol', 'Tolerance. Percentange (0.0 ... 100.0) within which dias in drilled_dias will be judged to be '
  60. 'the same as the ones in the tools from the Excellon object. E.g: if in drill_dias we have a '
  61. 'diameter with value 1.0, in the Excellon we have a tool with dia = 1.05 and we set a tolerance '
  62. 'diatol = 5.0 then the drills with the dia = (0.95 ... 1.05) '
  63. 'in Excellon will be processed. Float number.'),
  64. ('muted', 'It will not put errors in the Shell or status bar. Can be True (1) or False (0).'),
  65. ('outname', 'Name of the resulting Geometry object.')
  66. ]),
  67. 'examples': ['drillcncjob test.TXT -drillz -1.5 -travelz 14 -feedrate 222 -feedrate_rapid 456 -spindlespeed 777'
  68. ' -toolchangez 33 -endz 22 -pp default\n'
  69. 'Usage of -feedrate_rapid matter only when the preprocessor is using it, like -marlin-.']
  70. }
  71. def execute(self, args, unnamed_args):
  72. """
  73. execute current TCL shell command
  74. :param args: array of known named arguments and options
  75. :param unnamed_args: array of other values which were passed into command
  76. without -somename and we do not have them in known arg_names
  77. :return: None or exception
  78. """
  79. name = args['name']
  80. obj = self.app.collection.get_by_name(name)
  81. if 'outname' not in args:
  82. args['outname'] = name + "_cnc"
  83. if 'muted' in args:
  84. muted = bool(eval(args['muted']))
  85. else:
  86. muted = False
  87. if obj is None:
  88. if muted is False:
  89. self.raise_tcl_error("Object not found: %s" % name)
  90. else:
  91. return "fail"
  92. if not isinstance(obj, FlatCAMExcellon):
  93. if muted is False:
  94. self.raise_tcl_error('Expected FlatCAMExcellon, got %s %s.' % (name, type(obj)))
  95. else:
  96. return "fail"
  97. xmin = obj.options['xmin']
  98. ymin = obj.options['ymin']
  99. xmax = obj.options['xmax']
  100. ymax = obj.options['ymax']
  101. def job_init(job_obj, app_obj):
  102. # tools = args["tools"] if "tools" in args else 'all'
  103. try:
  104. if 'drilled_dias' in args and args['drilled_dias'] != 'all':
  105. diameters = [x.strip() for x in args['drilled_dias'].split(",") if x != '']
  106. nr_diameters = len(diameters)
  107. req_tools = set()
  108. for tool in obj.tools:
  109. for req_dia in diameters:
  110. obj_dia_form = float('%.*f' % (obj.decimals, float(obj.tools[tool]["C"])))
  111. req_dia_form = float('%.*f' % (obj.decimals, float(req_dia)))
  112. if 'diatol' in args:
  113. tolerance = args['diatol'] / 100
  114. tolerance = 0.0 if tolerance < 0.0 else tolerance
  115. tolerance = 1.0 if tolerance > 1.0 else tolerance
  116. if math.isclose(obj_dia_form, req_dia_form, rel_tol=tolerance):
  117. req_tools.add(tool)
  118. nr_diameters -= 1
  119. else:
  120. if obj_dia_form == req_dia_form:
  121. req_tools.add(tool)
  122. nr_diameters -= 1
  123. if nr_diameters > 0:
  124. if muted is False:
  125. self.raise_tcl_error("One or more tool diameters of the drills to be drilled passed to the "
  126. "TclCommand are not actual tool diameters in the Excellon object.")
  127. else:
  128. return "fail"
  129. # make a string of diameters separated by comma; this is what generate_from_excellon_by_tool() is
  130. # expecting as tools parameter
  131. tools = ','.join(req_tools)
  132. # no longer needed
  133. del args['drilled_dias']
  134. del args['diatol']
  135. # Split and put back. We are passing the whole dictionary later.
  136. # args['milled_dias'] = [x.strip() for x in args['tools'].split(",")]
  137. else:
  138. tools = 'all'
  139. except Exception as e:
  140. tools = 'all'
  141. if muted is False:
  142. self.raise_tcl_error("Bad tools: %s" % str(e))
  143. else:
  144. return "fail"
  145. drillz = args["drillz"] if "drillz" in args and args["drillz"] is not None else obj.options["cutz"]
  146. if "toolchangez" in args:
  147. toolchange = True
  148. if args["toolchangez"] is not None:
  149. toolchangez = args["toolchangez"]
  150. else:
  151. toolchangez = obj.options["toolchangez"]
  152. else:
  153. toolchange = False
  154. toolchangez = 0.0
  155. endz = args["endz"] if "endz" in args and args["endz"] is not None else obj.options["endz"]
  156. opt_type = args["opt_type"] if "opt_type" in args and args["opt_type"] else 'B'
  157. job_obj.z_move = args["travelz"] if "travelz" in args and args["travelz"] else obj.options["travelz"]
  158. job_obj.feedrate = args["feedrate"] if "feedrate" in args and args["feedrate"] else obj.options["feedrate"]
  159. job_obj.z_feedrate = args["feedrate"] if "feedrate" in args and args["feedrate"] else \
  160. obj.options["feedrate"]
  161. job_obj.feedrate_rapid = args["feedrate_rapid"] \
  162. if "feedrate_rapid" in args and args["feedrate_rapid"] else obj.options["feedrate_rapid"]
  163. if 'dwelltime' in args:
  164. job_obj.dwell = True
  165. if args['dwelltime'] is not None:
  166. job_obj.dwelltime = float(args['dwelltime'])
  167. else:
  168. job_obj.dwelltime = float(obj.options["dwelltime"])
  169. else:
  170. job_obj.dwell = False
  171. job_obj.dwelltime = 0.0
  172. job_obj.spindlespeed = args["spindlespeed"] if "spindlespeed" in args else None
  173. job_obj.pp_excellon_name = args["pp"] if "pp" in args and args["pp"] \
  174. else obj.options["ppname_e"]
  175. job_obj.coords_decimals = int(self.app.defaults["cncjob_coords_decimals"])
  176. job_obj.fr_decimals = int(self.app.defaults["cncjob_fr_decimals"])
  177. job_obj.options['type'] = 'Excellon'
  178. job_obj.toolchangexy = args["toolchangexy"] if "toolchangexy" in args and args["toolchangexy"] else \
  179. obj.options["toolchangexy"]
  180. job_obj.toolchange_xy_type = "excellon"
  181. job_obj.options['xmin'] = xmin
  182. job_obj.options['ymin'] = ymin
  183. job_obj.options['xmax'] = xmax
  184. job_obj.options['ymax'] = ymax
  185. job_obj.generate_from_excellon_by_tool(obj, tools, drillz=drillz, toolchangez=toolchangez, endz=endz,
  186. toolchange=toolchange, excellon_optimization_type=opt_type)
  187. for t_item in job_obj.exc_cnc_tools:
  188. job_obj.exc_cnc_tools[t_item]['data']['offset'] = \
  189. float(job_obj.exc_cnc_tools[t_item]['offset_z']) + float(drillz)
  190. job_obj.exc_cnc_tools[t_item]['data']['ppname_e'] = obj.options['ppname_e']
  191. job_obj.origin_kind = 'excellon'
  192. job_obj.gcode_parse()
  193. job_obj.create_geometry()
  194. self.app.new_object("cncjob", args['outname'], job_init, plot=False)