Skip to content

Commit fc0c942

Browse files
authored
Add refdes + pathname-as-value mode for netlist generation (#412)
Probably a better default option, with a familiar refdes where that belongs, plus the pathname in the value field, which shows up on F.Fab in KiCad.
1 parent b61cfe8 commit fc0c942

File tree

96 files changed

+19569
-55086
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

96 files changed

+19569
-55086
lines changed

edg/BoardCompiler.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ def compile_board(design: Type[Block], target_dir_name: Optional[Tuple[str, str]
4646
raise core.ScalaCompilerInterface.CompilerCheckError(f"error during compilation:\n{compiled.errors_str()}")
4747

4848
netlist_all = NetlistBackend().run(compiled)
49-
netlist_refdes = NetlistBackend().run(compiled, {'RefdesMode': 'refdes'})
5049
bom_all = GenerateBom().run(compiled)
5150
svgpcb_all = SvgPcbBackend().run(compiled)
5251
assert len(netlist_all) == 1
@@ -55,9 +54,6 @@ def compile_board(design: Type[Block], target_dir_name: Optional[Tuple[str, str]
5554
with open(netlist_filename, 'w', encoding='utf-8') as net_file:
5655
net_file.write(netlist_all[0][1])
5756

58-
with open(netlist_refdes_filename, 'w', encoding='utf-8') as net_file:
59-
net_file.write(netlist_refdes[0][1])
60-
6157
with open(bom_filename, 'w', encoding='utf-8') as bom_file:
6258
bom_file.write(bom_all[0][1])
6359

edg/electronics_model/NetlistBackend.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,18 @@ class NetlistBackend(BaseBackend):
1010
def run(self, design: CompiledDesign, args: Dict[str, str] = {}) -> List[Tuple[edgir.LocalPath, str]]:
1111
if set(args.keys()) - {'RefdesMode'} != set():
1212
raise ValueError("Invalid argument found in args")
13-
refdes_mode = args.get("RefdesMode", "pathName")
14-
if refdes_mode == 'pathName':
15-
refdes_pathname = True
16-
elif refdes_mode == 'refdes':
17-
refdes_pathname = False
13+
refdes_mode_arg = args.get("RefdesMode", "refdesPathNameValue")
14+
if refdes_mode_arg == 'pathName':
15+
refdes_mode = kicad.RefdesMode.Pathname
16+
elif refdes_mode_arg == 'refdes':
17+
refdes_mode = kicad.RefdesMode.Conventional
18+
elif refdes_mode_arg == 'refdesPathNameValue':
19+
refdes_mode = kicad.RefdesMode.PathnameAsValue
1820
else:
19-
raise ValueError(f"Invalid valueMode value {refdes_mode}")
21+
raise ValueError(f"Invalid valueMode value {refdes_mode_arg}")
2022

2123
netlist = NetlistTransform(design).run()
22-
netlist_string = kicad.generate_netlist(netlist, refdes_pathname)
24+
netlist_string = kicad.generate_netlist(netlist, refdes_mode)
2325

2426
return [
2527
(edgir.LocalPath(), netlist_string)

edg/electronics_model/footprint.py

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
import zlib # for deterministic hash
2+
from enum import Enum, auto
23
from typing import List
34

4-
from .. import edgir
5-
from ..core import TransformUtil
65
from .NetlistGenerator import Netlist, NetBlock, Net
6+
from .. import edgir
7+
8+
9+
class RefdesMode(Enum):
10+
PathnameAsValue = auto() # conventional refdes w/ pathname as value, except for TPs
11+
Conventional = auto() # conventional refdes only, value passed through
12+
Pathname = auto() # pathname as refdes, value passed through
13+
714

815

916
###############################################################################################################################################################################################
@@ -17,17 +24,24 @@ def gen_header() -> str:
1724

1825
"""2. Generating Blocks"""
1926

20-
def block_name(block: NetBlock, refdes_pathname: bool) -> str:
21-
if refdes_pathname:
27+
def block_name(block: NetBlock, refdes_mode: RefdesMode) -> str:
28+
if refdes_mode == RefdesMode.Pathname:
2229
return '.'.join(block.path)
2330
else:
24-
return block.refdes
31+
return block.refdes # default is conventional refdes
2532

2633
def gen_block_comp(block_name: str) -> str:
2734
return f'(comp (ref "{block_name}")'
2835

29-
def gen_block_value(block_value: str) -> str:
30-
return f'(value "{block_value}")'
36+
def gen_block_value(block: NetBlock, refdes_mode: RefdesMode) -> str:
37+
if refdes_mode == RefdesMode.PathnameAsValue:
38+
if block.refdes.startswith('TP'): # test points keep their value
39+
return f'(value "{block.value}")'
40+
else:
41+
pathname = '.'.join(block.path)
42+
return f'(value "{pathname}")'
43+
else:
44+
return f'(value "{block.value}")'
3145

3246
def gen_block_footprint(block_footprint: str) -> str:
3347
return f'(footprint "{block_footprint}")'
@@ -59,19 +73,14 @@ def gen_block_prop_sheetfile(block_path: List[edgir.LibraryPath]) -> str:
5973
value = ""
6074
return f'(property (name "Sheetfile") (value "{value}"))'
6175

62-
def gen_block_prop_path(block_path: TransformUtil.Path) -> str:
63-
return f'(property (name "edg_path") (value "{".".join(block_path.to_tuple())}"))'
64-
65-
def gen_block_prop_shortpath(block_path: List[str]) -> str:
66-
return f'(property (name "edg_short_path") (value "{".".join(block_path)}"))'
67-
68-
def gen_block_prop_refdes(refdes: str) -> str:
69-
return f'(property (name "edg_refdes") (value "{refdes}"))'
70-
71-
def gen_block_prop_part(part: str) -> str:
72-
return f'(property (name "edg_part") (value "{part}"))'
76+
def gen_block_prop_edg(block: NetBlock, refdes_mode: RefdesMode) -> str:
77+
return f'(property (name "edg_path") (value "{".".join(block.full_path.to_tuple())}"))\n' +\
78+
f' (property (name "edg_short_path") (value "{".".join(block.path)}"))\n' + \
79+
f' (property (name "edg_refdes") (value "{block.refdes}"))\n' + \
80+
f' (property (name "edg_part") (value "{block.part}"))\n' + \
81+
f' (property (name "edg_value") (value "{block.value}"))\n'
7382

74-
def block_exp(blocks: List[NetBlock], refdes_pathname: bool) -> str:
83+
def block_exp(blocks: List[NetBlock], refdes_mode: RefdesMode) -> str:
7584
"""Given a dictionary of block_names (strings) as keys and Blocks (namedtuples) as corresponding values
7685
7786
Example:
@@ -87,15 +96,12 @@ def block_exp(blocks: List[NetBlock], refdes_pathname: bool) -> str:
8796
"""
8897
result = '(components'
8998
for block in blocks:
90-
result += '\n' + gen_block_comp(block_name(block, refdes_pathname)) + '\n' +\
91-
" " + gen_block_value(block.value) + '\n' + \
99+
result += '\n' + gen_block_comp(block_name(block, refdes_mode)) + '\n' +\
100+
" " + gen_block_value(block, refdes_mode) + '\n' + \
92101
" " + gen_block_footprint(block.footprint) + '\n' + \
93102
" " + gen_block_prop_sheetname(block.path) + '\n' + \
94103
" " + gen_block_prop_sheetfile(block.class_path) + '\n' + \
95-
" " + gen_block_prop_path(block.full_path) + '\n' + \
96-
" " + gen_block_prop_shortpath(block.path) + '\n' + \
97-
" " + gen_block_prop_refdes(block.refdes) + '\n' + \
98-
" " + gen_block_prop_part(block.part) + '\n' + \
104+
" " + gen_block_prop_edg(block, refdes_mode) + '\n' + \
99105
" " + gen_block_sheetpath(block.path[:-1]) + '\n' + \
100106
" " + gen_block_tstamp(block.path)
101107
return result + ')'
@@ -110,7 +116,7 @@ def gen_net_header(net_count: int, net_name: str) -> str:
110116
def gen_net_pin(block_name: str, pin_name: str) -> str:
111117
return "(node (ref {}) (pin {}))".format(block_name, pin_name)
112118

113-
def net_exp(nets: List[Net], blocks: List[NetBlock], refdes_pathname: bool) -> str:
119+
def net_exp(nets: List[Net], blocks: List[NetBlock], refdes_mode: RefdesMode) -> str:
114120
"""Given a dictionary of net names (strings) as keys and a list of connected Pins (namedtuples) as corresponding values
115121
116122
Example:
@@ -130,7 +136,7 @@ def net_exp(nets: List[Net], blocks: List[NetBlock], refdes_pathname: bool) -> s
130136
for i, net in enumerate(nets):
131137
result += '\n' + gen_net_header(i + 1, net.name)
132138
for pin in net.pins:
133-
result += '\n ' + gen_net_pin(block_name(block_dict[pin.block_path], refdes_pathname), pin.pin_name)
139+
result += '\n ' + gen_net_pin(block_name(block_dict[pin.block_path], refdes_mode), pin.pin_name)
134140
result += ')'
135141
return result + ')'
136142

@@ -139,6 +145,6 @@ def net_exp(nets: List[Net], blocks: List[NetBlock], refdes_pathname: bool) -> s
139145
"""4. Generate Full Netlist"""
140146

141147

142-
def generate_netlist(netlist: Netlist, refdes_pathname: bool) -> str:
143-
return gen_header() + '\n' + block_exp(netlist.blocks, refdes_pathname) + '\n' \
144-
+ net_exp(netlist.nets, netlist.blocks, refdes_pathname) + '\n' + ')'
148+
def generate_netlist(netlist: Netlist, refdes_mode: RefdesMode) -> str:
149+
return gen_header() + '\n' + block_exp(netlist.blocks, refdes_mode) + '\n' \
150+
+ net_exp(netlist.nets, netlist.blocks, refdes_mode) + '\n' + ')'

0 commit comments

Comments
 (0)