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
|
|
|
|
|
|
|
import collections
|
|
|
|
import math
|
2019-01-03 19:25:08 +00:00
|
|
|
|
|
|
|
|
2019-09-11 14:34:01 +00:00
|
|
|
class TclCommandMillSlots(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 = ['millslots', 'mills']
|
2019-01-03 19:25:08 +00:00
|
|
|
|
2020-04-13 16:15:20 +00:00
|
|
|
description = '%s %s' % ("--", "Create a Geometry Object for milling slot holes from Excellon.")
|
|
|
|
|
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),
|
2020-04-13 16:15:20 +00:00
|
|
|
('use_thread', str),
|
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 = {
|
2020-04-13 16:15:20 +00:00
|
|
|
'main': "Create a Geometry Object for milling slot holes from Excellon.",
|
2019-01-03 19:25:08 +00:00
|
|
|
'args': collections.OrderedDict([
|
2020-04-09 01:13:04 +00:00
|
|
|
('name', 'Name of the Excellon Object. Required.'),
|
|
|
|
('milled_dias', 'Comma separated tool diameters of the slots to be milled (example: 0.6, 1.0 or 3.125).\n'
|
|
|
|
'Exception: if you enter "all" then the slots for all tools will be milled.\n'
|
|
|
|
'WARNING: no spaces are allowed in the list of tools.\n'
|
|
|
|
'As a precaution you can enclose them with quotes.'),
|
2019-01-03 19:25:08 +00:00
|
|
|
('tooldia', 'Diameter of the milling tool (example: 0.1).'),
|
2020-04-09 01:13:04 +00:00
|
|
|
('outname', 'Name of object to be created holding the milled geometries.'),
|
2020-04-13 16:15:20 +00:00
|
|
|
('use_thread', 'If to use multithreading: True (1) or False (0).'),
|
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 slots with the dia = (0.95 ... 1.05) '
|
|
|
|
'in Excellon will be processed. Float number.')
|
2019-01-03 19:25:08 +00:00
|
|
|
]),
|
2020-04-09 01:13:04 +00:00
|
|
|
'examples': ['millslots myslots -milled_dias "0.6,0.8" -tooldia 0.1 -diatol 10 -outname milled_slots',
|
|
|
|
'mills 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']
|
|
|
|
|
|
|
|
try:
|
2019-09-11 14:50:52 +00:00
|
|
|
obj = self.app.collection.get_by_name(str(name))
|
2019-11-11 00:35:42 +00:00
|
|
|
except Exception:
|
2019-09-11 14:50:52 +00:00
|
|
|
obj = None
|
|
|
|
self.raise_tcl_error("Could not retrieve object: %s" % name)
|
|
|
|
|
2019-12-05 13:18:54 +00:00
|
|
|
if 'outname' not in args:
|
|
|
|
args['outname'] = name + "_mill_slots"
|
|
|
|
|
2019-12-08 20:11:39 +00:00
|
|
|
if 'use_thread' in args:
|
2020-04-22 11:37:03 +00:00
|
|
|
try:
|
|
|
|
par = args['use_thread'].capitalize()
|
|
|
|
except AttributeError:
|
|
|
|
par = args['use_thread']
|
|
|
|
args['use_thread'] = bool(eval(par))
|
2020-04-13 16:15:20 +00:00
|
|
|
else:
|
|
|
|
args['use_thread'] = False
|
2019-12-08 20:11:39 +00:00
|
|
|
|
2019-09-11 14:50:52 +00:00
|
|
|
if not obj.slots:
|
|
|
|
self.raise_tcl_error("The Excellon object has no slots: %s" % name)
|
|
|
|
|
2020-04-13 16:15:20 +00:00
|
|
|
# units = self.app.defaults['units'].upper()
|
2019-09-11 14:50:52 +00:00
|
|
|
try:
|
|
|
|
if 'milled_dias' in args and args['milled_dias'] != 'all':
|
2019-09-11 14:59:24 +00:00
|
|
|
diameters = [x.strip() for x in args['milled_dias'].split(",")]
|
2019-09-11 18:40:18 +00:00
|
|
|
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-12-05 13:18:54 +00:00
|
|
|
obj_dia_form = float('%.*f' % (obj.decimals, float(obj.tools[tool]["C"])))
|
|
|
|
req_dia_form = float('%.*f' % (obj.decimals, float(req_dia)))
|
2019-09-11 18:40:18 +00:00
|
|
|
|
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 slots 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))
|
|
|
|
|
2020-04-27 09:34:56 +00:00
|
|
|
if obj.kind != 'excellon':
|
2019-09-11 14:50:52 +00:00
|
|
|
self.raise_tcl_error('Only Excellon objects can have mill-slots, got %s %s.' % (name, type(obj)))
|
2019-01-03 19:25:08 +00:00
|
|
|
|
|
|
|
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_slots(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)
|