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.