Hi all.
*********************
Data:
Windows XP SP3 x86 Rus
AutoCAD 2009 SP3 x86 Enu
.Net Framework 3.5 SP1
*********************
I need save Autodesk.AutoCAD.Colors.Color to string, and parse it back. If color from "book" - I can't parse (method not work). Code below (problem in comments):
//Microsoft
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
//AutoCAD
using Autodesk.AutoCAD.Colors;
namespace Bushman.AutoCAD.Colors {
/// <summary>
/// Extension methods for Autodesk.AutoCAD.Colors.Color
/// </summary>
public static class ColorExtensionMethods {
/// <summary>
/// Parse string color to AutoCAD color
/// </summary>
/// <param name="color">object Autodesk.AutoCAD.Colors.Color</param>
/// <param name="strColor">string color</param>
/// <returns>return AutoCAD color</returns>
public static Color String2Color(this Color color, string strColor) {
short shortValue;
string[] values = strColor.Split(',');
//if short
if (short.TryParse(strColor, out shortValue))
return Color.FromColorIndex(ColorMethod.ByAci, shortValue);
else if (values.Length == 3) {
//If RGB
byte[] rgb = new byte[3];
for (int i = 0; i < values.Length; i++)
rgb[i] = byte.Parse(values[i]);
return Color.FromRgb(rgb[0], rgb[1], rgb[2]);
}
//If Color from book (WARNING: It is problem!!!)
else if (values.Length == 2)
return Color.FromNames(values[0], values[1]);//Method not work! Why?
//If other
else
throw new System.Exception(string.Format("Color \"{0}\" is not recognized!", strColor));
}
/// <summary>
/// Write AutoCAD color to string
/// </summary>
/// <param name="color">object Autodesk.AutoCAD.Colors.Color</param>
/// <returns>return string color</returns>
public static string Color2String(this Color color) {
//If color not from book - we can use ToString() method.
if (!color.HasBookName)
return color.ToString();
//If color from book - we can't use ToString() method, because it return
//ColorName only, but we need and BookName. Therefore create string
//with string.Format(...) method.
return string.Format("{0},{1}", color.ColorName, color.BookName);
}
}
}
Probably that I do something not so...
All thanks.