This section describes the basic steps to set up a .NET solution using Visual Studio and the Autodesk Civil 3D managed classes. The steps are the similar whether you use Microsoft Visual C# .NET or Visual Basic .NET. The example below uses C# in Visual Studio 2010. Express (free) versions of Visual Studio may look slightly different, but can also be used.
To create a new project that uses the Autodesk Civil 3D managed classes in Microsoft Visual Studio
New Project Dialog in Visual Studio
These are the base AutoCAD and Autodesk Civil 3D managed libraries. Your .NET assembly can use classes defined in additional libraries.
To allow debugging and reduce the disk space requirements for your projects, select these libraries in the Visual Studio Solution Explorer, and set the Copy Local property to False.
This option is not avaiable in Express (free) versions of Visual Studio.
using System; using Autodesk.AutoCAD.Runtime; namespace GettingStarted { public class Class1 : IExtensionApplication { #region IExtensionApplication Members public void Initialize() { throw new System.Exception("The method or operation is not implemented."); } public void Terminate() { throw new System.Exception("The method or operation is not implemented."); } #endregion } }
You can remove or comment out the default content of these methods. Initialize() is called when your assembly is first loaded by a NETLOAD command in Autodesk Civil 3D, and can be used for setting up resources, reading configuration files, and other initialization tasks. Terminate() is called when Autodesk Civil 3D shuts down (there is no NETUNLOAD command to unload .NET assemblies), and can be used for cleanup to free resources.
[CommandMethod("HelloWorld")] public void HelloWorld() { }
Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage("\nHello World!\n");
You can now build the assembly and run it. Start Autodesk Civil 3D, and type NETLOAD at the command line. In the Choose .NET Assembly dialog, browse to your assembly DLL (if you are using the project settings from step 1, this will be GettingStarted.dll). Type HELLOWORLD at the command line, and you will see the command output:
public void HelloWorld() { CivilDocument doc = Autodesk.Civil.ApplicationServices.CivilApplication.ActiveDocument; ObjectIdCollection alignments = doc.GetAlignmentIds(); ObjectIdCollection sites = doc.GetSiteIds(); String docInfo = String.Format("\nHello World!\nThis document has {0} alignments and {1} sites.\n", alignments.Count, sites.Count); Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(docInfo); }
Open or create a document in Autodesk Civil 3D that contains alignments and sites. When you run the HELLOWORLD command now, you should see output similar to this:
For more samples, look in the Autodesk Civil 3D\samples\dotNet directory.