TheSwamp

Code Red => .NET => Topic started by: Kerry on March 08, 2012, 07:17:33 PM

Title: StringToColorTest
Post by: Kerry on March 08, 2012, 07:17:33 PM
In response to :
http://forums.autodesk.com/t5/NET/Changing-the-Color-Line-type-and-Line-weight-using-Net/td-p/3362911

Just 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 ?
Code - C++: [Select]
  1. // (C) CodeHimBelonga kdub
  2.  
  3. using System;
  4. using System.Windows;
  5. using Autodesk.AutoCAD.ApplicationServices;
  6. using Autodesk.AutoCAD.DatabaseServices;
  7. using Autodesk.AutoCAD.EditorInput;
  8. using Autodesk.AutoCAD.Runtime;
  9. using StringToColorTest;
  10. using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;
  11.  
  12. [assembly: CommandClass(typeof (StringToColor))]
  13.  
  14. namespace StringToColorTest
  15. {
  16.     public class StringToColor
  17.     {
  18.         [CommandMethod("DoIt")]
  19.         public void StringToColorTest_01()
  20.         {
  21.             Editor ed           = AcadApp.DocumentManager.MdiActiveDocument.Editor;
  22.             string colorString  = AskUserForColorString(ed);
  23.             int colorIndex      = ColorIndexFromString(colorString);
  24.  
  25.             MessageBox.Show( "Color String : " +
  26.                                         colorString +
  27.                                         "\nTranslates to Integer : " +
  28.                                         colorIndex.ToString(),
  29.                               "StringToColorTest_01");
  30.         }
  31.  
  32.         //
  33.         //
  34.         public string AskUserForColorString(Editor ed)
  35.         {
  36.             PromptStringOptions strOpts = new PromptStringOptions("\nEnter Color: ");
  37.             strOpts.AllowSpaces         = false;
  38.             PromptResult res            = ed.GetString(strOpts);
  39.  
  40.             if (res.Status != PromptStatus.OK)
  41.             {
  42.                 return "ByLayer";
  43.             }
  44.             return res.StringResult;
  45.         }
  46.  
  47.         //
  48.         //
  49.         public int ColorIndexFromString(string colorString)
  50.         {
  51.             int result = -1;
  52.             if (int.TryParse(colorString, out result))
  53.             {
  54.                 result = ((result >= 0) && (result <= 256) ? result : 256);
  55.             }
  56.             else
  57.             {
  58.                 try
  59.                 {
  60.                     MyColors colorIndex =
  61.                         (MyColors) Enum.Parse(typeof (MyColors), colorString, true);
  62.                     if (Enum.IsDefined(typeof(MyColors), colorIndex))
  63.                     {
  64.                         result = (int) colorIndex;
  65.                     }
  66.                 }
  67.                 catch (ArgumentException)
  68.                 {
  69.                     AcadApp.DocumentManager.MdiActiveDocument.Editor.WriteMessage(
  70.                                                                         "Blooper !!");
  71.                     return 256;
  72.                 }
  73.             }
  74.             return result;
  75.         }
  76.  
  77.         //
  78.         //
  79.         private enum MyColors
  80.         {
  81.             ByBlock = 0,
  82.             Red     = 1,
  83.             Yellow  = 2,
  84.             Green   = 3,
  85.             Cyan    = 4,
  86.             Blue    = 5,
  87.             Magenta = 6,
  88.             White   = 7,
  89.             Grey    = 8,
  90.             ByLayer = 256
  91.         };
  92.     }
  93. }
  94.  
Title: Re: StringToColorTest
Post by: Kerry on March 12, 2012, 03:15:30 AM


ahhh ... no-one found the bugs  :-D