How to fetch entities from a block reference in dwg file using Aspose.cad for dotnet. The Block reference consists of a ellipse and text

how to fetch entities from a block reference in dwg file using Aspose.cad for dotnet. The Block reference consists of a ellipse and text.

@Asheesh32

To fetch entities from a block reference in a DWG file using Aspose.CAD for .NET, you can follow the example below. This example demonstrates how to load a DWG file, iterate through its entities, and specifically access the entities within block references, such as ellipses and text.

Here’s a sample code snippet to help you get started:

using System;
using Aspose.CAD;
using Aspose.CAD.FileFormats.Cad.CadObjects;

namespace Aspose.CAD.Examples
{
    public class FetchEntitiesFromBlockReference
    {
        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 block entities
                foreach (CadBlockEntity blockEntity in cadImage.BlockEntities.Values)
                {
                    Console.WriteLine($"Block Name: {blockEntity.BlockName}");

                    // Iterate through entities within the block
                    foreach (var entity in blockEntity.Entities)
                    {
                        // Process each entity based on its type
                        ProcessEntity(entity);
                    }
                }
            }
        }

        private static void ProcessEntity(CadEntityBase entity)
        {
            switch (entity.TypeName)
            {
                case CadEntityTypeName.ELLIPSE:
                    // Handle ellipse entity
                    Console.WriteLine("Found an ellipse.");
                    break;

                case CadEntityTypeName.TEXT:
                    // Handle text entity
                    CadText textEntity = (CadText)entity;
                    Console.WriteLine($"Found text: {textEntity.DefaultValue}");
                    break;

                // Add cases for other entity types as needed
            }
        }
    }
}

Explanation:

  1. Loading the DWG File: The code begins by loading the DWG file into a CadImage object.
  2. Iterating Through Block Entities: It then iterates through all block entities in the DWG file.
  3. Processing Each Entity: For each block entity, it processes the contained entities based on their type (e.g., ellipse, text).
  4. Output: The code prints the type of each entity found, allowing you to see the contents of the block reference.

Make sure to replace "path/to/your/file.dwg" with the actual path to your DWG file. This example should help you fetch and process entities from block references effectively.

For more detailed information, you can refer to the official documentation on Get Block Attribute Value Of External Reference.

Sources:
[1]: Get Block Attribute Value Of External Reference

how to get entities of “[[X, Aspose.CAD.FileFormats.Cad.CadObjects.AttEntities.CadAttDef]]”

@Asheesh32,
Hi,
please test if this is useful:

foreach (CadEntityBase baseEntity in cadImage.Entities)
{
	if (baseEntity.TypeName == CadEntityTypeName.INSERT)
	{
		 CadInsertObject insert = (CadInsertObject)baseEntity;

		 CadBlockEntity block = cadImage.BlockEntities[insert.Name];

		 foreach (CadEntityBase blockEntity in block.Entities)
		 {
			 if (blockEntity.TypeName == CadEntityTypeName.ATTDEF)
			 {
				 CadAttDef attdef = (CadAttDef)blockEntity;

				 foreach (CadEntityBase att in insert.ChildObjects)
				 {
					 if (att.TypeName == CadEntityTypeName.ATTRIB)
					 {
						 CadAttrib attrib = (CadAttrib)att;

						 if (attrib.DefinitionTagString == attdef.Id)
						 {
							 System.Console.WriteLine("{0}, {1}", attdef.Id, attrib.DefaultText);
							 break;
						 }
					 }
				 }
			 }
		 }
	}
}