TclCommandCutout.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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.
  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.',
  28. 'args': collections.OrderedDict([
  29. ('name', 'Name of the object.'),
  30. ('dia', 'Tool diameter.'),
  31. ('margin', 'Margin over bounds.'),
  32. ('gapsize', 'size of gap.'),
  33. ('gaps', 'type of gaps.'),
  34. ]),
  35. 'examples': []
  36. }
  37. def execute(self, args, unnamed_args):
  38. """
  39. :param args:
  40. :param unnamed_args:
  41. :return:
  42. """
  43. name = args['name']
  44. try:
  45. obj = self.app.collection.get_by_name(str(name))
  46. except:
  47. return "Could not retrieve object: %s" % name
  48. def geo_init_me(geo_obj, app_obj):
  49. margin = args['margin'] + args['dia'] / 2
  50. gap_size = args['dia'] + args['gapsize']
  51. minx, miny, maxx, maxy = obj.bounds()
  52. minx -= margin
  53. maxx += margin
  54. miny -= margin
  55. maxy += margin
  56. midx = 0.5 * (minx + maxx)
  57. midy = 0.5 * (miny + maxy)
  58. hgap = 0.5 * gap_size
  59. pts = [[midx - hgap, maxy],
  60. [minx, maxy],
  61. [minx, midy + hgap],
  62. [minx, midy - hgap],
  63. [minx, miny],
  64. [midx - hgap, miny],
  65. [midx + hgap, miny],
  66. [maxx, miny],
  67. [maxx, midy - hgap],
  68. [maxx, midy + hgap],
  69. [maxx, maxy],
  70. [midx + hgap, maxy]]
  71. cases = {"tb": [[pts[0], pts[1], pts[4], pts[5]],
  72. [pts[6], pts[7], pts[10], pts[11]]],
  73. "lr": [[pts[9], pts[10], pts[1], pts[2]],
  74. [pts[3], pts[4], pts[7], pts[8]]],
  75. "4": [[pts[0], pts[1], pts[2]],
  76. [pts[3], pts[4], pts[5]],
  77. [pts[6], pts[7], pts[8]],
  78. [pts[9], pts[10], pts[11]]]}
  79. cuts = cases[args['gaps']]
  80. geo_obj.solid_geometry = cascaded_union([LineString(segment) for segment in cuts])
  81. try:
  82. obj.app.new_object("geometry", name + "_cutout", geo_init_me)
  83. except Exception as e:
  84. return "Operation failed: %s" % str(e)