TclCommandCncjob.py 10 KB

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