TclCommandCopperClear.py 12 KB

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