TclCommandCopperClear.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. if 'tooldia' in args:
  78. tooldia = str(args['tooldia'])
  79. else:
  80. tooldia = self.app.defaults["tools_ncctools"]
  81. if 'overlap' in args:
  82. overlap = float(args['overlap'])
  83. else:
  84. overlap = float(self.app.defaults["tools_nccoverlap"])
  85. if 'order' in args:
  86. order = args['order']
  87. else:
  88. order = str(self.app.defaults["tools_nccorder"])
  89. if 'margin' in args:
  90. margin = float(args['margin'])
  91. else:
  92. margin = float(self.app.defaults["tools_nccmargin"])
  93. if 'method' in args:
  94. method = args['method']
  95. else:
  96. method = str(self.app.defaults["tools_nccmethod"])
  97. if 'connect' in args:
  98. connect = eval(str(args['connect']).capitalize())
  99. else:
  100. connect = eval(str(self.app.defaults["tools_nccconnect"]))
  101. if 'contour' in args:
  102. contour = eval(str(args['contour']).capitalize())
  103. else:
  104. contour = eval(str(self.app.defaults["tools_ncccontour"]))
  105. offset = 0.0
  106. if 'has_offset' in args:
  107. has_offset = args['has_offset']
  108. if args['has_offset'] is True:
  109. if 'offset' in args:
  110. offset = float(args['margin'])
  111. else:
  112. # 'offset' has to be in args if 'has_offset' is and it is set True
  113. self.raise_tcl_error("%s: %s" % (_("Could not retrieve object"), name))
  114. else:
  115. has_offset = False
  116. try:
  117. tools = [float(eval(dia)) for dia in tooldia.split(",") if dia != '']
  118. except AttributeError:
  119. tools = [float(tooldia)]
  120. # store here the default data for Geometry Data
  121. default_data = {}
  122. default_data.update({
  123. "name": '_paint',
  124. "plot": self.app.defaults["geometry_plot"],
  125. "cutz": self.app.defaults["geometry_cutz"],
  126. "vtipdia": 0.1,
  127. "vtipangle": 30,
  128. "travelz": self.app.defaults["geometry_travelz"],
  129. "feedrate": self.app.defaults["geometry_feedrate"],
  130. "feedrate_z": self.app.defaults["geometry_feedrate_z"],
  131. "feedrate_rapid": self.app.defaults["geometry_feedrate_rapid"],
  132. "dwell": self.app.defaults["geometry_dwell"],
  133. "dwelltime": self.app.defaults["geometry_dwelltime"],
  134. "multidepth": self.app.defaults["geometry_multidepth"],
  135. "ppname_g": self.app.defaults["geometry_ppname_g"],
  136. "depthperpass": self.app.defaults["geometry_depthperpass"],
  137. "extracut": self.app.defaults["geometry_extracut"],
  138. "toolchange": self.app.defaults["geometry_toolchange"],
  139. "toolchangez": self.app.defaults["geometry_toolchangez"],
  140. "endz": self.app.defaults["geometry_endz"],
  141. "spindlespeed": self.app.defaults["geometry_spindlespeed"],
  142. "toolchangexy": self.app.defaults["geometry_toolchangexy"],
  143. "startz": self.app.defaults["geometry_startz"],
  144. "tooldia": self.app.defaults["tools_painttooldia"],
  145. "paintmargin": self.app.defaults["tools_paintmargin"],
  146. "paintmethod": self.app.defaults["tools_paintmethod"],
  147. "selectmethod": self.app.defaults["tools_selectmethod"],
  148. "pathconnect": self.app.defaults["tools_pathconnect"],
  149. "paintcontour": self.app.defaults["tools_paintcontour"],
  150. "paintoverlap": self.app.defaults["tools_paintoverlap"]
  151. })
  152. ncc_tools = dict()
  153. tooluid = 0
  154. for tool in tools:
  155. tooluid += 1
  156. ncc_tools.update({
  157. int(tooluid): {
  158. 'tooldia': float('%.4f' % tool),
  159. 'offset': 'Path',
  160. 'offset_value': 0.0,
  161. 'type': 'Iso',
  162. 'tool_type': 'C1',
  163. 'data': dict(default_data),
  164. 'solid_geometry': []
  165. }
  166. })
  167. if 'rest' in args:
  168. rest = eval(str(args['rest']).capitalize())
  169. else:
  170. rest = eval(str(self.app.defaults["tools_nccrest"]))
  171. if 'outname' in args:
  172. outname = args['outname']
  173. else:
  174. if rest is True:
  175. outname = name + "_ncc"
  176. else:
  177. outname = name + "_ncc_rm"
  178. # Get source object.
  179. try:
  180. obj = self.app.collection.get_by_name(str(name))
  181. except Exception as e:
  182. log.debug("TclCommandCopperClear.execute() --> %s" % str(e))
  183. self.raise_tcl_error("%s: %s" % (_("Could not retrieve object"), name))
  184. return "Could not retrieve object: %s" % name
  185. if obj is None:
  186. return "Object not found: %s" % name
  187. # Non-Copper clear all polygons in the non-copper clear object
  188. if 'all' in args and args['all'] == 1:
  189. self.app.ncclear_tool.clear_copper(ncc_obj=obj,
  190. select_method='itself',
  191. ncctooldia=tooldia,
  192. overlap=overlap,
  193. order=order,
  194. margin=margin,
  195. has_offset=has_offset,
  196. offset=offset,
  197. method=method,
  198. outname=outname,
  199. connect=connect,
  200. contour=contour,
  201. rest=rest,
  202. tools_storage=ncc_tools,
  203. plot=False,
  204. run_threaded=False)
  205. return
  206. # Non-Copper clear all polygons found within the box object from the the non_copper cleared object
  207. elif 'ref' in args and args['ref'] == 1:
  208. if 'box' not in args:
  209. self.raise_tcl_error('%s' % _("Expected -box <value>."))
  210. else:
  211. box_name = args['box']
  212. # Get box source object.
  213. try:
  214. box_obj = self.app.collection.get_by_name(str(box_name))
  215. except Exception as e:
  216. log.debug("TclCommandCopperClear.execute() --> %s" % str(e))
  217. self.raise_tcl_error("%s: %s" % (_("Could not retrieve box object"), name))
  218. return "Could not retrieve object: %s" % name
  219. self.app.ncclear_tool.clear_copper(ncc_obj=obj,
  220. sel_obj=box_obj,
  221. select_method='box',
  222. ncctooldia=tooldia,
  223. overlap=overlap,
  224. order=order,
  225. margin=margin,
  226. has_offset=has_offset,
  227. offset=offset,
  228. method=method,
  229. outname=outname,
  230. connect=connect,
  231. contour=contour,
  232. rest=rest,
  233. tools_storage=ncc_tools,
  234. plot=False,
  235. run_threaded=False)
  236. return
  237. else:
  238. self.raise_tcl_error("%s:" % _("None of the following args: 'ref', 'all' were found or none was set to 1.\n"
  239. "Copper clearing failed."))
  240. return "None of the following args: 'ref', 'all' were found or none was set to 1.\n" \
  241. "Copper clearing failed."