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.
- Add the
usingdirective with theCADImportandCADImport.FaceModulenamespaces.
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?
- Use the control element of the
CADPictureBoxclass:- Set the
Locationproperty asnew Point(10, 30). - Set the
BackColorproperty asColor.Black. - Set the
Sizeproperty asnew Size(995, 500). - Finally, add it to the form.
- Set the
...
CADPictureBox pictureBox1 = new CADPictureBox(){
Location = new Point(10, 30),
BackColor = Color.Black,
Size = new Size(995, 500),
}
public Form1()
{
Controls.Add(pictureBox1);
InitializeComponent();
}
- Add a new button. Name it
ChangeFontStyle. Then create theChangeFontStyle_Clickfunction to change a font style by click.
private void ChangeFontStyle_Click(object sender, EventArgs e){
- Create a new instance of the
CADImageclass:- Call
InitializeSectionto create theCADobject manually. - Set the
Backgroundproperty asColor.Azure. - Set the
CurrentLayoutproperty asvDrawing.Layouts[0].
- Call
CADImage vDrawing = new CADImage();
vDrawing.Converter.InitializeSections();
vDrawing.BackgroundColor = Color.Azure;
vDrawing.CurrentLayout = vDrawing.Layouts[0];
- Create a new instance of the
CADStyleclass:- Set the
Nameproperty as"MyStyle". - Set the
FontNameproperty as"Courier New". - Set the
PrimaryFontproperty as"cour.ttf". - Add your style to the
Stylessection withvDrawing.Converter.GetSection(ConvSection.Styles).AddEntity(vStyle). - Load
vStyleto the specifiedvDrawing.Converterwith theLoadedmethod.
- Set the
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);
- Create a new instance of the
CADTextclass:- Add this entity to the current layout of
vDrawingusing theAddEntitymethod. - Set the
Styleproperty asvStyle. - Set the
Pointproperty asnew DPoint(200, 210, 0). - Set the
Heightproperty as2. - Set the
Colorproperty asColor.Black, - Set the
Textproperty asTest Text. - Use the
Loadsmethod to fill the internal data of the entity to prepare it for drawing.
- Add this entity to the current layout of
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);
- Use the
GetExtentsmethod to recalculate drawing extents.
vDrawing.GetExtents();
- Declare the local variable
vRectand specifyRectangleFas 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 topictureBox1. 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);
}
}
}