TclCommandGeoCutout.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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': []
  35. }
  36. def execute(self, args, unnamed_args):
  37. """
  38. execute current TCL shell command
  39. :param args: array of known named arguments and options
  40. :param unnamed_args: array of other values which were passed into command
  41. without -somename and we do not have them in known arg_names
  42. :return: None or exception
  43. """
  44. # How gaps wil be rendered:
  45. # lr - left + right
  46. # tb - top + bottom
  47. # 4 - left + right +top + bottom
  48. # 2lr - 2*left + 2*right
  49. # 2tb - 2*top + 2*bottom
  50. # 8 - 2*left + 2*right +2*top + 2*bottom
  51. name = args['name']
  52. obj = None
  53. def subtract_rectangle(obj_, x0, y0, x1, y1):
  54. pts = [(x0, y0), (x1, y0), (x1, y1), (x0, y1)]
  55. obj_.subtract_polygon(pts)
  56. try:
  57. obj = self.app.collection.get_by_name(str(name))
  58. except:
  59. self.raise_tcl_error("Could not retrieve object: %s" % name)
  60. # Get min and max data for each object as we just cut rectangles across X or Y
  61. xmin, ymin, xmax, ymax = obj.bounds()
  62. px = 0.5 * (xmin + xmax)
  63. py = 0.5 * (ymin + ymax)
  64. lenghtx = (xmax - xmin)
  65. lenghty = (ymax - ymin)
  66. gapsize = args['gapsize'] + args['dia'] / 2
  67. if args['gaps'] == '8' or args['gaps'] == '2lr':
  68. subtract_rectangle(obj,
  69. xmin - gapsize, # botleft_x
  70. py - gapsize + lenghty / 4, # botleft_y
  71. xmax + gapsize, # topright_x
  72. py + gapsize + lenghty / 4) # topright_y
  73. subtract_rectangle(obj,
  74. xmin - gapsize,
  75. py - gapsize - lenghty / 4,
  76. xmax + gapsize,
  77. py + gapsize - lenghty / 4)
  78. if args['gaps'] == '8' or args['gaps'] == '2tb':
  79. subtract_rectangle(obj,
  80. px - gapsize + lenghtx / 4,
  81. ymin - gapsize,
  82. px + gapsize + lenghtx / 4,
  83. ymax + gapsize)
  84. subtract_rectangle(obj,
  85. px - gapsize - lenghtx / 4,
  86. ymin - gapsize,
  87. px + gapsize - lenghtx / 4,
  88. ymax + gapsize)
  89. if args['gaps'] == '4' or args['gaps'] == 'lr':
  90. subtract_rectangle(obj,
  91. xmin - gapsize,
  92. py - gapsize,
  93. xmax + gapsize,
  94. py + gapsize)
  95. if args['gaps'] == '4' or args['gaps'] == 'tb':
  96. subtract_rectangle(obj,
  97. px - gapsize,
  98. ymin - gapsize,
  99. px + gapsize,
  100. ymax + gapsize)