new Circuit()
Domain aggregate representing a complete circuit design in the JSCircuit Editor.
- Description:
Domain aggregate representing a complete circuit design in the JSCircuit Editor.
This is the core aggregate in our Domain-Driven Design architecture that maintains the integrity of a circuit by managing elements and their connections. It enforces business rules such as element uniqueness and connection validity.
Key Responsibilities:
- Manage circuit elements (resistors, capacitors, wires, etc.)
- Validate element additions and connections
- Maintain connection mapping between elements
- Enforce domain business rules and constraints
Aggregate Root Pattern: As an aggregate root, Circuit is the only way to access and modify circuit elements. All mutations must go through this aggregate to maintain consistency.
- Source:
Example
const circuit = new Circuit();
const resistor = new Resistor('R1', [pos1, pos2], null, new Properties({resistance: 1000}));
circuit.validateAddElement(resistor);
circuit.addElement(resistor);
Classes
- Circuit
- Creates a new empty circuit.
Methods
getSerializedElements() → {Array.<Object>}
Serializes the circuit elements into a format suitable for export or persistence.
- Description:
Serializes the circuit elements into a format suitable for export or persistence.
This method converts all circuit elements into a plain JavaScript object format that can be easily serialized to JSON or other data formats. It's primarily used by export adapters like QucatNetlistAdapter for file operations.
- Source:
Example
const serialized = circuit.getSerializedElements();
console.log(serialized[0]); // { id: 'R1', type: 'resistor', nodes: [{x: 10, y: 20}], ... }
Returns:
Array of serialized element objects, each containing:
- {string} id - The unique element identifier
- {string} type - The element type (resistor, capacitor, wire, etc.)
- {Object[]} nodes - Array of node positions as {x, y} objects
- {Object} properties - Element properties as key-value pairs
- {string|null} label - Element label text or null if no label
- Type
- Array.<Object>
isNodeOnWireSegment(node, wireStart, wireEnd) → {boolean}
Checks if a node lies on the body of a wire, defined by a line segment between two endpoints.
- Description:
Checks if a node lies on the body of a wire, defined by a line segment between two endpoints.
This method is used to validate node-to-wire-body connections in a circuit. It ensures that a node is either at one of the wire's terminal positions or lies along any of the wire's intermediate line segments.
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
node |
Position | The node to check. |
wireStart |
Position | The starting point of the wire's line segment. |
wireEnd |
Position | The ending point of the wire's line segment. |
Returns:
True if the node lies on the wire's line segment, otherwise false.
- Type
- boolean
validateAddElement(element)
Validates whether an element can be added to the circuit.
- Description:
Validates whether an element can be added to the circuit.
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
element |
Element | The element to validate. |
Throws:
-
If the element violates circuit rules.
- Type
- Error
validateConnection(element1, element2)
Validates and establishes a connection between two elements in the circuit.
- Description:
Validates and establishes a connection between two elements in the circuit. This method handles two types of connections:
- Node-to-Node connections: Direct connections between nodes of two elements.
- Node-to-Wire-Body connections: A node connecting to any point along a wire's body.
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
element1 |
Element | The first element to connect. |
element2 |
Element | The second element to connect. |
Throws:
-
If the connection violates circuit rules.
- Type
- Error