I want to fetch the color of the hatch in dwg file in rgb format and the pattern used in it. What additional I have to add in my code?

foreach (var entity in _cadImage.Entities)
{
    var n = entity.LayerName;
    var l = _cadImage.Layers.GetLayer(n);
{
 if (entity is CadHatch hatch)
{ 
    Console.WriteLine("Polyline color ID is {0}, layer's name is {1}, layer's color ID is {2}", entity.ColorId, entity.LayerName, l.ColorId);

    foreach (var boundaryPathContainer in hatch.BoundaryPaths)
    {
        foreach (var boundaryPath in boundaryPathContainer.BoundaryPath)
        {
            var hatchData = new Hatch
            {
                Points = GetBoundaryPathPoints(boundaryPath)
                    .Select(p => TransformPoint(p.X, p.Y)).ToList()
            };
            extractedEntities.Hatches.Add(hatchData);
        }
    }
}
}

I want to fetch the color of the hatch in rgb format and the pattern used in it. What additional I have to add in my code? i am using aspose.cad for dot net for my dwg files.

@Asheesh32

To fetch the color of the hatch in RGB format and the pattern used in your DWG file using Aspose.CAD for .NET, you will need to make a few modifications to your existing code. Here’s how you can achieve that:

  1. Get the RGB Color: You can convert the ColorId of the hatch to an RGB format using the CadColor class.
  2. Retrieve the Hatch Pattern: You can access the hatch pattern through the Hatch object.

Here’s an updated version of your code that includes these modifications:

foreach (var entity in _cadImage.Entities)
{
    var n = entity.LayerName;
    var l = _cadImage.Layers.GetLayer(n);
    
    if (entity is CadHatch hatch)
    { 
        // Get the RGB color of the hatch
        var hatchColor = hatch.ColorId;
        var rgbColor = CadColor.FromColorId(hatchColor).ToRgb(); // Convert to RGB

        Console.WriteLine("Hatch color in RGB is {0}, layer's name is {1}, layer's color ID is {2}", 
                          rgbColor, entity.LayerName, l.ColorId);

        // Get the hatch pattern
        var hatchPattern = hatch.PatternName; // Assuming PatternName holds the pattern information

        Console.WriteLine("Hatch pattern is {0}", hatchPattern);

        foreach (var boundaryPathContainer in hatch.BoundaryPaths)
        {
            foreach (var boundaryPath in boundaryPathContainer.BoundaryPath)
            {
                var hatchData = new Hatch
                {
                    Points = GetBoundaryPathPoints(boundaryPath)
                        .Select(p => TransformPoint(p.X, p.Y)).ToList()
                };
                extractedEntities.Hatches.Add(hatchData);
            }
        }
    }
}

Key Changes:

  • RGB Color Conversion: The line var rgbColor = CadColor.FromColorId(hatchColor).ToRgb(); converts the hatch’s color ID to RGB format.
  • Hatch Pattern Retrieval: The line var hatchPattern = hatch.PatternName; retrieves the name of the hatch pattern.

Make sure to check the documentation for any additional properties or methods that might be useful for your specific requirements. This should help you extract the necessary information from your DWG files effectively.

CadColor does not exist in current context. Aspose.CAD does not have CadColor in it.
How to solve this problem ?

@Asheesh32,
You are right, the generated example above is not correct, please accept our apologies.

Obtaining color of entity as RGB can depend on few factors. The typical way color is set up is using color index (ColorId property) from known tables, e.g., this. If ColorValue property of the entity contains a value (HasValue == true) RGB value is stored in it directly. It could be helpful if you can attach the file with test Hatch entity here so we could assist better.

hfs.zip (203.9 KB)

This is the file. i want to know the rgb colors of the hatch in this file.

@Asheesh32,
All hatches here have index-based colors, so you need to map it to RGB as per example below and extend the mapping table with other existing colors.

using (CadImage cadImage = (CadImage)Aspose.CAD.Image.Load(fileName))
{
   foreach (var entity in cadImage.Entities)
   {
	   if (entity is CadHatch hatch)
	   {
		   if (!hatch.ColorValue.HasValue)
		   {
			   if (hatch.ColorId >= 1 && hatch.ColorId <= 255)
			   {
				   System.Console.WriteLine(hatch.ColorId + " is mapped to " + GetRgbFromAci(hatch.ColorId));
			   }
			   else
			   {
				   // colors are stored in respective layer (id=256) or block (id=0)
			   }
		   }
		   else
		   {
			   // RGB components are stored in hatch.ColorValue
		   }
	   }
   }
}

static (int R, int G, int B) GetRgbFromAci(int aci)
{
   Dictionary<int, (int R, int G, int B)> aciToRgb = new Dictionary<int, (int, int, int)>
   {
	   { 1, (255, 0, 0) },   
	   { 2, (255, 255, 0) }, 
	   { 3, (0, 255, 0) },   
	   { 4, (0, 255, 255) }, 
	   { 5, (0, 0, 255) },   
	   { 6, (255, 0, 255) }, 
	   { 7, (255, 255, 255) },
	   { 20, (255, 63, 0) },
	   { 24, (129, 31, 0)},
	   { 253, (130, 130, 130) },
   };

   return aciToRgb[aci];
}