保存和关闭图形
 
 

Use the SaveAs method of the Database object to save the contents of a Database object. When using the SaveAs method, you can specify if the database should be renamed and if a backup of the drawing on disk should be renamed to a backup file by providing True for the bBakAndRename parameter. You can determine if a database is using a default name of Drawing1, Drawing2, etc by checking the value of the DWGTITLED system variable. If DWGTITLED is 0, the drawing has not been renamed.

使用 Database 对象的SaveAs 方法保存 Database 对象的内容。在使用 SaveAs 方法时,用户可以指定 Database 是否应该重命名,如果 bBakAndRename 参数指定为 True,在磁盘上的图形的备份将要重命名。通过检查 DWGTITLED 系统变量的值,用户可以确定 Database 是否使用类似 Drawing1,Drawing2 之类的默认名字。如果 DWGTITLED 为0,图形将不用重命名。

Occasionally, you will want to check if the active drawing has any unsaved changes. It is a good idea to do this before you quit the AutoCAD session or start a new drawing. To check to see if a drawing file has been changed, you need to check the value of the DBMOD system variable.

有时候用户希望检查活动图形中是否存在未保存的更改。在退出 AutoCAD 任务或创建新图形之前,应当进行这样的检查。若要检查图形文件是否已经被修改,用户需要检查 DBMOD 系统变量的值。

关闭图形

The CloseAndDiscard or CloseAndSave methods of the Document object are used to close an open drawing and discard or save any changes made. You can use the CloseAll method of the DocumentCollection to close all open drawings in the AutoCAD.

Document 对象的 CloseAndDiscardCloseAndSave 方法用于关闭打开的图形并放弃或保存任何未保存的修改。也可以使用 DocumentCollection 的 CloseAll 方法关闭 AutoCAD 中所有打开的图形。

保存活动图形

This example saves the active drawing to "c:\MyDrawing.dwg" if it is currently not saved or under its current name.

本例中,如果活动图形未保存就保存到"c:\MyDrawing.dwg"否则使用它的当前名字保存。

VB.NET

Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.Runtime
 
<CommandMethod("SaveActiveDrawing")> _
Public Sub SaveActiveDrawing()
  Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
  Dim strDWGName As String = acDoc.Name
 
  Dim obj As Object = Application.GetSystemVariable("DWGTITLED")
 
  '' 检查图形是否有名字  Check to see if the drawing has been named
  If System.Convert.ToInt16(obj) = 0 Then
      '' 如果图形使用默认名字(Drawing1,Drawing2等等),那么指定一个新的名字  If the drawing is using a default name (Drawing1, Drawing2, etc)
      '' then provide a new name
      strDWGName = "c:\MyDrawing.dwg"
  End If
 
  '' 保存活动图形  Save the active drawing
  acDoc.Database.SaveAs(strDWGName, True, DwgVersion.Current, _
                        acDoc.Database.SecurityParameters)
End Sub

C#

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Runtime;
 
[CommandMethod("SaveActiveDrawing")]
public static void SaveActiveDrawing()
{
  Document acDoc = Application.DocumentManager.MdiActiveDocument;
  string strDWGName = acDoc.Name;
 
  object obj = Application.GetSystemVariable("DWGTITLED");
 
  // 检查图形是否有名字  Check to see if the drawing has been named
  if (System.Convert.ToInt16(obj) == 0)
  {
      // 如果图形使用默认名字(Drawing1,Drawing2等等),那么指定一个新的名字  If the drawing is using a default name (Drawing1, Drawing2, etc)
      // then provide a new name
      strDWGName = "c:\\MyDrawing.dwg";
  }
 
  // 保存活动图形  Save the active drawing
  acDoc.Database.SaveAs(strDWGName, true, DwgVersion.Current,
                        acDoc.Database.SecurityParameters);
}
VBA/ActiveX 代码参考

确定图形是否有未保存的更改

This example checks to see if there are unsaved changes and verifies with the user that it is OK to save the drawing (if it is not OK, skip to the end). If OK, use the SaveAs method to save the current drawing, as shown here:

本例检查是否存在未保存的更改,并验证用户是否选择“确定”以保存图形(如果没有选择“确定”,跳至结尾处)。如果用户选择“确定”,将使用 Saveas 方法保存当前图形,如下所示:

VB.NET

Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Runtime
 
<CommandMethod("DrawingSaved")> _
Public Sub DrawingSaved()
  Dim obj As Object = Application.GetSystemVariable("DBMOD")
 
  '' 检查 DBMOD 的值,如果为0那么图形就没有修改  Check the value of DBMOD, if 0 then the drawing has not been changed
  If Not (System.Convert.ToInt16(obj) = 0) Then
      If MsgBox("Do you wish to save this drawing?", _
                MsgBoxStyle.YesNo, _
                "Save Drawing") = MsgBoxResult.Yes Then
          Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
          acDoc.Database.SaveAs(acDoc.Name, True, DwgVersion.Current, _
                                acDoc.Database.SecurityParameters)
      End If
  End If
End Sub

C#

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
 
[CommandMethod("DrawingSaved")]
public static void DrawingSaved()
{
  object obj = Application.GetSystemVariable("DBMOD");
 
  // Check the value of DBMOD, if 0 then the drawing has no unsaved changes
  if (System.Convert.ToInt16(obj) != 0)
  {
      if (System.Windows.Forms.MessageBox.Show("Do you wish to save this drawing?",
                                "Save Drawing",
                                System.Windows.Forms.MessageBoxButtons.YesNo,
                                System.Windows.Forms.MessageBoxIcon.Question)
                                == System.Windows.Forms.DialogResult.Yes)
      {
          Document acDoc = Application.DocumentManager.MdiActiveDocument;
          acDoc.Database.SaveAs(acDoc.Name, true, DwgVersion.Current,
                                acDoc.Database.SecurityParameters);
      }
  }
}
VBA/ActiveX 代码参考