TclCommandGeoCutout.py 15 KB

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