CAD开发者社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

ObjectARX 开发指南

保存并关闭图形 (.NET)

2023-1-1 15:57| 发布者: admin| 查看: 430| 评论: 0|来自: AutoCAD

使用对象的方法保存对象的内容。使用该方法时,可以通过提供参数来指定是否应重命名数据库以及是否应将磁盘上图形的备份重命名为备份文件。您可以通过检查 DWGTITLED 系统变量的值来确定数据库是否正在使用 Drawing1、Drawing2 等默认名称。如果 DWGTITLED 为 0,则图形尚未重命名。SaveAsDatabaseDatabaseSaveAsTruebBakAndRename

有时,您需要检查活动图形是否有任何未保存的更改。最好在退出 AutoCAD 会话或启动新图形之前执行此操作。若要检查图形文件是否已更改,需要检查 DBMOD 系统变量的值。

关闭图形

对象的理论方法用于关闭打开的图形并放弃或保存所做的任何更改。您可以使用的方法关闭AutoCAD中所有打开的图形。CloseAndDiscardCloseAndSaveDocumentCloseAllDocumentCollectionExtension

保存活动图形

如果活动绘图当前未保存或当前名称,则本示例将活动绘图保存到“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
        '' 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)
    {
        // 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 代码参考

Sub SaveActiveDrawing()
    ' Save the active drawing under a new name
    ThisDrawing.SaveAs "MyDrawing.dwg"
End Sub

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

本示例检查是否存在未保存的更改,并与用户验证是否可以保存图形(如果保存不正常,请跳到最后)。如果确定,请使用该方法保存当前图形,如下所示: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")
 
    '' 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 代码参考

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

路过

雷人

握手

鲜花

鸡蛋

最新评论

QQ|Archiver|CAD开发者社区 ( 苏ICP备2022047690号-1 )

GMT+8, 2024-5-19 13:58

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

返回顶部