Skip to main content

How to add text and configure its style?

It is possible to add text and configure a user text style with the CADStyle class.

Let's create a function to change the font style.

  1. Add the using directive with the CADImport and CADImport.FaceModule namespaces.
using CADImport;
using CADImport.FaceModule;
More information about CADPictureBox

The CADPictureBox class is the basic implementation of the control element for displaying vector drawings. Visually CADPictureBox includes only area for drawing visualization and can be extended by the required control elements in the project under development.
To get more information about the CAD .NET controls, see What controls does CAD .NET have?

  1. Use the control element of the CADPictureBox class:
    • Set the Location property as new Point(10, 30).
    • Set the BackColor property as Color.Black.
    • Set the Size property as new Size(995, 500).
    • Finally, add it to the form.
...

CADPictureBox pictureBox1 = new CADPictureBox(){
Location = new Point(10, 30),
BackColor = Color.Black,
Size = new Size(995, 500),
}

public Form1()
{
Controls.Add(pictureBox1);
InitializeComponent();
}
  1. Add a new button. Name it ChangeFontStyle. Then create the ChangeFontStyle_Click function to change a font style by click.
 private void ChangeFontStyle_Click(object sender, EventArgs e){
  1. Create a new instance of the CADImage class:
    • Call InitializeSection to create the CAD object manually.
    • Set the Background property as Color.Azure.
    • Set the CurrentLayout property as vDrawing.Layouts[0].
  CADImage vDrawing = new CADImage();
vDrawing.Converter.InitializeSections();
vDrawing.BackgroundColor = Color.Azure;
vDrawing.CurrentLayout = vDrawing.Layouts[0];
  1. Create a new instance of the CADStyle class:
    • Set the Name property as "MyStyle".
    • Set the FontName property as "Courier New".
    • Set the PrimaryFont property as "cour.ttf".
    • Add your style to the Styles section with vDrawing.Converter.GetSection(ConvSection.Styles).AddEntity(vStyle).
    • Load vStyle to the specified vDrawing.Converter with the Loaded method.
  CADStyle vStyle = new CADStyle();
vStyle.Name = "MyStyle";
vStyle.FontName = "Courier New";
vStyle.PrimaryFont = "cour.ttf";
vDrawing.Converter.GetSection(ConvSection.Styles).AddEntity(vStyle);
vStyle.Loaded(vDrawing.Converter);
  1. Create a new instance of the CADText class:
    • Add this entity to the current layout of vDrawing using the AddEntity method.
    • Set the Style property as vStyle.
    • Set the Point property as new DPoint(200, 210, 0).
    • Set the Height property as 2.
    • Set the Color property as Color.Black,
    • Set the Text property as Test Text.
    • Use the Loads method to fill the internal data of the entity to prepare it for drawing.
  CADText vText = new CADText();
vDrawing.CurrentLayout.AddEntity(vText);
vText.Style = vStyle;
vText.Point = new DPoint(200, 210, 0);
vText.Height = 2;
vText.Color = Color.Black;
vText.Text = "Test text";
vDrawing.Converter.Loads(vText);
  1. Use the GetExtents method to recalculate drawing extents.
  vDrawing.GetExtents();
  1. Declare the local variable vRect and specify RectangleF as its type. This variable stores four floating values that represent the location and size of a CAD file. Use the following code to fit the CAD file to pictureBox1. Finally, render the result.
  RectangleF vRect;
vRect = new RectangleF(0, 0, (float)pictureBox1.ClientSize.Width, (float)(pictureBox1.ClientSize.Height));
vDrawing.Draw(pictureBox1.CreateGraphics(), vRect);

You have created the function to change a font style.

The full code listing.

...
using CADImport;
using CADImport.FaceModule;

namespace WindowsFormsApp1
{
public partial class Form1 : Form

{
CADPictureBox pictureBox1 = new CADPictureBox()
{
Location = new Point(10, 30),
TabIndex = 10,
BackColor = Color.Black,
Size = new Size(995, 500)
};
public Form1()
{
Controls.Add(pictureBox1);
InitializeComponent();
}
private void ChangeFontStyle_Click(object sender, EventArgs e)
{
CADImage vDrawing = new CADImage();
vDrawing.Converter.InitializeSections();
vDrawing.BackgroundColor = Color.Azure;
vDrawing.CurrentLayout = vDrawing.Layouts[0];
CADStyle vStyle = new CADStyle();
vStyle.FontName = "Courier New";
vStyle.PrimaryFont = "cour.ttf";
vStyle.Loaded(vDrawing.Converter);
CADText vText = new CADText();
vDrawing.CurrentLayout.AddEntity(vText);
vText.Style = vStyle;
vText.Point = new DPoint(200, 210, 0);
vText.Height = 2;
vText.Color = Color.Black;
vText.Text = "Test text";
vDrawing.Converter.Loads(vText);
vDrawing.GetExtents();
RectangleF vRect;
vRect = new RectangleF(0, 0, (float)pictureBox1.ClientSize.Width, (float)(pictureBox1.ClientSize.Height));
vDrawing.Draw(pictureBox1.CreateGraphics(), vRect);
}
}
}