-
Notifications
You must be signed in to change notification settings - Fork 7
/
output_KiCad_dynamic_spiral.py
103 lines (69 loc) · 2.51 KB
/
output_KiCad_dynamic_spiral.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import math
import numpy as np
from pathlib import Path
from configparser import ConfigParser
'''
EXPERIMENTAL
Functions that can output a KiCad file of a variable trace width square spiral
'''
# Read configuration
config = ConfigParser()
config.read(Path(__file__).with_name('config.ini'))
config = config['Configuration']
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
class SpiralShape:
def __init__(self):
self.coils = []
self.widths = []
def add_coil(self, width, *args):
self.coils.append([*args])
self.widths.append(width)
def flip(self):
for coil in self.coils:
for point in coil:
point = point[1], point[0]
def get_KiCad_text(self, layer):
out = ""
if layer % 2 == 1:
self.flip()
for coil_i in range(len(self.coils)):
coil = self.coils[coil_i]
width = self.widths[coil_i]
out += get_segment(width, get_layer_name(layer), *coil[0], *coil[1])
for point_i in range(2, len(coil)):
out += get_segment(width, get_layer_name(layer),
*coil[point_i-1], *coil[point_i])
out += get_segment(1, get_layer_name(layer), *self.coils[-1][-1], config.getfloat("OuterRadius"), config.getfloat("OuterRadius"))
return out
def get_segment(width, layer, x1, y1, x2, y2):
net = 0
return "(segment (start {:.4f} {:.4f}) (end {:.4f} {:.4f}) (width {:.4f}) (layer {}) (net {}))\n".format(
x1,
y1,
x2,
y2,
width, layer, net)
def get_layer_name(layer):
if layer == 0:
return 'F.Cu'
elif layer == config.getint("NumberOfLayers")-1:
return 'B.Cu'
else:
return f"In{layer}.Cu"
def save_spiral(exterior_shape, interior_shape):
num = config.getint("NumberOfLayers")
out = exterior_shape.get_KiCad_text(0)
for i in range(num-2):
out += interior_shape.get_KiCad_text(i+1)
out += exterior_shape.get_KiCad_text(num-1)
p = Path(__file__).with_name('KiCad_spiral.txt')
f = open(p, "w")
f.write(out)
f.close()
print("Saved optimal spiral in KiCad_spiral.txt")
print("Paste its entire content just before the final closing parantheses of your *.kicad_pcb file")
print("Save the file, and open KiCad. Your spiral should appear in the PCB editor.")
print("Be sure to add through-vias connecting the different spiral layers.")