.NET

Reply
Valued Contributor
chockalingam
Posts: 84
Registered: ‎03-06-2012
Message 1 of 10 (1,246 Views)
Accepted Solution

Changing the Color, Line type and Line weight using .Net

1246 Views, 9 Replies
03-08-2012 04:37 AM

How to change the Color, line type and Line weight for the Line using .Net. 

 

One option:

 

Code solution is posted at  http://www.theswamp.org/index.php?topic=41172.msg463389#msg463389

 

// (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
};
}
}

 

 

 

Valued Mentor
KerryBrown
Posts: 276
Registered: ‎11-29-2008
Message 2 of 10 (1,242 Views)

Re: Changing the Color, Line type and Line weight using .Net

03-08-2012 04:45 AM in reply to: chockalingam

 

What have you tried. ?

 

... and what documentation have you looked at ?

//-------------------------------------------------------

Everything will work just as you expect it to, unless your expectations are incorrect.

class keyThumper<T> : Lazy<T>;      another  Swamper

Valued Contributor
chockalingam
Posts: 84
Registered: ‎03-06-2012
Message 3 of 10 (1,237 Views)

Re: Changing the Color, Line type and Line weight using .Net

03-08-2012 04:50 AM in reply to: KerryBrown

I tried the following

newentLine.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(Autodesk.AutoCAD.Colors.ColorMethod.ByLayer, 190)

 

where, newentLine is a entity

This works,  but the issue is i want to change the color with a string input. I don't know how to do it

 

 

Valued Mentor
KerryBrown
Posts: 276
Registered: ‎11-29-2008
Message 4 of 10 (1,226 Views)

Re: Changing the Color, Line type and Line weight using .Net

03-08-2012 04:56 AM in reply to: chockalingam

 

What strings do you want to use ?

 

 

//-------------------------------------------------------

Everything will work just as you expect it to, unless your expectations are incorrect.

class keyThumper<T> : Lazy<T>;      another  Swamper

Valued Contributor
chockalingam
Posts: 84
Registered: ‎03-06-2012
Message 5 of 10 (1,224 Views)

Re: Changing the Color, Line type and Line weight using .Net

03-08-2012 04:57 AM in reply to: chockalingam

I want to send the color as string

 

ex: red, white, ByLayer........

Valued Mentor
KerryBrown
Posts: 276
Registered: ‎11-29-2008
Message 6 of 10 (1,210 Views)

Re: Changing the Color, Line type and Line weight using .Net

03-08-2012 05:18 AM in reply to: chockalingam

 

Sounds like a simple

if (..)

//--

else if ( .. )

//--

else if ( .. )

//--

else if ( .. )

//--

else ( .. )

 

construct would work for you.

 

... or you could use a dictionary if you're comfortable with that.

 

Depending on the source of the string you may be advised to change the case to a consistant format ....

 

Regards

 

 

 

 

//-------------------------------------------------------

Everything will work just as you expect it to, unless your expectations are incorrect.

class keyThumper<T> : Lazy<T>;      another  Swamper

*Expert Elite*
dgorsman
Posts: 5,613
Registered: ‎10-12-2006
Message 7 of 10 (1,189 Views)

Re: Changing the Color, Line type and Line weight using .Net

03-08-2012 10:33 AM in reply to: KerryBrown

Or a (switch...), since color values are mutually exclusive.  The various case statements would associate the strings with the enumerated values, or ACI values.

----------------------------------
If you are going to fly by the seat of your pants, expect friction burns.
Adopt. Adapt. Overcome. Or be overcome.
A good question will be halfway to a good answer.


Valued Mentor
KerryBrown
Posts: 276
Registered: ‎11-29-2008
Message 8 of 10 (1,177 Views)

Re: Changing the Color, Line type and Line weight using .Net

03-08-2012 04:26 PM in reply to: chockalingam

 

One option:

 

Code solution is posted at  http://www.theswamp.org/index.php?topic=41172.msg463389#msg463389

 

// (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
        };
    }
}

 

 

 

//-------------------------------------------------------

Everything will work just as you expect it to, unless your expectations are incorrect.

class keyThumper<T> : Lazy<T>;      another  Swamper

Valued Contributor
chockalingam
Posts: 84
Registered: ‎03-06-2012
Message 9 of 10 (1,162 Views)

Re: Changing the Color, Line type and Line weight using .Net

03-08-2012 08:03 PM in reply to: chockalingam

Thanks for solution, this code helps me................

Valued Mentor
KerryBrown
Posts: 276
Registered: ‎11-29-2008
Message 10 of 10 (1,134 Views)

Re: Changing the Color, Line type and Line weight using .Net

03-09-2012 05:49 PM in reply to: chockalingam

 

You're welcome.

It was an interesting technical problem.

 

Regards

 

//-------------------------------------------------------

Everything will work just as you expect it to, unless your expectations are incorrect.

class keyThumper<T> : Lazy<T>;      another  Swamper

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Are You Going To Be @ AU 2014? Feel free to drop by our AU topic post and share your plans, plug a class that you're teaching, or simply check out who else from the community might be in attendance. Ohh and don't forgot to stop by the Autodesk Help | Learn | Collaborate booths in the Exhibit Hall and meet our community team if you get a chance!