TclCommandGeoCutout.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. from tclCommands.TclCommand import *
  2. class TclCommandGeoCutout(TclCommandSignaled):
  3. """
  4. Tcl shell command to cut holding gaps from geometry.
  5. """
  6. # array of all command aliases, to be able use old names for backward compatibility (add_poly, add_polygon)
  7. aliases = ['geocutout']
  8. # Dictionary of types from Tcl command, needs to be ordered.
  9. # For positional arguments
  10. arg_names = collections.OrderedDict([
  11. ('name', str)
  12. ])
  13. # Dictionary of types from Tcl command, needs to be ordered.
  14. # For options like -optionname value
  15. option_types = collections.OrderedDict([
  16. ('dia', float),
  17. ('margin', float),
  18. ('gapsize', float),
  19. ('gaps', str)
  20. ])
  21. # array of mandatory options for current Tcl command: required = {'name','outname'}
  22. required = ['name']
  23. # structured help for current command, args needs to be ordered
  24. help = {
  25. 'main': "Cut holding gaps from geometry.",
  26. 'args': collections.OrderedDict([
  27. ('name', 'Name of the geometry object.'),
  28. ('dia', 'Tool diameter.'),
  29. ('margin', 'Margin over bounds.'),
  30. ('gapsize', 'Size of gap.'),
  31. ('gaps', 'Type of gaps.'),
  32. ]),
  33. 'examples': []
  34. }
  35. def execute(self, args, unnamed_args):
  36. """
  37. execute current TCL shell command
  38. :param args: array of known named arguments and options
  39. :param unnamed_args: array of other values which were passed into command
  40. without -somename and we do not have them in known arg_names
  41. :return: None or exception
  42. """
  43. # How gaps wil be rendered:
  44. # lr - left + right
  45. # tb - top + bottom
  46. # 4 - left + right +top + bottom
  47. # 2lr - 2*left + 2*right
  48. # 2tb - 2*top + 2*bottom
  49. # 8 - 2*left + 2*right +2*top + 2*bottom
  50. name = args['name']
  51. obj = None
  52. def subtract_rectangle(obj_, x0, y0, x1, y1):
  53. pts = [(x0, y0), (x1, y0), (x1, y1), (x0, y1)]
  54. obj_.subtract_polygon(pts)
  55. try:
  56. obj = self.app.collection.get_by_name(str(name))
  57. except:
  58. self.raise_tcl_error("Could not retrieve object: %s" % name)
  59. # Get min and max data for each object as we just cut rectangles across X or Y
  60. xmin, ymin, xmax, ymax = obj.bounds()
  61. px = 0.5 * (xmin + xmax)
  62. py = 0.5 * (ymin + ymax)
  63. lenghtx = (xmax - xmin)
  64. lenghty = (ymax - ymin)
  65. gapsize = args['gapsize'] + args['dia'] / 2
  66. if args['gaps'] == '8' or args['gaps'] == '2lr':
  67. subtract_rectangle(obj,
  68. xmin - gapsize, # botleft_x
  69. py - gapsize + lenghty / 4, # botleft_y
  70. xmax + gapsize, # topright_x
  71. py + gapsize + lenghty / 4) # topright_y
  72. subtract_rectangle(obj,
  73. xmin - gapsize,
  74. py - gapsize - lenghty / 4,
  75. xmax + gapsize,
  76. py + gapsize - lenghty / 4)
  77. if args['gaps'] == '8' or args['gaps'] == '2tb':
  78. subtract_rectangle(obj,
  79. px - gapsize + lenghtx / 4,
  80. ymin - gapsize,
  81. px + gapsize + lenghtx / 4,
  82. ymax + gapsize)
  83. subtract_rectangle(obj,
  84. px - gapsize - lenghtx / 4,
  85. ymin - gapsize,
  86. px + gapsize - lenghtx / 4,
  87. ymax + gapsize)
  88. if args['gaps'] == '4' or args['gaps'] == 'lr':
  89. subtract_rectangle(obj,
  90. xmin - gapsize,
  91. py - gapsize,
  92. xmax + gapsize,
  93. py + gapsize)
  94. if args['gaps'] == '4' or args['gaps'] == 'tb':
  95. subtract_rectangle(obj,
  96. px - gapsize,
  97. ymin - gapsize,
  98. px + gapsize,
  99. ymax + gapsize)