TclCommandDrillcncjob.py 9.9 KB

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