.. include:: /project-links.txt .. include:: /abbreviation.txt .. getthecode:: netlist-manipulations.py :language: python3 :hidden: :notebook: ======================= Netlist Manipulations ======================= This example shows how to manipulate netlist. .. code-block:: py3 import PySpice.Logging.Logging as Logging logger = Logging.setup_logging() from PySpice.Spice.Netlist import Circuit, SubCircuitFactory from PySpice.Unit import * class SubCircuit1(SubCircuitFactory): __name__ = 'sub_circuit1' __nodes__ = ('n1', 'n2') def __init__(self): super().__init__() self.R(1, 'n1', 'n2', 1@u_Ω) self.R(2, 'n1', 'n2', 2@u_Ω) Let define a circuit. .. code-block:: py3 circuit = Circuit('Test') When we add an element to a circuit, we can get a reference to it or ignore it: .. code-block:: py3 C1 = circuit.C(1, 0, 1, 1@u_uF) circuit.C(2, 1, 2, 2@u_uF) circuit.subcircuit(SubCircuit1()) circuit.X('1', 'sub_circuit1', 2, 0) We can get back an element of a circuit using its name, either as a class attribute or using the dictionary interface: .. code-block:: py3 C1 = circuit.C1 C1 = circuit['C1'] and modify it .. code-block:: py3 C1.capacitance = 10@u_F To get the SPICE netlist of a citcuit, we just have to convert it to a string: .. code-block:: py3 print(circuit) # str(circuit) is implicit here same apply to an element .. code-block:: py3 print(C1) We can disable an element in the circuit .. code-block:: py3 C1.enabled = False print(circuit) We can clone a circuit to another one .. code-block:: py3 circuit2 = circuit.clone(title='A clone') # title is optional print(circuit2) We can remove an element .. code-block:: py3 C2 = circuit2.C2.detach() print(circuit2)