-
Notifications
You must be signed in to change notification settings - Fork 0
/
cutout.py
executable file
·75 lines (58 loc) · 2.31 KB
/
cutout.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env python
import argparse
import sys
import dxfgrabber
from dxfwrite import DXFEngine
from Polygon import Polygon
import geo
# This utility has a lot of common functionality with gentrap.py, so you
# should see that script too.
def main(filein, fileout, width, height, gap):
d = dxfgrabber.read(filein)
def flipxy(p):
return (p[1], p[0])
layers = {}
for r in d.entities:
if not r.layer in layers:
layers[r.layer] = []
layers[r.layer].append(map(flipxy, r.points))
polys_to_cut = []
for (_, ps) in layers.iteritems():
polys_to_cut += map(lambda p: geo.extend_poly(gap, p, False), ps)
h = height / 2.0
w = width / 2.0
gndplane = Polygon([(h, w), (-h, w), (-h, -w), (h, -w)])
for p in polys_to_cut:
gndplane = gndplane - Polygon(p)
# Polygon.triStrip() returns a list of tristrips which need to be
# turned into quads, and the list needs to be flattened.
layers["GROUND"] = [
quad for quads in map(
geo.tristrip_to_quads,
gndplane.triStrip())
for quad in quads]
drawing = DXFEngine.drawing(fileout)
for (l, qs) in layers.iteritems():
for q in qs:
drawing.add(DXFEngine.face3d(map(flipxy, q), layer=l))
drawing.save()
if __name__ == '__main__':
DESC = "Cut out the ground plane for an electrode layout."
parser = argparse.ArgumentParser(description=DESC)
parser.add_argument('layout', metavar='LAYOUT', type=str, nargs='?',
help="DXF file containing just the electrode layout")
parser.add_argument('output', metavar='OUTPUT', type=str, nargs='?',
help="Output DXF file")
parser.add_argument('width', metavar='WIDTH', type=int, nargs='?',
help="Width of the ground plane to cut out")
parser.add_argument('height', metavar='HEIGHT', type=int, nargs='?',
help="Height of the ground plane to cut out")
parser.add_argument('gap', metavar='GAP', type=int, nargs='?',
help="Spacing between electrodes and ground plane")
if len(sys.argv) != 6:
parser.print_help()
sys.exit()
args = parser.parse_args()
print(args.layout)
main(file(args.layout), args.output, args.width, args.height, args.gap)
print("Wrote to {}".format(args.output))