In this post, we will switch gears from creating components to create a Wire class so we can draw interconnects between components on the schematicCanvas.
Wire Class
- Create a new class in the Wires folder called Wire.cs
- Make it a public class that inherits from Comp
Add public attributes to capture the wire start and end points.
// Create wire start and end points
public Point Pt1 = new Point();
public Point Pt2 = new Point();
Add parameters to capture the components connected to the wire
// Add components connected to the input and output of the wire
public Comp Cin = new Comp();
public Comp Cout = new Comp();
// End caps variables
private const int
endcap_radius = 3;
public bool
endcapsVisible = false;
- Add two constructors: 1 with no arguments and 1 that initializes the component parameters for Cin and Cout
public Wire()
{
}
public Wire(Comp cin, Comp cout)
{
Cin = cin;
Cout = cout;
Pt1 = new Point(cin.Pout.X, cin.Pout.Y);
Pt2 = new Point(cout.Pin.X, cin.Pout.Y);
}
- Add the Draw() method and the drawEndCaps() method
public override void
Draw(Graphics gr)
{
if (Pt1.X != Pt2.X || Pt1.Y != Pt2.Y)
{
// Draw L-shaped wire
gr.DrawLine(drawPen, Pt1.X, Pt1.Y, Pt1.X, Pt2.Y); // Vertical line
gr.DrawLine(drawPen, Pt1.X, Pt2.Y, Pt2.X, Pt2.Y); // Horizontal line
}
else
{
// Draw straight wire
gr.DrawLine(drawPen, Pt1, Pt2);
}
// Draw the wire end caps
drawEndCaps(gr);
}
private void drawEndCaps(Graphics gr)
{
if (this.endcapsVisible)
{
// Draw custom end cap for Pt1
Rectangle rect1 = new Rectangle(
Pt1.X -
endcap_radius, Pt1.Y - endcap_radius,
2 *
endcap_radius, 2 * endcap_radius);
gr.DrawRectangle(Pens.Red, rect1);
// Rectangular end cap
// Draw custom end cap for Pt2
Rectangle
rect2 = new Rectangle(
Pt2.X -
endcap_radius, Pt2.Y - endcap_radius,
2 * endcap_radius, 2 * endcap_radius);
gr.DrawRectangle(Pens.Red, rect2);
// Rectangular end cap
}
}
To test the Wire class, we will implement the RLC button on the panelCompMenu. When this button is pressed, the button handler will add a list of components to the circuit to be drawn on the Window:
- InPort
- Resistor
- Inductor
- Capacitor
- Outport
- Wire from InPort to Resistor
- Wire from Resistor to Inductor
- Wire from Inductor to Capacitor
- Wire from Capacitor to Output
Add the following code to the RLC button click event handler:
No comments:
Post a Comment