TclCommandCncjob.py 10 KB

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