TclCommandMillDrills.py 6.7 KB

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