TclCommandCopperClear.py 12 KB

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