TclCommandCopperClear.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. from ObjectCollection import *
  2. from tclCommands.TclCommand import TclCommand
  3. import gettext
  4. import FlatCAMTranslation as fcTranslate
  5. import builtins
  6. fcTranslate.apply_language('strings')
  7. if '_' not in builtins.__dict__:
  8. _ = gettext.gettext
  9. class TclCommandCopperClear(TclCommand):
  10. """
  11. Clear the non-copper areas.
  12. """
  13. # Array of all command aliases, to be able use old names for backward compatibility (add_poly, add_polygon)
  14. aliases = ['ncc_clear', 'ncc']
  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', str),
  22. ('overlap', float),
  23. ('order', str),
  24. ('margin', float),
  25. ('method', str),
  26. ('connect', bool),
  27. ('contour', bool),
  28. ('has_offset', bool),
  29. ('offset', float),
  30. ('rest', bool),
  31. ('all', int),
  32. ('ref', int),
  33. ('box', str),
  34. ('outname', str),
  35. ])
  36. # array of mandatory options for current Tcl command: required = {'name','outname'}
  37. required = ['name']
  38. # structured help for current command, args needs to be ordered
  39. help = {
  40. 'main': "Clear excess copper in polygons. Basically it's a negative Paint.",
  41. 'args': collections.OrderedDict([
  42. ('name', 'Name of the source Geometry object. String.'),
  43. ('tooldia', 'Diameter of the tool to be used. Can be a comma separated list of diameters. No space is '
  44. 'allowed between tool diameters. E.g: correct: 0.5,1 / incorrect: 0.5, 1'),
  45. ('overlap', 'Fraction of the tool diameter to overlap cuts. Float number.'),
  46. ('margin', 'Bounding box margin. Float number.'),
  47. ('order', 'Can have the values: "no", "fwd" and "rev". String.'
  48. 'It is useful when there are multiple tools in tooldia parameter.'
  49. '"no" -> the order used is the one provided.'
  50. '"fwd" -> tools are ordered from smallest to biggest.'
  51. '"rev" -> tools are ordered from biggest to smallest.'),
  52. ('method', 'Algorithm for copper clearing. Can be: "standard", "seed" or "lines".'),
  53. ('connect', 'Draw lines to minimize tool lifts. True or False'),
  54. ('contour', 'Cut around the perimeter of the painting. True or False'),
  55. ('rest', 'Use rest-machining. True or False'),
  56. ('has_offset', 'The offset will used only if this is set True or present in args. True or False.'),
  57. ('offset', 'The copper clearing will finish to a distance from copper features. Float number.'),
  58. ('all', 'Will copper clear the whole object. 1 = enabled, anything else = disabled'),
  59. ('ref', 'Will clear of extra copper all polygons within a specified object with the name in "box" '
  60. 'parameter. 1 = enabled, anything else = disabled'),
  61. ('box', 'Name of the object to be used as reference. Required when selecting "ref" = 1. String.'),
  62. ('outname', 'Name of the resulting Geometry object. String.'),
  63. ]),
  64. 'examples': []
  65. }
  66. def execute(self, args, unnamed_args):
  67. """
  68. execute current TCL shell command
  69. :param args: array of known named arguments and options
  70. :param unnamed_args: array of other values which were passed into command
  71. without -somename and we do not have them in known arg_names
  72. :return: None or exception
  73. """
  74. name = args['name']
  75. if 'tooldia' in args:
  76. tooldia = str(args['tooldia'])
  77. else:
  78. tooldia = self.app.defaults["tools_ncctools"]
  79. if 'overlap' in args:
  80. overlap = float(args['overlap'])
  81. else:
  82. overlap = float(self.app.defaults["tools_nccoverlap"])
  83. if 'order' in args:
  84. order = args['order']
  85. else:
  86. order = str(self.app.defaults["tools_nccorder"])
  87. if 'margin' in args:
  88. margin = float(args['margin'])
  89. else:
  90. margin = float(self.app.defaults["tools_nccmargin"])
  91. if 'method' in args:
  92. method = args['method']
  93. else:
  94. method = str(self.app.defaults["tools_nccmethod"])
  95. if 'connect' in args:
  96. connect = eval(str(args['connect']).capitalize())
  97. else:
  98. connect = eval(str(self.app.defaults["tools_nccconnect"]))
  99. if 'contour' in args:
  100. contour = eval(str(args['contour']).capitalize())
  101. else:
  102. contour = eval(str(self.app.defaults["tools_ncccontour"]))
  103. offset = 0.0
  104. if 'has_offset' in args:
  105. has_offset = args['has_offset']
  106. if args['has_offset'] is True:
  107. if 'offset' in args:
  108. offset = float(args['margin'])
  109. else:
  110. # 'offset' has to be in args if 'has_offset' is and it is set True
  111. self.raise_tcl_error("%s: %s" % (_("Could not retrieve object"), name))
  112. else:
  113. has_offset = False
  114. try:
  115. tools = [float(eval(dia)) for dia in tooldia.split(",") if dia != '']
  116. except AttributeError:
  117. tools = [float(tooldia)]
  118. # store here the default data for Geometry Data
  119. default_data = {}
  120. default_data.update({
  121. "name": '_paint',
  122. "plot": self.app.defaults["geometry_plot"],
  123. "cutz": self.app.defaults["geometry_cutz"],
  124. "vtipdia": 0.1,
  125. "vtipangle": 30,
  126. "travelz": self.app.defaults["geometry_travelz"],
  127. "feedrate": self.app.defaults["geometry_feedrate"],
  128. "feedrate_z": self.app.defaults["geometry_feedrate_z"],
  129. "feedrate_rapid": self.app.defaults["geometry_feedrate_rapid"],
  130. "dwell": self.app.defaults["geometry_dwell"],
  131. "dwelltime": self.app.defaults["geometry_dwelltime"],
  132. "multidepth": self.app.defaults["geometry_multidepth"],
  133. "ppname_g": self.app.defaults["geometry_ppname_g"],
  134. "depthperpass": self.app.defaults["geometry_depthperpass"],
  135. "extracut": self.app.defaults["geometry_extracut"],
  136. "toolchange": self.app.defaults["geometry_toolchange"],
  137. "toolchangez": self.app.defaults["geometry_toolchangez"],
  138. "endz": self.app.defaults["geometry_endz"],
  139. "spindlespeed": self.app.defaults["geometry_spindlespeed"],
  140. "toolchangexy": self.app.defaults["geometry_toolchangexy"],
  141. "startz": self.app.defaults["geometry_startz"],
  142. "tooldia": self.app.defaults["tools_painttooldia"],
  143. "paintmargin": self.app.defaults["tools_paintmargin"],
  144. "paintmethod": self.app.defaults["tools_paintmethod"],
  145. "selectmethod": self.app.defaults["tools_selectmethod"],
  146. "pathconnect": self.app.defaults["tools_pathconnect"],
  147. "paintcontour": self.app.defaults["tools_paintcontour"],
  148. "paintoverlap": self.app.defaults["tools_paintoverlap"]
  149. })
  150. ncc_tools = dict()
  151. tooluid = 0
  152. for tool in tools:
  153. tooluid += 1
  154. ncc_tools.update({
  155. int(tooluid): {
  156. 'tooldia': float('%.4f' % tool),
  157. 'offset': 'Path',
  158. 'offset_value': 0.0,
  159. 'type': 'Iso',
  160. 'tool_type': 'C1',
  161. 'data': dict(default_data),
  162. 'solid_geometry': []
  163. }
  164. })
  165. if 'rest' in args:
  166. rest = eval(str(args['rest']).capitalize())
  167. else:
  168. rest = eval(str(self.app.defaults["tools_nccrest"]))
  169. if 'outname' in args:
  170. outname = args['outname']
  171. else:
  172. if rest is True:
  173. outname = name + "_ncc"
  174. else:
  175. outname = name + "_ncc_rm"
  176. # Get source object.
  177. try:
  178. obj = self.app.collection.get_by_name(str(name))
  179. except Exception as e:
  180. log.debug("TclCommandCopperClear.execute() --> %s" % str(e))
  181. self.raise_tcl_error("%s: %s" % (_("Could not retrieve object"), name))
  182. return "Could not retrieve object: %s" % name
  183. if obj is None:
  184. return "Object not found: %s" % name
  185. # Non-Copper clear all polygons in the non-copper clear object
  186. if 'all' in args and args['all'] == 1:
  187. self.app.ncclear_tool.clear_copper(ncc_obj=obj,
  188. select_method='itself',
  189. ncctooldia=tooldia,
  190. overlap=overlap,
  191. order=order,
  192. margin=margin,
  193. has_offset=has_offset,
  194. offset=offset,
  195. method=method,
  196. outname=outname,
  197. connect=connect,
  198. contour=contour,
  199. rest=rest,
  200. tools_storage=ncc_tools,
  201. plot=False,
  202. run_threaded=False)
  203. return
  204. # Non-Copper clear all polygons found within the box object from the the non_copper cleared object
  205. elif 'ref' in args and args['ref'] == 1:
  206. if 'box' not in args:
  207. self.raise_tcl_error('%s' % _("Expected -box <value>."))
  208. else:
  209. box_name = args['box']
  210. # Get box source object.
  211. try:
  212. box_obj = self.app.collection.get_by_name(str(box_name))
  213. except Exception as e:
  214. log.debug("TclCommandCopperClear.execute() --> %s" % str(e))
  215. self.raise_tcl_error("%s: %s" % (_("Could not retrieve box object"), name))
  216. return "Could not retrieve object: %s" % name
  217. self.app.ncclear_tool.clear_copper(ncc_obj=obj,
  218. sel_obj=box_obj,
  219. select_method='box',
  220. tooldia=tooldia,
  221. overlap=overlap,
  222. order=order,
  223. margin=margin,
  224. has_offset=has_offset,
  225. offset=offset,
  226. method=method,
  227. outname=outname,
  228. connect=connect,
  229. contour=contour,
  230. rest=rest,
  231. tools_storage=ncc_tools,
  232. plot=False,
  233. run_threaded=False)
  234. return
  235. else:
  236. self.raise_tcl_error("%s:" % _("None of the following args: 'ref', 'all' were found or none was set to 1.\n"
  237. "Copper clearing failed."))
  238. return "None of the following args: 'ref', 'all' were found or none was set to 1.\n" \
  239. "Copper clearing failed."