矿业信息技术基础第五章采矿编程初步.ppt

上传人:牧羊曲112 文档编号:6007875 上传时间:2023-09-14 格式:PPT 页数:82 大小:934.50KB
返回 下载 相关 举报
矿业信息技术基础第五章采矿编程初步.ppt_第1页
第1页 / 共82页
矿业信息技术基础第五章采矿编程初步.ppt_第2页
第2页 / 共82页
矿业信息技术基础第五章采矿编程初步.ppt_第3页
第3页 / 共82页
矿业信息技术基础第五章采矿编程初步.ppt_第4页
第4页 / 共82页
矿业信息技术基础第五章采矿编程初步.ppt_第5页
第5页 / 共82页
点击查看更多>>
资源描述

《矿业信息技术基础第五章采矿编程初步.ppt》由会员分享,可在线阅读,更多相关《矿业信息技术基础第五章采矿编程初步.ppt(82页珍藏版)》请在三一办公上搜索。

1、电子表格系统,Microsoft Excel 97,第 五 章,第五章 采矿编程初步,中国矿业大学 采矿工程专业,矿业信息技术基础,多媒体教学课件,VBAIDE 激活 VBA IDE。VBA IDE 可以用来以交互方式编辑、运行和调试程序。虽然 VBA IDE 只能在 AutoCAD 运行时被调用,但是可以独立于 AutoCAD 应用程序窗口对它进行最小化、打开和关闭操作。VBAMAN 显示 VBA 管理器,供用户查看、创建、加载、关闭、内嵌和提取工程。,5.1 AutoCAD VBA 命令,Microsoft VBA 是一个面向对象的编程环境,可提供类似 Visual Basic(VB)的丰

2、富开发功能。VBA 和 VB 的主要差别是 VBA 和 AutoCAD 在同一进程空间中运行,提供的是具有 AutoCAD 智能的、非常快速的编程环境。VBA 也向其他支持 VBA 的应用程序提供应用程序集成。这就意味着 AutoCAD(使用其他应用程序对象库)可以是如 Microsoft Word 或 Excel 之类的其他应用程序的 Automation 控制程序。,在 AutoCAD 中实现 VBA 有四大优点:Visual Basic 编程环境易于学习和使用。VBA 可与 AutoCAD 在同一进程空间中运行。这使程序执行得非常快。对话框的构造快速而有效。这使开发人员可以构造原型应用程

3、序并迅速收到设计的反馈。工程可以是独立的,也可以嵌入到图形中。这样就为开发人员提供了非常灵活的方式来发布他们的应用程序。,VBA 通过 AutoCAD ActiveX Automation 接口将消息发送到 AutoCAD。AutoCAD VBA 允许 VBA 环境与 AutoCAD 同时运行,并通过 ActiveX Automation 接口对 AutoCAD 进行编程控制。AutoCAD、ActiveX Automation 和 VBA 的这种结合方式不仅为操作 AutoCAD 对象,而且为向其他应用程序发送或检索数据提供了功能极为强大的接口。以下是 AutoCAD 中定义 ActiveX

4、 和 VBA 编程的三个基本元素。,5.2 VBA 在 AutoCAD 中的实现,第一个是 AutoCAD 本身,它拥有丰富的封装了 AutoCAD 图元、数据和命令的对象集。因为 AutoCAD 是一个设计为具有多层接口的开放架构应用程序,因此熟悉 AutoCAD 编程功能对于有效使用 VBA 来说是非常必要的。如果使用过 AutoLISP 编程来控制 AutoCAD,就应该已经对 AutoCAD 的机制有了一定的了解。然而,VBA 的基于对象的处理方式和 AutoLISP 的方式却很不一样。,第二个元素是 AutoCAD ActiveX Automation 接口,它建立与 AutoCAD

5、 对象的消息传递(通信)。用 VBA 编程需要对 ActiveX Automation 有基本的了解。关于 AutoCAD ActiveX Automation 接口的说明,请参见 ActiveX and VBA Reference Guide。即使是有经验的 VB 编程人员也会发现要理解和开发 AutoCAD VBA 应用程序,AutoCAD ActiveX Automation 接口是非常重要的。,第三个元素是 VBA 编程环境(IDE),它具有自己的对象组、关键词和常量等,能提供程序流、控制、调试和执行等功能。AutoCAD VBA 包含 Microsoft VBA 扩展联机帮助,可以在

6、 VBA IDE 中通过以下方法访问:按键盘上的 F1 键从 VBA IDE 菜单栏中选择“帮助”单击 VBA IDE 工具栏上的“?”图标,使用 VBA 管理器可以查看当前 AutoCAD 任务中加载的所有 VBA 工程。它是一种 AutoCAD 工具,可让用户加载、卸载、保存、创建、内嵌和提取 VBA 工程。打开 VBA 管理器的步骤 从“工具”菜单中选择“宏”“VBA 管理器”。在 AutoCAD 中调用 VBAMAN 命令。,5.3 使用 VBA 管理器组织工程,加载现有的工程 卸载工程 将工程嵌入到图形中 从图形中提取工程 创建新工程 保存工程,加载现有的工程,在“VBA 管理器”中

7、,使用“加载”选项打开“打开 VBA 工程”对话框。在“打开 VBA 工程”对话框中,选择要打开的工程文件。“VBA 工程”对话框只能打开有效的 DVB 文件。如果用户试图打开其他类型的文件,则会收到一条错误信息。选择“打开”。用户还可以使用以下任意方法来加载工程文件:输入 VBALOAD 命令,打开“打开 VBA 工程”对话框。将 DVB 文件从 Windows 资源管理器拖到 AutoCAD 窗口中打开的图形中。,卸载 VBA 工程的步骤,在 VBA 管理器中选择要卸载的工程。选择“卸载”。或者,使用 VBAUNLOAD 命令,该命令会提示输入要卸载的工程。,将工程嵌入到 AutoCAD

8、图形中的步骤,打开 VBA 管理器,并选择要嵌入的工程。选择“嵌入”。,创建新工程,新工程会被创建为未保存的全局工程。创建完成后,可将它嵌入到图形中或保存到工程文件中。创建新 VBA 工程的步骤 打开 VBA 管理器。选择“新建”。新工程将用默认的名称 ACADProject 创建。要更改工程名,必须进入 VBA IDE 中。关于在 VBA IDE 中命名工程的详细信息,请参见 命名工程。,更改工程名称的步骤,在 VBA IDE 的“工程”窗口中,选择要更改的工程。在“特性”窗口中编辑工程的 Name 特性。,更改工程的文件名的步骤,在 VBA IDE 中,从“文件”菜单中选择“保存”选项。在

9、“另存为”对话框中输入工程文件的新名称和位置。,使用 VBA IDE 编辑工程,将工程加载到 AutoCAD 中后,用户就可以使用 VBA 交互式开发环境来编辑该工程的代码、窗体和引用,而且还可以在 VBA IDE 中调试和运行工程。打开之后,使用 VBA IDE 可以访问所有已加载的工程。,查看工程信息 定义工程中的组件 输入现有组件 编辑组件 命名工程 保存工程 引用其他 VBA 工程 设置 VBA IDE 选项,5.4 使用 VBA IDE编辑工程,查看工程信息,VBA IDE 包含了“工程”窗口,其中显示了所有已加载的 VBA 工程列表。这个窗口还显示工程中包含的代码、类和窗体模块,与

10、工程关联的文档,从工程引用的所有其他 VBA 工程,以及工程的实际位置(路径)。,“工程”窗口有自己的工具栏,可用来打开工程的各个组件进行编辑。使用“查看代码”按钮可打开选定模块的代码。使用“查看对象”按钮可显示选定的对象,例如窗体。,定义工程中的组件,对象 窗体 标准模块 类模块 引用 添加新组件,对象组件代表 VBA 代码将访问的对象或文档的类型。对于 AutoCAD VBA 工程,此对象代表当前的 AutoCAD 图形。窗体组件包含由用户构造的、用于工程的自定义对话框。代码模块组件包含用户的常用过程和函数。标准模块也称为代码模块,或简称为模块。类模块组件包含用户自己定义的所有对象(定义为

11、类)。引用组件包含对其他工程或库的所有引用。,添加新组件,添加新组件将在工程中创建空白组件。用户可以将新的模块、窗体和类模块添加到自己的工程中。用户需要自己更新所有的组件特性(例如组件名称),并填入适当的代码。在命名新组件时,请注意其他开发人员在未来的应用程序中可能会使用您的组件。请遵循开发团队的命名惯例。,向工程中添加新组件的步骤:在 VBA IDE 的“工程”窗口中选择要向其中添加组件的工程。从“插入”菜单中选择“用户窗体”、“模块”或“类模块”来向工程中添加新组件。新组件将会被添加到工程中并显示在“工程”窗口中。,编辑组件,在 VBA IDE 中,可以编辑标准模块、类模块和窗体。标准模块

12、和类模块均在“代码”窗口中编辑;窗体则在“用户窗体”窗口中使用特殊的的工具箱进行编辑。由于可以打开的“代码”窗口与拥有的模块数目一致,因此可以很容易地查看不同窗体或模块中的代码,并在它们之间互相复制和粘贴。,编辑工程组件的步骤在 VBA IDE 的“工程”窗口中,选择要编辑的组件。在“工程”窗口中,选择“查看代码”按钮打开“代码”窗口。在“工程”窗口中,选择“查看对象”按钮打开“用户窗体”窗口和关联的工具箱。,使用“代码”窗口“代码”窗口包含两个下拉列表、一个拆分栏、一个边界指示栏、一个完全视图图标和一个过程视图图标。,使用“用户窗体”窗口通过“用户窗体”窗口,可以在工程中创建自定义对话框 自

13、动拥有“最大化”、“最小化”和“关闭”按钮添加控件添加控件代码,访问窗体关联代码的步骤请双击“窗体”窗口中的任意控件。“代码”窗口中将打开与该控件关联的代码。,进行初步练习,上面已经学习了 AutoCAD VBA 编程的基本知识,接下来让我们练习创建简单的“Hello World”。在此练习中,将会创建新的 AutoCAD 图形,并向该图形中添加一行文字,然后保存图形,全部操作均通过 VBA 来完成。,“Hello World”例程 从 AutoCAD 命令行输入以下命令来打开 VBA IDE:命令:VBAIDE 从 VBA IDE 的“查看”菜单中选择“代码”选项打开“代码”窗口。从 VBA

14、 IDE 的“插入”菜单中选择“过程”选项,在工程中创建新过程。当提示输入过程信息时,输入名称,例如 HelloWorld。请确保选定的“类型”是 Sub,选定的“作用域”是 Public。选择“确定”。,在行 Public Sub HelloWorld()和行 End Sub 之间输入以下代码。(用于打开新图形)紧接代码之后输入以下代码(用于创建字符串和定义其插入位置)。Dim insPoint(0 To 2)As Double 声明插入点Dim textHeight As Double 声明文字高度Dim textStr As String 声明字符串Dim textObj As Acad

15、Text 声明文字对象insPoint(0)=2 设置插入点的 x 坐标insPoint(1)=4 设置插入点的 y 坐标insPoint(2)=0 设置插入点的 z 坐标textHeight=1 强文字高度设置为 1.0textStr=Hello World!设置字符串创建 Text 对象Set textObj=_(textStr,insPoint,textHeight),紧接在步骤 7 中的输入代码之后输入以下代码(用于保存图形)。ThisDrawing.SaveAs(Hello.dwg)从 VBA IDE 的“运行”菜单中选择“运行子程序或用户窗体”选项来运行程序。当程序运行完时,Aut

16、oCAD 应用程序将置于最前。此时应该可以在图形中看到文字“Hello World!”。图形的名称应为 Hello.dwg。,AutoCAD VBA进阶,Add,RetVal=object.Add(Name)Object:The collection to add the new object to.Dictionaries,DimStyles,Documents,Groups;Layers,Layouts,Linetypes,PopupMenus,RegisteredApplications,SelectionSets,TextStyles,Toolbars,Views,ViewportsN

17、ame:The name of the object to add to the collection.String;input-only;optional for the Documents collection.RetVal:The newly added object.Dictionary,DimStyle,Document,Group,Layer,Layout,Linetype,PopupMenu,RegisteredApp,SelectionSet,TextStyle,Toolbar,View,Viewport,ADDLAYER,This example creates a new

18、layer called New_Layer Dim layerObj As AcadLayer Add the layer to the layers collection Set layerObj=ThisDrawing.Layers.Add(New_Layer)Make the new layer the active layer for the drawingThisDrawing.ActiveLayer=layerObj Display the status of the new layer MsgBox layerObj.name&has been added.&vbCrLf&_

19、LayerOn Status:&layerObj.LayerOn&vbCrLf&_ Freeze Status:&layerObj.Freeze&vbCrLf&_ Lock Status:&layerObj.Lock&vbCrLf&_ Color:&layerObj.Color,Add Example,ADDTEXTSTYLE,Create a textstyle named New_Textstyle in current drawing Dim txtStyleObj As AcadTextStyle Add the textstyle to the textstyles collecti

20、on Set txtStyleObj=ThisDrawing.TextStyles.Add(New_Textstyle)MsgBox txtStyleObj.name&has been added.&vbCrLf&_ Height:&txtStyleObj.height&vbCrLf&_ Width:&txtStyleObj.width,Add Example Return,创建和修改文字样式,新的文字会从当前的文字样式继承高度、宽度比例、倾斜角度和文字生成等特性。要创建文字样式,请使用 Add 方法创建新的 TextStyle 对象并将其添加到 TextStyles 集合中。Add 方法需要

21、输入 TextStyle 名称。文字样式创建之后,用户不能通过 AutoCAD ActiveX Automation 修改其名称。,样式名可以包含字母、数字和特殊字符,例如美元符($)、下划线(_)和连字符(-)。AutoCAD 将字符转换为大写字符。如果用户不输入样式名,AutoCAD 自动将其命名为 Stylen,其中 n 是从 1 开始的数字。每个新样式会加 1 来显示。用户可以通过修改 TextStyle 对象的特性来修改现有的样式,也可以更新该样式的现有文字以反映更改。,请使用以下特性来修改 TextStyle 对象:FontFile 指定与字体(字符样式)关联的文件。BigFont

22、File 指定适用于非 ASCII 字符集的特殊形定义文件。Height 指定字符高度。Width 指定字符的扩展或压缩,ObliqueAngle 指定字符的倾斜度。TextGenerationFlag 指定反向文字、倒置文字或两者。如果用户修改现有样式的字体或方向,所有使用该样式的文字均被修改为使用新的字体或方向。更改文字的高度、宽度比例和倾斜角不会改变现有的文字,但会应用到以后创建的文字对象上。注意 必须调用 Regen 或 Update 方法,才能看到对上述特性所做更改的效果。,Load Method,object.Load LineTypeName,FileName ObjectLin

23、etypesThe object or objects this method applies to.LineTypeNameString;input-onlyThe name of the linetype to load.FileNameString;input-onlyThe name of the file the linetype is in.,Linetype Example,Sub Example_Linetype()This example searches for the linetype DashDot.If it is not found,it is added from

24、 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 entry In ThisDrawing.Linetypes If StrComp(entry.name,DASHDOT,1)=0 Then found=True Exit For End If

25、 Next If Not(found)Then 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#:startPoint(1)=1#:startPoint(2)=0#endPoint(0)=4#:endPoint(1)=4#:endPoint(2)=0#Set lineObj=ThisDrawing.ModelSpace.AddLine(startPoint,endPoint)

26、Change the linetype of the line lineObj.Linetype=DASHDOT ZoomAll End Sub,LineWeight Property,Specifies the lineweight of an individual object or the default lineweight for the drawing.Signatureobject.Lineweight ObjectAll Drawing objects;DatabasePreferences,Layer,acLnWtByLayer acLnWtByBlock acLnWtByL

27、wDefault acLnWt000 acLnWt005 acLnWt009 acLnWt013 acLnWt015 acLnWt018 acLnWt020 acLnWt025 acLnWt030 acLnWt035 acLnWt040 acLnWt050 acLnWt053 acLnWt060 acLnWt070 acLnWt080 acLnWt090 acLnWt100 acLnWt106 acLnWt120 acLnWt140 acLnWt158 acLnWt200 acLnWt211,AddText,Creates a single line of text.RetVal=object

28、.AddText(TextString,InsertionPoint,Height)Object:The object or objects this method applies to.ModelSpace Collection,PaperSpace Collection,BlockTextString:The actual text to be displayed.String;input-onlyInsertionPoint:The 3D WCS coordinates on the drawing where the text is placed.Variant(three-eleme

29、nt array of doubles);input-onlyHeight:The height of the text.Must be a positive number.Double;input-onlyRetVal:The 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)A

30、s Double Dim height As Double Define the text object textString=Hello,World.insertionPoint(0)=2:insertionPoint(1)=2:insertionPoint(2)=0height=0.5 Create the text object in model space Set textObj=ThisDrawing.ModelSpace.AddText(textString,insertionPoint,height)ZoomAll End Sub,GetBoundingBox,Gets two

31、points of a box enclosing the specified object.object.GetBoundingBox MinPoint,MaxPoint Object:The object or objects this method applies to.All Drawing Objects,AttributeReferenceMinPoint:The 3D WCS coordinates specifying the minimum point of the objects bounding box.Variant(three-element array of dou

32、bles);output-onlyMaxPoint:The 3D WCS coordinates specifying the maximum point of the objects bounding boxVariant(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

33、the box.Dim startPoint(0 To 2)As Double Dim endPoint(0 To 2)As Double 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

34、 minExt As Variant Dim maxExt As Variant Return 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:&vbCrLf _&

35、Min Extent:&minExt(0)&,&minExt(1)&,&minExt(2)_&vbCrLf&Max Extent:&maxExt(0)&,&maxExt(1)&,&maxExt(2),vbInformation,GetBoundingBox Example End Sub,GetPoint,Gets the point selected in AutoCAD.RetVal=GetPoint(Point,Prompt)Object:The object or objects this method applies to.UtilityPoint:The 3D WCS coordi

36、nates specifying the relative base point.Variant(three-element array of doubles);input-only;optionalPrompt:The text used to prompt the user for input.Variant(string);input-only;optionalRetVal:The 3D WCS coordinates of the point the AutoCAD user has selected.Variant(three-element array of doubles),Su

37、b Example_GetPoint()This example returns a point entered by the user.Dim returnPnt As Variant Return a point using a prompt returnPnt=(,Enter a point:)MsgBox The WCS of the point is:&returnPnt(0)&,&returnPnt(1)&,&returnPnt(2)&vbCrLf&_(Enter the next value without prompting.),GetPoint Example Return

38、a point,no prompt returnPnt=MsgBox The WCS of the point is:&returnPnt(0)&,&returnPnt(1)&,&returnPnt(2),GetPoint Example,Return a point using a base point and a prompt Dim basePnt(0 To 2)As Double basePnt(0)=2#:basePnt(1)=2#:basePnt(2)=0#returnPnt=ThisDrawing.Utility.GetPoint(basePnt,Enter a point:)M

39、sgBox The WCS of the point is:&returnPnt(0)&,&returnPnt(1)&,&returnPnt(2)Create a line from the base point and the last point entered Dim lineObj As AcadLine Set lineObj=ThisDrawing.ModelSpace.AddLine(basePnt,returnPnt)ZoomAll End Sub,用VBA实现如下标注形式(采矿工程毕业设计绘图标准),思路:先绘制文字 AddText利用GetBoundingBox获得文字的m

40、inExt,maxExt根据minExt,maxExt确定线段的startPoint根据minExt,maxExt确定线段的endPoint在文字下方划线,Dim textObj As AcadTextDim textString As StringDim startPoint(0 To 2)As DoubleDim endPoint(0 To 2)As DoubleDim textHeight As DoubleDim returnPnt1 As Variant 申明端点坐标Me.HideWith ThisDrawing.Utility returnPnt1=.GetPoint(,请输入标注

41、汉字的位置:)获取文本标注起点坐标End With,Define the text objecttextString=TextBox1.TextDim width As Double Create the text object in model spacetextHeight=3Set textObj=ThisDrawing.ModelSpace.AddText(textString,returnPnt1,textHeight)textObj.UpdateDim minExt As VariantDim maxExt As Variant 获得文字的右下角坐标(minExt)和左上角坐标(m

42、axExt)textObj.GetBoundingBox minExt,maxExt,在文字下面划线startPoint(0)=minExt(0)1startPoint(1)=minExt(1)1startPoint(2)=0endPoint(0)=maxExt(0)+1endPoint(1)=minExt(1)1endPoint(2)=0Set lineObj=ThisDrawing.ModelSpace.AddLine(startPoint,endPoint)Set lineObj=ThisDrawing.ModelSpace.AddLine(minExt,maxExt),用VBA实现如下

43、图框绘制(采矿工程毕业设计绘图标准:A0图纸),Alignment,Object:The object or objects this property applies to.Attribute,AttributeRef,TextAlignment:acAlignment enum;read-writeacAlignmentLeft acAlignmentCenter acAlignmentRight acAlignmentAligned acAlignmentMiddle acAlignmentFit acAlignmentTopLeft acAlignmentTopCenter acAli

44、gnmentTopRight acAlignmentMiddleLeft acAlignmentMiddleCenter acAlignmentMiddleRight acAlignmentBottomLeft acAlignmentBottomCenter acAlignmentBottomRight,Remarks Text aligned to acAlignmentLeft uses the InsertionPoint property to position the text.Text aligned to acAlignmentAligned,or acAlignmentFit

45、uses both the InsertionPoint and TextAlignmentPoint properties to position the text.Text aligned to any other position uses the TextAlignmentPoint property to position the text.,AutoCAD VBA高阶,SaveAS Method,Saves the document or menu group to a specified file.object.SaveAs FileName,FileType,SecurityP

46、arams Object:Document,MenuGroupFileName:String;input-onlyThe full path and file name,or valid URL address,for the file.The active document or menu group takes on the new name.,AcSaveAsType enum;input-only;optional for Document objectsac2000_dwg AutoCAD 2000 DWG(*.dwg)ac2000_dxf AutoCAD 2000 DXF(*.dx

47、f)ac2000_Template AutoCAD 2000 Drawing Template File(*.dwt)ac2004_dwg AutoCAD 2004 DWG(*.dwg)ac2004_dxf AutoCAD 2004 DXF(*.dxf)ac2004_Template AutoCAD 2004 Drawing Template File(*.dwt)acNative A synonym for the latest drawing release.In this release,this value equals ac2004_dwg.,Remarks The default

48、file type for documents is ac2004_dwg.The following values are obsolete:acR14_DWG and acR14_DXF.Documents 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 appli

49、cation to the document for processing.Signature object.SendCommand(Command)ObjectDocumentThe object or objects this method applies to.CommandString;input-onlyThe command to send to the document.Use a space or the ASCII carriage return character(vbCr)at the end of the command string to end the comman

50、d;this is equivalent to pressing ENTER on the keyboard.,This method processes any AutoCAD command-line functionThis 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 will return as soon as

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号