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...!!"