TclCommandGeoCutout.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. from ObjectCollection import *
  2. from tclCommands.TclCommand import TclCommandSignaled
  3. class TclCommandGeoCutout(TclCommandSignaled):
  4. """
  5. Tcl shell command to cut holding gaps from geometry.
  6. """
  7. # array of all command aliases, to be able use old names for backward compatibility (add_poly, add_polygon)
  8. aliases = ['geocutout']
  9. # Dictionary of types from Tcl command, needs to be ordered.
  10. # For positional arguments
  11. arg_names = collections.OrderedDict([
  12. ('name', str)
  13. ])
  14. # Dictionary of types from Tcl command, needs to be ordered.
  15. # 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': "Cut holding gaps from geometry.",
  27. 'args': collections.OrderedDict([
  28. ('name', 'Name of the geometry object.'),
  29. ('dia', 'Tool diameter.'),
  30. ('margin', 'Margin over bounds.'),
  31. ('gapsize', 'Size of gap.'),
  32. ('gaps', 'Type of gaps.'),
  33. ]),
  34. 'examples': [" #isolate margin for example from fritzing arduino shield or any svg etc\n" +
  35. " isolate BCu_margin -dia 3 -overlap 1\n" +
  36. "\n" +
  37. " #create exteriors from isolated object\n" +
  38. " exteriors BCu_margin_iso -outname BCu_margin_iso_exterior\n" +
  39. "\n" +
  40. " #delete isolated object if you dond need id anymore\n" +
  41. " delete BCu_margin_iso\n" +
  42. "\n" +
  43. " #finally cut holding gaps\n" +
  44. " geocutout BCu_margin_iso_exterior -dia 3 -gapsize 0.6 -gaps 4\n"]
  45. }
  46. def execute(self, args, unnamed_args):
  47. """
  48. execute current TCL shell command
  49. :param args: array of known named arguments and options
  50. :param unnamed_args: array of other values which were passed into command
  51. without -somename and we do not have them in known arg_names
  52. :return: None or exception
  53. """
  54. # How gaps wil be rendered:
  55. # lr - left + right
  56. # tb - top + bottom
  57. # 4 - left + right +top + bottom
  58. # 2lr - 2*left + 2*right
  59. # 2tb - 2*top + 2*bottom
  60. # 8 - 2*left + 2*right +2*top + 2*bottom
  61. name = args['name']
  62. obj = None
  63. def subtract_rectangle(obj_, x0, y0, x1, y1):
  64. pts = [(x0, y0), (x1, y0), (x1, y1), (x0, y1)]
  65. obj_.subtract_polygon(pts)
  66. try:
  67. obj = self.app.collection.get_by_name(str(name))
  68. except:
  69. self.raise_tcl_error("Could not retrieve object: %s" % name)
  70. # Get min and max data for each object as we just cut rectangles across X or Y
  71. xmin, ymin, xmax, ymax = obj.bounds()
  72. px = 0.5 * (xmin + xmax)
  73. py = 0.5 * (ymin + ymax)
  74. lenghtx = (xmax - xmin)
  75. lenghty = (ymax - ymin)
  76. gapsize = args['gapsize'] + (args['dia'] / 2)
  77. if args['gaps'] == '8' or args['gaps'] == '2lr':
  78. subtract_rectangle(obj,
  79. xmin - gapsize, # botleft_x
  80. py - gapsize + lenghty / 4, # botleft_y
  81. xmax + gapsize, # topright_x
  82. py + gapsize + lenghty / 4) # topright_y
  83. subtract_rectangle(obj,
  84. xmin - gapsize,
  85. py - gapsize - lenghty / 4,
  86. xmax + gapsize,
  87. py + gapsize - lenghty / 4)
  88. if args['gaps'] == '8' or args['gaps'] == '2tb':
  89. subtract_rectangle(obj,
  90. px - gapsize + lenghtx / 4,
  91. ymin - gapsize,
  92. px + gapsize + lenghtx / 4,
  93. ymax + gapsize)
  94. subtract_rectangle(obj,
  95. px - gapsize - lenghtx / 4,
  96. ymin - gapsize,
  97. px + gapsize - lenghtx / 4,
  98. ymax + gapsize)
  99. if args['gaps'] == '4' or args['gaps'] == 'lr':
  100. subtract_rectangle(obj,
  101. xmin - gapsize,
  102. py - gapsize,
  103. xmax + gapsize,
  104. py + gapsize)
  105. if args['gaps'] == '4' or args['gaps'] == 'tb':
  106. subtract_rectangle(obj,
  107. px - gapsize,
  108. ymin - gapsize,
  109. px + gapsize,
  110. ymax + gapsize)