TclCommandGeoCutout.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. from ObjectCollection import *
  2. from tclCommands.TclCommand import TclCommandSignaled
  3. class TclCommandGeoCutout(TclCommandSignaled):
  4. """
  5. Tcl shell command to create a board cutout geometry. Allow cutout for any shape. Cuts holding gaps from geometry.
  6. example:
  7. """
  8. # List of all command aliases, to be able use old
  9. # names for backward compatibility (add_poly, add_polygon)
  10. aliases = ['geocutout', 'geoc']
  11. # Dictionary of types from Tcl command, needs to be ordered
  12. arg_names = collections.OrderedDict([
  13. ('name', str),
  14. ])
  15. # Dictionary of types from Tcl command, needs to be ordered,
  16. # this is for options like -optionname value
  17. option_types = collections.OrderedDict([
  18. ('dia', float),
  19. ('margin', float),
  20. ('gapsize', float),
  21. ('gaps', str)
  22. ])
  23. # array of mandatory options for current Tcl command: required = {'name','outname'}
  24. required = ['name']
  25. # structured help for current command, args needs to be ordered
  26. help = {
  27. 'main': 'Creates board cutout from an object (Gerber or Geometry) of any shape',
  28. 'args': collections.OrderedDict([
  29. ('name', 'Name of the object.'),
  30. ('dia', 'Tool diameter.'),
  31. ('margin', 'Margin over bounds.'),
  32. ('gapsize', 'size of gap.'),
  33. ('gaps', "type of gaps. Can be: 'tb' = top-bottom, 'lr' = left-right, '2tb' = 2top-2bottom, "
  34. "'2lr' = 2left-2right, '4' = 4 cuts, '8' = 8 cuts")
  35. ]),
  36. 'examples': [" #isolate margin for example from fritzing arduino shield or any svg etc\n" +
  37. " isolate BCu_margin -dia 3 -overlap 1\n" +
  38. "\n" +
  39. " #create exteriors from isolated object\n" +
  40. " exteriors BCu_margin_iso -outname BCu_margin_iso_exterior\n" +
  41. "\n" +
  42. " #delete isolated object if you dond need id anymore\n" +
  43. " delete BCu_margin_iso\n" +
  44. "\n" +
  45. " #finally cut holding gaps\n" +
  46. " geocutout BCu_margin_iso_exterior -dia 3 -gapsize 0.6 -gaps 4\n"]
  47. }
  48. def execute(self, args, unnamed_args):
  49. """
  50. :param args:
  51. :param unnamed_args:
  52. :return:
  53. """
  54. def subtract_rectangle(obj_, x0, y0, x1, y1):
  55. pts = [(x0, y0), (x1, y0), (x1, y1), (x0, y1)]
  56. obj_.subtract_polygon(pts)
  57. if 'name' in args:
  58. name = args['name']
  59. else:
  60. self.app.inform.emit(
  61. "[WARNING]The name of the object for which cutout is done is missing. Add it and retry.")
  62. return
  63. if 'margin' in args:
  64. margin = args['margin']
  65. else:
  66. margin = 0.001
  67. if 'dia' in args:
  68. dia = args['dia']
  69. else:
  70. dia = 0.1
  71. if 'gaps' in args:
  72. gaps = args['gaps']
  73. else:
  74. gaps = 4
  75. if 'gapsize' in args:
  76. gapsize = args['gapsize']
  77. else:
  78. gapsize = 0.1
  79. # Get source object.
  80. try:
  81. cutout_obj = self.app.collection.get_by_name(str(name))
  82. except:
  83. return "Could not retrieve object: %s" % name
  84. if 0 in {dia}:
  85. self.app.inform.emit("[WARNING]Tool Diameter is zero value. Change it to a positive integer.")
  86. return "Tool Diameter is zero value. Change it to a positive integer."
  87. if gaps not in ['lr', 'tb', '2lr', '2tb', 4, 8]:
  88. self.app.inform.emit("[WARNING]Gaps value can be only one of: 'lr', 'tb', '2lr', '2tb', 4 or 8. "
  89. "Fill in a correct value and retry. ")
  90. return
  91. # Get min and max data for each object as we just cut rectangles across X or Y
  92. xmin, ymin, xmax, ymax = cutout_obj.bounds()
  93. px = 0.5 * (xmin + xmax) + margin
  94. py = 0.5 * (ymin + ymax) + margin
  95. lenghtx = (xmax - xmin) + (margin * 2)
  96. lenghty = (ymax - ymin) + (margin * 2)
  97. gapsize = gapsize + (dia / 2)
  98. if isinstance(cutout_obj, FlatCAMGeometry):
  99. # rename the obj name so it can be identified as cutout
  100. cutout_obj.options["name"] += "_cutout"
  101. elif isinstance(cutout_obj, FlatCAMGerber):
  102. def geo_init(geo_obj, app_obj):
  103. geo_obj.solid_geometry = obj_exteriors
  104. outname = cutout_obj.options["name"] + "_cutout"
  105. cutout_obj.isolate(dia=dia, passes=1, overlap=1, combine=False, outname="_temp")
  106. ext_obj = self.app.collection.get_by_name("_temp")
  107. try:
  108. obj_exteriors = ext_obj.get_exteriors()
  109. except:
  110. obj_exteriors = ext_obj.solid_geometry
  111. self.app.new_object('geometry', outname, geo_init)
  112. self.app.collection.set_all_inactive()
  113. self.app.collection.set_active("_temp")
  114. self.app.on_delete()
  115. cutout_obj = self.app.collection.get_by_name(outname)
  116. else:
  117. self.app.inform.emit("[ERROR]Cancelled. Object type is not supported.")
  118. return
  119. try:
  120. gaps_u = int(gaps)
  121. except ValueError:
  122. gaps_u = gaps
  123. if gaps_u == 8 or gaps_u == '2lr':
  124. subtract_rectangle(cutout_obj,
  125. xmin - gapsize, # botleft_x
  126. py - gapsize + lenghty / 4, # botleft_y
  127. xmax + gapsize, # topright_x
  128. py + gapsize + lenghty / 4) # topright_y
  129. subtract_rectangle(cutout_obj,
  130. xmin - gapsize,
  131. py - gapsize - lenghty / 4,
  132. xmax + gapsize,
  133. py + gapsize - lenghty / 4)
  134. if gaps_u == 8 or gaps_u == '2tb':
  135. subtract_rectangle(cutout_obj,
  136. px - gapsize + lenghtx / 4,
  137. ymin - gapsize,
  138. px + gapsize + lenghtx / 4,
  139. ymax + gapsize)
  140. subtract_rectangle(cutout_obj,
  141. px - gapsize - lenghtx / 4,
  142. ymin - gapsize,
  143. px + gapsize - lenghtx / 4,
  144. ymax + gapsize)
  145. if gaps_u == 4 or gaps_u == 'lr':
  146. subtract_rectangle(cutout_obj,
  147. xmin - gapsize,
  148. py - gapsize,
  149. xmax + gapsize,
  150. py + gapsize)
  151. if gaps_u == 4 or gaps_u == 'tb':
  152. subtract_rectangle(cutout_obj,
  153. px - gapsize,
  154. ymin - gapsize,
  155. px + gapsize,
  156. ymax + gapsize)
  157. cutout_obj.plot()
  158. self.app.inform.emit("[success]Any-form Cutout operation finished.")