How can I fetch the mtext that is in my dwg file using aspose.cad for dot net. i dont want any metadata. i only want the text that is visible in the drawing.
To fetch the visible text (mtext) from a DWG file using Aspose.CAD for .NET, you can utilize the CadImage
class to load your DWG file and then iterate through its entities to extract the text. Below is a sample code snippet that demonstrates how to achieve this:
using System;
using Aspose.CAD;
using Aspose.CAD.FileFormats.Cad.CadObjects;
namespace Aspose.CAD.Examples
{
public class ExtractMText
{
public static void Run()
{
// Specify the path to your DWG file
string sourceFilePath = "path_to_your_file.dwg";
// Load the DWG file as a CadImage
using (CadImage cadImage = (CadImage)Image.Load(sourceFilePath))
{
// Iterate through all entities in the DWG file
foreach (CadEntityBase entity in cadImage.Entities)
{
ExtractText(entity);
}
// Optionally, you can also check block entities
foreach (CadBlockEntity blockEntity in cadImage.BlockEntities.Values)
{
foreach (CadEntityBase entity in blockEntity.Entities)
{
ExtractText(entity);
}
}
}
}
private static void ExtractText(CadEntityBase obj)
{
// Check the type of the entity and extract text accordingly
switch (obj.TypeName)
{
case CadEntityTypeName.MTEXT:
CadMText mText = (CadMText)obj;
Console.WriteLine(mText.Text); // Output the visible mtext
break;
// You can also handle other text types if needed
case CadEntityTypeName.TEXT:
CadText text = (CadText)obj;
Console.WriteLine(text.DefaultValue); // Output the visible text
break;
// Handle nested entities if necessary
case CadEntityTypeName.INSERT:
CadInsertObject insertObject = (CadInsertObject)obj;
foreach (var child in insertObject.ChildObjects)
{
ExtractText(child);
}
break;
}
}
}
}
In this code:
- The
CadImage
class is used to load the DWG file. - The
ExtractText
method checks the type of each entity and prints the text forMTEXT
andTEXT
entities. - You can also handle nested entities by checking for
INSERT
types.
Make sure to replace "path_to_your_file.dwg"
with the actual path to your DWG file. This approach will help you extract only the visible text from your drawing without any metadata.