Friday, April 15, 2022

24-Wire Class

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();

Next we will add variables used to draw wire end caps when the wire is being drawn. The end caps will disappear when drawing is complete. End caps are small red rectangles drawn at the start and end points of the wire.

       // 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:

        private void btnRLC_Click(object sender, EventArgs e)
        {
            int[] nodes = new int[] { 0, 0 };
            InPort pin = new InPort(50.0f, new Point(160, 230), nodes);
            ckt.comps.Add(pin);
 
            nodes = new int[] { 0, 0 };
            RES res = new RES(75.0f, new Point(200, 230), nodes);
            ckt.comps.Add(res);
 
            nodes = new int[] { 0, 0 };
            IND ind = new IND(5.0f, new Point(300, 230), nodes);
            ckt.comps.Add(ind);
 
            nodes = new int[] { 0, 0 };
            CAP cap = new CAP(1.0f, new Point(400, 230), nodes);
            ckt.comps.Add(cap);
 
            nodes = new int[] { 0, 0 };
            OutPort pout = new OutPort(50.0f, new Point(500, 230), nodes);
            ckt.comps.Add(pout);
 
            //_ = new Wire(pin, res); // Use of discard _
            //_ = new Wire(res, ind);
            //_ = new Wire(ind, cap);
            //_ = new Wire(cap, pout);
 
            Wire w1 = new Wire(pin, res); ckt.comps.Add(w1);
            Wire w2 = new Wire(res, ind); ckt.comps.Add(w2);
            Wire w3 = new Wire(ind, cap); ckt.comps.Add(w3);
            Wire w4 = new Wire(cap, pout); ckt.comps.Add(w4);
 
            schematicCanvas.Invalidate();
        }

Run the program and ensure that the RLC circuit is drawn with wires between the components. The complete source code for this post is available on GitHub.



No comments:

Post a Comment

34-Microwave Tools with Analysis (Series Final Post)

In this final blog post, I have integrated Y-Matrix analysis into the Microwave Tools project. This version has addition Lumped, Ideal, Micr...