TclCommandGeoCutout.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. from tclCommands.TclCommand import TclCommandSignaled
  2. from FlatCAMObj import FlatCAMGerber, FlatCAMGeometry
  3. import logging
  4. import collections
  5. from copy import deepcopy
  6. from shapely.ops import cascaded_union
  7. from shapely.geometry import Polygon, LineString, LinearRing
  8. import gettext
  9. import FlatCAMTranslation as fcTranslate
  10. import builtins
  11. log = logging.getLogger('base')
  12. fcTranslate.apply_language('strings')
  13. if '_' not in builtins.__dict__:
  14. _ = gettext.gettext
  15. class TclCommandGeoCutout(TclCommandSignaled):
  16. """
  17. Tcl shell command to create a board cutout geometry.
  18. Allow cutout for any shape.
  19. Cuts holding gaps from geometry.
  20. example:
  21. """
  22. # List of all command aliases, to be able use old
  23. # names for backward compatibility (add_poly, add_polygon)
  24. aliases = ['geocutout', 'geoc']
  25. description = '%s %s' % ("--", "Creates board cutout from an object (Gerber or Geometry) of any shape.")
  26. # Dictionary of types from Tcl command, needs to be ordered
  27. arg_names = collections.OrderedDict([
  28. ('name', str),
  29. ])
  30. # Dictionary of types from Tcl command, needs to be ordered,
  31. # this is for options like -optionname value
  32. option_types = collections.OrderedDict([
  33. ('dia', float),
  34. ('margin', float),
  35. ('gapsize', float),
  36. ('gaps', str),
  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': 'Creates board cutout from an object (Gerber or Geometry) of any shape.',
  44. 'args': collections.OrderedDict([
  45. ('name', 'Name of the object to be cutout. Required'),
  46. ('dia', 'Tool diameter.'),
  47. ('margin', 'Margin over bounds.'),
  48. ('gapsize', 'size of gap.'),
  49. ('gaps', "type of gaps. Can be: 'tb' = top-bottom, 'lr' = left-right, '2tb' = 2top-2bottom, "
  50. "'2lr' = 2left-2right, '4' = 4 cuts, '8' = 8 cuts"),
  51. ('outname', 'Name of the resulting Geometry object.'),
  52. ]),
  53. 'examples': [" #isolate margin for example from Fritzing arduino shield or any svg etc\n" +
  54. " isolate BCu_margin -dia 3 -overlap 1\n" +
  55. "\n" +
  56. " #create exteriors from isolated object\n" +
  57. " exteriors BCu_margin_iso -outname BCu_margin_iso_exterior\n" +
  58. "\n" +
  59. " #delete isolated object if you dond need id anymore\n" +
  60. " delete BCu_margin_iso\n" +
  61. "\n" +
  62. " #finally cut holding gaps\n" +
  63. " geocutout BCu_margin_iso_exterior -dia 3 -gapsize 0.6 -gaps 4 -outname cutout_geo\n"]
  64. }
  65. flat_geometry = []
  66. def execute(self, args, unnamed_args):
  67. """
  68. :param args:
  69. :param unnamed_args:
  70. :return:
  71. """
  72. # def subtract_rectangle(obj_, x0, y0, x1, y1):
  73. # pts = [(x0, y0), (x1, y0), (x1, y1), (x0, y1)]
  74. # obj_.subtract_polygon(pts)
  75. def substract_rectangle_geo(geo, x0, y0, x1, y1):
  76. pts = [(x0, y0), (x1, y0), (x1, y1), (x0, y1)]
  77. def flatten(geometry=None, reset=True, pathonly=False):
  78. """
  79. Creates a list of non-iterable linear geometry objects.
  80. Polygons are expanded into its exterior and interiors if specified.
  81. Results are placed in flat_geometry
  82. :param geometry: Shapely type or list or list of list of such.
  83. :param reset: Clears the contents of self.flat_geometry.
  84. :param pathonly: Expands polygons into linear elements.
  85. """
  86. if reset:
  87. self.flat_geometry = []
  88. # If iterable, expand recursively.
  89. try:
  90. for geo_el in geometry:
  91. if geo_el is not None:
  92. flatten(geometry=geo_el,
  93. reset=False,
  94. pathonly=pathonly)
  95. # Not iterable, do the actual indexing and add.
  96. except TypeError:
  97. if pathonly and type(geometry) == Polygon:
  98. self.flat_geometry.append(geometry.exterior)
  99. flatten(geometry=geometry.interiors,
  100. reset=False,
  101. pathonly=True)
  102. else:
  103. self.flat_geometry.append(geometry)
  104. return self.flat_geometry
  105. flat_geometry = flatten(geo, pathonly=True)
  106. polygon = Polygon(pts)
  107. toolgeo = cascaded_union(polygon)
  108. diffs = []
  109. for target in flat_geometry:
  110. if type(target) == LineString or type(target) == LinearRing:
  111. diffs.append(target.difference(toolgeo))
  112. else:
  113. log.warning("Not implemented.")
  114. return cascaded_union(diffs)
  115. if 'name' in args:
  116. name = args['name']
  117. else:
  118. self.app.inform.emit(
  119. "[WARNING] %s" % _("The name of the object for which cutout is done is missing. Add it and retry."))
  120. return
  121. if 'margin' in args:
  122. margin = float(args['margin'])
  123. else:
  124. margin = float(self.app.defaults["tools_cutoutmargin"])
  125. if 'dia' in args:
  126. dia = float(args['dia'])
  127. else:
  128. dia = float(self.app.defaults["tools_cutouttooldia"])
  129. if 'gaps' in args:
  130. gaps = args['gaps']
  131. else:
  132. gaps = str(self.app.defaults["tools_gaps_ff"])
  133. if 'gapsize' in args:
  134. gapsize = float(args['gapsize'])
  135. else:
  136. gapsize = float(self.app.defaults["tools_cutoutgapsize"])
  137. if 'outname' in args:
  138. outname = args['outname']
  139. else:
  140. outname = str(name) + "_cutout"
  141. # Get source object.
  142. try:
  143. cutout_obj = self.app.collection.get_by_name(str(name))
  144. except Exception as e:
  145. log.debug("TclCommandGeoCutout --> %s" % str(e))
  146. return "Could not retrieve object: %s" % name
  147. if 0 in {dia}:
  148. self.app.inform.emit(
  149. "[WARNING] %s" % _("Tool Diameter is zero value. Change it to a positive real number."))
  150. return "Tool Diameter is zero value. Change it to a positive real number."
  151. if gaps not in ['lr', 'tb', '2lr', '2tb', '4', '8']:
  152. self.app.inform.emit(
  153. "[WARNING] %s" % _("Gaps value can be only one of: 'lr', 'tb', '2lr', '2tb', 4 or 8."))
  154. return
  155. # Get min and max data for each object as we just cut rectangles across X or Y
  156. xmin, ymin, xmax, ymax = cutout_obj.bounds()
  157. cutout_obj.options['xmin'] = xmin
  158. cutout_obj.options['ymin'] = ymin
  159. cutout_obj.options['xmax'] = xmax
  160. cutout_obj.options['ymax'] = ymax
  161. px = 0.5 * (xmin + xmax) + margin
  162. py = 0.5 * (ymin + ymax) + margin
  163. lenghtx = (xmax - xmin) + (margin * 2)
  164. lenghty = (ymax - ymin) + (margin * 2)
  165. gapsize = gapsize / 2 + (dia / 2)
  166. try:
  167. gaps_u = int(gaps)
  168. except ValueError:
  169. gaps_u = gaps
  170. if isinstance(cutout_obj, FlatCAMGeometry):
  171. # rename the obj name so it can be identified as cutout
  172. # cutout_obj.options["name"] += "_cutout"
  173. # if gaps_u == 8 or gaps_u == '2lr':
  174. # subtract_rectangle(cutout_obj,
  175. # xmin - gapsize, # botleft_x
  176. # py - gapsize + lenghty / 4, # botleft_y
  177. # xmax + gapsize, # topright_x
  178. # py + gapsize + lenghty / 4) # topright_y
  179. # subtract_rectangle(cutout_obj,
  180. # xmin - gapsize,
  181. # py - gapsize - lenghty / 4,
  182. # xmax + gapsize,
  183. # py + gapsize - lenghty / 4)
  184. #
  185. # if gaps_u == 8 or gaps_u == '2tb':
  186. # subtract_rectangle(cutout_obj,
  187. # px - gapsize + lenghtx / 4,
  188. # ymin - gapsize,
  189. # px + gapsize + lenghtx / 4,
  190. # ymax + gapsize)
  191. # subtract_rectangle(cutout_obj,
  192. # px - gapsize - lenghtx / 4,
  193. # ymin - gapsize,
  194. # px + gapsize - lenghtx / 4,
  195. # ymax + gapsize)
  196. #
  197. # if gaps_u == 4 or gaps_u == 'lr':
  198. # subtract_rectangle(cutout_obj,
  199. # xmin - gapsize,
  200. # py - gapsize,
  201. # xmax + gapsize,
  202. # py + gapsize)
  203. #
  204. # if gaps_u == 4 or gaps_u == 'tb':
  205. # subtract_rectangle(cutout_obj,
  206. # px - gapsize,
  207. # ymin - gapsize,
  208. # px + gapsize,
  209. # ymax + gapsize)
  210. def geo_init(geo_obj, app_obj):
  211. geo = deepcopy(cutout_obj.solid_geometry)
  212. if gaps_u == 8 or gaps_u == '2lr':
  213. geo = substract_rectangle_geo(geo,
  214. xmin - gapsize, # botleft_x
  215. py - gapsize + lenghty / 4, # botleft_y
  216. xmax + gapsize, # topright_x
  217. py + gapsize + lenghty / 4) # topright_y
  218. geo = substract_rectangle_geo(geo,
  219. xmin - gapsize,
  220. py - gapsize - lenghty / 4,
  221. xmax + gapsize,
  222. py + gapsize - lenghty / 4)
  223. if gaps_u == 8 or gaps_u == '2tb':
  224. geo = substract_rectangle_geo(geo,
  225. px - gapsize + lenghtx / 4,
  226. ymin - gapsize,
  227. px + gapsize + lenghtx / 4,
  228. ymax + gapsize)
  229. geo = substract_rectangle_geo(geo,
  230. px - gapsize - lenghtx / 4,
  231. ymin - gapsize,
  232. px + gapsize - lenghtx / 4,
  233. ymax + gapsize)
  234. if gaps_u == 4 or gaps_u == 'lr':
  235. geo = substract_rectangle_geo(geo,
  236. xmin - gapsize,
  237. py - gapsize,
  238. xmax + gapsize,
  239. py + gapsize)
  240. if gaps_u == 4 or gaps_u == 'tb':
  241. geo = substract_rectangle_geo(geo,
  242. px - gapsize,
  243. ymin - gapsize,
  244. px + gapsize,
  245. ymax + gapsize)
  246. geo_obj.solid_geometry = deepcopy(geo)
  247. geo_obj.options['xmin'] = cutout_obj.options['xmin']
  248. geo_obj.options['ymin'] = cutout_obj.options['ymin']
  249. geo_obj.options['xmax'] = cutout_obj.options['xmax']
  250. geo_obj.options['ymax'] = cutout_obj.options['ymax']
  251. app_obj.disable_plots(objects=[cutout_obj])
  252. app_obj.inform.emit("[success] %s" % _("Any-form Cutout operation finished."))
  253. self.app.new_object('geometry', outname, geo_init, plot=False)
  254. # cutout_obj.plot()
  255. # self.app.inform.emit("[success] Any-form Cutout operation finished.")
  256. # self.app.plots_updated.emit()
  257. elif isinstance(cutout_obj, FlatCAMGerber):
  258. def geo_init(geo_obj, app_obj):
  259. try:
  260. geo = cutout_obj.isolation_geometry((dia / 2), iso_type=0, corner=2, follow=None)
  261. except Exception as exc:
  262. log.debug("TclCommandGeoCutout.execute() --> %s" % str(exc))
  263. return 'fail'
  264. if gaps_u == 8 or gaps_u == '2lr':
  265. geo = substract_rectangle_geo(geo,
  266. xmin - gapsize, # botleft_x
  267. py - gapsize + lenghty / 4, # botleft_y
  268. xmax + gapsize, # topright_x
  269. py + gapsize + lenghty / 4) # topright_y
  270. geo = substract_rectangle_geo(geo,
  271. xmin - gapsize,
  272. py - gapsize - lenghty / 4,
  273. xmax + gapsize,
  274. py + gapsize - lenghty / 4)
  275. if gaps_u == 8 or gaps_u == '2tb':
  276. geo = substract_rectangle_geo(geo,
  277. px - gapsize + lenghtx / 4,
  278. ymin - gapsize,
  279. px + gapsize + lenghtx / 4,
  280. ymax + gapsize)
  281. geo = substract_rectangle_geo(geo,
  282. px - gapsize - lenghtx / 4,
  283. ymin - gapsize,
  284. px + gapsize - lenghtx / 4,
  285. ymax + gapsize)
  286. if gaps_u == 4 or gaps_u == 'lr':
  287. geo = substract_rectangle_geo(geo,
  288. xmin - gapsize,
  289. py - gapsize,
  290. xmax + gapsize,
  291. py + gapsize)
  292. if gaps_u == 4 or gaps_u == 'tb':
  293. geo = substract_rectangle_geo(geo,
  294. px - gapsize,
  295. ymin - gapsize,
  296. px + gapsize,
  297. ymax + gapsize)
  298. geo_obj.solid_geometry = deepcopy(geo)
  299. geo_obj.options['xmin'] = cutout_obj.options['xmin']
  300. geo_obj.options['ymin'] = cutout_obj.options['ymin']
  301. geo_obj.options['xmax'] = cutout_obj.options['xmax']
  302. geo_obj.options['ymax'] = cutout_obj.options['ymax']
  303. app_obj.inform.emit("[success] %s" % _("Any-form Cutout operation finished."))
  304. self.app.new_object('geometry', outname, geo_init, plot=False)
  305. cutout_obj = self.app.collection.get_by_name(outname)
  306. else:
  307. self.app.inform.emit("[ERROR] %s" % _("Cancelled. Object type is not supported."))
  308. return