2019-10-24 23:10:52 +00:00
|
|
|
import collections
|
2019-01-03 19:25:08 +00:00
|
|
|
from tclCommands.TclCommand import TclCommandSignaled
|
2019-10-24 23:10:52 +00:00
|
|
|
|
|
|
|
from shapely.geometry import Point
|
|
|
|
import shapely.affinity as affinity
|
2019-01-03 19:25:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TclCommandAlignDrill(TclCommandSignaled):
|
|
|
|
"""
|
|
|
|
Tcl shell command to create excellon with drills for aligment.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# array of all command aliases, to be able use old names for
|
|
|
|
# backward compatibility (add_poly, add_polygon)
|
|
|
|
aliases = ['aligndrill']
|
|
|
|
|
2020-04-13 16:15:20 +00:00
|
|
|
description = '%s %s' % ("--", "Create an Excellon object with drills for alignment.")
|
|
|
|
|
2019-01-03 19:25:08 +00:00
|
|
|
# Dictionary of types from Tcl command, needs to be ordered.
|
|
|
|
# For positional arguments
|
|
|
|
arg_names = collections.OrderedDict([
|
|
|
|
('name', str)
|
|
|
|
])
|
|
|
|
|
|
|
|
# Dictionary of types from Tcl command, needs to be ordered.
|
|
|
|
# For options like -optionname value
|
|
|
|
option_types = collections.OrderedDict([
|
|
|
|
('box', str),
|
|
|
|
('axis', str),
|
|
|
|
('holes', str),
|
|
|
|
('grid', float),
|
|
|
|
('minoffset', float),
|
|
|
|
('gridoffset', float),
|
|
|
|
('axisoffset', float),
|
|
|
|
('dia', float),
|
|
|
|
('dist', float),
|
2019-09-16 00:12:37 +00:00
|
|
|
('outname', str),
|
2019-01-03 19:25:08 +00:00
|
|
|
])
|
|
|
|
|
|
|
|
# array of mandatory options for current Tcl command: required = {'name','outname'}
|
|
|
|
required = ['name', 'axis']
|
|
|
|
|
|
|
|
# structured help for current command, args needs to be ordered
|
|
|
|
help = {
|
2020-04-09 01:13:04 +00:00
|
|
|
'main': "Create an Excellon object with drills for alignment.",
|
2019-01-03 19:25:08 +00:00
|
|
|
'args': collections.OrderedDict([
|
|
|
|
('name', 'Name of the object (Gerber or Excellon) to mirror.'),
|
|
|
|
('dia', 'Tool diameter'),
|
|
|
|
('box', 'Name of object which act as box (cutout for example.)'),
|
2019-10-25 13:16:43 +00:00
|
|
|
('holes', 'Tuple of tuples where each tuple it is a set of x, y coordinates. '
|
|
|
|
'E.g: (x0, y0), (x1, y1), ... '),
|
2019-01-03 19:25:08 +00:00
|
|
|
('grid', 'Aligning to grid, for those, who have aligning pins'
|
|
|
|
'inside table in grid (-5,0),(5,0),(15,0)...'),
|
|
|
|
('gridoffset', 'offset of grid from 0 position.'),
|
|
|
|
('minoffset', 'min and max distance between align hole and pcb.'),
|
|
|
|
('axisoffset', 'Offset on second axis before aligment holes'),
|
|
|
|
('axis', 'Mirror axis parallel to the X or Y axis.'),
|
2019-09-16 00:12:37 +00:00
|
|
|
('dist', 'Distance of the mirror axis to the X or Y axis.'),
|
|
|
|
('outname', 'Name of the resulting Excellon object.'),
|
2019-01-03 19:25:08 +00:00
|
|
|
]),
|
2019-09-16 00:12:37 +00:00
|
|
|
'examples': ['aligndrill my_object -axis X -box my_object -dia 3.125 -grid 1 '
|
|
|
|
'-gridoffset 0 -minoffset 2 -axisoffset 2']
|
2019-01-03 19:25:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
def execute(self, args, unnamed_args):
|
|
|
|
"""
|
|
|
|
execute current TCL shell command
|
|
|
|
|
|
|
|
: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']
|
|
|
|
|
2019-09-16 00:12:37 +00:00
|
|
|
if 'outname' in args:
|
|
|
|
outname = args['outname']
|
|
|
|
else:
|
|
|
|
outname = name + "_aligndrill"
|
|
|
|
|
2019-01-03 19:25:08 +00:00
|
|
|
# Get source object.
|
|
|
|
try:
|
|
|
|
obj = self.app.collection.get_by_name(str(name))
|
2019-11-11 00:35:42 +00:00
|
|
|
except Exception:
|
2019-01-03 19:25:08 +00:00
|
|
|
return "Could not retrieve object: %s" % name
|
|
|
|
|
|
|
|
if obj is None:
|
|
|
|
return "Object not found: %s" % name
|
|
|
|
|
2020-04-27 09:34:56 +00:00
|
|
|
if obj.kind != "geometry" and obj.kind != 'gerber' and obj.kind != 'excellon':
|
2019-01-03 19:25:08 +00:00
|
|
|
return "ERROR: Only Gerber, Geometry and Excellon objects can be used."
|
|
|
|
|
|
|
|
# Axis
|
|
|
|
try:
|
|
|
|
axis = args['axis'].upper()
|
|
|
|
except KeyError:
|
|
|
|
return "ERROR: Specify -axis X or -axis Y"
|
|
|
|
|
|
|
|
if not ('holes' in args or ('grid' in args and 'gridoffset' in args)):
|
|
|
|
return "ERROR: Specify -holes or -grid with -gridoffset "
|
|
|
|
|
|
|
|
if 'holes' in args:
|
|
|
|
try:
|
|
|
|
holes = eval("[" + args['holes'] + "]")
|
|
|
|
except KeyError:
|
|
|
|
return "ERROR: Wrong -holes format (X1,Y1),(X2,Y2)"
|
|
|
|
|
|
|
|
xscale, yscale = {"X": (1.0, -1.0), "Y": (-1.0, 1.0)}[axis]
|
|
|
|
|
|
|
|
# Tools
|
|
|
|
tools = {"1": {"C": args['dia']}}
|
|
|
|
|
|
|
|
def alligndrill_init_me(init_obj, app_obj):
|
|
|
|
"""
|
|
|
|
This function is used to initialize the new
|
|
|
|
object once it's created.
|
|
|
|
|
|
|
|
:param init_obj: The new object.
|
|
|
|
:param app_obj: The application (FlatCAMApp)
|
|
|
|
:return: None
|
|
|
|
"""
|
|
|
|
|
|
|
|
drills = []
|
|
|
|
if 'holes' in args:
|
|
|
|
for hole in holes:
|
|
|
|
point = Point(hole)
|
|
|
|
point_mirror = affinity.scale(point, xscale, yscale, origin=(px, py))
|
|
|
|
drills.append({"point": point, "tool": "1"})
|
|
|
|
drills.append({"point": point_mirror, "tool": "1"})
|
|
|
|
else:
|
|
|
|
if 'box' not in args:
|
|
|
|
return "ERROR: -grid can be used only for -box"
|
|
|
|
|
|
|
|
if 'axisoffset' in args:
|
|
|
|
axisoffset = args['axisoffset']
|
|
|
|
else:
|
|
|
|
axisoffset = 0
|
|
|
|
|
|
|
|
# This will align hole to given aligngridoffset and minimal offset from pcb, based on selected axis
|
|
|
|
if axis == "X":
|
|
|
|
firstpoint = args['gridoffset']
|
|
|
|
|
|
|
|
while (xmin - args['minoffset']) < firstpoint:
|
|
|
|
firstpoint = firstpoint - args['grid']
|
|
|
|
|
|
|
|
lastpoint = args['gridoffset']
|
|
|
|
|
|
|
|
while (xmax + args['minoffset']) > lastpoint:
|
|
|
|
lastpoint = lastpoint + args['grid']
|
|
|
|
|
|
|
|
localholes = (firstpoint, axisoffset), (lastpoint, axisoffset)
|
|
|
|
|
|
|
|
else:
|
|
|
|
firstpoint = args['gridoffset']
|
|
|
|
|
|
|
|
while (ymin - args['minoffset']) < firstpoint:
|
|
|
|
firstpoint = firstpoint - args['grid']
|
|
|
|
|
|
|
|
lastpoint = args['gridoffset']
|
|
|
|
|
|
|
|
while (ymax + args['minoffset']) > lastpoint:
|
|
|
|
lastpoint = lastpoint + args['grid']
|
|
|
|
|
|
|
|
localholes = (axisoffset, firstpoint), (axisoffset, lastpoint)
|
|
|
|
|
|
|
|
for hole in localholes:
|
|
|
|
point = Point(hole)
|
|
|
|
point_mirror = affinity.scale(point, xscale, yscale, origin=(px, py))
|
|
|
|
drills.append({"point": point, "tool": "1"})
|
|
|
|
drills.append({"point": point_mirror, "tool": "1"})
|
|
|
|
|
|
|
|
init_obj.tools = tools
|
|
|
|
init_obj.drills = drills
|
|
|
|
init_obj.create_geometry()
|
|
|
|
|
|
|
|
# Box
|
|
|
|
if 'box' in args:
|
|
|
|
try:
|
|
|
|
box = self.app.collection.get_by_name(args['box'])
|
2019-11-11 00:35:42 +00:00
|
|
|
except Exception:
|
2019-01-03 19:25:08 +00:00
|
|
|
return "Could not retrieve object box: %s" % args['box']
|
|
|
|
|
|
|
|
if box is None:
|
|
|
|
return "Object box not found: %s" % args['box']
|
|
|
|
|
|
|
|
try:
|
|
|
|
xmin, ymin, xmax, ymax = box.bounds()
|
|
|
|
px = 0.5 * (xmin + xmax)
|
|
|
|
py = 0.5 * (ymin + ymax)
|
|
|
|
|
2019-09-16 00:12:37 +00:00
|
|
|
obj.app.new_object("excellon", outname, alligndrill_init_me, plot=False)
|
2019-01-03 19:25:08 +00:00
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
return "Operation failed: %s" % str(e)
|
|
|
|
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
dist = float(args['dist'])
|
|
|
|
except KeyError:
|
|
|
|
dist = 0.0
|
|
|
|
except ValueError:
|
|
|
|
return "Invalid distance: %s" % args['dist']
|
|
|
|
|
|
|
|
try:
|
|
|
|
px = dist
|
|
|
|
py = dist
|
2019-09-16 00:12:37 +00:00
|
|
|
obj.app.new_object("excellon", outname, alligndrill_init_me, plot=False)
|
2019-01-03 19:25:08 +00:00
|
|
|
except Exception as e:
|
|
|
|
return "Operation failed: %s" % str(e)
|
|
|
|
|
2019-09-16 00:12:37 +00:00
|
|
|
return 'Ok. Align Drills Excellon object created'
|