Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active Jan 31, 2023
Embed
What would you like to do?
Search Text in DWG File using C# .NET
// 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