TclCommandAlignDrillGrid.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import collections
  2. from tclCommands.TclCommand import TclCommandSignaled
  3. from shapely.geometry import Point
  4. class TclCommandAlignDrillGrid(TclCommandSignaled):
  5. """
  6. Tcl shell command to create an Excellon object
  7. with drills for aligment grid.
  8. Todo: What is an alignment grid?
  9. """
  10. # array of all command aliases, to be able use old names for
  11. # backward compatibility (add_poly, add_polygon)
  12. aliases = ['aligndrillgrid']
  13. description = '%s %s' % ("--", "Create an Excellon object with drills for alignment arranged in a grid.")
  14. # Dictionary of types from Tcl command, needs to be ordered.
  15. # For positional arguments
  16. arg_names = collections.OrderedDict([
  17. ])
  18. # Dictionary of types from Tcl command, needs to be ordered.
  19. # For options like -optionname value
  20. option_types = collections.OrderedDict([
  21. ('dia', float),
  22. ('gridx', float),
  23. ('gridoffsetx', float),
  24. ('gridy', float),
  25. ('gridoffsety', float),
  26. ('columns', int),
  27. ('rows', int),
  28. ('outname', str)
  29. ])
  30. # array of mandatory options for current Tcl command: required = {'name','outname'}
  31. required = ['gridx', 'gridy', 'columns', 'rows']
  32. # structured help for current command, args needs to be ordered
  33. help = {
  34. 'main': "Create an Excellon object with drills for alignment arranged in a grid.",
  35. 'args': collections.OrderedDict([
  36. ('dia', 'Tool diameter.'),
  37. ('gridx', 'Grid size in X axis.'),
  38. ('gridoffsetx', 'Move grid from origin.'),
  39. ('gridy', 'Grid size in Y axis.'),
  40. ('gridoffsety', 'Move grid from origin.'),
  41. ('colums', 'Number of grid holes on X axis.'),
  42. ('rows', 'Number of grid holes on Y axis.'),
  43. ('outname', 'Name of the object to create.')
  44. ]),
  45. 'examples': ['aligndrillgrid -rows 2 -columns 2 -gridoffsetx 10 -gridoffsety 10 -gridx 2.54 -gridy 5.08']
  46. }
  47. def execute(self, args, unnamed_args):
  48. """
  49. execute current TCL shell command
  50. :param args: array of known named arguments and options
  51. :param unnamed_args: array of other values which were passed into command
  52. without -somename and we do not have them in known arg_names
  53. :return: None or exception
  54. """
  55. if 'outname' in args:
  56. outname = args['outname']
  57. else:
  58. outname = "new_aligndrill_grid"
  59. if 'gridoffsetx' not in args:
  60. gridoffsetx = 0
  61. else:
  62. gridoffsetx = args['gridoffsetx']
  63. if 'gridoffsety' not in args:
  64. gridoffsety = 0
  65. else:
  66. gridoffsety = args['gridoffsety']
  67. # Tools
  68. tools = {"1": {"C": args['dia']}}
  69. def aligndrillgrid_init_me(init_obj, app_obj):
  70. """
  71. This function is used to initialize the new
  72. object once it's created.
  73. :param init_obj: The new object.
  74. :param app_obj: The application (FlatCAMApp)
  75. :return: None
  76. """
  77. drills = []
  78. currenty = 0
  79. for row in range(args['rows']):
  80. currentx = 0
  81. for col in range(args['columns']):
  82. point = Point(currentx + gridoffsetx, currenty + gridoffsety)
  83. drills.append({"point": point, "tool": "1"})
  84. currentx = currentx + args['gridx']
  85. currenty = currenty + args['gridy']
  86. init_obj.tools = tools
  87. init_obj.drills = drills
  88. init_obj.create_geometry()
  89. # Create the new object
  90. self.app.new_object("excellon", outname, aligndrillgrid_init_me, plot=False)