Author Topic: Color Serialization  (Read 3534 times)

0 Members and 1 Guest are viewing this topic.

Andrey Bushman

  • Swamp Rat
  • Posts: 864
Color Serialization
« 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):
Code: [Select]
//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.
« Last Edit: September 28, 2011, 03:52:12 AM by Andrey »

kaefer

  • Guest
Re: Color Serialization
« Reply #1 on: September 28, 2011, 07:15:30 AM »
StringSplitOptions.RemoveEmptyEntries?

Andrey Bushman

  • Swamp Rat
  • Posts: 864
Re: Color Serialization
« Reply #2 on: September 28, 2011, 08:26:39 AM »
StringSplitOptions.RemoveEmptyEntries?
What relation has it to my question? The parametres transferred in method FromNames not empty (I checked in a debugger).

kaefer

  • Guest
Re: Color Serialization
« Reply #3 on: September 28, 2011, 08:43:10 AM »
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?
Quote
Parameters  Description 
string colorName  Input color name 
string bookName  Input optional book name 

Andrey Bushman

  • Swamp Rat
  • Posts: 864
Re: Color Serialization
« Reply #4 on: September 28, 2011, 11:28:16 AM »
What happens when you omit the the color book name?
Quote
Parameters  Description 
string colorName  Input color name 
string bookName  Input optional book name 
Upon a layer doesn't receive color assigned by me.

Andrey Bushman

  • Swamp Rat
  • Posts: 864
Re: Color Serialization
« Reply #5 on: October 03, 2011, 07:29:04 AM »
My methods:
Code: [Select]
    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!!!):

Code: [Select]
        [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:



Before command LayerOperations is run:



After command LayerOperations is run:



I think that a problem that a method the Color.FromNames not correctly works.
« Last Edit: October 03, 2011, 10:50:51 AM by Andrey »

Andrey Bushman

  • Swamp Rat
  • Posts: 864
Re: Color Serialization
« Reply #6 on: October 03, 2011, 08:24:49 AM »
Has found the problem resolution here.
One more interesting link here.

Topic is closed.

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Color Serialization
« Reply #7 on: October 03, 2011, 09:55:42 AM »
Andrey what year are you using?
All I changed was First to Find so it would compile
Code: [Select]
LayerTableRecord target = targets.Find(n => n.Name == rec.Name + endName);

In 2012 it worked fine and changed the color correctly.
 

Andrey Bushman

  • Swamp Rat
  • Posts: 864
Re: Color Serialization
« Reply #8 on: October 03, 2011, 10:10:55 AM »
Andrey what year are you using?
All I changed was First to Find so it would compile
Code: [Select]
LayerTableRecord target = targets.Find(n => n.Name == rec.Name + endName);

In 2012 it worked fine and changed the color correctly.
row
Code: [Select]
LayerTableRecord target = targets.First(n => n.Name == rec.Name + endName);correctly compile too.

I use AutoCAD 2009
« Last Edit: October 03, 2011, 10:34:15 AM by Andrey »

Andrey Bushman

  • Swamp Rat
  • Posts: 864
Re: Color Serialization
« Reply #9 on: October 04, 2011, 04:16:56 AM »
From Autodesk:
Quote
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.