TclCommandCopperClear.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. description = '%s %s' % ("--", "Clear excess copper.")
  18. # dictionary of types from Tcl command, needs to be ordered
  19. arg_names = collections.OrderedDict([
  20. ('name', str),
  21. ])
  22. # dictionary of types from Tcl command, needs to be ordered , this is for options like -optionname value
  23. option_types = collections.OrderedDict([
  24. ('tooldia', str),
  25. ('overlap', float),
  26. ('order', str),
  27. ('margin', float),
  28. ('method', str),
  29. ('connect', str),
  30. ('contour', str),
  31. ('offset', float),
  32. ('rest', str),
  33. ('all', int),
  34. ('ref', str),
  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', 'Percentage of tool diameter to overlap current pass over previous pass. Float [0, 99.9999]\n'
  48. 'E.g: for a 25% from tool diameter overlap use -overlap 25'),
  49. ('margin', 'Bounding box margin. Float number.'),
  50. ('order', 'Can have the values: "no", "fwd" and "rev". String.'
  51. 'It is useful when there are multiple tools in tooldia parameter.'
  52. '"no" -> the order used is the one provided.'
  53. '"fwd" -> tools are ordered from smallest to biggest.'
  54. '"rev" -> tools are ordered from biggest to smallest.'),
  55. ('method', 'Algorithm for copper clearing. Can be: "standard", "seed" or "lines".'),
  56. ('connect', 'Draw lines to minimize tool lifts. True (1) or False (0)'),
  57. ('contour', 'Cut around the perimeter of the painting. True (1) or False (0)'),
  58. ('rest', 'Use rest-machining. True (1) or False (0)'),
  59. ('offset', 'If used, the copper clearing will finish to a distance from copper features. Float number.'),
  60. ('all', 'If used will copper clear the whole object. Either "-all" or "-box <value>" has to be used.'),
  61. ('box', 'Name of the object to be used as reference. Either "-all" or "-box <value>" has to be used. '
  62. 'String.'),
  63. ('outname', 'Name of the resulting Geometry object. String.'),
  64. ]),
  65. 'examples': ["ncc obj_name -tooldia 0.3,1 -overlap 10 -margin 1.0 -method 'lines' -all"]
  66. }
  67. def execute(self, args, unnamed_args):
  68. """
  69. execute current TCL shell command
  70. :param args: array of known named arguments and options
  71. :param unnamed_args: array of other values which were passed into command
  72. without -somename and we do not have them in known arg_names
  73. :return: None or exception
  74. """
  75. name = args['name']
  76. # Get source object.
  77. try:
  78. obj = self.app.collection.get_by_name(str(name))
  79. except Exception as e:
  80. log.debug("TclCommandCopperClear.execute() --> %s" % str(e))
  81. self.raise_tcl_error("%s: %s" % (_("Could not retrieve object"), name))
  82. return "Could not retrieve object: %s" % name
  83. if obj is None:
  84. return "Object not found: %s" % name
  85. if 'tooldia' in args:
  86. tooldia = str(args['tooldia'])
  87. else:
  88. tooldia = self.app.defaults["tools_ncctools"]
  89. if 'overlap' in args:
  90. overlap = float(args['overlap']) / 100.0
  91. else:
  92. overlap = float(self.app.defaults["tools_nccoverlap"]) / 100.0
  93. if 'order' in args:
  94. order = args['order']
  95. else:
  96. order = str(self.app.defaults["tools_nccorder"])
  97. if 'margin' in args:
  98. margin = float(args['margin'])
  99. else:
  100. margin = float(self.app.defaults["tools_nccmargin"])
  101. if 'method' in args:
  102. method = args['method']
  103. else:
  104. method = str(self.app.defaults["tools_nccmethod"])
  105. if 'connect' in args:
  106. try:
  107. par = args['connect'].capitalize()
  108. except AttributeError:
  109. par = args['connect']
  110. connect = bool(eval(par))
  111. else:
  112. connect = bool(eval(str(self.app.defaults["tools_nccconnect"])))
  113. if 'contour' in args:
  114. try:
  115. par = args['contour'].capitalize()
  116. except AttributeError:
  117. par = args['contour']
  118. contour = bool(eval(par))
  119. else:
  120. contour = bool(eval(str(self.app.defaults["tools_ncccontour"])))
  121. offset = 0.0
  122. if 'offset' in args:
  123. offset = float(args['offset'])
  124. has_offset = True
  125. else:
  126. has_offset = False
  127. try:
  128. tools = [float(eval(dia)) for dia in tooldia.split(",") if dia != '']
  129. except AttributeError:
  130. tools = [float(tooldia)]
  131. # store here the default data for Geometry Data
  132. default_data = {}
  133. default_data.update({
  134. "name": '_paint',
  135. "plot": self.app.defaults["geometry_plot"],
  136. "cutz": self.app.defaults["geometry_cutz"],
  137. "vtipdia": 0.1,
  138. "vtipangle": 30,
  139. "travelz": self.app.defaults["geometry_travelz"],
  140. "feedrate": self.app.defaults["geometry_feedrate"],
  141. "feedrate_z": self.app.defaults["geometry_feedrate_z"],
  142. "feedrate_rapid": self.app.defaults["geometry_feedrate_rapid"],
  143. "dwell": self.app.defaults["geometry_dwell"],
  144. "dwelltime": self.app.defaults["geometry_dwelltime"],
  145. "multidepth": self.app.defaults["geometry_multidepth"],
  146. "ppname_g": self.app.defaults["geometry_ppname_g"],
  147. "depthperpass": self.app.defaults["geometry_depthperpass"],
  148. "extracut": self.app.defaults["geometry_extracut"],
  149. "extracut_length": self.app.defaults["geometry_extracut_length"],
  150. "toolchange": self.app.defaults["geometry_toolchange"],
  151. "toolchangez": self.app.defaults["geometry_toolchangez"],
  152. "endz": self.app.defaults["geometry_endz"],
  153. "endxy": self.app.defaults["geometry_endxy"],
  154. "spindlespeed": self.app.defaults["geometry_spindlespeed"],
  155. "toolchangexy": self.app.defaults["geometry_toolchangexy"],
  156. "startz": self.app.defaults["geometry_startz"],
  157. "tooldia": self.app.defaults["tools_painttooldia"],
  158. "paintmargin": self.app.defaults["tools_paintmargin"],
  159. "paintmethod": self.app.defaults["tools_paintmethod"],
  160. "selectmethod": self.app.defaults["tools_selectmethod"],
  161. "pathconnect": self.app.defaults["tools_pathconnect"],
  162. "paintcontour": self.app.defaults["tools_paintcontour"],
  163. "paintoverlap": self.app.defaults["tools_paintoverlap"]
  164. })
  165. ncc_tools = {}
  166. tooluid = 0
  167. for tool in tools:
  168. tooluid += 1
  169. ncc_tools.update({
  170. int(tooluid): {
  171. 'tooldia': float('%.*f' % (obj.decimals, tool)),
  172. 'offset': 'Path',
  173. 'offset_value': 0.0,
  174. 'type': 'Iso',
  175. 'tool_type': 'C1',
  176. 'data': dict(default_data),
  177. 'solid_geometry': []
  178. }
  179. })
  180. if 'rest' in args:
  181. try:
  182. par = args['rest'].capitalize()
  183. except AttributeError:
  184. par = args['rest']
  185. rest = bool(eval(par))
  186. else:
  187. rest = bool(eval(str(self.app.defaults["tools_nccrest"])))
  188. if 'outname' in args:
  189. outname = args['outname']
  190. else:
  191. if rest is True:
  192. outname = name + "_ncc"
  193. else:
  194. outname = name + "_ncc_rm"
  195. # Non-Copper clear all polygons in the non-copper clear object
  196. if 'all' in args:
  197. self.app.ncclear_tool.clear_copper_tcl(ncc_obj=obj,
  198. select_method='itself',
  199. ncctooldia=tooldia,
  200. overlap=overlap,
  201. order=order,
  202. margin=margin,
  203. has_offset=has_offset,
  204. offset=offset,
  205. method=method,
  206. outname=outname,
  207. connect=connect,
  208. contour=contour,
  209. rest=rest,
  210. tools_storage=ncc_tools,
  211. plot=False,
  212. run_threaded=False)
  213. return
  214. # Non-Copper clear all polygons found within the box object from the the non_copper cleared object
  215. if 'box' in args:
  216. box_name = args['box']
  217. # Get box source object.
  218. try:
  219. box_obj = self.app.collection.get_by_name(str(box_name))
  220. except Exception as e:
  221. log.debug("TclCommandCopperClear.execute() --> %s" % str(e))
  222. self.raise_tcl_error("%s: %s" % (_("Could not retrieve box object"), name))
  223. return "Could not retrieve object: %s" % name
  224. self.app.ncclear_tool.clear_copper_tcl(ncc_obj=obj,
  225. sel_obj=box_obj,
  226. select_method='box',
  227. ncctooldia=tooldia,
  228. overlap=overlap,
  229. order=order,
  230. margin=margin,
  231. has_offset=has_offset,
  232. offset=offset,
  233. method=method,
  234. outname=outname,
  235. connect=connect,
  236. contour=contour,
  237. rest=rest,
  238. tools_storage=ncc_tools,
  239. plot=False,
  240. run_threaded=False)
  241. return
  242. # if the program reached this then it's an error because neither -all or -box <value> was used.
  243. self.raise_tcl_error('%s' % _("Expected either -box <value> or -all."))
  244. return "Expected either -box <value> or -all. Copper clearing failed."