2019-10-13 15:13:39 +00:00
|
|
|
# ##########################################################
|
|
|
|
# FlatCAM: 2D Post-processing for Manufacturing #
|
|
|
|
# File Author: Marius Adrian Stanciu (c) #
|
|
|
|
# Date: 8/17/2019 #
|
|
|
|
# MIT Licence #
|
|
|
|
# ##########################################################
|
|
|
|
|
2019-01-03 19:25:08 +00:00
|
|
|
from tclCommands.TclCommand import TclCommandSignaled
|
2019-10-24 23:10:52 +00:00
|
|
|
from FlatCAMObj import FlatCAMExcellon
|
2019-01-03 19:25:08 +00:00
|
|
|
|
2019-10-24 23:10:52 +00:00
|
|
|
import math
|
|
|
|
import collections
|
2019-01-03 19:25:08 +00:00
|
|
|
|
2019-09-11 14:34:01 +00:00
|
|
|
class TclCommandMillDrills(TclCommandSignaled):
|
2019-01-03 19:25:08 +00:00
|
|
|
"""
|
|
|
|
Tcl shell command to Create Geometry Object for milling holes from Excellon.
|
|
|
|
|
|
|
|
example:
|
|
|
|
millholes my_drill -tools 1,2,3 -tooldia 0.1 -outname mill_holes_geo
|
|
|
|
"""
|
|
|
|
|
|
|
|
# List of all command aliases, to be able use old names for backward compatibility (add_poly, add_polygon)
|
2019-09-11 14:34:01 +00:00
|
|
|
aliases = ['milldrills', 'milld']
|
2019-01-03 19:25:08 +00:00
|
|
|
|
|
|
|
# Dictionary of types from Tcl command, needs to be ordered
|
|
|
|
arg_names = collections.OrderedDict([
|
|
|
|
('name', str)
|
|
|
|
])
|
|
|
|
|
|
|
|
# Dictionary of types from Tcl command, needs to be ordered.
|
|
|
|
# This is for options like -optionname value
|
|
|
|
option_types = collections.OrderedDict([
|
2019-09-11 14:50:52 +00:00
|
|
|
('milled_dias', str),
|
2019-01-03 19:25:08 +00:00
|
|
|
('outname', str),
|
|
|
|
('tooldia', float),
|
2019-09-11 23:07:32 +00:00
|
|
|
('use_threads', bool),
|
2019-09-12 14:28:56 +00:00
|
|
|
('diatol', float)
|
2019-01-03 19:25:08 +00:00
|
|
|
])
|
|
|
|
|
|
|
|
# array of mandatory options for current Tcl command: required = {'name','outname'}
|
|
|
|
required = ['name']
|
|
|
|
|
|
|
|
# structured help for current command, args needs to be ordered
|
|
|
|
help = {
|
2019-09-11 14:34:01 +00:00
|
|
|
'main': "Create Geometry Object for milling drill holes from Excellon.",
|
2019-01-03 19:25:08 +00:00
|
|
|
'args': collections.OrderedDict([
|
|
|
|
('name', 'Name of the Excellon Object.'),
|
2019-09-11 14:50:52 +00:00
|
|
|
('milled_dias', 'Comma separated tool diameters of the drills to be milled (example: 0.6, 1.0 or 3.125).'),
|
2019-01-03 19:25:08 +00:00
|
|
|
('tooldia', 'Diameter of the milling tool (example: 0.1).'),
|
|
|
|
('outname', 'Name of object to create.'),
|
2019-09-11 23:07:32 +00:00
|
|
|
('use_thread', 'If to use multithreading: True or False.'),
|
2019-09-12 14:28:56 +00:00
|
|
|
('diatol', 'Tolerance. Percentange (0.0 ... 100.0) within which dias in milled_dias will be judged to be '
|
|
|
|
'the same as the ones in the tools from the Excellon object. E.g: if in milled_dias we have a '
|
|
|
|
'diameter with value 1.0, in the Excellon we have a tool with dia = 1.05 and we set a tolerance '
|
|
|
|
'diatol = 5.0 then the drills with the dia = (0.95 ... 1.05) '
|
|
|
|
'in Excellon will be processed. Float number.')
|
2019-01-03 19:25:08 +00:00
|
|
|
]),
|
2019-09-12 14:28:56 +00:00
|
|
|
'examples': ['milldrills mydrills', 'milld my_excellon.drl']
|
2019-01-03 19:25:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
def execute(self, args, unnamed_args):
|
|
|
|
"""
|
|
|
|
|
|
|
|
:param args: array of known named arguments and options
|
|
|
|
:param unnamed_args: array of other values which were passed into command
|
|
|
|
without -somename and we do not have them in known arg_names
|
|
|
|
:return: None or exception
|
|
|
|
"""
|
|
|
|
|
|
|
|
name = args['name']
|
|
|
|
|
|
|
|
if 'outname' not in args:
|
2019-09-11 14:50:52 +00:00
|
|
|
args['outname'] = name + "_mill_drills"
|
2019-01-03 19:25:08 +00:00
|
|
|
|
|
|
|
try:
|
2019-09-11 14:50:52 +00:00
|
|
|
obj = self.app.collection.get_by_name(str(name))
|
2019-10-24 23:10:52 +00:00
|
|
|
except Exception as e:
|
2019-09-11 14:50:52 +00:00
|
|
|
obj = None
|
|
|
|
self.raise_tcl_error("Could not retrieve object: %s" % name)
|
|
|
|
|
|
|
|
if not obj.drills:
|
|
|
|
self.raise_tcl_error("The Excellon object has no drills: %s" % name)
|
|
|
|
|
2019-09-11 18:40:18 +00:00
|
|
|
units = self.app.ui.general_defaults_form.general_app_group.units_radio.get_value().upper()
|
|
|
|
|
2019-09-11 14:50:52 +00:00
|
|
|
try:
|
|
|
|
if 'milled_dias' in args and args['milled_dias'] != 'all':
|
2019-09-11 18:40:18 +00:00
|
|
|
diameters = [x.strip() for x in args['milled_dias'].split(",") if x != '']
|
|
|
|
nr_diameters = len(diameters)
|
|
|
|
|
2019-09-11 23:07:32 +00:00
|
|
|
req_tools = set()
|
2019-09-11 14:50:52 +00:00
|
|
|
for tool in obj.tools:
|
|
|
|
for req_dia in diameters:
|
2019-09-11 18:40:18 +00:00
|
|
|
obj_dia_form = float('%.2f' % float(obj.tools[tool]["C"])) if units == 'MM' else \
|
|
|
|
float('%.4f' % float(obj.tools[tool]["C"]))
|
|
|
|
req_dia_form = float('%.2f' % float(req_dia)) if units == 'MM' else \
|
|
|
|
float('%.4f' % float(req_dia))
|
|
|
|
|
2019-09-12 14:28:56 +00:00
|
|
|
if 'diatol' in args:
|
|
|
|
tolerance = args['diatol'] / 100
|
2019-09-11 23:07:32 +00:00
|
|
|
|
|
|
|
tolerance = 0.0 if tolerance < 0.0 else tolerance
|
|
|
|
tolerance = 1.0 if tolerance > 1.0 else tolerance
|
|
|
|
if math.isclose(obj_dia_form, req_dia_form, rel_tol=tolerance):
|
|
|
|
req_tools.add(tool)
|
|
|
|
nr_diameters -= 1
|
|
|
|
else:
|
|
|
|
if obj_dia_form == req_dia_form:
|
|
|
|
req_tools.add(tool)
|
|
|
|
nr_diameters -= 1
|
2019-09-11 18:40:18 +00:00
|
|
|
|
|
|
|
if nr_diameters > 0:
|
|
|
|
self.raise_tcl_error("One or more tool diameters of the drills to be milled passed to the "
|
|
|
|
"TclCommand are not actual tool diameters in the Excellon object.")
|
2019-09-11 14:50:52 +00:00
|
|
|
|
|
|
|
args['tools'] = req_tools
|
|
|
|
|
|
|
|
# no longer needed
|
|
|
|
del args['milled_dias']
|
2019-09-12 14:28:56 +00:00
|
|
|
del args['diatol']
|
2019-09-11 14:50:52 +00:00
|
|
|
|
2019-01-03 19:25:08 +00:00
|
|
|
# Split and put back. We are passing the whole dictionary later.
|
2019-09-11 14:50:52 +00:00
|
|
|
# args['milled_dias'] = [x.strip() for x in args['tools'].split(",")]
|
2019-01-03 19:25:08 +00:00
|
|
|
else:
|
|
|
|
args['tools'] = 'all'
|
|
|
|
except Exception as e:
|
|
|
|
self.raise_tcl_error("Bad tools: %s" % str(e))
|
|
|
|
|
|
|
|
if not isinstance(obj, FlatCAMExcellon):
|
|
|
|
self.raise_tcl_error('Only Excellon objects can be mill-drilled, got %s %s.' % (name, type(obj)))
|
|
|
|
|
|
|
|
if self.app.collection.has_promises():
|
|
|
|
self.raise_tcl_error('!!!Promises exists, but should not here!!!')
|
|
|
|
|
|
|
|
try:
|
|
|
|
# 'name' is not an argument of obj.generate_milling()
|
|
|
|
del args['name']
|
|
|
|
|
|
|
|
# This runs in the background... Is blocking handled?
|
2019-09-15 22:39:11 +00:00
|
|
|
success, msg = obj.generate_milling_drills(plot=False, **args)
|
2019-01-03 19:25:08 +00:00
|
|
|
except Exception as e:
|
2019-09-11 14:50:52 +00:00
|
|
|
success = None
|
|
|
|
msg = None
|
2019-01-03 19:25:08 +00:00
|
|
|
self.raise_tcl_error("Operation failed: %s" % str(e))
|
|
|
|
|
|
|
|
if not success:
|
|
|
|
self.raise_tcl_error(msg)
|