Saturday, April 16, 2022

27-Component Text

 In this post, we will add Text that displays the component value such as R=75Ω. Let's start by adding a virtual method to the Comp.cs base class that can be overriden by the subclasses.

Add Text variables to Comp.cs that define the text font, brush, string format, string to be drawn and a point defining the text location.

        // Component text variables
        // Create font and brush.
        private Font drawFont = new Font("Arial", 10);
        private SolidBrush drawBrush = new SolidBrush(Color.White);
        private StringFormat drawFormat = new StringFormat();
 
        // Component text variables
        protected String compText;
        protected Point pt;

Add the drawCompText() method to Comp.cs;

       public virtual void drawCompText(Graphics gr, Point p1, String drawString)
        {
            // Convert Point ints to floats
            float x = p1.X;
            float y = p1.Y;
 
            // Draw string to screen.
            gr.DrawString(drawString, drawFont, drawBrush, x, y, drawFormat);
        }

This method accepts the graphics attribute from the Paint() method, a point defining its location and the string to be drawn. The text is drawn on the canvas using the graphics DrawString() method. Note that this method draws one string. It can be called multiple times to draw more than one string. It is also a virtual method so that subclasses can override it with other implementations, for example, if we defined a RLC component, it would required 3 text fields to display 3 values.

Resistor Text

To test the component text capability, let's modify the RES.cs class to draw a text field with the graphics symbol.

Modify the Draw() method to call the drawCompText() method we created in the base class

        public override void Draw(Graphics gr)
        {
            . . .
 
            // Draw the component text
            compText = "R = " + this.Value + "Ω";
            pt = new Point(Loc.X+5, Loc.Y-35);
            drawCompText(gr, pt, compText);
        }

Run the program and click on the RLC button. Confirm that the resistor text is displayed above the resistor symbol. Note that the position of the text is found by trial and error.

Other Component Text

Next, modify the Draw() methods for the following components
  • IND:              compText = "L = " + this.value + "nH";
  • CAP:             compText = "C = " + this.value + "pF";
  • InComp:        compText = "Pin";
  • OutComp:     compText = "Pout";
  • MLIN:          compText = "W = " + this.value + "mils";
  • MCROS:      compText = "W = " + this.value + "mils";
  • MTEE:         compText = "W = " + this.value + "mils";
Ground does not require text.

Run the program and confirm that text is drawn for all components. The source code for this post can be found on GitHub. Note that I changed the Value of all microstrip components to 20 in MainForm.cs in the microstrip button handlers.



























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...