diff --git a/PySpice/Spice/BasicElement.py b/PySpice/Spice/BasicElement.py index a8322a0d..762cdca4 100644 --- a/PySpice/Spice/BasicElement.py +++ b/PySpice/Spice/BasicElement.py @@ -127,6 +127,7 @@ IntKeyParameter, ModelPositionalParameter, ) +from . import Library from .StringTools import join_list, join_dict from .unit import str_spice @@ -164,7 +165,27 @@ class SubCircuitElement(NPinElement): ############################################## - def __init__(self, netlist: 'Netlist', name: str, subcircuit_name, *nodes, **parameters) -> None: + def __init__( + self, + netlist: 'Netlist', + name: str, + subcircuit_name: str | Library.Subcircuit, + *nodes, + **parameters, + ) -> None: + if isinstance(subcircuit_name, Library.Subcircuit): + subcircuit = subcircuit_name + subcircuit_name = subcircuit.name + pins = {} + for pin in subcircuit.pin_names: + _ = parameters.pop(pin, None) + if _ is None: + raise ValueError(f"Missing pin {pin} for subcircuit {subcircuit_name}") + pins[pin] = _ + nodes = subcircuit.map_nodes(**pins) + # isinstance(netlist, Circuit) + if hasattr(netlist, 'include'): + netlist.include(subcircuit) super().__init__(netlist, name, nodes, subcircuit_name) # Fixme: match parameters to subcircuit self.parameters = parameters diff --git a/examples/diode/diode-characteristic-curve.py b/examples/diode/diode-characteristic-curve.py index 1a33796d..23d7f6a8 100755 --- a/examples/diode/diode-characteristic-curve.py +++ b/examples/diode/diode-characteristic-curve.py @@ -96,10 +96,9 @@ circuit.V('input', 'in', circuit.gnd, 10@u_V) circuit.R(1, 'in', 'out', 1@u_Ω) # not required for simulation D1N4148 = spice_library['1N4148'] -circuit.include(D1N4148) +# circuit.include(D1N4148) # circuit.X('D1', '1N4148', 'out', circuit.gnd) -diode_nodes = D1N4148.map_nodes(anode=0, cathode='out') -circuit.X('D1', D1N4148.name, *diode_nodes) +circuit.X('D1', D1N4148, cathode='out', anode=circuit.gnd) #r# We simulate the circuit at these temperatures: 0, 25 and 100 °C.