TclCommandGeoCutout.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. cutout_obj.options['xmin'] = xmin
  137. cutout_obj.options['ymin'] = ymin
  138. cutout_obj.options['xmax'] = xmax
  139. cutout_obj.options['ymax'] = ymax
  140. px = 0.5 * (xmin + xmax) + margin
  141. py = 0.5 * (ymin + ymax) + margin
  142. lenghtx = (xmax - xmin) + (margin * 2)
  143. lenghty = (ymax - ymin) + (margin * 2)
  144. gapsize = gapsize / 2 + (dia / 2)
  145. try:
  146. gaps_u = int(gaps)
  147. except ValueError:
  148. gaps_u = gaps
  149. if isinstance(cutout_obj, FlatCAMGeometry):
  150. # rename the obj name so it can be identified as cutout
  151. # cutout_obj.options["name"] += "_cutout"
  152. # if gaps_u == 8 or gaps_u == '2lr':
  153. # subtract_rectangle(cutout_obj,
  154. # xmin - gapsize, # botleft_x
  155. # py - gapsize + lenghty / 4, # botleft_y
  156. # xmax + gapsize, # topright_x
  157. # py + gapsize + lenghty / 4) # topright_y
  158. # subtract_rectangle(cutout_obj,
  159. # xmin - gapsize,
  160. # py - gapsize - lenghty / 4,
  161. # xmax + gapsize,
  162. # py + gapsize - lenghty / 4)
  163. #
  164. # if gaps_u == 8 or gaps_u == '2tb':
  165. # subtract_rectangle(cutout_obj,
  166. # px - gapsize + lenghtx / 4,
  167. # ymin - gapsize,
  168. # px + gapsize + lenghtx / 4,
  169. # ymax + gapsize)
  170. # subtract_rectangle(cutout_obj,
  171. # px - gapsize - lenghtx / 4,
  172. # ymin - gapsize,
  173. # px + gapsize - lenghtx / 4,
  174. # ymax + gapsize)
  175. #
  176. # if gaps_u == 4 or gaps_u == 'lr':
  177. # subtract_rectangle(cutout_obj,
  178. # xmin - gapsize,
  179. # py - gapsize,
  180. # xmax + gapsize,
  181. # py + gapsize)
  182. #
  183. # if gaps_u == 4 or gaps_u == 'tb':
  184. # subtract_rectangle(cutout_obj,
  185. # px - gapsize,
  186. # ymin - gapsize,
  187. # px + gapsize,
  188. # ymax + gapsize)
  189. def geo_init(geo_obj, app_obj):
  190. geo = deepcopy(cutout_obj.solid_geometry)
  191. if gaps_u == 8 or gaps_u == '2lr':
  192. geo = substract_rectangle_geo(geo,
  193. xmin - gapsize, # botleft_x
  194. py - gapsize + lenghty / 4, # botleft_y
  195. xmax + gapsize, # topright_x
  196. py + gapsize + lenghty / 4) # topright_y
  197. geo = substract_rectangle_geo(geo,
  198. xmin - gapsize,
  199. py - gapsize - lenghty / 4,
  200. xmax + gapsize,
  201. py + gapsize - lenghty / 4)
  202. if gaps_u == 8 or gaps_u == '2tb':
  203. geo = substract_rectangle_geo(geo,
  204. px - gapsize + lenghtx / 4,
  205. ymin - gapsize,
  206. px + gapsize + lenghtx / 4,
  207. ymax + gapsize)
  208. geo = substract_rectangle_geo(geo,
  209. px - gapsize - lenghtx / 4,
  210. ymin - gapsize,
  211. px + gapsize - lenghtx / 4,
  212. ymax + gapsize)
  213. if gaps_u == 4 or gaps_u == 'lr':
  214. geo = substract_rectangle_geo(geo,
  215. xmin - gapsize,
  216. py - gapsize,
  217. xmax + gapsize,
  218. py + gapsize)
  219. if gaps_u == 4 or gaps_u == 'tb':
  220. geo = substract_rectangle_geo(geo,
  221. px - gapsize,
  222. ymin - gapsize,
  223. px + gapsize,
  224. ymax + gapsize)
  225. geo_obj.solid_geometry = deepcopy(geo)
  226. geo_obj.options['xmin'] = cutout_obj.options['xmin']
  227. geo_obj.options['ymin'] = cutout_obj.options['ymin']
  228. geo_obj.options['xmax'] = cutout_obj.options['xmax']
  229. geo_obj.options['ymax'] = cutout_obj.options['ymax']
  230. app_obj.disable_plots(objects=[cutout_obj])
  231. app_obj.inform.emit("[success] Any-form Cutout operation finished.")
  232. outname = cutout_obj.options["name"] + "_cutout"
  233. self.app.new_object('geometry', outname, geo_init)
  234. # cutout_obj.plot()
  235. # self.app.inform.emit("[success] Any-form Cutout operation finished.")
  236. # self.app.plots_updated.emit()
  237. elif isinstance(cutout_obj, FlatCAMGerber):
  238. def geo_init(geo_obj, app_obj):
  239. try:
  240. geo = cutout_obj.isolation_geometry((dia / 2), iso_type=0, corner=2, follow=None)
  241. except Exception as exc:
  242. log.debug("TclCommandGeoCutout.execute() --> %s" % str(exc))
  243. return 'fail'
  244. if gaps_u == 8 or gaps_u == '2lr':
  245. geo = substract_rectangle_geo(geo,
  246. xmin - gapsize, # botleft_x
  247. py - gapsize + lenghty / 4, # botleft_y
  248. xmax + gapsize, # topright_x
  249. py + gapsize + lenghty / 4) # topright_y
  250. geo = substract_rectangle_geo(geo,
  251. xmin - gapsize,
  252. py - gapsize - lenghty / 4,
  253. xmax + gapsize,
  254. py + gapsize - lenghty / 4)
  255. if gaps_u == 8 or gaps_u == '2tb':
  256. geo = substract_rectangle_geo(geo,
  257. px - gapsize + lenghtx / 4,
  258. ymin - gapsize,
  259. px + gapsize + lenghtx / 4,
  260. ymax + gapsize)
  261. geo = substract_rectangle_geo(geo,
  262. px - gapsize - lenghtx / 4,
  263. ymin - gapsize,
  264. px + gapsize - lenghtx / 4,
  265. ymax + gapsize)
  266. if gaps_u == 4 or gaps_u == 'lr':
  267. geo = substract_rectangle_geo(geo,
  268. xmin - gapsize,
  269. py - gapsize,
  270. xmax + gapsize,
  271. py + gapsize)
  272. if gaps_u == 4 or gaps_u == 'tb':
  273. geo = substract_rectangle_geo(geo,
  274. px - gapsize,
  275. ymin - gapsize,
  276. px + gapsize,
  277. ymax + gapsize)
  278. geo_obj.solid_geometry = deepcopy(geo)
  279. geo_obj.options['xmin'] = cutout_obj.options['xmin']
  280. geo_obj.options['ymin'] = cutout_obj.options['ymin']
  281. geo_obj.options['xmax'] = cutout_obj.options['xmax']
  282. geo_obj.options['ymax'] = cutout_obj.options['ymax']
  283. app_obj.inform.emit("[success] Any-form Cutout operation finished.")
  284. outname = cutout_obj.options["name"] + "_cutout"
  285. self.app.new_object('geometry', outname, geo_init)
  286. cutout_obj = self.app.collection.get_by_name(outname)
  287. else:
  288. self.app.inform.emit("[ERROR]Cancelled. Object type is not supported.")
  289. return