TclCommandCncjob.py 10 KB

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