DEV Community

Dmitry Matuzko
Dmitry Matuzko

Posted on

Introduction to Aspose.CAD, Part 4

Introduction to Aspose.CAD, Part 4

In this part I will continue to describe Aspose.CAD features specific to DWG\DXF files, as these file formats are the most common use for Aspose.CAD.

Iterating over entities and blocks

The DWG\DXF specific CadImage class has an Entities property, which contains all entities of the file. Blocks, which are specific for DWG format, are put as instances of CadBlockEntity into a CadBlockDictionary instance that is accessible via BlockEntities property of a CadImage. Block names are used as keys in the dictionary, so blocks are accessible by their names. Entities of a specific block can be accessed via Entities propery of CadBlockEntity.

// Load an existing DWG file as CadImage.
using (Aspose.CAD.FileFormats.Cad.CadImage cadImage 
    = (Aspose.CAD.FileFormats.Cad.CadImage)Aspose.CAD.Image.Load(sourceFilePath))
{
  // Go through each entity inside the DWG/DXF file
    foreach (Aspose.CAD.FileFormats.Cad.CadObjects.CadBaseEntity entity in image.Entities)
    {
        Console.WriteLine(entity.TypeName);
    }

//Get information about the block by its name
    System.Console.WriteLine(cadImage.BlockEntities["*MODEL_SPACE"].Description);
}
Enter fullscreen mode Exit fullscreen mode

Getting underlay information

Underlays are stored like any other entity and are represented by CadUnderlay class. As such, getting information about underlays is as simple as this:

// Load an existing DWG file and convert it into CadImage 
using (Aspose.CAD.FileFormats.Cad.CadImage image 
        = (Aspose.CAD.FileFormats.Cad.CadImage)Image.Load(fileName))
{
    // Go through each entity inside the DWG file
    foreach (Aspose.CAD.FileFormats.Cad.CadObjects.CadBaseEntity entity in image.Entities)
    {
        // Check if entity is of CadDgnUnderlay type
        if (entity is Aspose.CAD.FileFormats.Cad.CadObjects.CadDgnUnderlay)
        {
            // Access different underlay flags 
            Aspose.CAD.FileFormats.Cad.CadObjects.CadUnderlay underlay 
                = entity as Aspose.CAD.FileFormats.Cad.CadObjects.CadUnderlay;
            //This is the external path to underlay file
            Console.WriteLine(underlay.UnderlayPath);
            Console.WriteLine(underlay.UnderlayName);
            Console.WriteLine(underlay.InsertionPoint.X);
            Console.WriteLine(underlay.InsertionPoint.Y);
            Console.WriteLine(underlay.RotationAngle);
            Console.WriteLine(underlay.ScaleX);
            Console.WriteLine(underlay.ScaleY);
            Console.WriteLine(underlay.ScaleZ);
            //Accessing the packed flags
            Console.WriteLine(
                (underlay.Flags & Aspose.CAD.FileFormats.Cad.CadObjects.UnderlayFlags.UnderlayIsOn)
                == Aspose.CAD.FileFormats.Cad.CadObjects.UnderlayFlags.UnderlayIsOn);
            Console.WriteLine(
                (underlay.Flags & Aspose.CAD.FileFormats.Cad.CadObjects.UnderlayFlags.ClippingIsOn)
                == Aspose.CAD.FileFormats.Cad.CadObjects.UnderlayFlags.ClippingIsOn);
            Console.WriteLine(
                (underlay.Flags & Aspose.CAD.FileFormats.Cad.CadObjects.UnderlayFlags.Monochrome)
                != Aspose.CAD.FileFormats.Cad.CadObjects.UnderlayFlags.Monochrome);
            break;
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

The only more complex nuance is accessing the flags packed as bit fields, which is covered in this example.

Associating block with layout

Association of a CadBlockEntity with a layout is stored in an instance of CadBlockTableObject, which are listed under BlocksTables property of a CadImage. The CadBlockTableObject has an HardPointerToLayout property, which contains value of a Layout's ObjectHandle property (not an reference, just the same string value). Note that every object in a file, be it entity, table or block or object's attribute has the ObjectHandle property. Another CadBlockTableObject's property, BlockName contains block's name, which can be used to get block from BlockEntities of a CadImage.

foreach (Aspose.CAD.FileFormats.Cad.CadObjects.CadLayout layout in layouts.Values)
{
    layoutNames[i++] = layout.LayoutName;
    System.Console.WriteLine("Layout " + layout.LayoutName + " is found");

    // Find block, applicable for DWG only
    Aspose.CAD.FileFormats.Cad.CadTables.CadBlockTableObject blockTableObjectReference
     = null;
    foreach (Aspose.CAD.FileFormats.Cad.CadTables.CadBlockTableObject tableObject
                in cadImage.BlocksTables)
    {
        if (string.Equals(tableObject.HardPointerToLayout, layout.ObjectHandle))
        {
            blockTableObjectReference = tableObject;
            break;
        }
    }

     // Collection cadBlockEntity.Entities contains information
    //about all entities on specific layout if this
    //collection has no elements it means layout is a copy
    // of Model layout and contains the same entities
    Aspose.CAD.FileFormats.Cad.CadObjects.CadBlockEntity cadBlockEntity = 
    cadImage.BlockEntities[blockTableObjectReference.BlockName];
}
Enter fullscreen mode Exit fullscreen mode

For now, that's all, stay tuned!

For more examples please visit the Aspose.CAD GitHub page. There's also Twitter and Facebook pages for news on Aspose.CAD.

Top comments (0)