Database オブジェクトの内容を保存するには、Database オブジェクトの SaveAs メソッドを使用します。SaveAs を使用するときは、bBakAndRename パラメータに True を指定することにより、データベースの名前を変更する必要があるかどうか、およびディスク上の図面のバックアップの名前をバックアップ ファイルに変更する必要があるかどうかを指定できます。システム変数 DWGTITLED の値を調べることにより、データベースが既定の名前である Drawing1、Drawing2 などを使用しているかどうかを確認できます。DWGTITLED が 0 である場合、図面の名前は変更されていません。
アクティブな図面に保存されていない変更がないかどうか調べたい場合があります。AutoCAD セッションを終える前に、または新規図面の開始前に、このような確認を行うことをお勧めします。図面ファイルが変更されているかどうかを確認するには、システム変数 DBMOD の値を調べる必要があります。
開いている図面を閉じて、変更を破棄または保存するには、Document オブジェクトの CloseAndDiscard または CloseAndSave メソッドが使用されます。AutoCAD で開いているすべての図面を閉じるには、DocumentCollectionExtension の CloseAll メソッドを使用できます。
この例では、アクティブな図面が現在保存されていないか、現在の名前のままの場合、その図面を c:¥MyDrawing.dwg に保存します。
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
'' 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
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)
{
// 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);
}
Sub SaveActiveDrawing()
' Save the active drawing under a new name
ThisDrawing.SaveAs "MyDrawing.dwg"
End Sub
以下の例は、未保存の変更がないかどうかをテストし、ユーザに図面を保存してよいかどうかを確認します(OK でなければ、最後にスキップします)。OK ならば、次のように SaveAs メソッドを使って現在の図面を保存します。
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Runtime
<CommandMethod("DrawingSaved")> _
Public Sub DrawingSaved()
Dim obj As Object = Application.GetSystemVariable("DBMOD")
'' 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
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);
}
}
}
Sub DrawingSaved()
If Not (ThisDrawing.Saved) Then
If MsgBox("Do you wish to save this drawing?", _
vbYesNo) = vbYes Then
ThisDrawing.Save
End If
End If
End Sub