TclCommandGeoCutout.py 15 KB

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