TclCommandCutoutAny.py 6.2 KB

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