为 AutoCAD Civil 3D搭建一个.NET项目

本节描述了使用Visual Studio和AutoCAD Civil 3D 托管类搭建一个.NET解决方案的基本步骤。无论你是使用Microsoft Visual C# .NET 还是 Visual Basic .NET,这些步骤都是类似的。下面的例子使用Visual Studio中的Visual Studio 2010. Express (free) 版的C#,可能看起来有点不一样,但也可以用。

要在Microsoft Visual Studio上用AutoCAD Civil 3D托管类构建一个新的项目

  1. 在 Visual Studio 2010中,创建一个新的类库解决方案和项目(class library solution and project)。

    Visual Studio的新项目对话框

  2. 选择Project 菜单添加引用,或在解决方案浏览器中右键点击“引用”,并选择“添加引用“
    .

  3. 浏览到AutoCAD Civil 3D的安装文件夹,然后选择基类库acdbmgd.dll, acmgd.dll,accoremgd.dll,AecBaseMgd.dll, 和 AeccDbMgd.dll.
    注意:
    这些是AutoCAD 和AutoCAD Civil 3D托管基类库。您的.NET 组件也可以使用定义在其他库中的类。

    为了允许调试并减少磁盘空间占用,在Visual Studio解决方案浏览器中选择这些类库并将”复制到本地设为“ 否( False.)

  4. Optionally, you can configure your project to start AutoCAD Civil 3D when you run the application from Visual Studio, which is useful for debugging.
    Note:
    This option is not avaiable in Express (free) versions of Visual Studio.

    1. On the project Properties page, select the Debug panel.
    2. Under Start Action , choose Start external program , and enter the path to the acad.exe executable in the AutoCAD Civil 3D directory.
    3. Under Start Options , fill in the Command line arguments : /ld "C:\Program Files\AutoCAD Civil 3D 2017\AecBase.dbx" /p "<<C3D_Imperial>>"
    4. Under Start Options, fill in the Working directory, for example: C:\Program Files\AutoCAD Civil 3D 2017\UserDataCache\
  5. Implement the IExtensionApplication interface in your main class. Add the Autodesk.AutoCAD.Runtime namespace (which is where this interface is defined), and IExtensionApplication after your class definition: Visual Studio will provide a code complete option to implement stubs for the interface. Your code should now look like this:

    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 aNETLOADcommand inAutoCAD Civil 3D, and can be used for setting up resources, reading configuration files, and other initialization tasks.Terminate()is called whenAutoCAD Civil 3Dshuts down (there is noNETUNLOADcommand to unload .NET assemblies), and can be used for cleanup to free resources.

  6. You are now ready to create a public method that is the target of a CommandMethod attribute. This attribute defines the AutoCAD Civil 3D command that invokes the method. For example:

    [CommandMethod("HelloWorld")]
    public void HelloWorld()
    {
    
    }
    
  7. Let’s make the method print out a “Hello World” message on the command line. Add the Autodesk.AutoCAD.ApplicationServices namespace, and add this line to the HelloWorld() method:

    Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage("\nHello World!\n");
    

    You can now build the assembly and run it. StartAutoCAD Civil 3D, and typeNETLOADat the command line. In theChoose .NET Assemblydialog, browse to your assembly DLL (if you are using the project settings from step 1, this will beGettingStarted.dll). TypeHELLOWORLDat the command line, and you will see the command output:

  8. The previous step used functionality from the AutoCAD Application class. Let’s include some functionality specific to the
    AutoCAD Civil 3D managed classes. First, add two more namespaces:
    Autodesk.AutoCAD.DatabaseServices and Autodesk.Civil.ApplicationServices. Then add these lines to obtain the current Civil document, get some basic information about it, and print the information out:

    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 inAutoCAD Civil 3Dthat contains alignments and sites. When you run theHELLOWORLDcommand now, you should see output similar to this:

For more samples, look in theAutoCAD Civil 3D\samples\dotNetdirectory.

父主题:

开始

results matching ""

    No results matching ""