![Search Text in DWG C#](images/Search-Text-in-DWG-csharp.jpg#center)
DWG drawing files contain 2D or 3D design data. It is widely used by designers, engineers, and architects. In certain scenarios, you might need to find some text in a DWG file. Accordingly, this article discusses how to search text in a DWG file programmatically in C#.
- Find Text in DWG Drawing File – C# API Configuration
- How to Search Text in DWG File using C#
- Search Text in DWG Drawing File in C#
Find Text in DWG Drawing File – C# API Configuration
Aspose.CAD for .NET supports creating or manipulating different CAD drawings like DWG, DXF, and several other file formats. You can easily set up the API by downloading the DLL files from the Downloads section or running the following NuGet installation command:
PM> Install-Package Aspose.CAD
How to Search Text in DWG File using C#
The following steps demonstrate how to search text in a DWG file using C#:
- Load an existing DWG file.
- Search for text by iterating through CadText entities.
- Search for text on a specific layout.
- Export the file to PDF format.
Search Text in DWG Drawing File in C#
You need to follow the steps below to search text in a DWG file using C#:
- Load an existing DWG file with CadImage class.
- Search for text by iterating through CadText entities.
- Search for text on specific layout.
- Export the file to PDF format.
The code sample below shows how to search text in a DWG file using C#:
// Load an existing DWG file as CadImage. | |
using (CadImage cadImage = (CadImage)Aspose.CAD.Image.Load(sourceFilePath)) | |
{ | |
// Search for text in the file | |
foreach (CadBaseEntity entity in cadImage.Entities) | |
{ | |
// We iterate through CadText entities, but some other entities may contain text also, e.g. CadMText and others | |
IterateCADNodes(entity); | |
} | |
// Search for text on specific layout get all layout names and link each layout with corresponding block with entities | |
CadLayoutDictionary layouts = cadImage.Layouts; | |
string[] layoutNames = new string[layouts.Count]; | |
int i = 0; | |
foreach (CadLayout layout in layouts.Values) | |
{ | |
layoutNames[i++] = layout.LayoutName; | |
System.Console.WriteLine("Layout " + layout.LayoutName + " is found"); | |
// Find block, applicable for DWG only | |
CadBlockTableObject blockTableObjectReference = null; | |
foreach (CadBlockTableObject tableObject in cadImage.BlocksTables) | |
{ | |
if (string.Equals(tableObject.HardPointerToLayout, layout.ObjectHandle)) | |
{ | |
blockTableObjectReference = tableObject; | |
break; | |
} | |
} | |
if (blockTableObjectReference != null) | |
{ | |
// Collection cadBlockEntity.Entities contains information about all entities on specific layout | |
CadBlockEntity cadBlockEntity = cadImage.BlockEntities[blockTableObjectReference.BlockName]; | |
} | |
} | |
// Export to PDF | |
CadRasterizationOptions rasterizationOptions = new CadRasterizationOptions(); | |
rasterizationOptions.PageWidth = 1600; | |
rasterizationOptions.PageHeight = 1600; | |
rasterizationOptions.AutomaticLayoutsScaling = true; | |
// If cadBlockEntity collection for selected layout or entitiesOnLayouts collection by layout's BlockTableRecordHandle (for dxf) is empty | |
rasterizationOptions.Layouts = new[] { "Layout1" }; | |
ImageOptions.PdfOptions pdfOptions = new PdfOptions(); | |
pdfOptions.VectorRasterizationOptions = rasterizationOptions; | |
cadImage.Save("SearchText_CAD.pdf", pdfOptions); | |
} | |
public static void SearchTextInDWGAutoCADFile() | |
{ | |
// The path to the documents directory. | |
string sourceFilePath = "search.dwg"; | |
// Load an existing DWG file as CadImage. | |
CadImage cadImage = (CadImage)Aspose.CAD.Image.Load(sourceFilePath); | |
// Search for text in the entities section | |
foreach (var entity in cadImage.Entities) | |
{ | |
IterateCADNodes(entity); | |
} | |
// Search for text in the block section | |
foreach (CadBlockEntity blockEntity in cadImage.BlockEntities.Values) | |
{ | |
foreach (var entity in blockEntity.Entities) | |
{ | |
IterateCADNodes(entity); | |
} | |
} | |
} | |
private static void IterateCADNodes(CadBaseEntity obj) | |
{ | |
switch (obj.TypeName) | |
{ | |
case CadEntityTypeName.TEXT: | |
CadText childObjectText = (CadText)obj; | |
Console.WriteLine(childObjectText.DefaultValue); | |
break; | |
case CadEntityTypeName.MTEXT: | |
CadMText childObjectMText = (CadMText)obj; | |
Console.WriteLine(childObjectMText.Text); | |
break; | |
case CadEntityTypeName.INSERT: | |
CadInsertObject childInsertObject = (CadInsertObject)obj; | |
foreach (var tempobj in childInsertObject.ChildObjects) | |
{ | |
IterateCADNodes(tempobj); | |
} | |
break; | |
case CadEntityTypeName.ATTDEF: | |
CadAttDef attDef = (CadAttDef)obj; | |
Console.WriteLine(attDef.DefaultString); | |
break; | |
case CadEntityTypeName.ATTRIB: | |
CadAttrib attAttrib = (CadAttrib)obj; | |
Console.WriteLine(attAttrib.DefaultText); | |
break; | |
} | |
} |
Get Free Temporary License
You can test the features of the API without any evaluation limitations by requesting a free temporary license.
Conclusion
In this article, you have learned how to search text in a DWG file programmatically in C#. Moreover, you can explore different features of the API by taking a look at the documentation section. In case of any concerns, please write to us at forum.