TclCommandMillSlots.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. ('diatol', float)
  23. ])
  24. # array of mandatory options for current Tcl command: required = {'name','outname'}
  25. required = ['name']
  26. # structured help for current command, args needs to be ordered
  27. help = {
  28. 'main': "Create Geometry Object for milling slot holes from Excellon.",
  29. 'args': collections.OrderedDict([
  30. ('name', 'Name of the Excellon Object.'),
  31. ('milled_dias', 'Comma separated tool diameters of the slots to be milled (example: 0.6, 1.0 or 3.125).'),
  32. ('tooldia', 'Diameter of the milling tool (example: 0.1).'),
  33. ('outname', 'Name of object to create.'),
  34. ('use_thread', 'If to use multithreading: True or False.'),
  35. ('diatol', 'Tolerance. Percentange (0.0 ... 100.0) within which dias in milled_dias will be judged to be '
  36. 'the same as the ones in the tools from the Excellon object. E.g: if in milled_dias we have a '
  37. 'diameter with value 1.0, in the Excellon we have a tool with dia = 1.05 and we set a tolerance '
  38. 'diatol = 5.0 then the slots with the dia = (0.95 ... 1.05) '
  39. 'in Excellon will be processed. Float number.')
  40. ]),
  41. 'examples': ['millslots mydrills', 'mills my_excellon.drl']
  42. }
  43. def execute(self, args, unnamed_args):
  44. """
  45. :param args: array of known named arguments and options
  46. :param unnamed_args: array of other values which were passed into command
  47. without -somename and we do not have them in known arg_names
  48. :return: None or exception
  49. """
  50. name = args['name']
  51. if 'outname' not in args:
  52. args['outname'] = name + "_mill_slots"
  53. try:
  54. obj = self.app.collection.get_by_name(str(name))
  55. except:
  56. obj = None
  57. self.raise_tcl_error("Could not retrieve object: %s" % name)
  58. if not obj.slots:
  59. self.raise_tcl_error("The Excellon object has no slots: %s" % name)
  60. units = self.app.ui.general_defaults_form.general_app_group.units_radio.get_value().upper()
  61. try:
  62. if 'milled_dias' in args and args['milled_dias'] != 'all':
  63. diameters = [x.strip() for x in args['milled_dias'].split(",")]
  64. nr_diameters = len(diameters)
  65. req_tools = set()
  66. for tool in obj.tools:
  67. for req_dia in diameters:
  68. obj_dia_form = float('%.2f' % float(obj.tools[tool]["C"])) if units == 'MM' else \
  69. float('%.4f' % float(obj.tools[tool]["C"]))
  70. req_dia_form = float('%.2f' % float(req_dia)) if units == 'MM' else \
  71. float('%.4f' % float(req_dia))
  72. if 'diatol' in args:
  73. tolerance = args['diatol'] / 100
  74. tolerance = 0.0 if tolerance < 0.0 else tolerance
  75. tolerance = 1.0 if tolerance > 1.0 else tolerance
  76. if math.isclose(obj_dia_form, req_dia_form, rel_tol=tolerance):
  77. req_tools.add(tool)
  78. nr_diameters -= 1
  79. else:
  80. if obj_dia_form == req_dia_form:
  81. req_tools.add(tool)
  82. nr_diameters -= 1
  83. if nr_diameters > 0:
  84. self.raise_tcl_error("One or more tool diameters of the slots to be milled passed to the "
  85. "TclCommand are not actual tool diameters in the Excellon object.")
  86. args['tools'] = req_tools
  87. # no longer needed
  88. del args['milled_dias']
  89. del args['diatol']
  90. # Split and put back. We are passing the whole dictionary later.
  91. # args['milled_dias'] = [x.strip() for x in args['tools'].split(",")]
  92. else:
  93. args['tools'] = 'all'
  94. except Exception as e:
  95. self.raise_tcl_error("Bad tools: %s" % str(e))
  96. if not isinstance(obj, FlatCAMExcellon):
  97. self.raise_tcl_error('Only Excellon objects can have mill-slots, got %s %s.' % (name, type(obj)))
  98. if self.app.collection.has_promises():
  99. self.raise_tcl_error('!!!Promises exists, but should not here!!!')
  100. try:
  101. # 'name' is not an argument of obj.generate_milling()
  102. del args['name']
  103. # This runs in the background... Is blocking handled?
  104. success, msg = obj.generate_milling_slots(plot=False, **args)
  105. except Exception as e:
  106. success = None
  107. msg = None
  108. self.raise_tcl_error("Operation failed: %s" % str(e))
  109. if not success:
  110. self.raise_tcl_error(msg)