Skip to content

How SubCircuits are implemented?

Shubhankar Sharma edited this page May 17, 2020 · 4 revisions

Contributing author: @shubhankarsharma00

Subcircuits are self-contained circuits that appear as "black boxes" in other circuits. A given subcircuit may be used any number of times at multiple levels just as long as it does not become a subcircuit of itself i.e. no circular dependency.

When a user tries to add a subcircuit to a circuit a prompt is presented to him and after he selects the circuit he wants to include in as a subcircuit, a call to SubCircuit constructor is made with the id = scope of the circuit that is being included and scope = circuit in which we are drawing the subcircuit. SubCircuit class extends from CircuitElement which means undefined x and y will make the element follow the mouse movement before it is dropped into position.

Once the SubCircuit is drawn onto the canvas we check for 3 errors:

  1. Cyclic dependency.
  2. If there are no Input/Output nodes.
  3. If the subCircuitScope(the actual circuit which we are trying to add into the current circuit) exists.

If all the above conditions do not cause any error the we proceed further and in the constructor, we call scheduleBackup function on the subCircuitScope to generate data for the subCircuit's scope in JSON. This data is then used in next 2 function calls : buildCircuit() and makeConnections() buildCircuit loads all the data of the subCircuitScope and draws all its nodes and makeConnections connects all these nodes wirelessly. This makes a whole connected circuit onto the current circuit but without actually drawing wires.

Loading Subcircuits:

While loading circuits with Subcircuits we can pass in savedData argument during the SubCircuit constructor call in loadScope() made in the main load() function which will draw the subcircuit according to the given data.

Clone this wiki locally