Create and Open a Drawing (.NET)

To create a new drawing or open an existing drawing, use the methods of the DocumentCollectionExtension object. The Add method creates a new drawing file based on a drawing template and adds that drawing to the DocumentCollectionExtension. The Open method opens an existing drawing file.

Create a new drawing

This example uses the Add method to create a new drawing based on the acad.dwt drawing template file.

C# Example

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
 
[CommandMethod("NewDrawing", CommandFlags.Session)]
public static void NewDrawing()
{
    // Specify the template to use, if the template is not found
    // the default settings are used.
    string strTemplatePath = "acad.dwt";

    DocumentCollection acDocMgr = Application.DocumentManager;
    Document acDoc = acDocMgr.Add(strTemplatePath);

    acDocMgr.MdiActiveDocument = acDoc;
}

Open an existing drawing

This example uses the Open method to open an existing drawing. Before opening the drawing, the code checks for the existence of the file before trying to open it.

C# Example

using System.IO;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
 
[CommandMethod("OpenDrawing", CommandFlags.Session)]
public static void OpenDrawing()
{
    string strFileName = "C:\\campus.dwg";
    DocumentCollection acDocMgr = Application.DocumentManager;

    if (File.Exists(strFileName))
    {
        acDocMgr.Open(strFileName, false);
    }
    else
    {
        acDocMgr.MdiActiveDocument.Editor.WriteMessage("File " + strFileName +
                                                       " does not exist.");
    }
}