加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

02-03 控制AutoCAD环境(三) 创建、打开、保存和关闭图形

发布时间:2020-12-16 22:53:13 所属栏目:大数据 来源:网络整理
导读:Create,Open,Save,and Close Drawings 新建、打开、保存和关闭图形 The DocumentCollection,Document,and Database objects provide access to the AutoCAD ? file methods. DocumentCollection对象、Document对象和Database对象提供了访问AutoCAD ? 图像文

Create,Open,Save,and Close Drawings

新建、打开、保存和关闭图形

The DocumentCollection,Document,and Database objects provide access to the AutoCAD? file methods.

DocumentCollection对象、Document对象和Database对象提供了访问AutoCAD?图像文件的方法。

VBA/ActiveX Cross Reference VBA/ActiveX交叉参考

VBA/ActiveX Class

.NET API Class

Documents collection

DocumentCollection

Document

Document and Database

Document.Saved

System.Convert.ToInt16(Application.GetSystemVariable("DBMOD"))

Topics in this section本节主题

· Create and Open a Drawing 新建和打开图形文件

· Save and Close a Drawing 保存和关闭图形文件

· Work with No Documents Open 没有文件打开时

1、Create and Open a Drawing新建和打开图形文件

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方法打开一个已存在的图形文件。

Create a new drawing 创建新图形

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

本例使用Add方法基于图形样板文件acad.dwt创建一个新图形。

VB.NET

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

C#

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;

}

VBA/ActiveX Code Reference VBA/ActiveX交叉参考

Sub NewDrawing()

Dim strTemplatePath As String

strTemplatePath = "acad.dwt"

Dim docObj As AcadDocument

Set docObj = ThisDrawing.Application.Documents.Add(strTemplatePath)

End Sub

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.

本例使用Open方法打开一个已存在的图形。打开图形之前,代码会在试图打开文件前检查文件是否存在。

VB.NET

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

C#

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.");

}

}

VBA/ActiveX Code Reference

Sub OpenDrawing()

Dim dwgName As String

dwgName = "c:campus.dwg"

If Dir(dwgName) <> "" Then

ThisDrawing.Application.Documents.Open dwgName

Else

MsgBox "File " & dwgName & " does not exist."

End If

End Sub

2、Save and Close a Drawing保存和关闭图形文件

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方法保存数据库对象的内容。使用SaveAs方法时,我们可以指定是否要重命名数据库,以及是否要将硬盘上的图形备份重命名为备份文件(.bak文件,通过将参数bBakAndRename设置为True实现),我们还可以通过检查系统变量DWGTITLED确定数据库是否在使用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的值。

Close a Drawing 关闭图形文件

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对象的CloseAndDiscard方法或CloseAndSave方法用来关闭图形并忽略或保存所作的修改。我们还可以使用DocumentCollection的CloseAll方法关闭AutoCAD中打开的所有图形。

Save the active drawing 保存活动图形

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

'' If the drawing is using a default name (Drawing1,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;

usingAutodesk.AutoCAD.DatabaseServices;

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图形命名了吗?0-没呢

if (System.Convert.ToInt16(obj) == 0)

{

// If the drawing is using a default name (Drawing1,etc)

// then provide a new name

strDWGName = "c:MyDrawing.dwg";

}

// Save the active drawing

acDoc.Database.SaveAs(strDWGName,true,

acDoc.Database.SecurityParameters);

}

VBA/ActiveX Code Reference

Sub SaveActiveDrawing()

' Save the active drawing under a new name

ThisDrawing.SaveAs "MyDrawing.dwg"

End Sub

Determine if a drawing has unsaved changes 判断图形是否有尚未保存的修改

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:

本例检查看是否有尚未保存的修改并由用户核实是否已OK可以保存图形了(如果未OK,跳至结束)。如果OK了,使用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,_

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

//检查DBMOD的值,0表示没有未保存修改

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,

acDoc.Database.SecurityParameters);

}

}

}

VBA/ActiveX Code Reference

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

3、Work with No Documents Open没有文档打开时

AutoCAD always starts up with a new or existing document open. It is possible,however,to close all documents during the current session.

AutoCAD总是启动于新建一个文档或打开一个已存在的文档。也有可能,在当前运行过程中所有的文档都关闭了。

If you close all the documents in the AutoCAD user interface,you will notice a few changes to the application window. The Quick Access toolbar and application menu offer limited options. These limited options are related to creating and opening drawings,displaying the Sheet Set Manager,and recovering drawings. If the menu bar is displayed,simplified File,View,Window,and Help menus are also displayed. You will also notice that there is no command line.

如果在AutoCAD用户界面下的所有文档都关闭了,我们会注意到应用程序窗口发生了一些变化。快速访问工具条和应用程序菜单只提供了有限的选项,这些有限的选项是有关创建和打开图形、显示图纸集管理器及恢复图形的。如果显示菜单条的话,也只出现简化了的文件、视图、窗口和帮助等菜单项。我们还会注意到这时候没有命令行栏。

When working in zero document state,you can do the following:

工作在零文档状态时,我们可以进行下列操作:

· You can create a new or open an existing document 新建文档或打开已存在文档;

· You can customize the zero document states of the application menu and menu bar 自定义应用程序菜单和菜单条的零文档状态;

· You can shutdown AutoCAD 关掉AutoCAD;

To react to AutoCAD when it enters zero document state,you should use the DocumentDestroyed event. The DocumentDestroyed event is triggered when an open document is closed. The document count when the last document is closed will be 1. Use the Count property of the DocumentManager to determine the number of open documents at the time the DocumentDestroyed event is triggered.

要想在AutoCAD进入零文档状态时对它作出反应,应使用DocumentDestroyed事件。DocumentDestroyed事件在关闭文档时被触发。当关闭最后一个文档时,文档计数为1。可以使用DocumentManager的Count属性来确定DocumentDestroyed事件被触发时打开的文档的个数。

For more information on the using events in AutoCAD,see Use Events.

更多的关于AutoCAD中使用事件的信息,参见使用事件

Customize the application menu 自定义应用程序菜单

This example code uses the DocumentDestroyed event to monitor when the last drawing is closed and when zero document state is entered. Once zero document state is entered,the Opening event is registered with the application menu. When the application menu is clicked,the Opening event is triggered. During the Opening event,a new menu item is added to the application menu. The new menu item displays a message box.

本示例代码使用DocumentDestroyed事件监控最后一个文档何时关闭及何时进入零文档状态。一旦进入零文档状态,就将Opening事件注册到应用程序菜单。单击应用程序菜单就触发Opening事件。Opening事件中向应用程序菜单添加一个新菜单项,单击新菜单项将显示一个消息框。

Note: You must reference AdWindows.dll to your project in order to use the following example code. AdWindows.dll contains the namespace used to customize the application menu and can be found in the install folder of AutoCAD or part of the ObjectARX SDK. You will also need to reference WindowsBase which can be found on the .NET tab of the Add Reference dialog box.

注意:下例需要在工程中引用程序集AdWindows.dll。AdWindows.dll包含用来自定义应用程序菜单的命名空间,可以在AutoCAD的安装目录或ObjectARX SDK中找到。还需要引用程序集WindowsBase,可以在添加引用对话框的.NET选项卡上找到。(译者注:还需要引用程序集PresentationCore.dll)

VB.NET

Imports System.Windows.Input

Imports Autodesk.Windows

Imports Autodesk.AutoCAD.Runtime

Imports Autodesk.AutoCAD.ApplicationServices

'' Create the command handler for the custom application menu item

Public Class MyCommandHandler

Implements ICommand

Event CanExecuteChanged(ByVal sender As Object,ByVal e As EventArgs) _

Implements ICommand.CanExecuteChanged

Function CanExecute(ByVal parameter As Object) As Boolean _

Implements ICommand.CanExecute

Return True

End Function

Sub Execute(ByVal parameter As Object) Implements ICommand.Execute

Application.ShowAlertDialog("MyMenuItem has been clicked")

End Sub

End Class

Public Class Chapter4

''Global var for ZeroDocState

Dim acApMenuItem As ApplicationMenuItem = Nothing

<CommandMethod("AddZeroDocEvent")> _

Public Sub AddZeroDocEvent()

'' Get the DocumentCollection and register the DocumentDestroyed event

Dim acDocMgr As DocumentCollection = Application.DocumentManager

AddHandler acDocMgr.DocumentDestroyed,AddressOf docDestroyed

End Sub

Public Sub docDestroyed(ByVal obj As Object,_

ByVal acDocDesEvtArgs As DocumentDestroyedEventArgs)

'' Determine if the menu item already exists and the number of documents open

If Application.DocumentManager.Count = 1 And IsNothing(acApMenuItem) Then

'' Add the event handler to watch for when the application menu is opened

'' AdWindows.dll must be referenced to the project

AddHandler ComponentManager.ApplicationMenu.Opening,_

AddressOf ApplicationMenu_Opening

End If

End Sub

Public Sub ApplicationMenu_Opening(ByVal sender As Object,_

ByVal e As EventArgs)

'' Check to see if the custom menu item was added previously

If IsNothing(acApMenuItem) Then

'' Get the application menu component

Dim acApMenu As ApplicationMenu = ComponentManager.ApplicationMenu

'' Create a new application menu item

acApMenuItem = New ApplicationMenuItem()

acApMenuItem.Text = "MyMenuItem"

acApMenuItem.CommandHandler = New MyCommandHandler()

'' Append the new menu item

acApMenu.MenuContent.Items.Add(acApMenuItem)

'' Remove the application menu Opening event handler

RemoveHandler ComponentManager.ApplicationMenu.Opening,_

AddressOf ApplicationMenu_Opening

End If

End Sub

End Class

C#

using Autodesk.Windows;

using Autodesk.AutoCAD.Runtime;

using Autodesk.AutoCAD.ApplicationServices;

// Create the command handler for the custom application menu item

//为自定义应用程序菜单项创建命令处理器

public class MyCommandHandler : System.Windows.Input.ICommand

{

public bool CanExecute(object parameter)

{

return true;

}

public event EventHandler CanExecuteChanged;

public void Execute(object parameter)

{

Application.ShowAlertDialog("MyMenuItem has been clicked");

}

}

class Chapter4

{

//Global var for ZeroDocState全局变量

ApplicationMenuItem acApMenuItem = null;

[CommandMethod("AddZeroDocEvent")]

public void AddZeroDocEvent()

{

// Get the DocumentCollection and register the DocumentDestroyed event

//获取DocumentCollection并注册DocumentDestroyed事件

DocumentCollection acDocMgr = Application.DocumentManager;

acDocMgr.DocumentDestroyed +=

new DocumentDestroyedEventHandler(docDestroyed);

}

public void docDestroyed(object obj,

DocumentDestroyedEventArgs acDocDesEvtArgs)

{

// Determine if the menu item already exists and the number of documents open

//确定菜单项是否已存在及打开的文档数

if (Application.DocumentManager.Count == 1 && acApMenuItem == null)

{

// Add the event handler to watch for when the application menu is opened

//添加事件处理器来守候应用程序菜单

// AdWindows.dll must be referenced to the project

//记着添加引用啊~

ComponentManager.ApplicationMenu.Opening +=

new EventHandler<EventArgs>(ApplicationMenu_Opening);

}

}

void ApplicationMenu_Opening(object sender,EventArgs e)

{

// Check to see if the custom menu item was added previously

//检查菜单项,看看之前添加过吗

if (acApMenuItem == null)

{

// Get the application menu component获取应用程序菜单组件

ApplicationMenu acApMenu = ComponentManager.ApplicationMenu;

// Create a new application menu item创建新菜单项

acApMenuItem = new ApplicationMenuItem();

acApMenuItem.Text = "MyMenuItem";

acApMenuItem.CommandHandler = new MyCommandHandler();

// append the new menu item追加新菜单项

acApMenu.MenuContent.Items.Add(acApMenuItem);

// Remove the application menu Opening event handler

//移除事件处理器

ComponentManager.ApplicationMenu.Opening -=

new EventHandler<EventArgs>(ApplicationMenu_Opening);

}

}

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读