TclCommandMillSlots.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 collections
  9. import math
  10. class TclCommandMillSlots(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 = ['millslots', 'mills']
  18. description = '%s %s' % ("--", "Create a Geometry Object for milling slot holes from Excellon.")
  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_thread', str),
  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 a Geometry Object for milling slot holes from Excellon.",
  37. 'args': collections.OrderedDict([
  38. ('name', 'Name of the Excellon Object. Required.'),
  39. ('milled_dias', 'Comma separated tool diameters of the slots to be milled (example: 0.6, 1.0 or 3.125).\n'
  40. 'Exception: if you enter "all" then the slots for all tools will be milled.\n'
  41. 'WARNING: no spaces are allowed in the list of tools.\n'
  42. 'As a precaution you can enclose them with quotes.'),
  43. ('tooldia', 'Diameter of the milling tool (example: 0.1).'),
  44. ('outname', 'Name of object to be created holding the milled geometries.'),
  45. ('use_thread', 'If to use multithreading: True (1) or False (0).'),
  46. ('diatol', 'Tolerance. Percentange (0.0 ... 100.0) within which dias in milled_dias will be judged to be '
  47. 'the same as the ones in the tools from the Excellon object. E.g: if in milled_dias we have a '
  48. 'diameter with value 1.0, in the Excellon we have a tool with dia = 1.05 and we set a tolerance '
  49. 'diatol = 5.0 then the slots with the dia = (0.95 ... 1.05) '
  50. 'in Excellon will be processed. Float number.')
  51. ]),
  52. 'examples': ['millslots myslots -milled_dias "0.6,0.8" -tooldia 0.1 -diatol 10 -outname milled_slots',
  53. 'mills my_excellon.drl']
  54. }
  55. def execute(self, args, unnamed_args):
  56. """
  57. :param args: array of known named arguments and options
  58. :param unnamed_args: array of other values which were passed into command
  59. without -somename and we do not have them in known arg_names
  60. :return: None or exception
  61. """
  62. name = args['name']
  63. try:
  64. obj = self.app.collection.get_by_name(str(name))
  65. except Exception:
  66. obj = None
  67. self.raise_tcl_error("Could not retrieve object: %s" % name)
  68. if 'outname' not in args:
  69. args['outname'] = name + "_mill_slots"
  70. if 'use_thread' in args:
  71. try:
  72. par = args['use_thread'].capitalize()
  73. except AttributeError:
  74. par = args['use_thread']
  75. args['use_thread'] = bool(eval(par))
  76. else:
  77. args['use_thread'] = False
  78. if not obj.slots:
  79. self.raise_tcl_error("The Excellon object has no slots: %s" % name)
  80. # units = self.app.defaults['units'].upper()
  81. try:
  82. if 'milled_dias' in args and args['milled_dias'] != 'all':
  83. diameters = [x.strip() for x in args['milled_dias'].split(",")]
  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]["C"])))
  89. req_dia_form = float('%.*f' % (obj.decimals, float(req_dia)))
  90. if 'diatol' in args:
  91. tolerance = 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 slots to be milled passed to the "
  103. "TclCommand are not actual tool diameters in the Excellon object.")
  104. args['tools'] = req_tools
  105. # no longer needed
  106. del args['milled_dias']
  107. del args['diatol']
  108. # Split and put back. We are passing the whole dictionary later.
  109. # args['milled_dias'] = [x.strip() for x in args['tools'].split(",")]
  110. else:
  111. args['tools'] = 'all'
  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('Only Excellon objects can have mill-slots, got %s %s.' % (name, type(obj)))
  116. if self.app.collection.has_promises():
  117. self.raise_tcl_error('!!!Promises exists, but should not here!!!')
  118. try:
  119. # 'name' is not an argument of obj.generate_milling()
  120. del args['name']
  121. # This runs in the background... Is blocking handled?
  122. success, msg = obj.generate_milling_slots(plot=False, **args)
  123. except Exception as e:
  124. success = None
  125. msg = None
  126. self.raise_tcl_error("Operation failed: %s" % str(e))
  127. if not success:
  128. self.raise_tcl_error(msg)