TclCommandCncjob.py 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. from tclCommands.TclCommand import TclCommandSignaled
  2. from FlatCAMObj import FlatCAMGeometry
  3. import collections
  4. from copy import deepcopy
  5. class TclCommandCncjob(TclCommandSignaled):
  6. """
  7. Tcl shell command to Generates a CNC Job from a Geometry Object.
  8. example:
  9. set_sys units MM
  10. new
  11. open_gerber tests/gerber_files/simple1.gbr -outname margin
  12. isolate margin -dia 3
  13. cncjob margin_iso
  14. """
  15. # array of all command aliases, to be able use old names for backward compatibility (add_poly, add_polygon)
  16. aliases = ['cncjob']
  17. # dictionary of types from Tcl command, needs to be ordered
  18. arg_names = collections.OrderedDict([
  19. ('name', str)
  20. ])
  21. # dictionary of types from Tcl command, needs to be ordered , this is for options like -optionname value
  22. option_types = collections.OrderedDict([
  23. ('dia', float),
  24. ('z_cut', float),
  25. ('z_move', float),
  26. ('feedrate', float),
  27. ('feedrate_z', float),
  28. ('feedrate_rapid', float),
  29. ('multidepth', bool),
  30. ('extracut', bool),
  31. ('depthperpass', float),
  32. ('toolchange', int),
  33. ('toolchangez', float),
  34. ('toolchangexy', tuple),
  35. ('startz', float),
  36. ('endz', float),
  37. ('spindlespeed', int),
  38. ('dwell', bool),
  39. ('dwelltime', float),
  40. ('pp', str),
  41. ('muted', int),
  42. ('outname', str)
  43. ])
  44. # array of mandatory options for current Tcl command: required = {'name','outname'}
  45. required = []
  46. # structured help for current command, args needs to be ordered
  47. help = {
  48. 'main': "Generates a CNC Job from a Geometry Object.",
  49. 'args': collections.OrderedDict([
  50. ('name', 'Name of the source object.'),
  51. ('dia', 'Tool diameter to show on screen.'),
  52. ('z_cut', 'Z-axis cutting position.'),
  53. ('z_move', 'Z-axis moving position.'),
  54. ('feedrate', 'Moving speed on X-Y plane when cutting.'),
  55. ('feedrate_z', 'Moving speed on Z plane when cutting.'),
  56. ('feedrate_rapid', 'Rapid moving at speed when cutting.'),
  57. ('multidepth', 'Use or not multidepth cnc cut. (True or False)'),
  58. ('extracut', 'Use or not an extra cnccut over the first point in path,in the job end (example: True)'),
  59. ('depthperpass', 'Height of one layer for multidepth.'),
  60. ('toolchange', 'Enable tool changes (example: True).'),
  61. ('toolchangez', 'Z distance for toolchange (example: 30.0).'),
  62. ('toolchangexy', 'X, Y coordonates for toolchange in format (x, y) (example: (2.0, 3.1) ).'),
  63. ('startz', 'Height before the first move.'),
  64. ('endz', 'Height where the last move will park.'),
  65. ('spindlespeed', 'Speed of the spindle in rpm (example: 4000).'),
  66. ('dwell', 'True or False; use (or not) the dwell'),
  67. ('dwelltime', 'Time to pause to allow the spindle to reach the full speed'),
  68. ('outname', 'Name of the resulting Geometry object.'),
  69. ('pp', 'Name of the Geometry preprocessor. No quotes, case sensitive'),
  70. ('muted', 'It will not put errors in the Shell.')
  71. ]),
  72. 'examples': ['cncjob geo_name -dia 0.5 -z_cut -1.7 -z_move 2 -feedrate 120 -pp default']
  73. }
  74. def execute(self, args, unnamed_args):
  75. """
  76. execute current TCL shell command
  77. :param args: array of known named arguments and options
  78. :param unnamed_args: array of other values which were passed into command
  79. without -somename and we do not have them in known arg_names
  80. :return: None or exception
  81. """
  82. name = ''
  83. if 'muted' in args:
  84. muted = args['muted']
  85. else:
  86. muted = 0
  87. try:
  88. name = args['name']
  89. except KeyError:
  90. if muted == 0:
  91. self.raise_tcl_error("Object name is missing")
  92. else:
  93. return "fail"
  94. if 'outname' not in args:
  95. args['outname'] = str(name) + "_cnc"
  96. obj = self.app.collection.get_by_name(str(name), isCaseSensitive=False)
  97. if obj is None:
  98. if muted == 0:
  99. self.raise_tcl_error("Object not found: %s" % str(name))
  100. else:
  101. return "fail"
  102. if not isinstance(obj, FlatCAMGeometry):
  103. if muted == 0:
  104. self.raise_tcl_error('Expected FlatCAMGeometry, got %s %s.' % (str(name), type(obj)))
  105. else:
  106. return
  107. args["dia"] = args["dia"] if "dia" in args and args["dia"] else obj.options["cnctooldia"]
  108. args["z_cut"] = args["z_cut"] if "z_cut" in args and args["z_cut"] else obj.options["cutz"]
  109. args["z_move"] = args["z_move"] if "z_move" in args and args["z_move"] else obj.options["travelz"]
  110. args["feedrate"] = args["feedrate"] if "feedrate" in args and args["feedrate"] else obj.options["feedrate"]
  111. args["feedrate_z"] = args["feedrate_z"] if "feedrate_z" in args and args["feedrate_z"] else \
  112. obj.options["feedrate_z"]
  113. args["feedrate_rapid"] = args["feedrate_rapid"] if "feedrate_rapid" in args and args["feedrate_rapid"] else \
  114. obj.options["feedrate_rapid"]
  115. args["multidepth"] = bool(args["multidepth"]) if "multidepth" in args else obj.options["multidepth"]
  116. args["extracut"] = bool(args["extracut"]) if "extracut" in args else obj.options["extracut"]
  117. args["depthperpass"] = args["depthperpass"] if "depthperpass" in args and args["depthperpass"] else \
  118. obj.options["depthperpass"]
  119. args["startz"] = args["startz"] if "startz" in args and args["startz"] else \
  120. self.app.defaults["geometry_startz"]
  121. args["endz"] = args["endz"] if "endz" in args and args["endz"] else obj.options["endz"]
  122. args["spindlespeed"] = args["spindlespeed"] if "spindlespeed" in args and args["spindlespeed"] else None
  123. args["dwell"] = bool(args["dwell"]) if "dwell" in args else obj.options["dwell"]
  124. args["dwelltime"] = args["dwelltime"] if "dwelltime" in args and args["dwelltime"] else obj.options["dwelltime"]
  125. args["pp"] = args["pp"] if "pp" in args and args["pp"] else obj.options["ppname_g"]
  126. args["toolchange"] = True if "toolchange" in args and args["toolchange"] == 1 else False
  127. args["toolchangez"] = args["toolchangez"] if "toolchangez" in args and args["toolchangez"] else \
  128. obj.options["toolchangez"]
  129. args["toolchangexy"] = args["toolchangexy"] if "toolchangexy" in args and args["toolchangexy"] else \
  130. self.app.defaults["geometry_toolchangexy"]
  131. del args['name']
  132. for arg in args:
  133. if arg == "toolchange_xy" or arg == "spindlespeed" or arg == "startz":
  134. continue
  135. else:
  136. if args[arg] is None:
  137. print(arg, args[arg])
  138. if muted == 0:
  139. self.raise_tcl_error('One of the command parameters that have to be not None, is None.\n'
  140. 'The parameter that is None is in the default values found in the list \n'
  141. 'generated by the TclCommand "list_sys geom". or in the arguments.')
  142. else:
  143. return
  144. # HACK !!! Should be solved elsewhere!!!
  145. # default option for multidepth is False
  146. obj.options['multidepth'] = False
  147. if not obj.multigeo:
  148. obj.generatecncjob(use_thread=False, plot=False, **args)
  149. else:
  150. # Update the local_tools_dict values with the args value
  151. local_tools_dict = deepcopy(obj.tools)
  152. for tool_uid in list(local_tools_dict.keys()):
  153. if 'data' in local_tools_dict[tool_uid]:
  154. local_tools_dict[tool_uid]['data']['cutz'] = args["z_cut"]
  155. local_tools_dict[tool_uid]['data']['travelz'] = args["z_move"]
  156. local_tools_dict[tool_uid]['data']['feedrate'] = args["feedrate"]
  157. local_tools_dict[tool_uid]['data']['feedrate_z'] = args["feedrate_z"]
  158. local_tools_dict[tool_uid]['data']['feedrate_rapid'] = args["feedrate_rapid"]
  159. local_tools_dict[tool_uid]['data']['multidepth'] = args["multidepth"]
  160. local_tools_dict[tool_uid]['data']['extracut'] = args["extracut"]
  161. local_tools_dict[tool_uid]['data']['depthperpass'] = args["depthperpass"]
  162. local_tools_dict[tool_uid]['data']['toolchange'] = args["toolchange"]
  163. local_tools_dict[tool_uid]['data']['toolchangez'] = args["toolchangez"]
  164. local_tools_dict[tool_uid]['data']['toolchangexy'] = args["toolchangexy"]
  165. local_tools_dict[tool_uid]['data']['startz'] = args["startz"]
  166. local_tools_dict[tool_uid]['data']['endz'] = args["endz"]
  167. local_tools_dict[tool_uid]['data']['spindlespeed'] = args["spindlespeed"]
  168. local_tools_dict[tool_uid]['data']['dwell'] = args["dwell"]
  169. local_tools_dict[tool_uid]['data']['dwelltime'] = args["dwelltime"]
  170. local_tools_dict[tool_uid]['data']['ppname_g'] = args["pp"]
  171. obj.mtool_gen_cncjob(
  172. outname=args['outname'],
  173. tools_dict=local_tools_dict,
  174. tools_in_use=[],
  175. use_thread=False,
  176. plot=False)
  177. # self.raise_tcl_error('The object is a multi-geo geometry which is not supported in cncjob Tcl Command')