To create a new drawing or open an existing drawing, use the methods of the DocumentCollection object. The Add method creates a new drawing file based on a drawing template and adds that drawing to the DocumentCollection. The Open method opens an existing drawing file.
要创建新图形或打开现有图形,请使用 DocumentCollection 对象的方法。Add 方法用于根据一个图形模板创建新图形并将此图形添加到 DocumentCollection 中。Open 方法用于打开现有图形文件。
This example uses the Add method to create a new drawing based on the acad.dwt drawing template file.
本例使用 Add 方法根据 acad.dwt 图形模板文件创建新图形。
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Runtime
<CommandMethod("NewDrawing", CommandFlags.Session)> _
Public Sub NewDrawing()
'' 指定要使用的模板文件,如果模板不存在,就使用默认设置 Specify the template to use, if the template is not found
'' the default settings are used.
Dim strTemplatePath As String = "acad.dwt"
Dim acDocMgr As DocumentCollection = Application.DocumentManager
Dim acDoc As Document = acDocMgr.Add(strTemplatePath)
acDocMgr.MdiActiveDocument = acDoc
End Sub
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;
}
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.
本例使用 Open 方法打开现有图形。在打开图形前,代码将检查文件是否存在。
Imports System.IO
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Runtime
<CommandMethod("OpenDrawing", CommandFlags.Session)> _
Public Sub OpenDrawing()
Dim strFileName As String = "C:\campus.dwg"
Dim acDocMgr As DocumentCollection = Application.DocumentManager
If (File.Exists(strFileName)) Then
acDocMgr.Open(strFileName, False)
Else
acDocMgr.MdiActiveDocument.Editor.WriteMessage("File " & strFileName & _
" does not exist.")
End If
End Sub
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.");
}
}