TclCommandGeoCutout.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. if gaps_u == 8 or gaps_u == '2tb':
  160. subtract_rectangle(cutout_obj,
  161. px - gapsize + lenghtx / 4,
  162. ymin - gapsize,
  163. px + gapsize + lenghtx / 4,
  164. ymax + gapsize)
  165. subtract_rectangle(cutout_obj,
  166. px - gapsize - lenghtx / 4,
  167. ymin - gapsize,
  168. px + gapsize - lenghtx / 4,
  169. ymax + gapsize)
  170. if gaps_u == 4 or gaps_u == 'lr':
  171. subtract_rectangle(cutout_obj,
  172. xmin - gapsize,
  173. py - gapsize,
  174. xmax + gapsize,
  175. py + gapsize)
  176. if gaps_u == 4 or gaps_u == 'tb':
  177. subtract_rectangle(cutout_obj,
  178. px - gapsize,
  179. ymin - gapsize,
  180. px + gapsize,
  181. ymax + gapsize)
  182. cutout_obj.plot()
  183. self.app.inform.emit("[success] Any-form Cutout operation finished.")
  184. elif isinstance(cutout_obj, FlatCAMGerber):
  185. def geo_init(geo_obj, app_obj):
  186. try:
  187. geo = cutout_obj.isolation_geometry((dia / 2), iso_type=0, corner=2, follow=None)
  188. except Exception as exc:
  189. log.debug("TclCommandGeoCutout.execute() --> %s" % str(exc))
  190. return 'fail'
  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 = geo
  226. outname = cutout_obj.options["name"] + "_cutout"
  227. self.app.new_object('geometry', outname, geo_init)
  228. cutout_obj = self.app.collection.get_by_name(outname)
  229. else:
  230. self.app.inform.emit("[ERROR]Cancelled. Object type is not supported.")
  231. return