TclCommandGeoCutout.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. def execute(self, args, unnamed_args):
  50. """
  51. :param args:
  52. :param unnamed_args:
  53. :return:
  54. """
  55. def subtract_rectangle(obj_, x0, y0, x1, y1):
  56. pts = [(x0, y0), (x1, y0), (x1, y1), (x0, y1)]
  57. obj_.subtract_polygon(pts)
  58. if 'name' in args:
  59. name = args['name']
  60. else:
  61. self.app.inform.emit(
  62. "[WARNING]The name of the object for which cutout is done is missing. Add it and retry.")
  63. return
  64. if 'margin' in args:
  65. margin = args['margin']
  66. else:
  67. margin = 0.001
  68. if 'dia' in args:
  69. dia = args['dia']
  70. else:
  71. dia = 0.1
  72. if 'gaps' in args:
  73. gaps = args['gaps']
  74. else:
  75. gaps = 4
  76. if 'gapsize' in args:
  77. gapsize = args['gapsize']
  78. else:
  79. gapsize = 0.1
  80. # Get source object.
  81. try:
  82. cutout_obj = self.app.collection.get_by_name(str(name))
  83. except:
  84. return "Could not retrieve object: %s" % name
  85. if 0 in {dia}:
  86. self.app.inform.emit("[WARNING]Tool Diameter is zero value. Change it to a positive integer.")
  87. return "Tool Diameter is zero value. Change it to a positive integer."
  88. if gaps not in ['lr', 'tb', '2lr', '2tb', 4, 8]:
  89. self.app.inform.emit("[WARNING]Gaps value can be only one of: 'lr', 'tb', '2lr', '2tb', 4 or 8. "
  90. "Fill in a correct value and retry. ")
  91. return
  92. # Get min and max data for each object as we just cut rectangles across X or Y
  93. xmin, ymin, xmax, ymax = cutout_obj.bounds()
  94. px = 0.5 * (xmin + xmax) + margin
  95. py = 0.5 * (ymin + ymax) + margin
  96. lenghtx = (xmax - xmin) + (margin * 2)
  97. lenghty = (ymax - ymin) + (margin * 2)
  98. gapsize = gapsize + (dia / 2)
  99. if isinstance(cutout_obj, FlatCAMGeometry):
  100. # rename the obj name so it can be identified as cutout
  101. cutout_obj.options["name"] += "_cutout"
  102. elif isinstance(cutout_obj, FlatCAMGerber):
  103. def geo_init(geo_obj, app_obj):
  104. try:
  105. geo_obj.solid_geometry = cutout_obj.isolation_geometry((dia / 2), iso_type=0)
  106. except Exception as e:
  107. log.debug("TclCommandGeoCutout.execute() --> %s" % str(e))
  108. return 'fail'
  109. outname = cutout_obj.options["name"] + "_cutout"
  110. self.app.new_object('geometry', outname, geo_init)
  111. cutout_obj = self.app.collection.get_by_name(outname)
  112. else:
  113. self.app.inform.emit("[ERROR]Cancelled. Object type is not supported.")
  114. return
  115. try:
  116. gaps_u = int(gaps)
  117. except ValueError:
  118. gaps_u = gaps
  119. if gaps_u == 8 or gaps_u == '2lr':
  120. subtract_rectangle(cutout_obj,
  121. xmin - gapsize, # botleft_x
  122. py - gapsize + lenghty / 4, # botleft_y
  123. xmax + gapsize, # topright_x
  124. py + gapsize + lenghty / 4) # topright_y
  125. subtract_rectangle(cutout_obj,
  126. xmin - gapsize,
  127. py - gapsize - lenghty / 4,
  128. xmax + gapsize,
  129. py + gapsize - lenghty / 4)
  130. if gaps_u == 8 or gaps_u == '2tb':
  131. subtract_rectangle(cutout_obj,
  132. px - gapsize + lenghtx / 4,
  133. ymin - gapsize,
  134. px + gapsize + lenghtx / 4,
  135. ymax + gapsize)
  136. subtract_rectangle(cutout_obj,
  137. px - gapsize - lenghtx / 4,
  138. ymin - gapsize,
  139. px + gapsize - lenghtx / 4,
  140. ymax + gapsize)
  141. if gaps_u == 4 or gaps_u == 'lr':
  142. subtract_rectangle(cutout_obj,
  143. xmin - gapsize,
  144. py - gapsize,
  145. xmax + gapsize,
  146. py + gapsize)
  147. if gaps_u == 4 or gaps_u == 'tb':
  148. subtract_rectangle(cutout_obj,
  149. px - gapsize,
  150. ymin - gapsize,
  151. px + gapsize,
  152. ymax + gapsize)
  153. cutout_obj.plot()
  154. self.app.inform.emit("[success]Any-form Cutout operation finished.")