Set ColorIndex instead of RGB

Discuss and ask questions about CAD .NET library.

Moderators: SDS, support, admin

Post Reply
Sing
Posts: 48
Joined: 12 Apr 2020, 17:39

Set ColorIndex instead of RGB

Post by Sing » 26 Aug 2020, 08:47

Hello~

I want to set the layer color as ColorIndex instead of RGB.

when I print out a drawing, polylines with white(ColorIndex: 7) come out as black.
However, RGB white color doesn't come out as black.

My code is below.

CADLayer layer = new CADLayer()
{
Name = "layer_name",
Color = System.Drawing.Color.White // or Color.FromArgb(255)
};

Is there any way to set ColorIndex to layer or polyline?
If not, do you have any idea to convert the white color to black when printing out?

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

Re: Set ColorIndex instead of RGB

Post by support » 26 Aug 2020, 20:55

Hello,

For your information, the color with index 7 indicates Black/White that means an object can be Black or White depending on the background color, these two colors are inverted when you change the background color from Black to White and vice versa. Unfortunately, CAD .NET doesn't allow to assign AutoCAD Color Index number to an entity or layer.

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

Sing
Posts: 48
Joined: 12 Apr 2020, 17:39

Re: Set ColorIndex instead of RGB

Post by Sing » 27 Aug 2020, 17:02

Thank you for your reply.

Anyway, I solved it in another way.
I hope it helps someone.

---------------------------------------------
1. Make a dwg file.
2. Add layers and choose ACI(Autocad Color Index) Colors for the layers
3. In code, load that .dwg file and copy the colors of layers
4. assign them to any colors of layers.
---------------------------------------------

Code: Select all


public class ACIColorInfo
{
	public int index = -1;
	/// <summary>
	/// Autocad Color Index String
	/// </summary>
	public string IndexStr
	{
		get => index.ToString();
		set
		{
			int.TryParse(value, out index);
		}
	}
	/// <summary>
	/// Autocad Color Index
	/// </summary>
	public int Index { get => index; set => index = value; }
	/// <summary>
	/// Custom Color Name
	/// </summary>
	public string Name { get; set; }
	/// <summary>
	/// Color
	/// </summary>
	public Color Color { get; set; }
}

Code: Select all

public Dictionary<int, ACIColorInfo> GetACIColors()
{
	CADImage cadImageColorIndex = null;

	try
	{
		string pathDWG = "ColorIndex.dwg";  // <= Change to your dwg file path

		cadImageColorIndex = CADImage.CreateImageByExtension(pathDWG);
		cadImageColorIndex.LoadFromFile(pathDWG);

		Dictionary<int, ACIColorInfo> aciColorInfoDic = new Dictionary<int, ACIColorInfo>();
		//
		// ByLayer
		//
		aciColorInfoDic.Add(256, new ACIColorInfo() { index = 0, Name = "ByLayer", Color = DrawingColor.FromArgb(47, 255, 255, 255) });
		//
		// ByBlock
		//

		// hasn't been implemented yet

		//
		//
		//
		cadImageColorIndex.Converter.Layers.ForEach(entity =>
		{
			//
			// "0" is default layer added automatically when creating a dwg file.
			// However, Number '0' will be used as key of the ACI dictionary for ByLayer Color Type.
			//
			if (entity.EntName == "0") return;

			ACIColorInfo aciColorInfo = new ACIColorInfo();

			aciColorInfo.IndexStr = entity.EntName;
			aciColorInfo.Name = entity.Color.Name;
			aciColorInfo.Color = entity.Color;

			aciColorInfoDic.Add(aciColorInfo.index, aciColorInfo);
		});

		return aciColorInfoDic;
	}
	catch (Exception ee)
	{
		TraceManager.AddLog(string.Format("{0}r\n{1}", ee.StackTrace, ee.Message));
		System.Diagnostics.Debug.WriteLine(string.Format("{0}r\n{1}", ee.StackTrace, ee.Message));

		return null;
	}
	finally
	{
		if (cadImageColorIndex != null)
		{
			cadImageColorIndex.ClearBuffer();
			cadImageColorIndex.Dispose();
			cadImageColorIndex = null;
		}

		GC.Collect();
	}
}

Code: Select all


//
// Use
//
Dictionary<int, ACIColorInfo> aciColorInfoDic = GetACIColors();
Color colorByLayer = aciColorInfoDic.ContainsKey(256) ? aciColorInfoDic[256].Color : DrawingColor.White;
Color color_7 = aciColorInfoDic.ContainsKey(7) ? aciColorInfoDic[7].Color : colorByLayer;
//
// Assign to
//
yourLayer.Color = color_7;

Code: Select all

/// ACI  Color Properties Info
/// 
/// Index | Name       | ARGB            | IsKnownColor | IsNamedColor | IsSystemColor |
/// ------|------------|-----------------|--------------|--------------|---------------|
/// 1     | Red        | 255,255,  0,  0 | true		| true	   	| false		   |
/// 2     | Yellow     | 255,255,255,  0 | true 	| true	   	| false		   |
/// 3     | Lime       | 255,  0,255,  0 | true 	| true	   	| false		   |
/// 4     | Aqua       | 255,  0,255,255 | true 	| true	   	| false		   |
/// 5     | Blue       | 255,  0,  0,255 | true 	| true	   	| false		   |
/// 6     | Fuchsia    | 255,255,  0,255 | true 	| true	   	| false		   |
/// 7     | 536870911  |   0,  0,  0,  0 | false 	| true	   	| false		   |
/// 8     | Gray       | 255,128,128,128 | true 	| true	   	| false		   |
/// 9     | Silver     | 255,192,192,192 | true 	| true		| false		   |
/// ...
/// 250   | ff333333   | 255, 51, 51, 51 | false 	| false	   	| false		   |
/// 251   | ff565656   | 255, 91, 91, 91 | false 	| false	   	| false		   |
/// ...
/// 255   | 4fffffff   |  79,255,255,255 | false	| false	       	| false		   |
/// 
/// 
/// ByBlock
/// 0     | 
/// ByLayer
/// 256   | 2fffffff   | 47,255,255,255  | false        | false        	| false         |
ColorIndex.dwg.PNG
ColorIndex.dwg.PNG (37.91 KiB) Viewed 7117 times
Attachments
ColorIndex.zip
(13.87 KiB) Downloaded 594 times

Post Reply