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
using
directive with theCADImport
andCADImport.FaceModule
namespaces.
using CADImport;
using CADImport.FaceModule;
More information about CADPictureBox
- Use the control element of the
CADPictureBox
class:- Set the
Location
property asnew Point(10, 30)
. - Set the
BackColor
property asColor.Black
. - Set the
Size
property 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_Click
function to change a font style by click.
private void ChangeFontStyle_Click(object sender, EventArgs e){
- Create a new instance of the
CADImage
class:- Call
InitializeSection
to create theCAD
object manually. - Set the
Background
property asColor.Azure
. - Set the
CurrentLayout
property 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
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 withvDrawing.Converter.GetSection(ConvSection.Styles).AddEntity(vStyle)
. - Load
vStyle
to the specifiedvDrawing.Converter
with theLoaded
method.
- 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
CADText
class:- Add this entity to the current layout of
vDrawing
using theAddEntity
method. - Set the
Style
property asvStyle
. - Set the
Point
property asnew DPoint(200, 210, 0)
. - Set the
Height
property as2
. - Set the
Color
property asColor.Black
, - Set the
Text
property asTest Text
. - Use the
Loads
method 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
GetExtents
method to recalculate drawing extents.
vDrawing.GetExtents();
- Declare the local variable
vRect
and specifyRectangleF
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 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);
}
}
}