TclCommandPaint.py 12 KB

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