Saturday, August 11, 2007

AutoCAD Prompts & Error Handling

In this post, we are going to start with basic prompt operations and error handling. You will see samples on common AutoCAD prompts like string prompt, point prompt, distance prompt etc. The first sample in this session also covers very basic form of error handling. There are three commands (OPENDWG, DISPOINT & GETDIST) in the following code. The OPENDWG accepts a drawing path string input from the user and opens that drawing if exists. The DISPOINT prompts the user to pick a point and show the point value in the command prompt. The GETDIST prompts the user to pick two points and returns the distance between them. We will have a look at advanced prompt operations and creating AutoCAD geometry in the forthcoming sessions.

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.EditorInput;

namespace AcadNetSamples
{
public class Commands
{
[CommandMethod("OPENDWG")]
public static void OpenDrawing()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
PromptResult res = ed.GetString("\nEnter a drawing path: ");

if (res.Status == PromptStatus.OK)
{
try
{
Application.DocumentManager.Open(res.StringResult,false);
}
catch
{
ed.WriteMessage("\nUnable to open drawing file.");
return;
}
finally
{
ed.WriteMessage("\nThis is an AutoCAD.Net sample command.");
}
}
}
[CommandMethod("DISPOINT")]
public void DisplayPoint()
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
PromptPointOptions prPO = new PromptPointOptions("Select a point: ");
PromptPointResult prPR = ed.GetPoint(prPO);
if (prPR.Status != PromptStatus.OK)
{
ed.WriteMessage("An error occured...!");
}
else
{
ed.WriteMessage("The point is " + prPR.Value.ToString());
}
}

[CommandMethod("GETDIST")]
public void GetDistance()
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
PromptDistanceOptions prDO = new PromptDistanceOptions("Select first point: ");

PromptDoubleResult prDR = ed.GetDistance(prDO);

if (prDR.Status != PromptStatus.OK)
{
ed.WriteMessage("An error occured...!");
}
else
{
ed.WriteMessage("Distance: " + prDR.Value.ToString());
}
}
}
}

No comments: