TclCommandMillSlots.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. from ObjectCollection import *
  2. from tclCommands.TclCommand import TclCommandSignaled
  3. class TclCommandMillSlots(TclCommandSignaled):
  4. """
  5. Tcl shell command to Create Geometry Object for milling holes from Excellon.
  6. example:
  7. millholes my_drill -tools 1,2,3 -tooldia 0.1 -outname mill_holes_geo
  8. """
  9. # List of all command aliases, to be able use old names for backward compatibility (add_poly, add_polygon)
  10. aliases = ['millslots', 'mills']
  11. # Dictionary of types from Tcl command, needs to be ordered
  12. arg_names = collections.OrderedDict([
  13. ('name', str)
  14. ])
  15. # Dictionary of types from Tcl command, needs to be ordered.
  16. # This is for options like -optionname value
  17. option_types = collections.OrderedDict([
  18. ('milled_dias', str),
  19. ('outname', str),
  20. ('tooldia', float),
  21. ('use_threads', bool)
  22. ])
  23. # array of mandatory options for current Tcl command: required = {'name','outname'}
  24. required = ['name']
  25. # structured help for current command, args needs to be ordered
  26. help = {
  27. 'main': "Create Geometry Object for milling slot holes from Excellon.",
  28. 'args': collections.OrderedDict([
  29. ('name', 'Name of the Excellon Object.'),
  30. ('milled_dias', 'Comma separated tool diameters of the slots to be milled (example: 0.6, 1.0 or 3.125).'),
  31. ('tooldia', 'Diameter of the milling tool (example: 0.1).'),
  32. ('outname', 'Name of object to create.'),
  33. ('use_thread', 'If to use multithreading: True or False.')
  34. ]),
  35. 'examples': ['millholes mydrills']
  36. }
  37. def execute(self, args, unnamed_args):
  38. """
  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. name = args['name']
  45. if 'outname' not in args:
  46. args['outname'] = name + "_mill_slots"
  47. try:
  48. obj = self.app.collection.get_by_name(str(name))
  49. except:
  50. obj = None
  51. self.raise_tcl_error("Could not retrieve object: %s" % name)
  52. if not obj.slots:
  53. self.raise_tcl_error("The Excellon object has no slots: %s" % name)
  54. try:
  55. if 'milled_dias' in args and args['milled_dias'] != 'all':
  56. diameters = [x.strip() for x in args['milled_dias'].split(",")]
  57. req_tools = []
  58. for tool in obj.tools:
  59. for req_dia in diameters:
  60. if float('%.4f' % float(obj.tools[tool]["C"])) == float('%.4f' % float(req_dia)):
  61. req_tools.append(tool)
  62. args['tools'] = req_tools
  63. # no longer needed
  64. del args['milled_dias']
  65. # Split and put back. We are passing the whole dictionary later.
  66. # args['milled_dias'] = [x.strip() for x in args['tools'].split(",")]
  67. else:
  68. args['tools'] = 'all'
  69. except Exception as e:
  70. self.raise_tcl_error("Bad tools: %s" % str(e))
  71. if not isinstance(obj, FlatCAMExcellon):
  72. self.raise_tcl_error('Only Excellon objects can have mill-slots, got %s %s.' % (name, type(obj)))
  73. if self.app.collection.has_promises():
  74. self.raise_tcl_error('!!!Promises exists, but should not here!!!')
  75. try:
  76. # 'name' is not an argument of obj.generate_milling()
  77. del args['name']
  78. # This runs in the background... Is blocking handled?
  79. success, msg = obj.generate_milling_slots(**args)
  80. except Exception as e:
  81. success = None
  82. msg = None
  83. self.raise_tcl_error("Operation failed: %s" % str(e))
  84. if not success:
  85. self.raise_tcl_error(msg)