Author Topic: AutoCAD Color Convertor  (Read 10202 times)

0 Members and 1 Guest are viewing this topic.

mcarson

  • Guest
AutoCAD Color Convertor
« on: March 21, 2008, 09:32:20 AM »
Hello again guys!
I am STILL working on a custom layer creation function that you helped me out with before. No point posting it until complete (like 2022!)

I am attempting to create a layer creation tool that creates a layer to company CAD standards. This isn't such a problem after you guys helped me out with the plot styles thingy. The program is almost complete (what I tell everyone), but for a routine that converts a string to an AutoCAD color; any AutoCAD color.

I had a look at Kerry's Color Labs, which is great and helped with calling the color dialog. http://www.theswamp.org/index.php?topic=14977.0

Our CAD users would like a facility to save their collection of standard layers to a 'favorites' database (or something) to allow easy insertion of their commonly used standard layers each time they start a new drawing. Saving the layers to an external database is no problem, however importing and creating layers is.

Is there any way that a string (representing an AutoCAD color) can be restored and converted to an AutoCAD color type to be used with creating layers?

ReneRam

  • Guest
Re: AutoCAD Color Convertor
« Reply #1 on: March 21, 2008, 11:01:25 AM »
I have used the attached functions for years, but now I use the following:

Code: [Select]
        ' Relative to Color Variables
        Dim myClrIndex, myRed, myGreen, myBlue As Byte

' <cutting code relative to my form>

        ' ShowDialog
        Dim myClr As New Autodesk.AutoCAD.Windows.ColorDialog
        With myClr
            .Color = Colors.Color.FromColorIndex(Colors.ColorMethod.ByAci, 1)
            Dim myRGB As DialogResult = .ShowDialog

            If myRGB = System.Windows.Forms.DialogResult.OK Then
                ' User has selected a Color
                myClrIndex = .Color.ColorIndex
            End If

            ' Update Form
            myRed = .Color.ColorValue.R
            myGreen = .Color.ColorValue.G
            myBlue = .Color.ColorValue.B
            lblColor.BackColor = Drawing.Color.FromArgb(myRed, myGreen, myBlue)
            lblColor.Text = "ACI: " & myClrIndex & vbCrLf & _
                               "(" & myRed & ", " & myGreen & ", " & myBlue & ")"
            ToolTip1.SetToolTip(lblColor, lblColor.Text)
        End With


hope it helps :-)
René

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: AutoCAD Color Convertor
« Reply #2 on: March 21, 2008, 10:08:15 PM »
<  ... >

Is there any way that a string (representing an AutoCAD color) can be restored and converted to an AutoCAD color type to be used with creating layers?

I assume you'd be restoring the value from a txt based db, so
how about something like ;
Code: [Select]
            string sLayerColor = "12";
//
//
            //Set the color for the new record
            ltr.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(
                            ColorMethod.ByAci,
                            Convert.ToInt16(sLayerColor));
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

mcarson

  • Guest
Re: AutoCAD Color Convertor
« Reply #3 on: March 24, 2008, 12:16:47 PM »
Guys,

This AutoCAD .NET programming is definitely full of challenges!

the code for displaying and returning a selected color:

Code: [Select]
public string ShowColorDialog()
{
    string result = null;
    Autodesk.AutoCAD.Windows.ColorDialog clr = new Autodesk.AutoCAD.Windows.ColorDialog();
    clr.IncludeByBlockByLayer = false;
    if ((objLayer.LayerColor != null)) {
        clr.Color = objWAFLayer.LayerColor;
    }
    if (clr.ShowDialog() != Windows.Forms.DialogResult.OK) {
        result = null;
    }
    else {
        objLayer.LayerColor = clr.Color;
        result = clr.Color.ToString;
    }
    return result;
}
private void SelectColor(object sender, System.EventArgs e)
{
    if ((ShowColorDialog != null)) {
        txtColor.Text = objWAFLayer.LayerColor.ToString;
    }
   
    UpdateForm();
}
private Autodesk.AutoCAD.Colors.Color LayerColor {
    get {
        if ((objLayer.LayerColor != null)) {
            return objLayer.LayerColor;
        }
        else {
            Autodesk.AutoCAD.Colors.Color _layercolor;
            Autodesk.AutoCAD.Colors.ColorConverter _conv = new Autodesk.AutoCAD.Colors.ColorConverter();
            _layercolor = _conv.ConvertFromString("7");
            return _layercolor;
        }
    }
}

the code for adding a favorite layer to a database:
Code: [Select]
public void AddToFavorites()
{

    string strLayerName = objLayer.LayerName;
    AcColors.Color objLayerColor = objLayer.LayerColor;
    AcDb.LineWeight objLayerLineweight = objLayer.LayerLineweight;
    string strLayerPlotStyle = objLayer.LayerPlotStyle;
    //The layer plot style name
   
    string name = strLayerName;
    // The color for the layer stored as argb?
    string color = (string)objLayerColor.ColorValue.ToArgb;

    string lineweight = objLayerLineweight.ToString;
    string plotstyle = strLayerPlotStyle;
   
    //frmUserFavorites fav = new frmUserFavorites();
    fav.AddLayerToFavorites(name, color, lineweight, plotstyle);
   
}

Kerry,
yeah I could read a string value from the db and convert it to a color (by the acad color index), but what about reading and converting Color Books and True Color?

ReneRam,
Thanks for that :-) I was wondering how to display the color as a background of a control

I wouldn't even be considering this conversion from color to string and back again; but with the AutoCAD color dialog providing the user with the option of picking a 'non' ACI color, I have to code my program to handle this. It would be a lot easier to 'hide' the True Color and Color Books tabs from the AutoCAD color dialog! :ugly:

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: AutoCAD Color Convertor
« Reply #4 on: March 25, 2008, 05:18:07 AM »
<  >
Kerry,
yeah I could read a string value from the db and convert it to a color (by the acad color index), but what about reading and converting Color Books and True Color?

You're asking the incorrect question :
How would you store the Color  ? ... all else follows from that.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: AutoCAD Color Convertor
« Reply #5 on: March 25, 2008, 05:25:19 AM »

say after me ; R, G, B :-)
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Glenn R

  • Guest
Re: AutoCAD Color Convertor
« Reply #6 on: March 25, 2008, 06:22:05 AM »
 :lmao:

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8747
  • AKA Daniel
Re: AutoCAD Color Convertor
« Reply #7 on: March 25, 2008, 11:20:40 AM »
You can use an html color or ‘Hex’ value too
example

Code: [Select]
  [Serializable]
  public struct MyFunkyColors
  {
    private byte m_red;
    private byte m_green;
    private byte m_blue;

    public MyFunkyColors(Byte red, Byte green, Byte blue)
    {
      this.m_red = red;
      this.m_green = green;
      this.m_blue = blue;
    }

    public MyFunkyColors(Autodesk.AutoCAD.Colors.Color color)
    {
      this.m_red = color.ColorValue.R;
      this.m_green = color.ColorValue.G;
      this.m_blue = color.ColorValue.B;
    }

    public MyFunkyColors(String htmlcolor)
    {
      System.Drawing.Color color = System.Drawing.ColorTranslator.FromHtml(htmlcolor);
      this.m_red = color.R;
      this.m_green = color.G;
      this.m_blue = color.B;
    }

    public void Invert()
    {
      m_red = (byte)(255 - m_red);
      m_green = (byte)(255 - m_green);
      m_blue = (byte)(255 - m_blue);
    }

    public byte R
    { get { return m_red; } }

    public byte G
    { get { return m_green; } }

    public byte B
    { get { return m_blue; } }

    public  string HTMLColor
    { get { return "#"+ m_red.ToString("X") + m_green.ToString("X") + m_blue.ToString("X"); } }

    public Autodesk.AutoCAD.Colors.Color Color
    { get { return Autodesk.AutoCAD.Colors.Color.FromRgb(m_red, m_green, m_blue); } }

    public static Autodesk.AutoCAD.Colors.Color Snow
    { get { return Autodesk.AutoCAD.Colors.Color.FromRgb(255, 250, 250); } }

    public static Autodesk.AutoCAD.Colors.Color LemonChiffon
    { get { return Autodesk.AutoCAD.Colors.Color.FromRgb(255, 250, 205); } }

    public static Autodesk.AutoCAD.Colors.Color DarkOrchid
    { get { return Autodesk.AutoCAD.Colors.Color.FromRgb(153, 50, 204); } }

    public override string ToString()
    {
      StringBuilder sb = new StringBuilder();
      sb.Append("(");
      sb.Append(m_red.ToString());
      sb.Append(",");
      sb.Append(m_green.ToString());
      sb.Append(",");
      sb.Append(m_blue.ToString());
      sb.Append(")");
      return sb.ToString();
    }

    public override int GetHashCode()
    {
      return base.GetHashCode();
    }

    public override bool Equals(object obj)
    {
      if (obj is MyFunkyColors)
      {
        MyFunkyColors o = (MyFunkyColors)obj;
        return ((o.R == m_red) && (o.G == m_green) && (o.B == m_blue));
      }
      else
      {
        return false;
      }
    }
    //
  }

Code: [Select]
    public static void doit()
    {
      LayerTableRecord lt1 = new LayerTableRecord();
      lt1.Color = MyFunkyColors.LemonChiffon;
      //...

      //or

      MyFunkyColors somecolor = new MyFunkyColors(250,250,250);
      LayerTableRecord lt2 = new LayerTableRecord();
      lt2.Color = somecolor.Color;

      //or

      MyFunkyColors newcolor = new MyFunkyColors("#FFFFEE");
      LayerTableRecord lt3 = new LayerTableRecord();
      lt3.Color = newcolor.Color;

    }
« Last Edit: March 25, 2008, 11:43:09 AM by Daniel »

mcarson

  • Guest
Re: AutoCAD Color Convertor
« Reply #8 on: March 25, 2008, 02:25:57 PM »
Having a laugh at my ability to ask the right question  :-D

So start again... (although you have probably answered it)

I have a custom layer creation dialog that allows users to select from a group of combo boxes a number of values that create the layer name. Additionally to this, the ability to set the plot style from a standard plot style table and the color, using the AutoCAD Color Dialog.
However, using this dialog every time you need to create a new standard layer would tend to be monotonous.
I created a new table in an Access (I know - I'll move to something better; later) to store favorite layers allowing the user to compile and later select a standard set of layers they wish to create/import using another form.
The only real option I seen at this stage was to store the layer properties as Text in the database and find a way to get the properties back in.
Having looked through the object browser and finding no immediate answer to convert a string value to color I posted a cryptic question that meant (using the above):

What is the best value I can read from the layer color that can be stored in a database as text and later converted back to a layer color?

Moving on, (still writing the code) if a user selects, for example 'PANTONE....' for their favorite layer and saves this (as ARGB); would the exact color be represented again converting the ARGB back using the color convertor? Likewise with True Color and with ACI?

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8747
  • AKA Daniel
Re: AutoCAD Color Convertor
« Reply #9 on: March 25, 2008, 03:02:18 PM »
So you go to your Pantone to HTML color web page like
http://www.mediarocket.com/colorchart.html  

or

http://www.logoorange.com/color/color-codes-chart.php

pick the color that you like, grab the HTML color you like i.e #F4E287

And you use the code I just posted  :wink:

Code: [Select]
   [CommandMethod("doit")]
    public static void doit()
    {
      string SearchedMyDatabaseForThisColor = "#F4E287";
      MyFunkyColors MyFavoritePantoneColor = new MyFunkyColors(SearchedMyDatabaseForThisColor);
      LayerTableRecord ltr = new LayerTableRecord();
      ltr.Color = MyFavoritePantoneColor.Color;
    }

« Last Edit: March 25, 2008, 03:17:14 PM by Daniel »

mcarson

  • Guest
Re: AutoCAD Color Convertor
« Reply #10 on: March 25, 2008, 03:06:52 PM »
Thanks :oops:

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8747
  • AKA Daniel
Re: AutoCAD Color Convertor
« Reply #11 on: March 25, 2008, 03:45:39 PM »
I hope it helps, I was trying to show,  that the best method might be to write a class/struct (Serializable of course)  to do the conversions for you. You can easily add constructors for other  types as well for example the color #F4E287 could just as well be stored as an Int32 (16048775). Have a search through the following name spaces and add the converters you might need

Code: [Select]
       
System.Drawing.ColorConverter;
System.Drawing.ColorTranslator;
Autodesk.AutoCAD.Colors.ColorConverter;

mcarson

  • Guest
Re: AutoCAD Color Convertor
« Reply #12 on: March 25, 2008, 04:56:59 PM »
Ah!
Now it makes sense. :roll:

I was still in the mindset of old vba in AutoCAD, when, if my memory serves me right, we had a custom form with all the aci colors with a conversion class sitting behind it - what a nightmare that looks now but it worked back then.

A bit more work than I thought, but what the h*ll.

I'll post my results at end of the week - must get this finished