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:
Here's the dialog shown by the LTDL command:
And the LWDL command:
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.






Subscribe via RSS
Hi Kean,
Can be these dialogs called from ObjectARX C++ API?
Posted by: Fernando Malard | February 09, 2008 at 06:05 PM
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
Posted by: Kean | February 11, 2008 at 09:41 AM
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.
Posted by: Martin Duke | February 12, 2008 at 06:21 AM
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
Posted by: Kean | February 12, 2008 at 09:52 AM
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.
Posted by: Mark | February 15, 2008 at 10:02 AM
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
Posted by: Kean | February 15, 2008 at 11:12 AM
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
Posted by: coddy | January 07, 2009 at 07:54 AM