TclCommandCutout.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. from tclCommands.TclCommand import *
  2. class TclCommandCutout(TclCommand):
  3. """
  4. Tcl shell command to create a board cutout geometry.
  5. example:
  6. """
  7. # List of all command aliases, to be able use old
  8. # names for backward compatibility (add_poly, add_polygon)
  9. aliases = ['cutout']
  10. # Dictionary of types from Tcl command, needs to be ordered
  11. arg_names = collections.OrderedDict([
  12. ('name', str),
  13. ])
  14. # Dictionary of types from Tcl command, needs to be ordered,
  15. # this is for options like -optionname value
  16. option_types = collections.OrderedDict([
  17. ('dia', float),
  18. ('margin', float),
  19. ('gapsize', float),
  20. ('gaps', str)
  21. ])
  22. # array of mandatory options for current Tcl command: required = {'name','outname'}
  23. required = ['name']
  24. # structured help for current command, args needs to be ordered
  25. help = {
  26. 'main': 'Creates board cutout.',
  27. 'args': collections.OrderedDict([
  28. ('name', 'Name of the object.'),
  29. ('dia', 'Tool diameter.'),
  30. ('margin', 'Margin over bounds.'),
  31. ('gapsize', 'size of gap.'),
  32. ('gaps', 'type of gaps.'),
  33. ]),
  34. 'examples': []
  35. }
  36. def execute(self, args, unnamed_args):
  37. """
  38. :param args:
  39. :param unnamed_args:
  40. :return:
  41. """
  42. name = args['name']
  43. try:
  44. obj = self.app.collection.get_by_name(str(name))
  45. except:
  46. return "Could not retrieve object: %s" % name
  47. def geo_init_me(geo_obj, app_obj):
  48. margin = args['margin'] + args['dia'] / 2
  49. gap_size = args['dia'] + args['gapsize']
  50. minx, miny, maxx, maxy = obj.bounds()
  51. minx -= margin
  52. maxx += margin
  53. miny -= margin
  54. maxy += margin
  55. midx = 0.5 * (minx + maxx)
  56. midy = 0.5 * (miny + maxy)
  57. hgap = 0.5 * gap_size
  58. pts = [[midx - hgap, maxy],
  59. [minx, maxy],
  60. [minx, midy + hgap],
  61. [minx, midy - hgap],
  62. [minx, miny],
  63. [midx - hgap, miny],
  64. [midx + hgap, miny],
  65. [maxx, miny],
  66. [maxx, midy - hgap],
  67. [maxx, midy + hgap],
  68. [maxx, maxy],
  69. [midx + hgap, maxy]]
  70. cases = {"tb": [[pts[0], pts[1], pts[4], pts[5]],
  71. [pts[6], pts[7], pts[10], pts[11]]],
  72. "lr": [[pts[9], pts[10], pts[1], pts[2]],
  73. [pts[3], pts[4], pts[7], pts[8]]],
  74. "4": [[pts[0], pts[1], pts[2]],
  75. [pts[3], pts[4], pts[5]],
  76. [pts[6], pts[7], pts[8]],
  77. [pts[9], pts[10], pts[11]]]}
  78. cuts = cases[args['gaps']]
  79. geo_obj.solid_geometry = cascaded_union([LineString(segment) for segment in cuts])
  80. try:
  81. obj.app.new_object("geometry", name + "_cutout", geo_init_me)
  82. except Exception as e:
  83. return "Operation failed: %s" % str(e)