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. # 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. "toolchange": self.app.defaults["geometry_toolchange"],
  148. "toolchangez": self.app.defaults["geometry_toolchangez"],
  149. "endz": self.app.defaults["geometry_endz"],
  150. "spindlespeed": self.app.defaults["geometry_spindlespeed"],
  151. "toolchangexy": self.app.defaults["geometry_toolchangexy"],
  152. "startz": self.app.defaults["geometry_startz"],
  153. "tooldia": self.app.defaults["tools_painttooldia"],
  154. "paintmargin": self.app.defaults["tools_paintmargin"],
  155. "paintmethod": self.app.defaults["tools_paintmethod"],
  156. "selectmethod": self.app.defaults["tools_selectmethod"],
  157. "pathconnect": self.app.defaults["tools_pathconnect"],
  158. "paintcontour": self.app.defaults["tools_paintcontour"],
  159. "paintoverlap": self.app.defaults["tools_paintoverlap"]
  160. })
  161. ncc_tools = dict()
  162. tooluid = 0
  163. for tool in tools:
  164. tooluid += 1
  165. ncc_tools.update({
  166. int(tooluid): {
  167. 'tooldia': float('%.*f' % (obj.decimals, tool)),
  168. 'offset': 'Path',
  169. 'offset_value': 0.0,
  170. 'type': 'Iso',
  171. 'tool_type': 'C1',
  172. 'data': dict(default_data),
  173. 'solid_geometry': []
  174. }
  175. })
  176. if 'rest' in args:
  177. rest = bool(args['rest'])
  178. else:
  179. rest = eval(str(self.app.defaults["tools_nccrest"]))
  180. if 'outname' in args:
  181. outname = args['outname']
  182. else:
  183. if rest is True:
  184. outname = name + "_ncc"
  185. else:
  186. outname = name + "_ncc_rm"
  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."