How can i fetch the entities inside the viewport of a layout in dwg file using Aspose.Cad for dot net.
To fetch the entities inside a viewport of a layout in a DWG file using Aspose.CAD for .NET, you can follow the code example below. This example demonstrates how to load a DWG file, iterate through its layouts, and identify the entities within each viewport.
using Aspose.CAD;
using Aspose.CAD.FileFormats.Cad.CadObjects;
using System.Collections.Generic;
public class FetchEntitiesInViewport
{
public static void Run(string fileName)
{
using (CadImage cadImage = (CadImage)Image.Load(fileName))
{
Dictionary<long, string> layoutBlockHandles = new Dictionary<long, string>();
// Iterate through each layout in the DWG file
foreach (CadLayout layout in cadImage.Layouts.Values)
{
if (layout.BlockTableRecordHandle != null)
{
layoutBlockHandles.Add(layout.BlockTableRecordHandle, layout.LayoutName);
}
}
// Iterate through each entity in the DWG file
foreach (CadEntityBase entity in cadImage.Entities)
{
if (entity.TypeName == CadEntityTypeName.VIEWPORT)
{
// Check if the entity belongs to a layout
if (layoutBlockHandles.ContainsKey(entity.SoftOwner))
{
string layoutName = layoutBlockHandles[entity.SoftOwner];
System.Console.WriteLine($"Layout: {layoutName}, Entity: {entity.TypeName}, Handle: {entity.ObjectHandle}");
}
}
}
}
}
}
Explanation:
- Loading the DWG File: The
CadImage
class is used to load the DWG file. - Iterating Layouts: A dictionary is created to map layout block handles to their names.
- Finding Viewports: The code checks each entity to see if it is a viewport. If it is, it checks if it belongs to a layout and prints the relevant information.
This approach allows you to effectively retrieve and display the entities located within the viewports of a DWG layout. For more detailed information, you can refer to the Aspose documentation on working with DWG files.
I want the entities that are stored in the viewport like the lines, arcs and polylines. After fetching the viewport how do i fetch the entities inside it.
@Asheesh32,
Hi.
Viewport object doesn’t store in the file any entities related to it. It stores only parameters of the view applied to the entire Model. So it seems the only way is to analyze all Model entities and check whether they fit the viewport visible area or not.
This example prints entities from all layouts including Model and could be helpful to get the list of entities in the Model sheet (because entities could be stored separately in the layout sheets).
CadImage.GetBounds() fills Bounds property for entities in Model sheet and could be helpful for further analysis.
using (CadImage cadImage = (CadImage)Aspose.CAD.Image.Load(fileName))
{
cadImage.GetBounds();
foreach (CadLayout layout in cadImage.Layouts.Values)
{
foreach (CadBlockTableObject tableObject in cadImage.BlocksTables)
{
if (string.Equals(tableObject.HardPointerToLayout, layout.ObjectHandle))
{
if (cadImage.BlockEntities.ContainsKey(tableObject.BlockName))
{
CadBlockEntity cadBlockEntity = cadImage.BlockEntities[tableObject.BlockName];
System.Console.WriteLine(layout.LayoutName + " has " + cadBlockEntity.Entities.Count + " entities");
foreach (CadEntityBase entityInLayout in cadBlockEntity.Entities)
{
System.Console.WriteLine(entityInLayout.TypeName);
if (entityInLayout.Bounds != null)
{
System.Console.WriteLine("Min point: " + entityInLayout.Bounds[0].X + " " + entityInLayout.Bounds[0].Y +
" Max point: " + entityInLayout.Bounds[1].X + " " + entityInLayout.Bounds[1].Y);
}
}
System.Console.WriteLine("----------------");
}
break;
}
}
}
}
This is giving me the entities in the layouts. I want the entities inside the viewport.
@Asheesh32,
There is no ready way to do this. Viewports have no entities associated with them. Viewport stores the parameters of view showing some part of the Model sheet. The correct way to solve this is to iterate over all entities in Model and check if they are inside the desired viewport or not.