TclCommandCopperClear.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. from tclCommands.TclCommand import TclCommand
  2. import collections
  3. import logging
  4. import gettext
  5. import appTranslation 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.\n'
  46. 'WARNING: No space is 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. No spaces.'),
  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 = str(self.app.defaults["tools_ncc_tools"])
  89. if 'overlap' in args:
  90. overlap = float(args['overlap']) / 100.0
  91. else:
  92. overlap = float(self.app.defaults["tools_ncc_overlap"]) / 100.0
  93. if 'order' in args:
  94. order = args['order']
  95. else:
  96. order = str(self.app.defaults["tools_ncc_order"])
  97. if 'margin' in args:
  98. margin = float(args['margin'])
  99. else:
  100. margin = float(self.app.defaults["tools_ncc_margin"])
  101. if 'method' in args:
  102. method = args['method']
  103. if method == "standard":
  104. method_data = 0
  105. elif method == "seed":
  106. method_data = 1
  107. else:
  108. method_data = 2
  109. else:
  110. method = str(self.app.defaults["tools_ncc_method"])
  111. method_data = method
  112. if 'connect' in args:
  113. try:
  114. par = args['connect'].capitalize()
  115. except AttributeError:
  116. par = args['connect']
  117. connect = bool(eval(par))
  118. else:
  119. connect = bool(eval(str(self.app.defaults["tools_ncc_connect"])))
  120. if 'contour' in args:
  121. try:
  122. par = args['contour'].capitalize()
  123. except AttributeError:
  124. par = args['contour']
  125. contour = bool(eval(par))
  126. else:
  127. contour = bool(eval(str(self.app.defaults["tools_ncc_contour"])))
  128. offset = 0.0
  129. if 'offset' in args:
  130. offset = float(args['offset'])
  131. has_offset = True
  132. else:
  133. has_offset = False
  134. try:
  135. tools = [float(eval(dia)) for dia in tooldia.split(",") if dia != '']
  136. except AttributeError:
  137. tools = [float(tooldia)]
  138. if 'rest' in args:
  139. try:
  140. par = args['rest'].capitalize()
  141. except AttributeError:
  142. par = args['rest']
  143. rest = bool(eval(par))
  144. else:
  145. rest = bool(eval(str(self.app.defaults["tools_ncc_rest"])))
  146. if 'outname' in args:
  147. outname = args['outname']
  148. else:
  149. if rest is True:
  150. outname = name + "_ncc"
  151. else:
  152. outname = name + "_ncc_rm"
  153. # used only to have correct information's in the obj.tools[tool]['data'] dict
  154. if "all" in args:
  155. select = 0 # 'ITSELF
  156. else:
  157. select = 2 # 'REFERENCE Object'
  158. # store here the default data for Geometry Data
  159. default_data = {}
  160. default_data.update({
  161. "name": outname,
  162. "plot": False,
  163. "cutz": self.app.defaults["geometry_cutz"],
  164. "vtipdia": float(self.app.defaults["geometry_vtipdia"]),
  165. "vtipangle": float(self.app.defaults["geometry_vtipangle"]),
  166. "travelz": self.app.defaults["geometry_travelz"],
  167. "feedrate": self.app.defaults["geometry_feedrate"],
  168. "feedrate_z": self.app.defaults["geometry_feedrate_z"],
  169. "feedrate_rapid": self.app.defaults["geometry_feedrate_rapid"],
  170. "dwell": self.app.defaults["geometry_dwell"],
  171. "dwelltime": self.app.defaults["geometry_dwelltime"],
  172. "multidepth": self.app.defaults["geometry_multidepth"],
  173. "ppname_g": self.app.defaults["geometry_ppname_g"],
  174. "depthperpass": self.app.defaults["geometry_depthperpass"],
  175. "extracut": self.app.defaults["geometry_extracut"],
  176. "extracut_length": self.app.defaults["geometry_extracut_length"],
  177. "toolchange": self.app.defaults["geometry_toolchange"],
  178. "toolchangez": self.app.defaults["geometry_toolchangez"],
  179. "endz": self.app.defaults["geometry_endz"],
  180. "endxy": self.app.defaults["geometry_endxy"],
  181. "spindlespeed": self.app.defaults["geometry_spindlespeed"],
  182. "toolchangexy": self.app.defaults["geometry_toolchangexy"],
  183. "startz": self.app.defaults["geometry_startz"],
  184. "area_exclusion": self.app.defaults["geometry_area_exclusion"],
  185. "area_shape": self.app.defaults["geometry_area_shape"],
  186. "area_strategy": self.app.defaults["geometry_area_strategy"],
  187. "area_overz": float(self.app.defaults["geometry_area_overz"]),
  188. "tooldia": tooldia,
  189. "tools_ncc_operation": self.app.defaults["tools_ncc_operation"],
  190. "tools_ncc_margin": margin,
  191. "tools_ncc_method": method_data,
  192. "tools_ncc_ref": select,
  193. "tools_ncc_connect": connect,
  194. "tools_ncc_contour": contour,
  195. "tools_ncc_overlap": overlap,
  196. "tools_ncc_offset_choice": self.app.defaults["tools_ncc_offset_choice"],
  197. "tools_ncc_offset_value": self.app.defaults["tools_ncc_offset_value"],
  198. "tools_ncc_milling_type": self.app.defaults["tools_ncc_milling_type"]
  199. })
  200. ncc_tools = {}
  201. tooluid = 0
  202. for tool in tools:
  203. tooluid += 1
  204. ncc_tools.update({
  205. int(tooluid): {
  206. 'tooldia': float('%.*f' % (obj.decimals, tool)),
  207. 'offset': 'Path',
  208. 'offset_value': 0.0,
  209. 'type': 'Iso',
  210. 'tool_type': 'C1',
  211. 'data': dict(default_data),
  212. 'solid_geometry': []
  213. }
  214. })
  215. ncc_tools[int(tooluid)]['data']['tooldia'] = self.app.dec_format(tool, obj.decimals)
  216. # Non-Copper clear all polygons in the non-copper clear object
  217. if 'all' in args:
  218. self.app.ncclear_tool.clear_copper_tcl(ncc_obj=obj,
  219. select_method=0, # ITSELF
  220. ncctooldia=tooldia,
  221. overlap=overlap,
  222. order=order,
  223. margin=margin,
  224. has_offset=has_offset,
  225. offset=offset,
  226. method=method_data,
  227. outname=outname,
  228. connect=connect,
  229. contour=contour,
  230. rest=rest,
  231. tools_storage=ncc_tools,
  232. plot=False,
  233. run_threaded=False)
  234. return
  235. # Non-Copper clear all polygons found within the box object from the the non_copper cleared object
  236. if 'box' in args: # Reference Object
  237. box_name = args['box']
  238. # Get box source object.
  239. try:
  240. box_obj = self.app.collection.get_by_name(str(box_name))
  241. except Exception as e:
  242. log.debug("TclCommandCopperClear.execute() --> %s" % str(e))
  243. self.raise_tcl_error("%s: %s" % (_("Could not retrieve object"), name))
  244. return "Could not retrieve object: %s" % name
  245. self.app.ncclear_tool.clear_copper_tcl(ncc_obj=obj,
  246. sel_obj=box_obj,
  247. select_method=2, # REFERENCE OBJECT
  248. ncctooldia=tooldia,
  249. overlap=overlap,
  250. order=order,
  251. margin=margin,
  252. has_offset=has_offset,
  253. offset=offset,
  254. method=method_data,
  255. outname=outname,
  256. connect=connect,
  257. contour=contour,
  258. rest=rest,
  259. tools_storage=ncc_tools,
  260. plot=False,
  261. run_threaded=False)
  262. return
  263. # if the program reached this then it's an error because neither -all or -box <value> was used.
  264. self.raise_tcl_error('%s' % _("Expected either -box <value> or -all."))
  265. return "Expected either -box <value> or -all. Copper clearing failed."