TclCommandGeoCutout.py 14 KB

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