Kean Walmsley

July 2009

Sun Mon Tue Wed Thu Fri Sat
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31  

Twitter Updates

    follow me on Twitter



    « Mardi Gras, Super Tuesday and F# | Main | Modifying the color, linetype and lineweight of an AutoCAD entity using standard dialogs from .NET »

    February 08, 2008

    Using standard AutoCAD dialogs to select colors, linetypes and lineweights with .NET

    AutoCAD has a number of handy dialogs available in the "Autodesk.AutoCAD.Windows" namespace. The following code shows how to use three, in particular: ColorDialog, LinetypeDialog and LineWeightDialog. These three classes allow you to very easily implement user-interfaces selecting their corresponding properties.

    Here's the C# code:

    using Autodesk.AutoCAD.Runtime;

    using Autodesk.AutoCAD.ApplicationServices;

    using Autodesk.AutoCAD.DatabaseServices;

    using Autodesk.AutoCAD.EditorInput;

    using Autodesk.AutoCAD.Windows;


    namespace AutoCADDialogs

    {

      public class Commands

      {

        [CommandMethod("CDL")]

        public void ShowColorDialog()

        {

          Document doc =

            Application.DocumentManager.MdiActiveDocument;

          Database db = doc.Database;

          Editor ed = doc.Editor;


          ColorDialog cd = new ColorDialog();

          System.Windows.Forms.DialogResult dr =

            cd.ShowDialog();

          if (dr == System.Windows.Forms.DialogResult.OK)

          {

            ed.WriteMessage(

              "\nColor selected: " +

              cd.Color.ToString()

            );

          }

        }


        [CommandMethod("LTDL")]

        public void ShowLinetypeDialog()

        {

          Document doc =

            Application.DocumentManager.MdiActiveDocument;

          Database db = doc.Database;

          Editor ed = doc.Editor;


          LinetypeDialog ltd = new LinetypeDialog();

          System.Windows.Forms.DialogResult dr =

            ltd.ShowDialog();

          if (dr == System.Windows.Forms.DialogResult.OK)

          {

            ed.WriteMessage(

              "\nLinetype selected: " +

              ltd.Linetype.ToString()

            );

          }

        }


        [CommandMethod("LWDL")]

        public void ShowLineWeightDialog()

        {

          Document doc =

            Application.DocumentManager.MdiActiveDocument;

          Database db = doc.Database;

          Editor ed = doc.Editor;


          LineWeightDialog lwd = new LineWeightDialog();

          System.Windows.Forms.DialogResult dr =

            lwd.ShowDialog();

          if (dr == System.Windows.Forms.DialogResult.OK)

          {

            ed.WriteMessage(

              "\nLineweight selected: " +

              lwd.LineWeight.ToString()

            );

          }

        }

      }

    }

    When we run the CDL command, we have three tabs available:

    Color_dialog_1

    Color_dialog_2

    Color_dialog_3

    Here's the dialog shown by the LTDL command:

    Linetype_dialog

    And the LWDL command:

    Lineweight_dialog

    And here is the command-line output for selecting each of the above items:

    Command: CDL

    Color selected: 91

    Command: CDL

    Color selected: 56,166,199

    Command: CDL

    Color selected: DIC 266

    Command: LTDL

    Linetype selected: (2130316464)

    Command: LWDL

    Lineweight selected: LineWeight009

    In the next post we'll look at at how to use these dialogs in a more realistic way.

    TrackBack

    TrackBack URL for this entry:
    http://www.typepad.com/services/trackback/6a00d83452464869e200e5503a4fa18834

    Listed below are links to weblogs that reference Using standard AutoCAD dialogs to select colors, linetypes and lineweights with .NET:

    Comments

    Hi Kean,

    Can be these dialogs called from ObjectARX C++ API?

    Hi Fernando,

    The following colour-related functions are in the ObjectARX SDK, in aced.h:

    acedSetColorDialog()
    acedSetColorDialogTrueColor()
    acedSetColorDialogTrueColorWithCallback()

    The linetype and lineweight dialogs are not officially exposed via ObjectARX, but they are exported off acad.exe as:

    acedLinetypeDialog()
    acedLineWeightDialog()

    If you're skilled with function de-decorating/unmangling, then you should be able to work out how to call them from the dumpbin output. This isn't supported, however.

    Oh, and you can certainly invoke the managed versions from C++, although I haven't done so myself.

    Cheers,

    Kean

    Hello Kean,

    These controls are great, but what I would really like, are Combobox versions of them as that is more appropriate on a form. I know ActiveX versions are available, but, from my experience they are not overly reliable in a .NET environment. Maybe you could show how to create Colour\Linetpye controls as Combos in .NET?


    Cheers

    Martin.

    Hi Martin,

    As you've mentioned, combobox versions of the AutoCAD controls (layer, linetype, lineweight, layer, UCS) as well as additional ActiveX controls for other types of content (DWG thumbnail, hatch-pattern, slide) are available for ADN members from here.

    The effort to re-create these in .NET would be non-trivial: if you have any feedback on using them from a .NET environment, please let us know via the ADN site, and we'll do our best to fix them.

    Regards,

    Kean

    Excellent example, thank you.
    I am implementing these dialogs in a Layer Standards dialog (VB.NET) and have managed to pass a color to the color dialog and return user selected. (using color convertor)
    However, with the linetype dialog (as with your example) the string returned is in an user-unfriendly format. I am assuming that it is possible to look at the linetype table record to get a description or like, but there does not seem to be an easy way to accomplish this.

    It's an ObjectID: if you use it inside a transaction to get the LinetypeTableRecord (by calling GetObject()), you can then access a more friendly identifier by the Name property.

    Kean

    Hi Kean,

    This question is not related to the above mentioned topic. I would like to know if it's possible to view sld files as thumbnails in listview control using C#. If not, what is the other method? Hope you will help me out.

    Thanks

    Verify your Comment

    Previewing your Comment

    This is only a preview. Your comment has not yet been posted.

    Working...
    Your comment could not be posted. Error type:
    Your comment has been posted. Post another comment

    The letters and numbers you entered did not match the image. Please try again.

    As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

    Having trouble reading this image? View an alternate.

    Working...

    Post a comment

    Feed & Share

    Search