TclCommandPaint.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. # 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. ('all', bool),
  31. ('single', bool),
  32. ('ref', bool),
  33. ('box', str),
  34. ('x', float),
  35. ('y', float),
  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': "Paint polygons",
  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 painting. 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. ('all', 'Paint all polygons in the object. True or False'),
  58. ('single', 'Paint a single polygon specified by "x" and "y" parameters. True or False'),
  59. ('ref', 'Paint all polygons within a specified object with the name in "box" parameter. True or False'),
  60. ('box', 'name of the object to be used as paint reference when selecting "ref"" True. String.'),
  61. ('x', 'X value of coordinate for the selection of a single polygon. Float number.'),
  62. ('y', 'Y value of coordinate for the selection of a single polygon. Float number.'),
  63. ('outname', 'Name of the resulting Geometry object. String.'),
  64. ]),
  65. 'examples': []
  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("TclCommandPaint.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 'tooldia' in args:
  84. tooldia = str(args['tooldia'])
  85. else:
  86. tooldia = float(self.app.defaults["tools_paintoverlap"])
  87. if 'overlap' in args:
  88. overlap = float(args['overlap'])
  89. else:
  90. overlap = float(self.app.defaults["tools_paintoverlap"])
  91. if 'order' in args:
  92. order = args['order']
  93. else:
  94. order = str(self.app.defaults["tools_paintorder"])
  95. if 'margin' in args:
  96. margin = float(args['margin'])
  97. else:
  98. margin = float(self.app.defaults["tools_paintmargin"])
  99. if 'method' in args:
  100. method = args['method']
  101. else:
  102. method = str(self.app.defaults["tools_paintmethod"])
  103. if 'connect' in args:
  104. connect = bool(args['connect'])
  105. else:
  106. connect = eval(str(self.app.defaults["tools_pathconnect"]))
  107. if 'contour' in args:
  108. contour = bool(args['contour'])
  109. else:
  110. contour = eval(str(self.app.defaults["tools_paintcontour"]))
  111. if 'outname' in args:
  112. outname = args['outname']
  113. else:
  114. outname = name + "_paint"
  115. try:
  116. tools = [float(eval(dia)) for dia in tooldia.split(",") if dia != '']
  117. except AttributeError:
  118. tools = [float(tooldia)]
  119. # store here the default data for Geometry Data
  120. default_data = {}
  121. default_data.update({
  122. "name": '_paint',
  123. "plot": self.app.defaults["geometry_plot"],
  124. "cutz": self.app.defaults["geometry_cutz"],
  125. "vtipdia": 0.1,
  126. "vtipangle": 30,
  127. "travelz": self.app.defaults["geometry_travelz"],
  128. "feedrate": self.app.defaults["geometry_feedrate"],
  129. "feedrate_z": self.app.defaults["geometry_feedrate_z"],
  130. "feedrate_rapid": self.app.defaults["geometry_feedrate_rapid"],
  131. "dwell": self.app.defaults["geometry_dwell"],
  132. "dwelltime": self.app.defaults["geometry_dwelltime"],
  133. "multidepth": self.app.defaults["geometry_multidepth"],
  134. "ppname_g": self.app.defaults["geometry_ppname_g"],
  135. "depthperpass": self.app.defaults["geometry_depthperpass"],
  136. "extracut": self.app.defaults["geometry_extracut"],
  137. "extracut_length": self.app.defaults["geometry_extracut_length"],
  138. "toolchange": self.app.defaults["geometry_toolchange"],
  139. "toolchangez": self.app.defaults["geometry_toolchangez"],
  140. "endz": self.app.defaults["geometry_endz"],
  141. "spindlespeed": self.app.defaults["geometry_spindlespeed"],
  142. "toolchangexy": self.app.defaults["geometry_toolchangexy"],
  143. "startz": self.app.defaults["geometry_startz"],
  144. "tooldia": self.app.defaults["tools_painttooldia"],
  145. "paintmargin": self.app.defaults["tools_paintmargin"],
  146. "paintmethod": self.app.defaults["tools_paintmethod"],
  147. "selectmethod": self.app.defaults["tools_selectmethod"],
  148. "pathconnect": self.app.defaults["tools_pathconnect"],
  149. "paintcontour": self.app.defaults["tools_paintcontour"],
  150. "paintoverlap": self.app.defaults["tools_paintoverlap"]
  151. })
  152. paint_tools = dict()
  153. tooluid = 0
  154. for tool in tools:
  155. tooluid += 1
  156. paint_tools.update({
  157. int(tooluid): {
  158. 'tooldia': float('%.*f' % (obj.decimals, tool)),
  159. 'offset': 'Path',
  160. 'offset_value': 0.0,
  161. 'type': 'Iso',
  162. 'tool_type': 'C1',
  163. 'data': dict(default_data),
  164. 'solid_geometry': []
  165. }
  166. })
  167. if obj is None:
  168. return "Object not found: %s" % name
  169. # Paint all polygons in the painted object
  170. if 'all' in args and bool(args['all']) is True:
  171. self.app.paint_tool.paint_poly_all(obj=obj,
  172. tooldia=tooldia,
  173. overlap=overlap,
  174. order=order,
  175. margin=margin,
  176. method=method,
  177. outname=outname,
  178. connect=connect,
  179. contour=contour,
  180. tools_storage=paint_tools,
  181. plot=False,
  182. run_threaded=False)
  183. return
  184. # Paint single polygon in the painted object
  185. elif 'single' in args and bool(args['single']) is True:
  186. if 'x' not in args or 'y' not in args:
  187. self.raise_tcl_error('%s' % _("Expected -x <value> and -y <value>."))
  188. else:
  189. x = args['x']
  190. y = args['y']
  191. self.app.paint_tool.paint_poly(obj=obj,
  192. inside_pt=[x, y],
  193. tooldia=tooldia,
  194. overlap=overlap,
  195. order=order,
  196. margin=margin,
  197. method=method,
  198. outname=outname,
  199. connect=connect,
  200. contour=contour,
  201. tools_storage=paint_tools,
  202. plot=False,
  203. run_threaded=False)
  204. return
  205. # Paint all polygons found within the box object from the the painted object
  206. elif 'ref' in args and bool(args['ref']) is True:
  207. if 'box' not in args:
  208. self.raise_tcl_error('%s' % _("Expected -box <value>."))
  209. else:
  210. box_name = args['box']
  211. # Get box source object.
  212. try:
  213. box_obj = self.app.collection.get_by_name(str(box_name))
  214. except Exception as e:
  215. log.debug("TclCommandPaint.execute() --> %s" % str(e))
  216. self.raise_tcl_error("%s: %s" % (_("Could not retrieve box object"), name))
  217. return "Could not retrieve object: %s" % name
  218. self.app.paint_tool.paint_poly_ref(obj=obj,
  219. sel_obj=box_obj,
  220. tooldia=tooldia,
  221. overlap=overlap,
  222. order=order,
  223. margin=margin,
  224. method=method,
  225. outname=outname,
  226. connect=connect,
  227. contour=contour,
  228. tools_storage=paint_tools,
  229. plot=False,
  230. run_threaded=False)
  231. return
  232. else:
  233. self.raise_tcl_error("%s:" % _("There was none of the following args: 'ref', 'single', 'all'.\n"
  234. "Paint failed."))
  235. return "There was none of the following args: 'ref', 'single', 'all'.\n" \
  236. "Paint failed."