In response to :
http://forums.autodesk.com/t5/NET/Changing-the-Color-Line-type-and-Line-weight-using-Net/td-p/3362911Just playing around :
One option to convert a string value to an integer representing an AutoCAD color index (ie: 0 to 256)
Allow the user to enter the color Name or enter an integer.
This could be used to return the Color object .
Code solution is attached.
Any comments ?
// (C) CodeHimBelonga kdub
using System;
using System.Windows;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using StringToColorTest;
using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;
[assembly: CommandClass(typeof (StringToColor))]
namespace StringToColorTest
{
public class StringToColor
{
[CommandMethod("DoIt")]
public void StringToColorTest_01()
{
Editor ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;
string colorString = AskUserForColorString(ed);
int colorIndex = ColorIndexFromString(colorString);
MessageBox.Show( "Color String : " +
colorString +
"\nTranslates to Integer : " +
colorIndex.ToString(),
"StringToColorTest_01");
}
//
//
public string AskUserForColorString(Editor ed)
{
PromptStringOptions strOpts = new PromptStringOptions("\nEnter Color: ");
strOpts.AllowSpaces = false;
PromptResult res = ed.GetString(strOpts);
if (res.Status != PromptStatus.OK)
{
return "ByLayer";
}
return res.StringResult;
}
//
//
public int ColorIndexFromString(string colorString)
{
int result = -1;
if (int.TryParse(colorString, out result))
{
result = ((result >= 0) && (result <= 256) ? result : 256);
}
else
{
try
{
MyColors colorIndex =
(MyColors) Enum.Parse(typeof (MyColors), colorString, true);
if (Enum.IsDefined(typeof(MyColors), colorIndex))
{
result = (int) colorIndex;
}
}
catch (ArgumentException)
{
AcadApp.DocumentManager.MdiActiveDocument.Editor.WriteMessage(
"Blooper !!");
return 256;
}
}
return result;
}
//
//
private enum MyColors
{
ByBlock = 0,
Red = 1,
Yellow = 2,
Green = 3,
Cyan = 4,
Blue = 5,
Magenta = 6,
White = 7,
Grey = 8,
ByLayer = 256
};
}
}