礦業(yè)信息技術(shù)基礎(chǔ) 第五章采礦編程初步_第1頁
礦業(yè)信息技術(shù)基礎(chǔ) 第五章采礦編程初步_第2頁
礦業(yè)信息技術(shù)基礎(chǔ) 第五章采礦編程初步_第3頁
礦業(yè)信息技術(shù)基礎(chǔ) 第五章采礦編程初步_第4頁
礦業(yè)信息技術(shù)基礎(chǔ) 第五章采礦編程初步_第5頁
已閱讀5頁,還剩77頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

1、第五章 采礦編程初 步 多媒體教學(xué)課 件 VBAIDE VBAIDE 激活 VBA IDE。 VBA IDE 可以用來以交互方式編輯、運行和 調(diào)試程序。雖然 VBA IDE 只能在 AutoCAD 運行時被調(diào)用,但是可以獨立于 AutoCAD 應(yīng)用程序窗口對它進(jìn)行最小化、打開和關(guān)閉 操作。 VBAMAN VBAMAN 顯示 VBA 管理器,供用戶查看、創(chuàng)建、加 載、關(guān)閉、內(nèi)嵌和提取工程。 vMicrosoft VBA 是一個面向?qū)ο蟮木幊淌且粋€面向?qū)ο蟮木幊?環(huán)境,可提供類似環(huán)境,可提供類似 Visual Basic (VB) 的的 豐富開發(fā)功能。豐富開發(fā)功能。VBA 和和 VB 的主要差別的

2、主要差別 是是 VBA 和和 AutoCAD 在同一進(jìn)程空間中在同一進(jìn)程空間中 運行,提供的是具有運行,提供的是具有 AutoCAD 智能的智能的 、非??焖俚木幊汰h(huán)境。、非??焖俚木幊汰h(huán)境。 vVBA 也向其他支持也向其他支持 VBA 的應(yīng)用程序提的應(yīng)用程序提 供應(yīng)用程序集成。這就意味著供應(yīng)用程序集成。這就意味著 AutoCAD (使用其他應(yīng)用程序?qū)ο髱欤┛梢允侨纾ㄊ褂闷渌麘?yīng)用程序?qū)ο髱欤┛梢允侨?Microsoft Word 或或 Excel 之類的其他應(yīng)之類的其他應(yīng) 用程序的用程序的 Automation 控制程序??刂瞥绦?。 v在在 AutoCAD AutoCAD 中實現(xiàn)中實現(xiàn) VBA

3、 VBA 有四大優(yōu)點:有四大優(yōu)點: Visual Basic 編程環(huán)境易于學(xué)習(xí)和使用。 VBA 可與 AutoCAD 在同一進(jìn)程空間中運行 。這使程序執(zhí)行得非常快。 對話框的構(gòu)造快速而有效。這使開發(fā)人員可 以構(gòu)造原型應(yīng)用程序并迅速收到設(shè)計的反饋 。 工程可以是獨立的,也可以嵌入到圖形中。 這樣就為開發(fā)人員提供了非常靈活的方式來 發(fā)布他們的應(yīng)用程序。 vVBA 通過通過 AutoCAD ActiveX Automation 接口將消息發(fā)送到接口將消息發(fā)送到 AutoCAD。AutoCAD VBA 允許允許 VBA 環(huán)境與環(huán)境與 AutoCAD 同時運同時運 行,并通過行,并通過 ActiveX

4、Automation 接口對接口對 AutoCAD 進(jìn)行編程控制。進(jìn)行編程控制。AutoCAD、 ActiveX Automation 和和 VBA 的這種結(jié)合的這種結(jié)合 方式不僅為操作方式不僅為操作 AutoCAD 對象,而且為對象,而且為 向其他應(yīng)用程序發(fā)送或檢索數(shù)據(jù)提供了功向其他應(yīng)用程序發(fā)送或檢索數(shù)據(jù)提供了功 能極為強大的接口。能極為強大的接口。 v以下是以下是 AutoCAD 中定義中定義 ActiveX 和和 VBA 編程的三個基本元素。編程的三個基本元素。 v第一個是第一個是 AutoCADAutoCAD 本身,它擁有豐富的本身,它擁有豐富的 封裝了封裝了 AutoCAD Auto

5、CAD 圖元、數(shù)據(jù)和命令的對圖元、數(shù)據(jù)和命令的對 象集。因為象集。因為 AutoCAD AutoCAD 是一個設(shè)計為具有是一個設(shè)計為具有 多層接口的開放架構(gòu)應(yīng)用程序,因此熟多層接口的開放架構(gòu)應(yīng)用程序,因此熟 悉悉 AutoCAD AutoCAD 編程功能對于有效使用編程功能對于有效使用 VBA VBA 來說是非常必要的。如果使用過來說是非常必要的。如果使用過 AutoLISPAutoLISP 編程來控制編程來控制 AutoCADAutoCAD,就應(yīng),就應(yīng) 該已經(jīng)對該已經(jīng)對 AutoCAD AutoCAD 的機制有了一定的了的機制有了一定的了 解。然而,解。然而,VBA VBA 的基于對象的處理

6、方式的基于對象的處理方式 和和 AutoLISP AutoLISP 的方式卻很不一樣。的方式卻很不一樣。 v第二個元素是第二個元素是 AutoCAD ActiveX AutoCAD ActiveX AutomationAutomation 接口,它建立與接口,它建立與 AutoCAD AutoCAD 對象的消息傳遞(通信)。用對象的消息傳遞(通信)。用 VBA VBA 編程編程 需要對需要對 ActiveX Automation ActiveX Automation 有基本的有基本的 了解。關(guān)于了解。關(guān)于 AutoCAD ActiveX AutoCAD ActiveX Automation A

7、utomation 接口的說明,請參見接口的說明,請參見 ActiveX and VBA Reference GuideActiveX and VBA Reference Guide。即。即 使是有經(jīng)驗的使是有經(jīng)驗的 VB VB 編程人員也會發(fā)現(xiàn)要編程人員也會發(fā)現(xiàn)要 理解和開發(fā)理解和開發(fā) AutoCAD VBA AutoCAD VBA 應(yīng)用程序,應(yīng)用程序, AutoCAD ActiveX Automation AutoCAD ActiveX Automation 接口是非接口是非 常重要的。常重要的。 v第三個元素是 VBA 編程環(huán)境 (IDE),它 具有自己的對象組、關(guān)鍵詞和常量等, 能提供

8、程序流、控制、調(diào)試和執(zhí)行等功 能。 vAutoCAD VBA 包含 Microsoft VBA 擴展 聯(lián)機幫助,可以在 VBA IDE 中通過以下 方法訪問: 按鍵盤上的 F1 鍵 從 VBA IDE 菜單欄中選擇“幫助” 單擊 VBA IDE 工具欄上的“?”圖標(biāo) v使用使用 VBA VBA 管理器可以查看當(dāng)前管理器可以查看當(dāng)前 AutoCAD AutoCAD 任務(wù)中加載的所有任務(wù)中加載的所有 VBA VBA 工程。它是一種工程。它是一種 AutoCAD AutoCAD 工具,可讓用戶加載、卸載、工具,可讓用戶加載、卸載、 保存、創(chuàng)建、內(nèi)嵌和提取保存、創(chuàng)建、內(nèi)嵌和提取 VBA VBA 工程。

9、工程。 v打開打開 VBA VBA 管理器的步驟管理器的步驟 從“工具”菜單中選擇“宏”“VBA 管理器 ”。 在 AutoCAD 中調(diào)用 VBAMAN 命令。 v加載現(xiàn)有的工程加載現(xiàn)有的工程 v卸載工程卸載工程 v將工程嵌入到圖形中將工程嵌入到圖形中 v從圖形中提取工程從圖形中提取工程 v創(chuàng)建新工程創(chuàng)建新工程 v保存工程保存工程 加載現(xiàn)有的工程 v在在“VBA VBA 管理器管理器”中,使用中,使用“加載加載”選項打開選項打開“打開打開 VBA VBA 工程工程”對話框。對話框。 v在在“打開打開 VBA VBA 工程工程”對話框中,選擇要打開的工程文件對話框中,選擇要打開的工程文件 ?!癡

10、BA VBA 工程工程”對話框只能打開有效的對話框只能打開有效的 DVB DVB 文件。如果文件。如果 用戶試圖打開其他類型的文件,則會收到一條錯誤信息。用戶試圖打開其他類型的文件,則會收到一條錯誤信息。 v選擇選擇“打開打開”。 v用戶還可以使用以下任意方法來加載工程文件:用戶還可以使用以下任意方法來加載工程文件: 輸入 VBALOAD 命令,打開“打開 VBA 工程”對話框 。 將 DVB 文件從 Windows 資源管理器拖到 AutoCAD 窗 口中打開的圖形中。 卸載 VBA 工程的步驟 v在在 VBA VBA 管理器中選擇要卸載的工程。管理器中選擇要卸載的工程。 v選擇選擇“卸載卸

11、載”。 v或者,使用或者,使用 VBAUNLOAD VBAUNLOAD 命令,該命令會命令,該命令會 提示輸入要卸載的工程。提示輸入要卸載的工程。 創(chuàng)建新工程 v新工程會被創(chuàng)建為未保存的全局工程。新工程會被創(chuàng)建為未保存的全局工程。 創(chuàng)建完成后,可將它嵌入到圖形中或保創(chuàng)建完成后,可將它嵌入到圖形中或保 存到工程文件中。存到工程文件中。 v創(chuàng)建新創(chuàng)建新 VBA VBA 工程的步驟工程的步驟 打開 VBA 管理器。 選擇“新建”。 新工程將用默認(rèn)的名稱 ACADProject 創(chuàng)建 。要更改工程名,必須進(jìn)入 VBA IDE 中。 關(guān)于在 VBA IDE 中命名工程的詳細(xì)信息, 請參見 命名工程。 更

12、改工程名稱的步驟 v在在 VBA IDE VBA IDE 的的“工程工程”窗口中,選擇要窗口中,選擇要 更改的工程。更改的工程。 v在在“特性特性”窗口中編輯工程的窗口中編輯工程的 Name Name 特特 性。性。 更改工程的文件名的步驟 v在在 VBA IDE VBA IDE 中,從中,從“文件文件”菜單中選擇菜單中選擇 “保存保存”選項。選項。 v在在“另存為另存為”對話框中輸入工程文件的對話框中輸入工程文件的 新名稱和位置。新名稱和位置。 v查看工程信息查看工程信息 v定義工程中的組件定義工程中的組件 v輸入現(xiàn)有組件輸入現(xiàn)有組件 v編輯組件編輯組件 v命名工程命名工程 v保存工程保存工

13、程 v引用其他引用其他 VBA VBA 工程工程 v設(shè)置設(shè)置 VBA IDE VBA IDE 選項選項 查看工程信息 vVBA IDE VBA IDE 包含了包含了“工程工程”窗口窗口,其中顯示了所有已加載,其中顯示了所有已加載 的的 VBA VBA 工程列表。這個窗口還顯示工程中包含的代碼工程列表。這個窗口還顯示工程中包含的代碼 、類和窗體模塊,與工程關(guān)聯(lián)的文檔,從工程引用的所、類和窗體模塊,與工程關(guān)聯(lián)的文檔,從工程引用的所 有其他有其他 VBA VBA 工程,以及工程的實際位置(路徑)。工程,以及工程的實際位置(路徑)。 定義工程中的組件 v對象 v窗體 v標(biāo)準(zhǔn)模塊 v類模塊 v引用 v添

14、加新組件 v對象組件對象組件代表代表 VBA VBA 代碼將訪問的對象或代碼將訪問的對象或 文檔的類型。對于文檔的類型。對于 AutoCAD VBA AutoCAD VBA 工程,工程, 此對象代表當(dāng)前的此對象代表當(dāng)前的 AutoCAD AutoCAD 圖形。圖形。 v窗體組件窗體組件包含由用戶構(gòu)造的、用于工程的包含由用戶構(gòu)造的、用于工程的 自定義對話框。自定義對話框。 v代碼模塊組件代碼模塊組件包含用戶的常用過程和函數(shù)包含用戶的常用過程和函數(shù) 。標(biāo)準(zhǔn)模塊也稱為代碼模塊,或簡稱為模。標(biāo)準(zhǔn)模塊也稱為代碼模塊,或簡稱為模 塊。塊。 v類模塊組件類模塊組件包含用戶自己定義的所有對象包含用戶自己定義的

15、所有對象 (定義為類)。(定義為類)。 v引用組件引用組件包含對其他工程或庫的所有引用包含對其他工程或庫的所有引用 。 添加新組件 v添加新組件將在工程中創(chuàng)建空白組件。添加新組件將在工程中創(chuàng)建空白組件。 用戶可以將新的模塊、窗體和類模塊添用戶可以將新的模塊、窗體和類模塊添 加到自己的工程中。用戶需要自己更新加到自己的工程中。用戶需要自己更新 所有的組件特性(例如組件名稱),并所有的組件特性(例如組件名稱),并 填入適當(dāng)?shù)拇a。在命名新組件時,請?zhí)钊脒m當(dāng)?shù)拇a。在命名新組件時,請 注意其他開發(fā)人員在未來的應(yīng)用程序中注意其他開發(fā)人員在未來的應(yīng)用程序中 可能會使用您的組件。請可能會使用您的組件。請遵

16、循開發(fā)團(tuán)隊遵循開發(fā)團(tuán)隊 的命名慣例的命名慣例。 v向工程中添加新組件的步驟:向工程中添加新組件的步驟: 在 VBA IDE 的“工程”窗口中選擇要向其中添 加組件的工程。 從“插入”菜單中選擇“用戶窗體”、“模塊 ”或“類模塊”來向工程中添加新組件。 新組件將會被添加到工程中并顯示在“工程” 窗口中。 編輯組件 v在在 VBA IDE VBA IDE 中,可以編輯標(biāo)準(zhǔn)模塊、類中,可以編輯標(biāo)準(zhǔn)模塊、類 模塊和窗體。標(biāo)準(zhǔn)模塊和類模塊均在模塊和窗體。標(biāo)準(zhǔn)模塊和類模塊均在“ 代碼代碼”窗口中編輯窗口中編輯;窗體則在;窗體則在“用戶窗用戶窗 體體”窗口窗口中使用特殊的的工具箱進(jìn)行編中使用特殊的的工具箱進(jìn)

17、行編 輯。輯。 v由于可以打開的由于可以打開的“代碼代碼”窗口與擁有的窗口與擁有的 模塊數(shù)目一致,因此可以很容易地查看模塊數(shù)目一致,因此可以很容易地查看 不同窗體或模塊中的代碼,并在它們之不同窗體或模塊中的代碼,并在它們之 間互相復(fù)制和粘貼。間互相復(fù)制和粘貼。 v編輯工程組件的步驟編輯工程組件的步驟 在 VBA IDE 的“工程”窗口中,選擇要編 輯的組件。 在“工程”窗口中,選擇“查看代碼”按鈕 打開“代碼”窗口。 在“工程”窗口中,選擇“查看對象”按鈕 打開“用戶窗體”窗口和關(guān)聯(lián)的工具箱。 v使用“代碼”窗口 “代碼”窗口包含兩個下拉列表、一個拆分欄、 一個邊界指示欄、一個完全視圖圖標(biāo)和一

18、個過程 視圖圖標(biāo)。 v使用使用“用戶窗體用戶窗體”窗口窗口 通過“用戶窗體”窗口,可以在工程中創(chuàng)建自 定義對話框 自動擁有“最大化”、“最小化”和“關(guān)閉” 按鈕 添加控件 添加控件代碼 該控件關(guān)聯(lián)的代碼。 進(jìn)行初步練習(xí) v上面已經(jīng)學(xué)習(xí)了 AutoCAD VBA 編程的基本 知識,接下來讓我們練習(xí)創(chuàng)建簡單的“Hello World”。在此練習(xí)中,將會創(chuàng)建新的 AutoCAD 圖形,并向該圖形中添加一行文字 ,然后保存圖形,全部操作均通過 VBA 來完 成。 “Hello WorldHello World”例程例程 1.從 AutoCAD 命令行輸入以下命令來打開 VBA IDE: 1.命令:VB

19、AIDE 2.從 VBA IDE 的“查看”菜單中選擇“代碼”選項打 開“代碼”窗口。 3.從 VBA IDE 的“插入”菜單中選擇“過程”選項, 在工程中創(chuàng)建新過程。 4.當(dāng)提示輸入過程信息時,輸入名稱,例如 HelloWorld。請確保選定的“類型”是 Sub,選定的 “作用域”是 Public。 5.選擇“確定”。 6.6.在行在行 Public Sub HelloWorld() Public Sub HelloWorld() 和行和行 End Sub End Sub 之間輸之間輸 入以下代碼。入以下代碼。 ThisDrawing.Application.Documents.Add (用

20、于打開新圖形) 7.7.緊接代碼之后輸入以下代碼(用于創(chuàng)建字符串和定義其緊接代碼之后輸入以下代碼(用于創(chuàng)建字符串和定義其 插入位置)。插入位置)。 Dim insPoint(0 To 2) As Double 聲明插入點 Dim textHeight As Double 聲明文字高度 Dim textStr As String 聲明字符串 Dim textObj As AcadText 聲明文字對象 insPoint(0) = 2 設(shè)置插入點的 x 坐標(biāo) insPoint(1) = 4 設(shè)置插入點的 y 坐標(biāo) insPoint(2) = 0 設(shè)置插入點的 z 坐標(biāo) textHeight = 1

21、 強文字高度設(shè)置為 1.0 textStr = Hello World! 設(shè)置字符串 創(chuàng)建 Text 對象 Set textObj = ThisDrawing.ModelSpace.AddText _ (textStr, insPoint, textHeight) 8.8.緊接在步驟緊接在步驟 7 7 中的輸入代碼之后輸入以下中的輸入代碼之后輸入以下 代碼(用于保存圖形)。代碼(用于保存圖形)。 9.9.ThisDrawing.SaveAs(Hello.dwg) ThisDrawing.SaveAs(Hello.dwg) 從從 VBA VBA IDE IDE 的的“運行運行”菜單中選擇菜單中選

22、擇“運行子程序或運行子程序或 用戶窗體用戶窗體”選項來運行程序。選項來運行程序。 10.10.當(dāng)程序運行完時,當(dāng)程序運行完時,AutoCAD AutoCAD 應(yīng)用程序?qū)⒅糜趹?yīng)用程序?qū)⒅糜?最前。此時應(yīng)該可以在圖形中看到文字最前。此時應(yīng)該可以在圖形中看到文字 “Hello World!Hello World!”。圖形的名稱應(yīng)為。圖形的名稱應(yīng)為 Hello.dwgHello.dwg。 Add vRetVal = object.Add(Name) vObject:The collection to add the new object to. Dictionaries, DimStyles, Doc

23、uments, Groups; Layers, Layouts, Linetypes, PopupMenus, RegisteredApplications, SelectionSets, TextStyles, Toolbars, Views, Viewports vName:The name of the object to add to the collection. String; input-only; optional for the Documents collection. vRetVal:The newly added object. Dictionary, DimStyle

24、, Document, Group, Layer, Layout, Linetype, PopupMenu, RegisteredApp, SelectionSet, TextStyle, Toolbar, View, Viewport ADDLAYER u This example creates a new layer called New_Layer uDim layerObj As AcadLayer u Add the layer to the layers collection uSet layerObj = ThisDrawing.Layers.Add(New_Layer) u

25、Make the new layer the active layer for the drawing uThisDrawing.ActiveLayer = layerObj u Display the status of the new layer uMsgBox layerO input-only The name of the linetype to load. uFileName uString; input-only The name of the file the linetype is in. Linetype Example Sub Example_Linetyp

26、e() This example searches for the linetype DashDot. If it is not found, it is added from the acad.lin file. Then a line is created and changed to the DashDot linetype. Search the linetypes collection for the DashDot linetype. Dim entry As AcadLineType Dim found As Boolean found = False For Each entr

27、y In ThisDrawing.Linetypes If StrComp(, DASHDOT, 1) = 0 Then found = True Exit For End If Next If Not (found) Then ThisDrawing.Linetypes.Load DASHDOT, acad.lin Create the line Dim lineObj As AcadLine Dim startPoint(0 To 2) As Double Dim endPoint(0 To 2) As Double startPoint(0) = 1#: startP

28、oint(1) = 1#: startPoint(2) = 0# endPoint(0) = 4#: endPoint(1) = 4#: endPoint(2) = 0# Set lineObj = ThisDrawing.ModelSpace.AddLine(startPoint, endPoint) Change the linetype of the line lineObj.Linetype = DASHDOT ZoomAll End Sub LineWeight Property Specifies the lineweight of an individual object or

29、the default lineweight for the drawing. Signature object.Lineweight Object All Drawing objects; DatabasePreferences, Layer acLnWtByLayer acLnWtByBlock acLnWtByLwDefault acLnWt000 acLnWt005 acLnWt009 acLnWt013 acLnWt015 acLnWt018 acLnWt020 acLnWt025 acLnWt030 acLnWt035 acLnWt040 acLnWt050 acLnWt053 a

30、cLnWt060 acLnWt070 acLnWt080 acLnWt090 acLnWt100 acLnWt106 acLnWt120 acLnWt140 acLnWt158 acLnWt200 acLnWt211 AddText vCreates a single line of text. vRetVal = object.AddText(TextString, InsertionPoint, Height) vObject:The object or objects this method applies to. ModelSpace Collection, PaperSpace Co

31、llection, Block vTextString:The actual text to be displayed. String; input-only vInsertionPoint:The 3D WCS coordinates on the drawing where the text is placed. Variant (three-element array of doubles); input-only vHeight:The height of the text. Must be a positive number. Double; input-only vRetVal:T

32、he newly created Text object. Text object Sub Example_AddText() This example creates a text object in model space. Dim textObj As AcadText Dim textString As String Dim insertionPoint(0 To 2) As Double Dim height As Double Define the text object textString = Hello, World. insertionPoint(0) = 2: inser

33、tionPoint(1) = 2: insertionPoint(2) = 0 height = 0.5 Create the text object in model space Set textObj = ThisDrawing.ModelSpace.AddText(textString, insertionPoint, height) ZoomAll End Sub GetBoundingBox vGets two points of a box enclosing the specified object. vobject.GetBoundingBox MinPoint, MaxPoi

34、nt vObject:The object or objects this method applies to. All Drawing Objects, AttributeReference vMinPoint:The 3D WCS coordinates specifying the minimum point of the objects bounding box. Variant (three-element array of doubles); output-only vMaxPoint:The 3D WCS coordinates specifying the maximum po

35、int of the objects bounding box Variant (three-element array of doubles); output-only Sub Example_GetBoundingBox() This example creates a line in model space. It then finds the bounding box for the line and displays the corners of the box. Dim startPoint(0 To 2) As Double Dim endPoint(0 To 2) As Dou

36、ble Dim lineObj As AcadLine Create the Line object in model space startPoint(0) = 2#: startPoint(1) = 2#: startPoint(2) = 0# endPoint(0) = 4#: endPoint(1) = 4#: endPoint(2) = 0# Set lineObj = ThisDrawing.ModelSpace.AddLine(startPoint, endPoint) ZoomAll Dim minExt As Variant Dim maxExt As Variant Ret

37、urn the bounding box for the line and return the minimum and maximum extents of the box in the minExt and maxExt variables. lineObj.GetBoundingBox minExt, maxExt Print the min and max extents MsgBox The extents of the bounding box for the line are: input-only; optional vPrompt:The text used to promp

38、t the user for input. Variant (string); input-only; optional vRetVal:The 3D WCS coordinates of the point the AutoCAD user has selected. Variant (three-element array of doubles) Sub Example_GetPoint() This example returns a point entered by the user. Dim returnPnt As Variant Return a point using a pr

39、ompt returnPnt = ThisDrawing.Utility.GetPoint(, Enter a point: ) MsgBox The WCS of the point is: read-write vacAlignmentLeft acAlignmentCenter acAlignmentRight vacAlignmentAligned acAlignmentMiddle acAlignmentFit vacAlignmentTopLeft acAlignmentTopCenter acAlignmentTopRight vacAlignmentMiddleLeft acA

40、lignmentMiddleCenter vacAlignmentMiddleRight acAlignmentBottomLeft vacAlignmentBottomCenter acAlignmentBottomRight vRemarks vText aligned to acAlignmentLeft uses the InsertionPoint property to position the text. vText aligned to acAlignmentAligned, or acAlignmentFit uses both the InsertionPoint and

41、TextAlignmentPoint properties to position the text. vText aligned to any other position uses the TextAlignmentPoint property to position the text. SaveAS Method vSaves the document or menu group to a specified file. vobject.SaveAs FileName, FileType , SecurityParams vObject: Document, MenuGroup vFil

42、eName: String; input-only The full path and file name, or valid URL address, for the file. The active document or menu group takes on the new name. vAcSaveAsType enum; input-only; optional for Document objects vac2000_dwg AutoCAD 2000 DWG (*.dwg) vac2000_dxf AutoCAD 2000 DXF (*.dxf) vac2000_Template

43、 AutoCAD 2000 Drawing Template File (*.dwt) vac2004_dwg AutoCAD 2004 DWG (*.dwg) vac2004_dxf AutoCAD 2004 DXF (*.dxf) vac2004_Template AutoCAD 2004 Drawing Template File (*.dwt) vacNative A synonym for the latest drawing release. In this release, this value equals ac2004_dwg. vRemarks vThe default f

44、ile type for documents is ac2004_dwg. The following values are obsolete: acR14_DWG and acR14_DXF. vDocuments can be saved only as files with the extensions indicated above. To save a document in a different file type, use the Export method. SendCommand Method Sends a command string from a VB or VBA

45、application to the document for processing. Signature object.SendCommand(Command) Object Document The object or objects this method applies to. Command String; input-only The command to send to the document. Use a space or the ASCII carriage return character (vbCr) at the end of the command string t

46、o end the command; this is equivalent to pressing ENTER on the keyboard. This method processes any AutoCAD command-line function This method is generally synchronous. However, if the command sent with this method requires any user interaction (such as picking a point on the screen) then this method

47、will return as soon as the user input begins. The command will then continue to be processed asynchronously. When this method is called from an event handler it is processed asynchronously. You should never use this method to issue a command for which there is an ActiveX method available. For exampl

48、e, do not use SendCommand VBALOAD . Instead, use the LoadDVB method. 將命令發(fā)送到 AutoCAD 命令行 下面的樣例創(chuàng)建一個圓心為 (2, 2, 0)、半徑為 4 的圓,然后將圖形縮放至圖形中的所有幾何 圖形都可見。注意,字符串末尾有一個空格, 表示最后按 ENTER 鍵開始執(zhí)行命令。 Sub Ch3_SendACommandToAutoCAD() ThisDrawing.SendCommand _Circle 2,2,0 4 ThisDrawing.SendCommand _zoom a “ End Sub 將 VBA 代

49、碼轉(zhuǎn)換為 VB 要更新代碼樣例以在要更新代碼樣例以在 VB 中使用,必須先引用中使用,必須先引用 AutoCAD 類型庫。要在類型庫。要在 VB 中完成此操作,請從中完成此操作,請從“工工 程程”菜單中選擇菜單中選擇“引用引用”選項,啟動選項,啟動“引用引用”對話框?qū)υ捒?。在。在“引用引用”對話框中,選擇對話框中,選擇 AutoCAD 類型庫,然類型庫,然 后單擊后單擊“確定確定”。 下一步,將代碼樣例中所有的下一步,將代碼樣例中所有的 ThisDrawing 引用替換引用替換 為引用活動文檔的用戶指定的變量。要完成這項操作為引用活動文檔的用戶指定的變量。要完成這項操作 ,請為,請為 Aut

50、oCAD 應(yīng)用程序應(yīng)用程序 (acadApp) 和當(dāng)前的文檔和當(dāng)前的文檔 (acadDoc) 定義變量。然后,將應(yīng)用程序變量設(shè)置為定義變量。然后,將應(yīng)用程序變量設(shè)置為 當(dāng)前的當(dāng)前的 AutoCAD 應(yīng)用程序。應(yīng)用程序。 要顯示要顯示 AutoCAD AutoCAD 圖形窗口,需要將圖形窗口,需要將 AutoCAD AutoCAD 應(yīng)用程序的應(yīng)用程序的 Visible Visible 特性設(shè)置特性設(shè)置 為為 TRUETRUE。 如果如果 GetObject GetObject 創(chuàng)建了一個新的創(chuàng)建了一個新的 AutoCAD AutoCAD 實例(即調(diào)用實例(即調(diào)用 GetObject GetObj

51、ect 時時 AutoCAD AutoCAD 沒有運行),沒有將沒有運行),沒有將 Visible Visible 設(shè)置為設(shè)置為 TRUE TRUE 會導(dǎo)致會導(dǎo)致 AutoCAD AutoCAD 應(yīng)用程序應(yīng)用程序 不可見,甚至不可見,甚至 AutoCAD AutoCAD 不顯示在不顯示在 Windows Windows 任務(wù)欄上。任務(wù)欄上。 使用 VB 創(chuàng)建直線 Sub Ch2_AddLineVB()Sub Ch2_AddLineVB() On Error Resume Next On Error Resume Next 連接至連接至 AutoCAD AutoCAD 應(yīng)用程序應(yīng)用程序 Dim

52、acadApp As AcadApplication Dim acadApp As AcadApplication Set acadApp = GetObject _ (, AutoCAD.Application.16) Set acadApp = GetObject _ (, AutoCAD.Application.16) If Err Then Err.Clear If Err Then Err.Clear Set acadApp = CreateObject _ (AutoCAD.Application.16) Set acadApp = CreateObject _ (AutoCAD.

53、Application.16) If Err ThenIf Err Then MsgBox Err.Description MsgBox Err.Description Exit SubExit Sub End If End If End If End If 連接至連接至 AutoCAD AutoCAD 圖形圖形 Dim acadDoc As AcadDocument Set acadDoc = acadApp.ActiveDocument 創(chuàng)建直線的端點 Dim lineObj As AcadLine Dim startPoint(0 To 2) As Double Dim endPoint

54、(0 To 2) As Double startPoint(0) = 1 startPoint(1) = 1 startPoint(2) = 0 endPoint(0) = 5 endPoint(1) = 5 endPoint(2) = 0 在模型空間中創(chuàng)建 Line 對象 Set lineObj = acadDoc.ModelSpace.AddLine _ (startPoint, endPoint) ZoomAll acadApp.visible = True End Sub 創(chuàng)建自己的菜單項 vAdds a popup menu item to a popup menu. vSee Al

55、so | Example vSignature vRetVal = object.AddMenuItem(Index, Label, Macro) vObject vPopupMenu The object or objects this method applies to. vIndex:The index location in the popup menu where the item is to be added. The index must be either an integer or a string. Variant; input-only vIf an integer is

56、 supplied to specify a given location in the menu, the index must be between 0 and N-1, where N is the number of objects in the popup menu. The new item will be added immediately before the specified index location. To add the new menu item to the end of a menu, set the index to be greater than N. v

57、If a string is specified and the indexed item does not exist, then the new menu item is added at the end of the menu. vLabel:The label for the menu item. String; input-only vMacro:The name of the macro associated with this menu item. String; input-only vRetVal:The newly created popup menu item. Popu

58、pMenuItem object Sub Example_AddMenuItem() Dim currMenuGroup As acadMenuGroup Set currMenuGroup = ThisDrawing.Application.MenuGroups.Item(0) Create the new menu Dim newMenu As AcadPopupMenu Set newMenu = currMenuGroup.Menus.Add(TestMenu) Add a menu item to the new menu Dim newMenuItem As AcadPopupMe

59、nuItem Dim openMacro As String Assign the macro string the VB equivalent of ESC ESC _open openMacro = Chr(3) input-only The index location in the popup menu where the item is to be added. The index must be either an integer or a string. If an integer is specified, the index must be between 0 and N-1, where N is the number of objects in the toolbar. The new item will be added immediately before the specified index location. vName vString; input-only

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論