Iterating through entities and extraction of color values

Discuss and ask questions about CAD .NET library.

Moderators: SDS, support, admin

Post Reply
dredgehqdev
Posts: 5
Joined: 19 Sep 2016, 19:51

Iterating through entities and extraction of color values

Post by dredgehqdev » 19 Sep 2016, 19:57

I'm using CAD.NET in C#. I'm importing DWG and DXF files as follows:

Code: Select all

            cadImage = CADImport.CADImage.CreateImageByExtension(fileName);
            if (cadImage != null)
            {
               cadImage.LoadFromFile(fileName);
                    for (int index = 0; index < cadImage.Layouts.Count; index++)
                    {
                        CADImport.CADLayout layout = cadImage.Layouts[index];
                        if (cadImage.Layouts[index].Name.Equals("Model", StringComparison.InvariantCultureIgnoreCase))
                        {
                            foreach (CADImport.CADEntity ent in layout.Entities)
                            {
                                ProcessDWGEntity(cadImage, ent);
                            }
                        }
                    }
           }

In ProcessDWGEntity, to get color for an entity, I do this

Code: Select all

	CADImport.CADInsert ins = ent as CADImport.CADInsert;		
	if(ent is CADImport.CADLeader)
	    ins = (ent as CADImport.CADLeader).Insert;
        Color entColor = CADImport.CADConst.EntColor(ent, ins);
         if (entColor.ToArgb() == 0)
                entColor = cadImage.DefaultColor;

I couldn't figure out how to determine if an entity is using a default color so I hacked the part about checking if it's transparent black. Is there a better way?
Should I be using Iterate instead of how I'm doing it?

support
Posts: 3254
Joined: 30 Mar 2005, 11:36
Contact:

Re: Iterating through entities and extraction of color value

Post by support » 19 Sep 2016, 21:22

Hello,

For iterating through the entities you may use a single foreach loop, it will simplify the code:

Code: Select all

foreach (CADImport.CADEntity ent in cadImage.Converter.Entities)
{
    ProcessDWGEntity(cadImage, ent);
}
To determine if an entity is using the default color, call this method in the ProcessDWGEntity routine:

Code: Select all

private bool HasDefaultColor(CADEntity entity, CADImage cadImage)
{
    if (entity.Color.ToArgb() == cadImage.DefaultColor.ToArgb())
    {
        return true;
    }
    else
    {
        return false;
    }
}
Mikhail
Technical Support E-mail: support@cadsofttools.com
Chat support on Skype: cadsofttools.support

dredgehqdev
Posts: 5
Joined: 19 Sep 2016, 19:51

Re: Iterating through entities and extraction of color value

Post by dredgehqdev » 19 Sep 2016, 22:49

My original problem was that the color returned from the API is transparent black (ARGB(0,0,0,0)), but CAD.NET Editor was listing the color as White. So, I assumed that the entity was not using a local color, but the default color (which also happened to be white).

So, what would be the cause of the discrepancy between the API returned color and what your Editor program shows?

support
Posts: 3254
Joined: 30 Mar 2005, 11:36
Contact:

Re: Iterating through entities and extraction of color value

Post by support » 18 Oct 2016, 18:45

Hello,

You should check what color type (ByValue, ByLayer, ByBlock or Default) is used by the entity, by using the Color_type property value displayed in the PropertyGrid. Most likely, the color type value is Default in the given case, that means the color value displayed in the PropertyGrid may be (255, 255, 255) or (0,0,0) depending on the background color.

If you set the color type to ByValue, the color value displayed in the PropertyGrid will be the same as the one returned from the API.


Mikhail
Technical Support E-mail: support@cadsofttools.com
Chat support on Skype: cadsofttools.support

Post Reply