TclCommandCncjob.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. from ObjectCollection import *
  2. from tclCommands.TclCommand import TclCommandSignaled
  3. class TclCommandCncjob(TclCommandSignaled):
  4. """
  5. Tcl shell command to Generates a CNC Job from a Geometry Object.
  6. example:
  7. set_sys units MM
  8. new
  9. open_gerber tests/gerber_files/simple1.gbr -outname margin
  10. isolate margin -dia 3
  11. cncjob margin_iso
  12. """
  13. # array of all command aliases, to be able use old names for backward compatibility (add_poly, add_polygon)
  14. aliases = ['cncjob']
  15. # dictionary of types from Tcl command, needs to be ordered
  16. arg_names = collections.OrderedDict([
  17. ('name', str)
  18. ])
  19. # dictionary of types from Tcl command, needs to be ordered , this is for options like -optionname value
  20. option_types = collections.OrderedDict([
  21. ('tooldia', float),
  22. ('z_cut', float),
  23. ('z_move', float),
  24. ('feedrate', float),
  25. ('feedrate_z', float),
  26. ('feedrate_rapid', float),
  27. ('multidepth', bool),
  28. ('extracut', bool),
  29. ('depthperpass', float),
  30. ('toolchange', int),
  31. ('toolchangez', float),
  32. ('toolchangexy', tuple),
  33. ('startz', float),
  34. ('endz', float),
  35. ('spindlespeed', int),
  36. ('dwell', bool),
  37. ('dwelltime', float),
  38. ('pp', str),
  39. ('outname', str)
  40. ])
  41. # array of mandatory options for current Tcl command: required = {'name','outname'}
  42. required = ['name']
  43. # structured help for current command, args needs to be ordered
  44. help = {
  45. 'main': "Generates a CNC Job from a Geometry Object.",
  46. 'args': collections.OrderedDict([
  47. ('name', 'Name of the source object.'),
  48. ('tooldia', '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. ('multidepth', 'Use or not multidepth cnccut. (True or False)'),
  55. ('extracut', 'Use or not an extra cnccut over the first point in path,in the job end (example: True)'),
  56. ('depthperpass', 'Height of one layer for multidepth.'),
  57. ('toolchange', 'Enable tool changes (example: True).'),
  58. ('toolchangez', 'Z distance for toolchange (example: 30.0).'),
  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. ('dwell', 'True or False; use (or not) the dwell'),
  64. ('dwelltime', 'Time to pause to allow the spindle to reach the full speed'),
  65. ('outname', 'Name of the resulting Geometry object.'),
  66. ('pp', 'Name of the Geometry postprocessor. No quotes, case sensitive')
  67. ]),
  68. 'examples': ['cncjob geo_name -tooldia 0.5 -z_cut -1.7 -z_move 2 -feedrate 120 -ppname_g 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 = args['name']
  79. if 'outname' not in args:
  80. args['outname'] = str(name) + "_cnc"
  81. obj = self.app.collection.get_by_name(str(name), isCaseSensitive=False)
  82. if obj is None:
  83. self.raise_tcl_error("Object not found: %s" % str(name))
  84. if not isinstance(obj, FlatCAMGeometry):
  85. self.raise_tcl_error('Expected FlatCAMGeometry, got %s %s.' % (str(name), type(obj)))
  86. args["tooldia"] = args["tooldia"] if "tooldia" in args else obj.options["cnctooldia"]
  87. args["z_cut"] = args["z_cut"] if "z_cut" in args else obj.options["cutz"]
  88. args["z_move"] = args["z_move"] if "z_move" in args else obj.options["travelz"]
  89. args["feedrate"] = args["feedrate"] if "feedrate" in args else obj.options["feedrate"]
  90. args["feedrate_z"] = args["feedrate_z"] if "feedrate_z" in args else obj.options["feedrate_z"]
  91. args["feedrate_rapid"] = args["feedrate_rapid"] if "feedrate_rapid" in args else obj.options["feedrate_rapid"]
  92. args["multidepth"] = args["multidepth"] if "multidepth" in args else obj.options["multidepth"]
  93. args["extracut"] = args["extracut"] if "extracut" in args else obj.options["extracut"]
  94. args["depthperpass"] = args["depthperpass"] if "depthperpass" in args else obj.options["depthperpass"]
  95. args["startz"] = args["startz"] if "startz" in args else \
  96. self.app.defaults["geometry_startz"]
  97. args["endz"] = args["endz"] if "endz" in args else obj.options["endz"]
  98. args["spindlespeed"] = args["spindlespeed"] if "spindlespeed" in args else None
  99. args["dwell"] = args["dwell"] if "dwell" in args else obj.options["dwell"]
  100. args["dwelltime"] = args["dwelltime"] if "dwelltime" in args else obj.options["dwelltime"]
  101. args["pp"] = args["pp"] if "pp" in args else obj.options["ppname_g"]
  102. args["toolchange"] = True if "toolchange" in args and args["toolchange"] == 1 else False
  103. args["toolchangez"] = args["toolchangez"] if "toolchangez" in args else obj.options["toolchangez"]
  104. args["toolchangexy"] = args["toolchangexy"] if "toolchangexy" in args else \
  105. self.app.defaults["geometry_toolchangexy"]
  106. del args['name']
  107. for arg in args:
  108. if arg == "toolchange_xy" or arg == "spindlespeed":
  109. continue
  110. else:
  111. if args[arg] is None:
  112. self.raise_tcl_error('One of the command parameters that have to be not None, is None.\n'
  113. 'The parameter that is None is in the default values found in the list \n'
  114. 'generated by the TclCommand "list_sys geom". or in the arguments.')
  115. # HACK !!! Should be solved elsewhere!!!
  116. # default option for multidepth is False
  117. obj.options['multidepth'] = False
  118. if not obj.multigeo:
  119. obj.generatecncjob(use_thread=False, **args)
  120. else:
  121. # Update the local_tools_dict values with the args value
  122. local_tools_dict = deepcopy(obj.tools)
  123. for tool_uid in list(local_tools_dict.keys()):
  124. if 'data' in local_tools_dict[tool_uid]:
  125. local_tools_dict[tool_uid]['data']['cutz'] = args["z_cut"]
  126. local_tools_dict[tool_uid]['data']['travelz'] = args["z_move"]
  127. local_tools_dict[tool_uid]['data']['feedrate'] = args["feedrate"]
  128. local_tools_dict[tool_uid]['data']['feedrate_z'] = args["feedrate_z"]
  129. local_tools_dict[tool_uid]['data']['feedrate_rapid'] = args["feedrate_rapid"]
  130. local_tools_dict[tool_uid]['data']['multidepth'] = args["multidepth"]
  131. local_tools_dict[tool_uid]['data']['extracut'] = args["extracut"]
  132. local_tools_dict[tool_uid]['data']['depthperpass'] = args["depthperpass"]
  133. local_tools_dict[tool_uid]['data']['toolchange'] = args["toolchange"]
  134. local_tools_dict[tool_uid]['data']['toolchangez'] = args["toolchangez"]
  135. local_tools_dict[tool_uid]['data']['toolchangexy'] = args["toolchangexy"]
  136. local_tools_dict[tool_uid]['data']['startz'] = args["startz"]
  137. local_tools_dict[tool_uid]['data']['endz'] = args["endz"]
  138. local_tools_dict[tool_uid]['data']['spindlespeed'] = args["spindlespeed"]
  139. local_tools_dict[tool_uid]['data']['dwell'] = args["dwell"]
  140. local_tools_dict[tool_uid]['data']['dwelltime'] = args["dwelltime"]
  141. local_tools_dict[tool_uid]['data']['ppname_g'] = args["pp"]
  142. obj.mtool_gen_cncjob(
  143. outname=args['outname'],
  144. tools_dict=local_tools_dict,
  145. tools_in_use=[],
  146. use_thread=False)
  147. # self.raise_tcl_error('The object is a multi-geo geometry which is not supported in cncjob Tcl Command')