TclCommandPaint.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 TclCommandPaint(TclCommand):
  12. """
  13. Paint the interior of polygons
  14. """
  15. # Array of all command aliases, to be able use old names for backward compatibility (add_poly, add_polygon)
  16. aliases = ['paint']
  17. description = '%s %s' % ("--", "Paint polygons in the specified object by covering them with toolpaths.")
  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. ('all', str),
  32. ('single', str),
  33. ('ref', str),
  34. ('box', str),
  35. ('x', float),
  36. ('y', float),
  37. ('outname', str),
  38. ])
  39. # array of mandatory options for current Tcl command: required = {'name','outname'}
  40. required = ['name']
  41. # structured help for current command, args needs to be ordered
  42. help = {
  43. 'main': "Paint polygons in the specified object by covering them with toolpaths.",
  44. 'args': collections.OrderedDict([
  45. ('name', 'Name of the source Geometry object. String.'),
  46. ('tooldia', 'Diameter of the tool to be used. Can be a comma separated list of diameters. No space is '
  47. 'allowed between tool diameters. E.g: correct: 0.5,1 / incorrect: 0.5, 1'),
  48. ('overlap', 'Percentage of tool diameter to overlap current pass over previous pass. Float [0, 99.9999]\n'
  49. 'E.g: for a 25% from tool diameter overlap use -overlap 25'),
  50. ('margin', 'Bounding box margin. Float number.'),
  51. ('order', 'Can have the values: "no", "fwd" and "rev". String.'
  52. 'It is useful when there are multiple tools in tooldia parameter.'
  53. '"no" -> the order used is the one provided.'
  54. '"fwd" -> tools are ordered from smallest to biggest.'
  55. '"rev" -> tools are ordered from biggest to smallest.'),
  56. ('method', 'Algorithm for painting. Can be: "standard", "seed" or "lines".'),
  57. ('connect', 'Draw lines to minimize tool lifts. True (1) or False (0)'),
  58. ('contour', 'Cut around the perimeter of the painting. True (1) or False (0)'),
  59. ('all', 'Paint all polygons in the object. True (1) or False (0)'),
  60. ('single', 'Paint a single polygon specified by "x" and "y" parameters. True (1) or False (0)'),
  61. ('ref', 'Paint all polygons within a specified object with the name in "box" parameter. '
  62. 'True (1) or False (0)'),
  63. ('box', 'name of the object to be used as paint reference when selecting "ref"" True. String.'),
  64. ('x', 'X value of coordinate for the selection of a single polygon. Float number.'),
  65. ('y', 'Y value of coordinate for the selection of a single polygon. Float number.'),
  66. ('outname', 'Name of the resulting Geometry object. String.'),
  67. ]),
  68. 'examples': ["paint obj_name -tooldia 0.3 -margin 0.1 -method 'seed' -all True"]
  69. }
  70. def execute(self, args, unnamed_args):
  71. """
  72. execute current TCL shell command
  73. :param args: array of known named arguments and options
  74. :param unnamed_args: array of other values which were passed into command
  75. without -somename and we do not have them in known arg_names
  76. :return: None or exception
  77. """
  78. name = args['name']
  79. # Get source object.
  80. try:
  81. obj = self.app.collection.get_by_name(str(name))
  82. except Exception as e:
  83. log.debug("TclCommandPaint.execute() --> %s" % str(e))
  84. self.raise_tcl_error("%s: %s" % (_("Could not retrieve object"), name))
  85. return "Could not retrieve object: %s" % name
  86. if 'tooldia' in args:
  87. tooldia = str(args['tooldia'])
  88. else:
  89. tooldia = float(self.app.defaults["tools_paintoverlap"])
  90. if 'overlap' in args:
  91. overlap = float(args['overlap']) / 100.0
  92. else:
  93. overlap = float(self.app.defaults["tools_paintoverlap"]) / 100.0
  94. if 'order' in args:
  95. order = args['order']
  96. else:
  97. order = str(self.app.defaults["tools_paintorder"])
  98. if 'margin' in args:
  99. margin = float(args['margin'])
  100. else:
  101. margin = float(self.app.defaults["tools_paintmargin"])
  102. if 'method' in args:
  103. method = args['method']
  104. else:
  105. method = str(self.app.defaults["tools_paintmethod"])
  106. if 'connect' in args:
  107. connect = bool(eval(args['connect']))
  108. else:
  109. connect = eval(str(self.app.defaults["tools_pathconnect"]))
  110. if 'contour' in args:
  111. contour = bool(eval(args['contour']))
  112. else:
  113. contour = eval(str(self.app.defaults["tools_paintcontour"]))
  114. if 'outname' in args:
  115. outname = args['outname']
  116. else:
  117. outname = name + "_paint"
  118. try:
  119. tools = [float(eval(dia)) for dia in tooldia.split(",") if dia != '']
  120. except AttributeError:
  121. tools = [float(tooldia)]
  122. # store here the default data for Geometry Data
  123. default_data = {}
  124. default_data.update({
  125. "name": '_paint',
  126. "plot": self.app.defaults["geometry_plot"],
  127. "cutz": self.app.defaults["geometry_cutz"],
  128. "vtipdia": 0.1,
  129. "vtipangle": 30,
  130. "travelz": self.app.defaults["geometry_travelz"],
  131. "feedrate": self.app.defaults["geometry_feedrate"],
  132. "feedrate_z": self.app.defaults["geometry_feedrate_z"],
  133. "feedrate_rapid": self.app.defaults["geometry_feedrate_rapid"],
  134. "dwell": self.app.defaults["geometry_dwell"],
  135. "dwelltime": self.app.defaults["geometry_dwelltime"],
  136. "multidepth": self.app.defaults["geometry_multidepth"],
  137. "ppname_g": self.app.defaults["geometry_ppname_g"],
  138. "depthperpass": self.app.defaults["geometry_depthperpass"],
  139. "extracut": self.app.defaults["geometry_extracut"],
  140. "extracut_length": self.app.defaults["geometry_extracut_length"],
  141. "toolchange": self.app.defaults["geometry_toolchange"],
  142. "toolchangez": self.app.defaults["geometry_toolchangez"],
  143. "endz": self.app.defaults["geometry_endz"],
  144. "endxy": self.app.defaults["geometry_endxy"],
  145. "spindlespeed": self.app.defaults["geometry_spindlespeed"],
  146. "toolchangexy": self.app.defaults["geometry_toolchangexy"],
  147. "startz": self.app.defaults["geometry_startz"],
  148. "tooldia": self.app.defaults["tools_painttooldia"],
  149. "paintmargin": self.app.defaults["tools_paintmargin"],
  150. "paintmethod": self.app.defaults["tools_paintmethod"],
  151. "selectmethod": self.app.defaults["tools_selectmethod"],
  152. "pathconnect": self.app.defaults["tools_pathconnect"],
  153. "paintcontour": self.app.defaults["tools_paintcontour"],
  154. "paintoverlap": self.app.defaults["tools_paintoverlap"]
  155. })
  156. paint_tools = {}
  157. tooluid = 0
  158. for tool in tools:
  159. tooluid += 1
  160. paint_tools.update({
  161. int(tooluid): {
  162. 'tooldia': float('%.*f' % (obj.decimals, tool)),
  163. 'offset': 'Path',
  164. 'offset_value': 0.0,
  165. 'type': 'Iso',
  166. 'tool_type': 'C1',
  167. 'data': dict(default_data),
  168. 'solid_geometry': []
  169. }
  170. })
  171. if obj is None:
  172. return "Object not found: %s" % name
  173. # Paint all polygons in the painted object
  174. if 'all' in args and bool(eval(args['all'])) is True:
  175. self.app.paint_tool.paint_poly_all(obj=obj,
  176. tooldia=tooldia,
  177. overlap=overlap,
  178. order=order,
  179. margin=margin,
  180. method=method,
  181. outname=outname,
  182. connect=connect,
  183. contour=contour,
  184. tools_storage=paint_tools,
  185. plot=False,
  186. run_threaded=False)
  187. return
  188. # Paint single polygon in the painted object
  189. elif 'single' in args and bool(eval(args['single'])) is True:
  190. if 'x' not in args or 'y' not in args:
  191. self.raise_tcl_error('%s' % _("Expected -x <value> and -y <value>."))
  192. else:
  193. x = args['x']
  194. y = args['y']
  195. self.app.paint_tool.paint_poly(obj=obj,
  196. inside_pt=[x, y],
  197. tooldia=tooldia,
  198. overlap=overlap,
  199. order=order,
  200. margin=margin,
  201. method=method,
  202. outname=outname,
  203. connect=connect,
  204. contour=contour,
  205. tools_storage=paint_tools,
  206. plot=False,
  207. run_threaded=False)
  208. return
  209. # Paint all polygons found within the box object from the the painted object
  210. elif 'ref' in args and bool(eval(args['ref'])) is True:
  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("TclCommandPaint.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.paint_tool.paint_poly_ref(obj=obj,
  223. sel_obj=box_obj,
  224. tooldia=tooldia,
  225. overlap=overlap,
  226. order=order,
  227. margin=margin,
  228. method=method,
  229. outname=outname,
  230. connect=connect,
  231. contour=contour,
  232. tools_storage=paint_tools,
  233. plot=False,
  234. run_threaded=False)
  235. return
  236. else:
  237. self.raise_tcl_error("%s:" % _("There was none of the following args: 'ref', 'single', 'all'.\n"
  238. "Paint failed."))
  239. return "There was none of the following args: 'ref', 'single', 'all'.\n" \
  240. "Paint failed."