TclCommandCutout.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. from ObjectCollection import *
  2. from tclCommands.TclCommand import TclCommand
  3. from shapely.ops import cascaded_union
  4. from shapely.geometry import LineString
  5. class TclCommandCutout(TclCommand):
  6. """
  7. Tcl shell command to create a board cutout geometry. Rectangular shape only.
  8. example:
  9. """
  10. # List of all command aliases, to be able use old
  11. # names for backward compatibility (add_poly, add_polygon)
  12. aliases = ['cutout']
  13. # Dictionary of types from Tcl command, needs to be ordered
  14. arg_names = collections.OrderedDict([
  15. ('name', str),
  16. ])
  17. # Dictionary of types from Tcl command, needs to be ordered,
  18. # this is for options like -optionname value
  19. option_types = collections.OrderedDict([
  20. ('dia', float),
  21. ('margin', float),
  22. ('gapsize', float),
  23. ('gaps', str)
  24. ])
  25. # array of mandatory options for current Tcl command: required = {'name','outname'}
  26. required = ['name']
  27. # structured help for current command, args needs to be ordered
  28. help = {
  29. 'main': 'Creates board cutout from an object (Gerber or Geometry) with a rectangular shape',
  30. 'args': collections.OrderedDict([
  31. ('name', 'Name of the object.'),
  32. ('dia', 'Tool diameter. Default = 0.1'),
  33. ('margin', 'Margin over bounds. Default = 0.001'),
  34. ('gapsize', 'Size of gap. Default = 0.1'),
  35. ('gaps', "Type of gaps. Can be: 'tb' = top-bottom, 'lr' = left-right and '4' = one each side. Default = 4"),
  36. ]),
  37. 'examples': []
  38. }
  39. def execute(self, args, unnamed_args):
  40. """
  41. :param args:
  42. :param unnamed_args:
  43. :return:
  44. """
  45. if 'name' in args:
  46. name = args['name']
  47. else:
  48. self.app.inform.emit(
  49. "[WARNING]The name of the object for which cutout is done is missing. Add it and retry.")
  50. return
  51. if 'margin' in args:
  52. margin_par = args['margin']
  53. else:
  54. margin_par = 0.001
  55. if 'dia' in args:
  56. dia_par = args['dia']
  57. else:
  58. dia_par = 0.1
  59. if 'gaps' in args:
  60. gaps_par = args['gaps']
  61. else:
  62. gaps_par = 4
  63. if 'gapsize' in args:
  64. gapsize_par = args['gapsize']
  65. else:
  66. gapsize_par = 0.1
  67. try:
  68. obj = self.app.collection.get_by_name(str(name))
  69. except Exception as e:
  70. log.debug("TclCommandCutout.execute() --> %s" % str(e))
  71. return "Could not retrieve object: %s" % name
  72. def geo_init_me(geo_obj, app_obj):
  73. margin = margin_par + dia_par / 2
  74. gap_size = dia_par + gapsize_par
  75. minx, miny, maxx, maxy = obj.bounds()
  76. minx -= margin
  77. maxx += margin
  78. miny -= margin
  79. maxy += margin
  80. midx = 0.5 * (minx + maxx)
  81. midy = 0.5 * (miny + maxy)
  82. hgap = 0.5 * gap_size
  83. pts = [[midx - hgap, maxy],
  84. [minx, maxy],
  85. [minx, midy + hgap],
  86. [minx, midy - hgap],
  87. [minx, miny],
  88. [midx - hgap, miny],
  89. [midx + hgap, miny],
  90. [maxx, miny],
  91. [maxx, midy - hgap],
  92. [maxx, midy + hgap],
  93. [maxx, maxy],
  94. [midx + hgap, maxy]]
  95. cases = {"tb": [[pts[0], pts[1], pts[4], pts[5]],
  96. [pts[6], pts[7], pts[10], pts[11]]],
  97. "lr": [[pts[9], pts[10], pts[1], pts[2]],
  98. [pts[3], pts[4], pts[7], pts[8]]],
  99. "4": [[pts[0], pts[1], pts[2]],
  100. [pts[3], pts[4], pts[5]],
  101. [pts[6], pts[7], pts[8]],
  102. [pts[9], pts[10], pts[11]]]}
  103. cuts = cases[gaps_par]
  104. geo_obj.solid_geometry = cascaded_union([LineString(segment) for segment in cuts])
  105. try:
  106. self.app.new_object("geometry", name + "_cutout", geo_init_me, plot=False)
  107. self.app.inform.emit("[success] Rectangular-form Cutout operation finished.")
  108. except Exception as e:
  109. return "Operation failed: %s" % str(e)