=============================
Creating / editing schematics
=============================

Launching the schematic editor from the GUI
===========================================

The *Structured Electronic Design Environment* interfaces with both SLiCAP and NGspice. Although schematics for both applications may look similar, models and component attributes differ for both applications. SLiCAP is intended for symbolic analysis of linear continuous-time, dynamic circuits, whereas NGspice supports numeric analysis of nonlinear, time-variant, dynamic circuits. For this reason, The GUI uses different symbol sets and libraries for SLiCAP and NGspice. SLiCAP schematic files use the file extension ``.slicap_sch`` and NGspice schematics use ``.spice_sch`` as file extension.

The schematic edditor can be started as follows:

#. From the main menu select File -> New SLiCAP Schematic
#. From the main menu select File -> New NGspice Schematic
#. From the main menu select File -> Open schematic

Below the screenshot after creating a new SLiCAP schematic.

.. Figure:: /GUI/img/new_schematic.png

After saving it as "my_circuit.slicap_sch" it becomes visible in the Project panel (in the ``.sch`` folder):

.. Figure:: /GUI/img/my_circuit.png

Launching the schematic editor from outside the GUI
===================================================

**From a Python session or Jupyter notebook** (the usual way):

.. code-block:: python

   import SLiCAP as sl
   sl.initProject("My Design")

   sl.startSchematic()                                # empty editor; choose type from the File menu
   sl.startSchematic(config='basic')                  # SLiCAP basic-symbol capture mode
   sl.startSchematic(file='sch/mydesign.slicap_sch')  # open an existing file
   sl.startSchematic(config='basic', file='sch/mydesign.slicap_sch')

The call returns immediately; the editor runs as an independent process alongside
the Python session (schematic-only: no Instruction/Log panels).

``config`` selects the **capture mode**.  It sets the symbol library *and*
restricts which schematic type may be created or opened:

.. list-table::
   :header-rows: 1
   :widths: 12 46 42

   * - ``config``
     - Symbol set loaded
     - Schematic type
   * - ``None`` *(default)*
     - chosen per schematic
     - both SLiCAP and NGspice allowed
   * - ``'basic'``
     - ``Symbols.slicap_sym`` only (basic IEC/SLiCAP symbols only)
     - SLiCAP only (NGspice disabled)
   * - ``'slicap'``
     - the complete SLiCAP library (all SVG files in the system symbols directory)
     - SLiCAP only (NGspice disabled)
   * - ``'ngspice'``
     - the NGspice symbol library
     - NGspice only (SLiCAP disabled)

In a restricted mode the entry of the other type,
:menuselection:`File --> New SLiCAP Schematic` or
:menuselection:`File --> New NGspice Schematic`, is greyed out.
:menuselection:`File --> Open schematic…` lists only files of the permitted
type.

``file`` is the path to a schematic to open at startup; its type is inferred from
the extension (``.slicap_sch`` or ``.spice_sch``).  **A canvas is shown only when
a file is given** — otherwise the editor opens empty and you create or open a
schematic from the File menu (honouring the capture mode).

**From the command line** (for scripting or desktop shortcuts):

.. code-block:: console

   $ slicap                                                       # full editor, both types
   $ slicap-schematics                                            # schematic-only editor, empty
   $ slicap-schematics --config basic                             # basic SLiCAP mode, empty
   $ slicap-schematics --config ngspice sch/mydesign.spice_sch    # open an NGspice file
   $ python -m SLiCAP.schematic.main --schematic-only --config basic

Without a ``file`` the editor opens empty; use the File menu to create or open a
schematic in the selected mode.

.. figure:: /GUI/img/main_window.png
   :alt: The main window
   :width: 100%
   
The schematic menu bar at a glance
==================================

By default each schematic is displayed in a separate tab, but each open schematic has its own panel with its own menu bar with actions on that schematic:

.. list-table::
   :header-rows: 1
   :widths: 18 82

   * - Menu
     - Contents
   * - **File**
     - Save schematic (:kbd:`Ctrl+S`), Save schematic as
       (:kbd:`Ctrl+Shift+S`), Schematic properties, Export netlist
       (:kbd:`Ctrl+E`), Export SVG, Export PDF, Print schematic
       (:kbd:`Ctrl+P`), Drawing preferences.
   * - **Edit**
     - Undo (:kbd:`Ctrl+Z`), Redo (:kbd:`Ctrl+Y`), Bring to front
       (:kbd:`Ctrl+Shift+]`), Bring forward (:kbd:`Ctrl+]`), Send backward
       (:kbd:`Ctrl+[`), Send to back (:kbd:`Ctrl+Shift+[`).
   * - **View**
     - Fit (:kbd:`F`), Zoom In (:kbd:`+`), Zoom Out (:kbd:`-`),
       Reset Zoom (:kbd:`Ctrl+0`).
   * - **Circuit**
     - What reaches the netlist: Symbol (:kbd:`S`), Wire (:kbd:`W`), Net
       Label (:kbd:`L`), Junction (:kbd:`J`), Library, New subcircuit symbol
       (see :doc:`/GUI/schematics/hierarchical_blocks`), Parameters, Define
       src / det / lg ref, Model definition.
   * - **Insert**
     - Objects that follow the runs: Figure from design data, LaTeX from
       design data, LaTeX snippet file, Image.
   * - **Draw**
     - Annotation by hand: Line, Rectangle, Ellipse, Polygon, Arc, Curve,
       Function curve, Text (:kbd:`T`), Hyperlink, LaTeX text.
   * - **Document**
     - Document properties, Border (:kbd:`B`).
   * - **Tools**
     - Rename Components, Load selected symbols from library, Update symbols
       from library, Import symbols from file.
   * - **Instruction**
     - Create circuit object, Create / edit SLiCAP instruction and Transfer
       function (SLiCAP schematics). Create netlist, Create / edit NGspice
       instruction and Create / edit NGspice control section (NGspice
       schematics). See :doc:`/GUI/instruction/instructions`.

A poster has no Circuit, Tools or Instruction menu. Its Insert menu also
places a schematic or another poster (see :doc:`/GUI/schematics/posters`).

Creating and opening schematics, and application-wide actions, live in the
**main window's** menu bar:

.. list-table::
   :header-rows: 1
   :widths: 18 82

   * - Menu
     - Contents
   * - **File**
     - New project, Select project folder, Save project, Close project.
       New SLiCAP Schematic, New NGspice Schematic, Open schematic
       (:kbd:`Ctrl+O`), New poster (see :doc:`/GUI/schematics/posters`), New
       symbol, Edit symbol (see :doc:`/GUI/schematics/symbol_editor`), New
       Instruction file, Open instruction file. Preferences, Edit main
       configuration file. Exit (:kbd:`Ctrl+Q`).
   * - **View**
     - Show/hide the Project, Design data, Instructions and Log panels.
       Reset panel layout.
   * - **Instruction**
     - Create / Edit Traces and Measurements, Axes and Figures. Create LaTeX
       snippet, Create RST (Sphinx) snippet (see :doc:`/GUI/reports/snippets`).
       Create / Edit specifications. Update schematic images. Run
       (:kbd:`F5`), Stop (:kbd:`F6`).
   * - **Help**
     - Show HTML Documentation (:kbd:`F1`), Keyboard shortcuts, Create
       shortcut, Check for updates, About.

*Help > Keyboard shortcuts* lists every shortcut: those of the menus, and the
keys the canvas handles itself, such as :kbd:`R` to rotate, :kbd:`M` to
mirror, :kbd:`/` to switch the corner of a wire, :kbd:`Del`, :kbd:`Esc` and
:kbd:`Enter`. The list is read from the program, so it always matches it.

*Help > Create shortcut* puts SLiCAP in the launcher of your system, with
the icon of SLiCAP:

* On Windows it makes a shortcut in the start menu and one on the desktop.
  To keep SLiCAP in the taskbar, right-click it in the start menu and
  choose *Pin to taskbar*.
* On Linux it makes the entry ``slicap.desktop`` in
  ``~/.local/share/applications``. SLiCAP then appears in the applications
  overview. To keep it in the dock, right-click it there and choose *Add to
  Favorites*.

The shortcut starts SLiCAP with the Python environment that runs the GUI
when you create it. In an Anaconda environment on Windows, the shortcut
first activates that environment, as Anaconda's own shortcuts do. When you
start SLiCAP from another environment, create the shortcut again: it then
replaces the old one.

Started from the shortcut, SLiCAP has no console window. Its messages go to
the file ``gui.log`` in the folder ``SLiCAP`` of your home folder. Look
there when SLiCAP does not start. The file is new at every start.

A first schematic in five steps
===============================

#. **Place a symbol.**  Open :menuselection:`Circuit --> Symbol…` (shortcut
   :kbd:`S`), pick a resistor and click on the canvas to drop it.  See
   :doc:`/GUI/schematics/placing_symbols`.

#. **Wire it up.**  Choose :menuselection:`Circuit --> Wire` (shortcut :kbd:`W`)
   and click from one pin to the next.  Unconnected pins show a small grey
   marker that disappears once a wire reaches them.  See :doc:`/GUI/schematics/wiring`.

#. **Set values.**  Double-click a component to open its **Properties** dialog
   and enter a value (for example ``R_s`` for a resistance).  See
   :doc:`/GUI/schematics/component_properties`.

#. **Mark source and detector.**  Use
   :menuselection:`Circuit --> Define src / det / lg ref…` to designate the
   independent source and the detector. Do this as the last step, with the
   schematic complete and saved: the dialog's drop-down lists come from the
   parsed circuit, so elements added later are not offered until the dialog
   is reopened (see :doc:`/GUI/schematics/labels_ports_parameters`).

#. **Save and export.**  :menuselection:`File --> Save schematic` writes the
   ``.slicap_sch`` file; :menuselection:`File --> Export netlist…` produces a
   ``.slicap_cir`` netlist for SLiCAP.  See :doc:`/GUI/schematics/netlist_and_export`.

A schematic can also be saved as a reusable **subcircuit** and placed as a
block in other schematics; see :doc:`/GUI/schematics/hierarchical_blocks`.
   
   
Below an example of a SLiCAP schematic with default preferences and LaTeX rendering enabled.

.. Figure:: /GUI/img/RCcircuit.png

LaTeX labels
============

With LaTeX rendering on, pdflatex and dvisvgm typeset the values and
reference designators of the components. The editor renders them in the
background:

* A placed, edited or pasted component shows its labels as plain text at
  once. The typeset labels replace them when they are ready. Meanwhile the
  status bar says *Rendering n LaTeX labels*.
* Opening a schematic, undo, redo, export and print wait until their labels
  are rendered, with the window responsive. After half a second a window
  says that LaTeX is rendering. *Continue with plain text* ends the wait.
* MiKTeX may ask in a window of its own to install a missing package. That
  window can open behind the editor. The rendering waits for the answer.
* A value whose LaTeX does not compile keeps its plain text.

The rendered labels are kept in the ``.cache`` sidecar, so the next opening
renders nothing.

Sidecar files
=============

A schematic is **self-contained**: everything it needs to look and behave the
same on another machine travels next to it in ``sch/``.  Saving
``my_circuit.slicap_sch`` creates and maintains a small set of sidecar files
with the same base name.

.. list-table::
   :header-rows: 1
   :widths: 28 72

   * - File
     - Contents
   * - ``my_circuit.slicap_sch``
     - The schematic itself (components, wires, labels, annotations) — the file
       you open and save.
   * - ``my_circuit.ini``
     - This schematic's **style** (line widths, colours, fonts), so it always
       looks as it did when saved.  See :doc:`/GUI/schematics/preferences`.
   * - ``my_circuit.symbols``
     - **Frozen copies** of every symbol the schematic uses, so it renders with
       the exact symbols it was drawn with.  See :doc:`/GUI/reference/symbol_libraries`.
   * - ``my_circuit.cache``
     - A cache of rendered LaTeX labels, so re-opening is fast.  Created only if
       the schematic actually contains typeset expressions; safe to delete.

Why sidecar files
=================

* **Portability** — copy the ``.slicap_sch`` together with its ``.ini`` and
  ``.symbols`` to another computer and it looks and netlists identically.
* **Reproducibility** — a figure in a book keeps its appearance even if the
  application's default symbols or style change later.
* **No clutter** — the render cache lives next to the schematic, not in a global
  folder that quietly grows over time.  Deleting a project removes all its
  files together.

.. note::

   The ``.ini`` and ``.symbols`` files are part of the project and should be
   kept (and version-controlled) with the schematic.  The ``.cache`` directory
   is regenerated automatically and can be ignored or deleted.
   


