TclCommandDrillcncjob.py 9.1 KB

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