Extracting text from DGN and DWF files


#1

Hi,

We are using Aspose.CAD version 19.5 and the following code to extract text from DWG, DXF and DWT formats:

        using (var cadImage = (CadImage) Image.Load(input))
        {
            var text = new StringBuilder();

            foreach (var entity in cadImage.Entities)
            {
                var txtFromNodes = ExtractTextFromCadNodes(entity);

                if (string.IsNullOrEmpty(txtFromNodes))
                    continue;

                text.Append(txtFromNodes);
                text.Append(" ");
            }

            foreach (CadBlockEntity blockEntity in cadImage.BlockEntities.Values)
            {
                foreach (var entity in blockEntity.Entities)
                {
                    var txtFromNodes = ExtractTextFromCadNodes(entity);

                    if (string.IsNullOrEmpty(txtFromNodes))
                        continue;

                    text.Append(txtFromNodes);
                    text.Append(" ");
                }
            }

            return text.ToString();
        }

We cannot use this code to extract text from DGN and DWF. We get the exception

System.NotSupportedException : Specified method is not supported.

Is the text extraction achievable for those 2 formats?

Thank you!


#2

@gwert,

I have observed the issue shared by you and request you to please provide the source file that we may use on our end to further investigate the issue.


#3

Hi,

Here they are: cad_dgn_dwf.zip (300.1 KB)

Thank you.


#4

@gwert ,

I like to inform that CadImage is used for dwg and dxf formats. So when you read dgn or dwf format, you can’t cast it to CadImage. DGN format readed into DgnImage, and it has “Elements” property with list of DgnDrawingElementBase entities. DWF format readed into DwfImage, and it has “Pages” property, and every page item has “Entities” property with list of DwfWhipDrawable entities. You have to change your code according to this.


#5

Hi,

I see. Is there any code snippet for this so I could start poking with it?

Thank you!


#6

@gwert,

I have shared code snippet with you for both DGN and DWF formats. Please check following code snippets.

using (DgnImage dgnimage = (DgnImage)Image.Load(path+“036.dgn”))
{
foreach (DgnDrawingElementBase element in dgnimage.Elements)
{
if (element is DgnTextElement)
{
var text = (element as DgnTextElement).Text;
}
}
}

using (DwfImage dwfimage = (DwfImage)Image.Load(path+""))
{
foreach (var page in dwfimage.Pages)
{
foreach (var drawableObject in page.Entities)
{
if (drawableObject is DwfWhipText)
{
var text = (drawableObject as DwfWhipText).Text;
}
}
}
}