TclCommandCncjob.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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', str),
  33. ('startz', float),
  34. ('endz', float),
  35. ('endxy', str),
  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. ('dpp', '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', 'The X,Y coordinates at Toolchange event in format (x, y) (example: (30.0, 15.2) or '
  60. 'without parenthesis like: 0.3,1.0). WARNING: no spaces allowed in the value.'),
  61. ('startz', 'Height before the first move.'),
  62. ('endz', 'Height where the last move will park.'),
  63. ('endxy', 'The X,Y coordinates at job end in format (x, y) (example: (2.0, 1.2) or without parenthesis'
  64. 'like: 0.3,1.0). WARNING: no spaces allowed in the value.'),
  65. ('spindlespeed', 'Speed of the spindle in rpm (example: 4000).'),
  66. ('dwelltime', 'Time to pause to allow the spindle to reach the full speed.\n'
  67. 'If it is not used in command then it will not be included'),
  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. Can be True (1) or False (0)')
  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. try:
  85. par = args['muted'].capitalize()
  86. except AttributeError:
  87. par = args['muted']
  88. muted = bool(eval(par))
  89. else:
  90. muted = False
  91. try:
  92. name = args['name']
  93. except KeyError:
  94. if muted is False:
  95. self.raise_tcl_error("Object name is missing")
  96. else:
  97. return "fail"
  98. if 'outname' not in args:
  99. args['outname'] = str(name) + "_cnc"
  100. obj = self.app.collection.get_by_name(str(name), isCaseSensitive=False)
  101. if obj is None:
  102. if muted is False:
  103. self.raise_tcl_error("Object not found: %s" % str(name))
  104. else:
  105. return "fail"
  106. if obj.kind != 'geometry':
  107. if muted is False:
  108. self.raise_tcl_error('Expected GeometryObject, got %s %s.' % (str(name), type(obj)))
  109. else:
  110. return
  111. args["dia"] = args["dia"] if "dia" in args and args["dia"] else self.app.defaults["geometry_cnctooldia"]
  112. args["z_cut"] = args["z_cut"] if "z_cut" in args and args["z_cut"] else self.app.defaults["geometry_cutz"]
  113. args["z_move"] = args["z_move"] if "z_move" in args and args["z_move"] else \
  114. self.app.defaults["geometry_travelz"]
  115. args["feedrate"] = args["feedrate"] if "feedrate" in args and args["feedrate"] else \
  116. self.app.defaults["geometry_feedrate"]
  117. args["feedrate_z"] = args["feedrate_z"] if "feedrate_z" in args and args["feedrate_z"] else \
  118. self.app.defaults["geometry_feedrate_z"]
  119. args["feedrate_rapid"] = args["feedrate_rapid"] if "feedrate_rapid" in args and args["feedrate_rapid"] else \
  120. self.app.defaults["geometry_feedrate_rapid"]
  121. if "extracut_length" in args:
  122. args["extracut"] = True
  123. if args["extracut_length"] is None:
  124. args["extracut_length"] = 0.0
  125. else:
  126. args["extracut_length"] = float(args["extracut_length"])
  127. else:
  128. args["extracut"] = self.app.defaults["geometry_extracut"]
  129. args["extracut_length"] = self.app.defaults["geometry_extracut_length"]
  130. if "dpp" in args:
  131. args["multidepth"] = True
  132. if args["dpp"] is None:
  133. args["dpp"] =self.app.defaults["geometry_depthperpass"]
  134. else:
  135. args["dpp"] = float(args["dpp"])
  136. else:
  137. args["multidepth"] = self.app.defaults["geometry_multidepth"]
  138. args["dpp"] = self.app.defaults["geometry_depthperpass"]
  139. args["startz"] = args["startz"] if "startz" in args and args["startz"] else \
  140. self.app.defaults["geometry_startz"]
  141. args["endz"] = args["endz"] if "endz" in args and args["endz"] else self.app.defaults["geometry_endz"]
  142. if "endxy" in args and args["endxy"]:
  143. args["endxy"] = args["endxy"]
  144. else:
  145. if self.app.defaults["geometry_endxy"]:
  146. args["endxy"] = self.app.defaults["geometry_endxy"]
  147. else:
  148. args["endxy"] = '0, 0'
  149. if len(eval(args["endxy"])) != 2:
  150. self.raise_tcl_error("The entered value for 'endxy' needs to have the format x,y or "
  151. "in format (x, y) - no spaces allowed. But always two comma separated values.")
  152. args["spindlespeed"] = args["spindlespeed"] if "spindlespeed" in args and args["spindlespeed"] != 0 else None
  153. if 'dwelltime' in args:
  154. args["dwell"] = True
  155. if args['dwelltime'] is None:
  156. args["dwelltime"] = float(obj.options["dwelltime"])
  157. else:
  158. args["dwelltime"] = float(args['dwelltime'])
  159. else:
  160. args["dwell"] = self.app.defaults["geometry_dwell"]
  161. args["dwelltime"] = self.app.defaults["geometry_dwelltime"]
  162. args["pp"] = args["pp"] if "pp" in args and args["pp"] else self.app.defaults["geometry_ppname_g"]
  163. if "toolchangez" in args:
  164. args["toolchange"] = True
  165. if args["toolchangez"] is not None:
  166. args["toolchangez"] = args["toolchangez"]
  167. else:
  168. args["toolchangez"] = self.app.defaults["geometry_toolchangez"]
  169. else:
  170. args["toolchange"] = self.app.defaults["geometry_toolchange"]
  171. args["toolchangez"] = self.app.defaults["geometry_toolchangez"]
  172. if "toolchangexy" in args and args["toolchangexy"]:
  173. args["toolchangexy"] = args["toolchangexy"]
  174. else:
  175. if self.app.defaults["geometry_toolchangexy"]:
  176. args["toolchangexy"] = self.app.defaults["geometry_toolchangexy"]
  177. else:
  178. args["toolchangexy"] = '0, 0'
  179. if len(eval(args["toolchangexy"])) != 2:
  180. self.raise_tcl_error("The entered value for 'toolchangexy' needs to have the format x,y or "
  181. "in format (x, y) - no spaces allowed. But always two comma separated values.")
  182. del args['name']
  183. for arg in args:
  184. if arg == "toolchange_xy" or arg == "spindlespeed" or arg == "startz":
  185. continue
  186. else:
  187. if args[arg] is None:
  188. print("None parameters: %s is None" % arg)
  189. if muted is False:
  190. self.raise_tcl_error('One of the command parameters that have to be not None, is None.\n'
  191. 'The parameter that is None is in the default values found in the list \n'
  192. 'generated by the TclCommand "list_sys geom". or in the arguments.')
  193. else:
  194. return
  195. # HACK !!! Should be solved elsewhere!!!
  196. # default option for multidepth is False
  197. # obj.options['multidepth'] = False
  198. if not obj.multigeo:
  199. obj.generatecncjob(use_thread=False, plot=False, **args)
  200. else:
  201. # Update the local_tools_dict values with the args value
  202. local_tools_dict = deepcopy(obj.tools)
  203. for tool_uid in list(local_tools_dict.keys()):
  204. if 'data' in local_tools_dict[tool_uid]:
  205. local_tools_dict[tool_uid]['data']['cutz'] = args["z_cut"]
  206. local_tools_dict[tool_uid]['data']['travelz'] = args["z_move"]
  207. local_tools_dict[tool_uid]['data']['feedrate'] = args["feedrate"]
  208. local_tools_dict[tool_uid]['data']['feedrate_z'] = args["feedrate_z"]
  209. local_tools_dict[tool_uid]['data']['feedrate_rapid'] = args["feedrate_rapid"]
  210. local_tools_dict[tool_uid]['data']['multidepth'] = args["multidepth"]
  211. local_tools_dict[tool_uid]['data']['extracut'] = args["extracut"]
  212. if args["extracut"] is True:
  213. local_tools_dict[tool_uid]['data']['extracut_length'] = args["extracut_length"]
  214. else:
  215. local_tools_dict[tool_uid]['data']['extracut_length'] = None
  216. local_tools_dict[tool_uid]['data']['depthperpass'] = args["dpp"]
  217. local_tools_dict[tool_uid]['data']['toolchange'] = args["toolchange"]
  218. local_tools_dict[tool_uid]['data']['toolchangez'] = args["toolchangez"]
  219. local_tools_dict[tool_uid]['data']['toolchangexy'] = args["toolchangexy"]
  220. local_tools_dict[tool_uid]['data']['startz'] = args["startz"]
  221. local_tools_dict[tool_uid]['data']['endz'] = args["endz"]
  222. local_tools_dict[tool_uid]['data']['endxy'] = args["endxy"]
  223. local_tools_dict[tool_uid]['data']['spindlespeed'] = args["spindlespeed"]
  224. local_tools_dict[tool_uid]['data']['dwell'] = args["dwell"]
  225. local_tools_dict[tool_uid]['data']['dwelltime'] = args["dwelltime"]
  226. local_tools_dict[tool_uid]['data']['ppname_g'] = args["pp"]
  227. obj.mtool_gen_cncjob(
  228. outname=args['outname'],
  229. tools_dict=local_tools_dict,
  230. tools_in_use=[],
  231. use_thread=False,
  232. plot=False)
  233. # self.raise_tcl_error('The object is a multi-geo geometry which is not supported in cncjob Tcl Command')