Tuesday, July 31, 2007

Getting Started

Instead of spending time on discussing the concepts and theories behind AutoCAD.Net programming, I think we should better start with some sample code. It defenitely helps us to raise the confindence level. The thrill of watching our first program output in a new technology will defenitely last long and help us to gain momentum and energy for further exploration. So here goes our first program. Please remember that I am also a beginner in this field. So the code may not be of professional standard and may contain errors. I highly appreciate your suggestions for the improvement of these code snippets.

The following code displays a welcome message in the AutoCAD Text Editor window.




Here is the a text version of the code if you want to copy and paste it in your editor. The above one is a screenshot from the code editor window. I used that image for a better understanding of the code. Once you copy the following code and paste it in Visual C# editor, it should arrange it in the manner shown above. So next post onwards, I may not include a screenshot.

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.EditorInput;
namespace AcadNetSamples
{
public class Commands
{
[CommandMethod("Hello")]
public static void SayHello()
{
Document doc=Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
ed.WriteMessage("\nHello and welcome to AutoCAD.Net world...!!");
}
}
}
Here goes a few thing to be taken care before you start using it.
  • You must have set reference to AutoCAD managed class libraries acmgd.dll and acdbmgd.dll . Otherwise the AutoCAD specific objects and methods will not get detected.
  • If you are using Visual C# editor, then you can use Solution Explorer window to add reference to the above files. If you are using a command line version of compiler (CSC.EXE), then use /r option to set reference. (Type CSC/? in the command line to get help on compiler options)
  • The project type has to be a Class Library in order to use it inside AutoCAD. If you start with VS Editor, the choose project template 'Class Library' in the beginning. If you use Command line, then choose /t:library option to compile it as a library project.

Those are the essentials and once you build the output, load the dll file (here AcadNetSamples.dll) from autocad using netload command. Type "Hello" in the command prompt and if everything goes right, you will see the following message.

"Hello and welcome to AutoCAD.Net world...!!"

No comments: