TclCommandDrillcncjob.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. from ObjectCollection import *
  2. from tclCommands.TclCommand import TclCommandSignaled
  3. import math
  4. class TclCommandDrillcncjob(TclCommandSignaled):
  5. """
  6. Tcl shell command to Generates a Drill CNC Job from a Excellon Object.
  7. """
  8. # array of all command aliases, to be able use old names for backward compatibility (add_poly, add_polygon)
  9. aliases = ['drillcncjob']
  10. # dictionary of types from Tcl command, needs to be ordered
  11. arg_names = collections.OrderedDict([
  12. ('name', str)
  13. ])
  14. # dictionary of types from Tcl command, needs to be ordered , this is for options like -optionname value
  15. option_types = collections.OrderedDict([
  16. ('drilled_dias', str),
  17. ('drillz', float),
  18. ('travelz', float),
  19. ('feedrate', float),
  20. ('feedrate_rapid', float),
  21. ('spindlespeed', int),
  22. ('toolchange', bool),
  23. ('toolchangez', float),
  24. ('toolchangexy', tuple),
  25. ('endz', float),
  26. ('dwell', bool),
  27. ('dwelltime', float),
  28. ('pp', str),
  29. ('outname', str),
  30. ('opt_type', str),
  31. ('diatol', float),
  32. ('muted', int)
  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. ('drillz', 'Drill depth into material (example: -2.0).'),
  44. ('travelz', 'Travel distance above material (example: 2.0).'),
  45. ('feedrate', 'Drilling feed rate.'),
  46. ('feedrate_rapid', 'Rapid drilling feed rate.'),
  47. ('spindlespeed', 'Speed of the spindle in rpm (example: 4000).'),
  48. ('toolchange', 'Enable tool changes (example: True).'),
  49. ('toolchangez', 'Z distance for toolchange (example: 30.0).'),
  50. ('toolchangexy', 'X, Y coordonates for toolchange in format (x, y) (example: (2.0, 3.1) ).'),
  51. ('endz', 'Z distance at job end (example: 30.0).'),
  52. ('dwell', 'True or False; use (or not) the dwell'),
  53. ('dwelltime', 'Time to pause to allow the spindle to reach the full speed'),
  54. ('pp', 'This is the Excellon postprocessor name: case_sensitive, no_quotes'),
  55. ('outname', 'Name of the resulting Geometry object.'),
  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.')
  65. ]),
  66. 'examples': ['drillcncjob test.TXT -drillz -1.5 -travelz 14 -feedrate 222 -feedrate_rapid 456 -spindlespeed 777'
  67. ' -toolchange True -toolchangez 33 -endz 22 -pp default\n'
  68. 'Usage of -feedrate_rapid matter only when the posptocessor is using it, like -marlin-.']
  69. }
  70. def execute(self, args, unnamed_args):
  71. """
  72. execute current TCL shell command
  73. :param args: array of known named arguments and options
  74. :param unnamed_args: array of other values which were passed into command
  75. without -somename and we do not have them in known arg_names
  76. :return: None or exception
  77. """
  78. name = args['name']
  79. if 'outname' not in args:
  80. args['outname'] = name + "_cnc"
  81. if 'muted' in args:
  82. muted = args['muted']
  83. else:
  84. muted = 0
  85. obj = self.app.collection.get_by_name(name)
  86. if obj is None:
  87. if muted == 0:
  88. self.raise_tcl_error("Object not found: %s" % name)
  89. else:
  90. return "fail"
  91. if not isinstance(obj, FlatCAMExcellon):
  92. if muted == 0:
  93. self.raise_tcl_error('Expected FlatCAMExcellon, got %s %s.' % (name, type(obj)))
  94. else:
  95. return "fail"
  96. xmin = obj.options['xmin']
  97. ymin = obj.options['ymin']
  98. xmax = obj.options['xmax']
  99. ymax = obj.options['ymax']
  100. def job_init(job_obj, app_obj):
  101. # tools = args["tools"] if "tools" in args else 'all'
  102. units = self.app.ui.general_defaults_form.general_app_group.units_radio.get_value().upper()
  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('%.2f' % float(obj.tools[tool]["C"])) if units == 'MM' else \
  111. float('%.4f' % float(obj.tools[tool]["C"]))
  112. req_dia_form = float('%.2f' % float(req_dia)) if units == 'MM' else \
  113. float('%.4f' % float(req_dia))
  114. if 'diatol' in args:
  115. tolerance = args['diatol'] / 100
  116. tolerance = 0.0 if tolerance < 0.0 else tolerance
  117. tolerance = 1.0 if tolerance > 1.0 else tolerance
  118. if math.isclose(obj_dia_form, req_dia_form, rel_tol=tolerance):
  119. req_tools.add(tool)
  120. nr_diameters -= 1
  121. else:
  122. if obj_dia_form == req_dia_form:
  123. req_tools.add(tool)
  124. nr_diameters -= 1
  125. if nr_diameters > 0:
  126. if muted == 0:
  127. self.raise_tcl_error("One or more tool diameters of the drills to be drilled passed to the "
  128. "TclCommand are not actual tool diameters in the Excellon object.")
  129. else:
  130. return "fail"
  131. # make a string of diameters separated by comma; this is what generate_from_excellon_by_tool() is
  132. # expecting as tools parameter
  133. tools = ','.join(req_tools)
  134. # no longer needed
  135. del args['drilled_dias']
  136. del args['diatol']
  137. # Split and put back. We are passing the whole dictionary later.
  138. # args['milled_dias'] = [x.strip() for x in args['tools'].split(",")]
  139. else:
  140. tools = 'all'
  141. except Exception as e:
  142. tools = 'all'
  143. if muted == 0:
  144. self.raise_tcl_error("Bad tools: %s" % str(e))
  145. else:
  146. return "fail"
  147. drillz = args["drillz"] if "drillz" in args and args["drillz"] else obj.options["drillz"]
  148. toolchangez = args["toolchangez"] if "toolchangez" in args and args["toolchangez"] else \
  149. obj.options["toolchangez"]
  150. endz = args["endz"] if "endz" in args and args["endz"] else obj.options["endz"]
  151. toolchange = True if "toolchange" in args and args["toolchange"] == 1 else False
  152. opt_type = args["opt_type"] if "opt_type" in args and args["opt_type"] else 'B'
  153. job_obj.z_move = args["travelz"] if "travelz" in args and args["travelz"] else obj.options["travelz"]
  154. job_obj.feedrate = args["feedrate"] if "feedrate" in args and args["feedrate"] else obj.options["feedrate"]
  155. job_obj.feedrate_rapid = args["feedrate_rapid"] \
  156. if "feedrate_rapid" in args and args["feedrate_rapid"] else obj.options["feedrate_rapid"]
  157. if args['dwell'] and args['dwelltime']:
  158. job_obj.dwell = True
  159. job_obj.dwelltime = float(args['dwelltime'])
  160. job_obj.spindlespeed = args["spindlespeed"] if "spindlespeed" in args else None
  161. job_obj.pp_excellon_name = args["pp"] if "pp" in args and args["pp"] \
  162. else obj.options["ppname_e"]
  163. job_obj.coords_decimals = int(self.app.defaults["cncjob_coords_decimals"])
  164. job_obj.fr_decimals = int(self.app.defaults["cncjob_fr_decimals"])
  165. job_obj.options['type'] = 'Excellon'
  166. job_obj.toolchangexy = args["toolchangexy"] if "toolchangexy" in args and args["toolchangexy"] else \
  167. obj.options["toolchangexy"]
  168. job_obj.toolchange_xy_type = "excellon"
  169. job_obj.options['xmin'] = xmin
  170. job_obj.options['ymin'] = ymin
  171. job_obj.options['xmax'] = xmax
  172. job_obj.options['ymax'] = ymax
  173. job_obj.generate_from_excellon_by_tool(obj, tools, drillz=drillz, toolchangez=toolchangez,
  174. endz=endz,
  175. toolchange=toolchange, excellon_optimization_type=opt_type)
  176. job_obj.gcode_parse()
  177. job_obj.create_geometry()
  178. self.app.new_object("cncjob", args['outname'], job_init, plot=False)