Coverage for klayout_pex/klayout/netlist_printer.py: 100%
38 statements
« prev ^ index » next coverage.py v7.11.0, created at 2025-10-31 20:14 +0000
« prev ^ index » next coverage.py v7.11.0, created at 2025-10-31 20:14 +0000
1#
2# --------------------------------------------------------------------------------
3# SPDX-FileCopyrightText: 2024-2025 Martin Jan Köhler and Harald Pretl
4# Johannes Kepler University, Institute for Integrated Circuits.
5#
6# This file is part of KPEX
7# (see https://github.com/iic-jku/klayout-pex).
8#
9# This program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 3 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program. If not, see <http://www.gnu.org/licenses/>.
21# SPDX-License-Identifier: GPL-3.0-or-later
22# --------------------------------------------------------------------------------
23#
25from datetime import datetime
26from pathlib import Path
27from typing import *
29import klayout.db as kdb
31from ..extraction_engine import ExtractionEngine
32from ..pdk_config import PDKConfig
33from ..util.unit_formatter import format_spice_number
34from ..version import __version__
37class NetlistPrinter(kdb.NetlistSpiceWriterDelegate):
38 def __init__(self,
39 extraction_engine: ExtractionEngine,
40 pdk: PDKConfig):
41 super().__init__()
43 self.extraction_engine = extraction_engine
44 self.pdk = pdk
46 self.spice_writer = kdb.NetlistSpiceWriter(self)
47 self.spice_writer.use_net_names = True
48 self.spice_writer.with_comments = False
50 def write(self,
51 netlist: kdb.Netlist,
52 output_path: str | Path):
53 netlist.write(output_path, self.spice_writer)
55 # --------------------------------------------------------------------------------
56 # NetlistSpiceWriterDelegate overwrites
58 def write_header(self, *args, **kwargs):
59 now = datetime.now()
60 header_date = now.strftime("%Y-%m-%d %H:%M:%S")
62 self.emit_line(f"*********************************************************")
63 self.emit_line(f"*** NGSPICE file created by KLayout-PEX {__version__}")
64 self.emit_line(f"*** -----------------------------------------------------")
65 self.emit_line(f"*** Extraction Engine: {self.extraction_engine}")
66 self.emit_line(f"*** Technology: {self.pdk.name.lower()}")
67 self.emit_line(f"*** Date: {header_date}")
68 self.emit_line(f"*********************************************************")
70 def write_device(self, device: kdb.Device):
71 dc = device.device_class()
72 match dc:
73 case kdb.DeviceClassCapacitor():
74 c_farad = device.parameter('C')
75 net1 = self.net_to_string(device.net_for_terminal(0))
76 net2 = self.net_to_string(device.net_for_terminal(1))
77 self.emit_line(f"C{device.name} {net1} {net2} {format_spice_number(c_farad)}")
79 case _:
80 super().write_device(device)