-
Notifications
You must be signed in to change notification settings - Fork 0
/
resgen_core.py
124 lines (116 loc) · 5.1 KB
/
resgen_core.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
""" resgen_core.py
Generates standard resistor symbols using the configuration found
in resgen.conf. Makes both a horizontal and a vertical version.
Will not overwrite an exisiting symbol. """
# -------------------------- Pure Python -----------------------------
import math
def processconf(rawfile):
resdict = {}
for line in rawfile.split('\n'):
if line.strip().startswith('value'):
resdict['value'] = line.split('=')[1].strip()
if line.strip().startswith('precision'):
resdict['precision'] = line.split('=')[1].strip()
if line.strip().startswith('part'):
resdict['part'] = line.split('=')[1].strip()
if line.strip().startswith('footprint'):
resdict['footprint'] = line.split('=')[1].strip()
return resdict
""" makename(dictionary containing part and value)
Creates the filename from the value parameter.
-- Every value will have three significant figures. """
def makename(resdict, footdict):
value = float(resdict['value'])
if (value/1e6 >= 1):
Mval = int(math.floor(value/1e6))
kval = int(value - Mval*1e6)
name = str(Mval) + 'M' + str(kval)
while (len(name) > 4):
name = name[0:-1] # Reduce to maximum of 3 significant figures
elif (value/1e3 >= 1):
kval = int(math.floor(value/1e3))
rval = int(value - kval*1e3)
name = str(kval) + 'k' + str(rval)
while (len(name) > 4):
name = name[0:-1] # Reduce to maximum of 3 significant figures
elif (value >= 1):
rval = int(math.floor(value))
name = str(rval) + 'r'
while (len(name) < 4):
name = name + '0'
elif (value < 1):
mval = int(math.floor(value * 1000))
name = str(mval) + 'm'
while (len(name) < 4):
name = name + '0'
footname = resdict['footprint']
name = name + '_' + footdict[footname] # Tack on the footprint name
return name
""" makevalue(Resistor dictionary)
Format the resistor value from the configuration file into the
string shown on a schematic.
--- Three significant digits and the engineering character for the
10x multiplier"""
def makevalue(resdict, footdict):
value = float(resdict['value'])
if (value/1e6 >= 1):
Mval = int(math.floor(value/1e6))
kval = int(value - Mval*1e6)
valstr = str(Mval) + '.' + str(kval)
while len(valstr) < (int(resdict['precision']) + 1):
valstr += '0' # Pad to specified precision
while len(valstr) > (int(resdict['precision']) + 1):
valstr = valstr[0:-1] # Reduce to specified precision
if valstr.endswith('.'):
valstr = valstr[0:-1] # Get rid of the decimal point
valstr += 'M'
elif (value/1e3 >= 1):
kval = int(math.floor(value/1e3))
rval = int(value - kval*1e3)
valstr = str(kval) + '.' + str(rval)
while len(valstr) < (int(resdict['precision']) + 1):
valstr += '0' # Pad to specified precision
while len(valstr) > (int(resdict['precision']) + 1):
valstr = valstr[0:-1] # Reduce to specified precision
if valstr.endswith('.'):
valstr = valstr[0:-1] # Get rid of the decimal point
valstr += 'k'
elif (value >= 1):
rval = int(math.floor(value))
valstr = str(rval)
if len(valstr) < (int(resdict['precision']) + 1):
valstr += '.'
while len(valstr) < (int(resdict['precision']) + 1):
valstr += '0' # Pad to specified precision
while len(valstr) > (int(resdict['precision']) + 1):
valstr = valstr[0:-1] # Reduce to specified precision
if valstr.endswith('.'):
valstr = valstr[0:-1] # Get rid of the decimal point
elif (value < 1):
mval = int(math.floor(value * 1000))
valstr = '0.' + str(mval)
while len(valstr) < (int(resdict['precision']) + 2):
valstr += '0' # Pad to specified precision
while len(valstr) > (int(resdict['precision']) + 2):
valstr = valstr[0:-1] # Reduce to specified precision
return valstr
def processhorz(resdict, template, footdict):
"""Construct a horizonal part"""
return "".join([template,
'T 0 1300 8 10 0 0 0 0 1' + '\n', # Footprint
'footprint=' + resdict['footprint'] + '\n',
'T 0 1095 8 10 0 0 0 0 1' + '\n', # Part number
'part=' + resdict['part'] + '\n',
'T 1300 0 8 10 1 1 0 0 1' + '\n', # Value
'value=' + makevalue(resdict, footdict) + '\n',
])
def processvert(resdict, template, footdict):
"""Construct a vertical part"""
return "".join([template,
'T 0 1700 8 10 0 0 0 0 1' + '\n', # Footprint
'footprint=' + resdict['footprint'] + '\n',
'T 0 1495 8 10 0 0 0 0 1' + '\n', # Part number
'part=' + resdict['part'] + '\n',
'T 300 500 8 10 1 1 0 0 1' + '\n', # Value
'value=' + makevalue(resdict, footdict) + '\n'
])