TclCommandMillSlots.py 6.0 KB

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