TclCommandCutout.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. from ObjectCollection import *
  2. from tclCommands.TclCommand import TclCommand
  3. class TclCommandCutout(TclCommand):
  4. """
  5. Tcl shell command to create a board cutout geometry. Rectangular shape only.
  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']
  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) with a rectangular shape',
  28. 'args': collections.OrderedDict([
  29. ('name', 'Name of the object.'),
  30. ('dia', 'Tool diameter. Default = 0.1'),
  31. ('margin', 'Margin over bounds. Default = 0.001'),
  32. ('gapsize', 'Size of gap. Default = 0.1'),
  33. ('gaps', "Type of gaps. Can be: 'tb' = top-bottom, 'lr' = left-right and '4' = one each side. Default = 4"),
  34. ]),
  35. 'examples': []
  36. }
  37. def execute(self, args, unnamed_args):
  38. """
  39. :param args:
  40. :param unnamed_args:
  41. :return:
  42. """
  43. if 'name' in args:
  44. name = args['name']
  45. else:
  46. self.app.inform.emit(
  47. "[WARNING]The name of the object for which cutout is done is missing. Add it and retry.")
  48. return
  49. if 'margin' in args:
  50. margin_par = args['margin']
  51. else:
  52. margin_par = 0.001
  53. if 'dia' in args:
  54. dia_par = args['dia']
  55. else:
  56. dia_par = 0.1
  57. if 'gaps' in args:
  58. gaps_par = args['gaps']
  59. else:
  60. gaps_par = 4
  61. if 'gapsize' in args:
  62. gapsize_par = args['gapsize']
  63. else:
  64. gapsize_par = 0.1
  65. try:
  66. obj = self.app.collection.get_by_name(str(name))
  67. except:
  68. return "Could not retrieve object: %s" % name
  69. def geo_init_me(geo_obj, app_obj):
  70. margin = margin_par + dia_par / 2
  71. gap_size = dia_par + gapsize_par
  72. minx, miny, maxx, maxy = obj.bounds()
  73. minx -= margin
  74. maxx += margin
  75. miny -= margin
  76. maxy += margin
  77. midx = 0.5 * (minx + maxx)
  78. midy = 0.5 * (miny + maxy)
  79. hgap = 0.5 * gap_size
  80. pts = [[midx - hgap, maxy],
  81. [minx, maxy],
  82. [minx, midy + hgap],
  83. [minx, midy - hgap],
  84. [minx, miny],
  85. [midx - hgap, miny],
  86. [midx + hgap, miny],
  87. [maxx, miny],
  88. [maxx, midy - hgap],
  89. [maxx, midy + hgap],
  90. [maxx, maxy],
  91. [midx + hgap, maxy]]
  92. cases = {"tb": [[pts[0], pts[1], pts[4], pts[5]],
  93. [pts[6], pts[7], pts[10], pts[11]]],
  94. "lr": [[pts[9], pts[10], pts[1], pts[2]],
  95. [pts[3], pts[4], pts[7], pts[8]]],
  96. "4": [[pts[0], pts[1], pts[2]],
  97. [pts[3], pts[4], pts[5]],
  98. [pts[6], pts[7], pts[8]],
  99. [pts[9], pts[10], pts[11]]]}
  100. cuts = cases[gaps_par]
  101. geo_obj.solid_geometry = cascaded_union([LineString(segment) for segment in cuts])
  102. try:
  103. self.app.new_object("geometry", name + "_cutout", geo_init_me)
  104. self.app.inform.emit("[success] Rectangular-form Cutout operation finished.")
  105. except Exception as e:
  106. return "Operation failed: %s" % str(e)