TclCommandCutout.py 4.8 KB

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