TheSwamp
Code Red => .NET => Topic started by: Andrey Bushman on September 28, 2011, 03:31:26 AM
-
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.
-
StringSplitOptions.RemoveEmptyEntries?
-
StringSplitOptions.RemoveEmptyEntries?
What relation has it to my question? The parametres transferred in method FromNames not empty (I checked in a debugger).
-
StringSplitOptions.RemoveEmptyEntries?
What relation has it to my question? The parametres transferred in method FromNames not empty (I checked in a debugger).
None. I checked in a script. (It's only relevant if you have multiple consecutive delimiters)
What happens when you omit the the color book name?
Parameters Description
string colorName Input color name
string bookName Input optional book name
-
What happens when you omit the the color book name?
Parameters Description
string colorName Input color name
string bookName Input optional book name
Upon a layer doesn't receive color assigned by me.
-
My methods:
public static class ColorExtentiomMethods {
/// <summary>
/// Parse string to AutoCAD color
/// </summary>
/// <param name="strColor"></param>
/// <returns></returns>
public static Color String2Color(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]);
}
else if (values.Length == 2)
return Color.FromNames(values[0], values[1]);
//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);
}
}
My test command (use for attached file only!!!):
[CommandMethod("LayerOperations", CommandFlags.Modal)]
public void LayerOperations() {
Document dwg = acad.DocumentManager.MdiActiveDocument;
Editor ed = dwg.Editor;
Database db = dwg.Database;
using (Transaction t = db.TransactionManager.StartTransaction()) {
LayerTable lt = (LayerTable) t.GetObject(db.LayerTableId, OpenMode.ForRead);
List<LayerTableRecord> sources = new List<LayerTableRecord>();
List<LayerTableRecord> targets = new List<LayerTableRecord>();
string endName = "Result";
foreach (ObjectId id in lt) {
LayerTableRecord record = (LayerTableRecord) t.GetObject(id, OpenMode.ForRead);
if (record.Name == "0")
continue;
if (!record.Name.EndsWith(endName))
sources.Add(record);
else {
record.UpgradeOpen();
targets.Add(record);
}
}
foreach (LayerTableRecord rec in sources) {
string strColor = rec.Color.Color2String();
LayerTableRecord target = targets.First(n => n.Name == rec.Name + endName);
target.Color = ColorExtentiomMethods.String2Color(strColor);
}
t.Commit();
}
}
Color for each layer was selected manually, by means of a standard window:
(http://s1.ipicture.ru/uploads/20111003/02gWRrnU.png)
Before command LayerOperations is run:
(http://s1.ipicture.ru/uploads/20111003/XzC7PEQh.png)
After command LayerOperations is run:
(http://s2.ipicture.ru/uploads/20111003/9mX11e1S.png)
I think that a problem that a method the Color.FromNames not correctly works.
-
Has found the problem resolution here (http://through-the-interface.typepad.com/through_the_interface/2007/12/using-a-color-b.html).
One more interesting link here (http://through-the-interface.typepad.com/through_the_interface/2007/12/getting-the-nam.html).
Topic is closed.
-
Andrey what year are you using?
All I changed was First to Find so it would compile
LayerTableRecord target = targets.Find(n => n.Name == rec.Name + endName);
In 2012 it worked fine and changed the color correctly.
-
Andrey what year are you using?
All I changed was First to Find so it would compile
LayerTableRecord target = targets.Find(n => n.Name == rec.Name + endName);
In 2012 it worked fine and changed the color correctly.
row
LayerTableRecord target = targets.First(n => n.Name == rec.Name + endName);
correctly compile too.
I use AutoCAD 2009
-
From Autodesk:
Hi Andrey,
We had a change request for this behavior which was resolved in AutoCAD 2010.
Unfortunately, for this to work in AutoCAD 2009, the workaround such as using “ColorConverter” will have to be used.
Topic is closed.