TclCommandCutout.py 4.3 KB

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