TclCommandAlignDrillGrid.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. # Dictionary of types from Tcl command, needs to be ordered.
  14. # For positional arguments
  15. arg_names = collections.OrderedDict([
  16. ])
  17. # Dictionary of types from Tcl command, needs to be ordered.
  18. # For options like -optionname value
  19. option_types = collections.OrderedDict([
  20. ('dia', float),
  21. ('gridx', float),
  22. ('gridoffsetx', float),
  23. ('gridy', float),
  24. ('gridoffsety', float),
  25. ('columns', int),
  26. ('rows', int),
  27. ('outname', str)
  28. ])
  29. # array of mandatory options for current Tcl command: required = {'name','outname'}
  30. required = ['gridx', 'gridy', 'columns', 'rows']
  31. # structured help for current command, args needs to be ordered
  32. help = {
  33. 'main': "Create excellon with drills for aligment grid.",
  34. 'args': collections.OrderedDict([
  35. ('outname', 'Name of the object to create.'),
  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. ]),
  44. 'examples': ['aligndrillgrid -rows 2 -columns 2 -gridoffsetx 10 -gridoffsety 10 -gridx 2.54 -gridy 5.08']
  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. if 'outname' in args:
  55. outname = args['outname']
  56. else:
  57. outname = "new_aligndrill_grid"
  58. if 'gridoffsetx' not in args:
  59. gridoffsetx = 0
  60. else:
  61. gridoffsetx = args['gridoffsetx']
  62. if 'gridoffsety' not in args:
  63. gridoffsety = 0
  64. else:
  65. gridoffsety = args['gridoffsety']
  66. # Tools
  67. tools = {"1": {"C": args['dia']}}
  68. def aligndrillgrid_init_me(init_obj, app_obj):
  69. """
  70. This function is used to initialize the new
  71. object once it's created.
  72. :param init_obj: The new object.
  73. :param app_obj: The application (FlatCAMApp)
  74. :return: None
  75. """
  76. drills = []
  77. currenty = 0
  78. for row in range(args['rows']):
  79. currentx = 0
  80. for col in range(args['columns']):
  81. point = Point(currentx + gridoffsetx, currenty + gridoffsety)
  82. drills.append({"point": point, "tool": "1"})
  83. currentx = currentx + args['gridx']
  84. currenty = currenty + args['gridy']
  85. init_obj.tools = tools
  86. init_obj.drills = drills
  87. init_obj.create_geometry()
  88. # Create the new object
  89. self.app.new_object("excellon", outname, aligndrillgrid_init_me, plot=False)