How to change a font style of existing text entities?
It is possible to change a font style of existing text entities with the CADStyle
class.
In this example we will use the Entities.dxf
file. You can find it in ...\CAD .NET 14\Files
.
By default, the CAD .NET folder is unpacked to Documents.
The following picture illustrates this file.
Let's create a function to change the font style of existing text entities.
- 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
ChangeFontOfExistingEnt
. Then create theChangeFontOfExistingEnt_Click
function to create a new CAD drawing by click.
private void ChangeFontOfExistingEnt_Click(object sender, EventArgs e){
- Create a new instance of the
CADImage
class.
CADImage vDrawing = CADImage.CreateImageByExtension(@"Entities.dxf");
vDrawing.LoadFromFile(@"Entities.dxf");
We recommend creating a new drawing object using CreateImageByExtension
in case of importing from an existing file or stream.
- Set the
CurrentLayout
property asvDrawing.Layouts[0]
.
vDrawing.CurrentLayout = vDrawing.Layouts[0];
- The text style named
"STANDARD"
is added by default to any drawing created with CAD .NET. Create an instance of theCADStyle
class and define it asvDrawing.Converter.StyleByName("STANDARD")
.
CADStyle vStyle = vDrawing.Converter.StyleByName("STANDARD");
- Set the
FontName
andPrimaryFont
asCourier New
andcour.ttf
. LoadvStyle
to the specifiedvDrawing.Converter
with theLoaded
method.
vStyle.FontName = "Courier New";
vStyle.PrimaryFont = "cour.ttf";
vStyle.Loaded(vDrawing.Converter);
- Create a cycle to change the font style of all existing
CADText
entities:- Create a cycle to search for entities on the current layout of
vDrawing
. - Find the
CADText
entities with theif/else
statement. - Find the
CADText
entities which style is"STANDARD"
.
- Create a cycle to search for entities on the current layout of
for (int i = 0; i < vDrawing.CurrentLayout.Count; i++)
{
if (vDrawing.CurrentLayout.Entities[i].EntType == EntityType.Text)
{
CADText vText = (CADText)vDrawing.CurrentLayout.Entities[i];
if (vText.Style.Name == vStyle.Name)
{
vDrawing.Converter.Loads(vText);
}
}
}
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 drawing with theDraw
method.
RectangleF vRect;
double vRatio = (double)(vDrawing.AbsHeight * pictureBox1.ClientSize.Width) / (vDrawing.AbsWidth * pictureBox1.ClientSize.Height);
if (vRatio > 1)
vRect = new RectangleF(0, 0, (float)(pictureBox1.ClientSize.Width / vRatio), (float)pictureBox1.ClientSize.Height);
else
vRect = new RectangleF(0, 0, (float)pictureBox1.ClientSize.Width, (float)(pictureBox1.ClientSize.Height * vRatio));
vDrawing.Draw(pictureBox1.CreateGraphics(), vRect);
}
You have created the function to change the font style of existing text entities.
The following picture illustrates the result.
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 ChangeFontOfExistingEnt_Click(object sender, EventArgs e)
{
CADImage vDrawing = CADImage.CreateImageByExtension(@"Entities.dxf");
vDrawing.LoadFromFile(@"Entities.dxf");
vDrawing.CurrentLayout = vDrawing.Layouts[0];
CADStyle vStyle = vDrawing.Converter.StyleByName("STANDARD");
vStyle.FontName = "Courier New";
vStyle.PrimaryFont = "cour.ttf";
vStyle.Loaded(vDrawing.Converter);
for (int i = 0; i < vDrawing.CurrentLayout.Count; i++)
{
if (vDrawing.CurrentLayout.Entities[i].EntType == EntityType.Text)
{
CADText vText = (CADText)vDrawing.CurrentLayout.Entities[i];
if (vText.Style.Name == vStyle.Name)
{
vDrawing.Converter.Loads(vText);
}
}
}
vDrawing.GetExtents();
RectangleF vRect;
double vRatio = (double)(vDrawing.AbsHeight * pictureBox1.ClientSize.Width) / (vDrawing.AbsWidth * pictureBox1.ClientSize.Height);
if (vRatio > 1)
vRect = new RectangleF(0, 0, (float)(pictureBox1.ClientSize.Width / vRatio), (float)pictureBox1.ClientSize.Height);
else
vRect = new RectangleF(0, 0, (float)pictureBox1.ClientSize.Width, (float)(pictureBox1.ClientSize.Height * vRatio));
vDrawing.Draw(pictureBox1.CreateGraphics(), vRect);
}
}
}