TclCommandDrillcncjob.py 7.6 KB

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