How to search for text entities?
There are two classes of text entities: CADMText
and CADText
. The instance of the CADMText
class represents a multiline text in a CAD file. While the instance of the CADText
class represents a single-line text in a CAD file.
In this example we will use the "Text.dxf"
file. The following picture illustrates this file.
Let's create a function to search for text entities on the current layout.
- Add the
using
directive with theCADImport
andCADImport.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?
- 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
SearchForTextEnt
. Then create theSearchForTextEnt_Click
function to search for text entities by click.
private void CreateCADDrawing(object sender, EventArgs e){
- Create a new instance of the
CADImage
class.
CADImage vDrawing = CADImage.CreateImageByExtension(@"ForText.dxf");
vDrawing.LoadFromFile(@"ForText.dxf");
We recommend creating a new drawing object using CreateImageByExtension
in case of importing from an existing file or stream.
- 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 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);
- Create a cycle to search for the
CADText
andCADMText
entities on the current layout ofvDrawing
:- Declare the
string
variableS
to store the text data in it. - Create a cycle to search for entities on the current layout of
vDrawing
. - Find the
CADText
entities with theif/else
statement. - Print the
Text
,Height
,FontName
, andStyle.Name
properties of eachCADText
instance toMessageBox
. - Find the
CADMText
entities with theif/else
statement. In case ofCADMText
, you should create one another cycle to search for all text lines. - Store the data in the
S
variable. Finally, print theS
variable toMessageBox
.
- Declare the
for (int i = 0; i < vDrawing.CurrentLayout.Count; i++)
{
if (vDrawing.CurrentLayout.Entities[i].EntType == EntityType.Text)
{
CADText vText = (CADText)vDrawing.CurrentLayout.Entities[i];
MessageBox.Show(" CADText: " + vText.Text + " Letters' height: " + vText.Height + " Font name: " + vText.FontName + "\n" + vText.Style.Name);
}
if (vDrawing.CurrentLayout.Entities[i].EntType == EntityType.MText)
{
CADMText vMText = (CADMText)vDrawing.CurrentLayout.Entities[i];
for (int j = 0; j < vMText.Block.Count; j++)
S = S + ((CADText)vMText.Block.Entities[j]).Text + "\n";
MessageBox.Show("CADMText: \n" + S);
S = "";
}
You have created the function to search for text entities on the current layout.
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 SearchForTextEnt_Click(object sender, EventArgs e)
{
CADImage vDrawing = CADImage.CreateImageByExtension(@"ForText.dxf");
vDrawing.LoadFromFile(@"ForText.dxf");
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);
string S = "";
for (int i = 0; i < vDrawing.CurrentLayout.Count; i++)
{
if (vDrawing.CurrentLayout.Entities[i].EntType == EntityType.Text)
{
CADText vText = (CADText)vDrawing.CurrentLayout.Entities[i];
MessageBox.Show(" CADText: " + vText.Text + " Letters' height: " + vText.Height + " Font name: " + vText.FontName + "\n" + vText.Style.Name);
}
if (vDrawing.CurrentLayout.Entities[i].EntType == EntityType.MText)
{
CADMText vMText = (CADMText)vDrawing.CurrentLayout.Entities[i];
for (int j = 0; j < vMText.Block.Count; j++)
S = S + ((CADText)vMText.Block.Entities[j]).Text + "\n";
MessageBox.Show("CADMText: \n" + S);
S = "";
}
}
}
}