TclCommandMillDrills.py 6.1 KB

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