You can read more details at: Search Text in DWG Drawing File Programmatically in C#
Last active
January 31, 2023 15:55
-
-
Save aspose-com-gists/4c7e9f7016076579ebf24d8793c109b4 to your computer and use it in GitHub Desktop.
Search Text in DWG File using C# .NET
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment