TclCommandMillDrills.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. description = '%s %s' % ("--", "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 as e:
  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. args['use_thread'] = bool(eval(args['use_thread']))
  73. else:
  74. args['use_thread'] = False
  75. if not obj.drills:
  76. self.raise_tcl_error("The Excellon object has no drills: %s" % name)
  77. try:
  78. if 'milled_dias' in args and args['milled_dias'] != 'all':
  79. diameters = [x.strip() for x in args['milled_dias'].split(",") if x != '']
  80. nr_diameters = len(diameters)
  81. req_tools = set()
  82. for tool in obj.tools:
  83. for req_dia in diameters:
  84. obj_dia_form = float('%.*f' % (obj.decimals, float(obj.tools[tool]["C"])))
  85. req_dia_form = float('%.*f' % (obj.decimals, float(req_dia)))
  86. if 'diatol' in args:
  87. tolerance = float(args['diatol']) / 100
  88. tolerance = 0.0 if tolerance < 0.0 else tolerance
  89. tolerance = 1.0 if tolerance > 1.0 else tolerance
  90. if math.isclose(obj_dia_form, req_dia_form, rel_tol=tolerance):
  91. req_tools.add(tool)
  92. nr_diameters -= 1
  93. else:
  94. if obj_dia_form == req_dia_form:
  95. req_tools.add(tool)
  96. nr_diameters -= 1
  97. if nr_diameters > 0:
  98. self.raise_tcl_error("One or more tool diameters of the drills to be milled passed to the "
  99. "TclCommand are not actual tool diameters in the Excellon object.")
  100. args['tools'] = req_tools
  101. # no longer needed
  102. del args['milled_dias']
  103. del args['diatol']
  104. # Split and put back. We are passing the whole dictionary later.
  105. # args['milled_dias'] = [x.strip() for x in args['tools'].split(",")]
  106. else:
  107. args['tools'] = 'all'
  108. except Exception as e:
  109. self.raise_tcl_error("Bad tools: %s" % str(e))
  110. if not isinstance(obj, FlatCAMExcellon):
  111. self.raise_tcl_error('Only Excellon objects can be mill-drilled, got %s %s.' % (name, type(obj)))
  112. if self.app.collection.has_promises():
  113. self.raise_tcl_error('!!!Promises exists, but should not here!!!')
  114. try:
  115. # 'name' is not an argument of obj.generate_milling()
  116. del args['name']
  117. # This runs in the background... Is blocking handled?
  118. success, msg = obj.generate_milling_drills(plot=False, **args)
  119. except Exception as e:
  120. success = None
  121. msg = None
  122. self.raise_tcl_error("Operation failed: %s" % str(e))
  123. if not success:
  124. self.raise_tcl_error(msg)