矿业信息技术基础第五章采矿编程初步_第1页
矿业信息技术基础第五章采矿编程初步_第2页
矿业信息技术基础第五章采矿编程初步_第3页
矿业信息技术基础第五章采矿编程初步_第4页
矿业信息技术基础第五章采矿编程初步_第5页
已阅读5页,还剩77页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

电子表格系统,MicrosoftExcel97,第五章,第五章采矿编程初步,中国矿业大学采矿工程专业,矿业信息技术基础,多媒体教学课件,VBAIDE激活VBAIDE。VBAIDE可以用来以交互方式编辑、运行和调试程序。虽然VBAIDE只能在AutoCAD运行时被调用,但是可以独立于AutoCAD应用程序窗口对它进行最小化、打开和关闭操作。VBAMAN显示VBA管理器,供用户查看、创建、加载、关闭、内嵌和提取工程。,5.1AutoCADVBA命令,MicrosoftVBA是一个面向对象的编程环境,可提供类似VisualBasic(VB)的丰富开发功能。VBA和VB的主要差别是VBA和AutoCAD在同一进程空间中运行,提供的是具有AutoCAD智能的、非常快速的编程环境。VBA也向其他支持VBA的应用程序提供应用程序集成。这就意味着AutoCAD(使用其他应用程序对象库)可以是如MicrosoftWord或Excel之类的其他应用程序的Automation控制程序。,在AutoCAD中实现VBA有四大优点:VisualBasic编程环境易于学习和使用。VBA可与AutoCAD在同一进程空间中运行。这使程序执行得非常快。对话框的构造快速而有效。这使开发人员可以构造原型应用程序并迅速收到设计的反馈。工程可以是独立的,也可以嵌入到图形中。这样就为开发人员提供了非常灵活的方式来发布他们的应用程序。,VBA通过AutoCADActiveXAutomation接口将消息发送到AutoCAD。AutoCADVBA允许VBA环境与AutoCAD同时运行,并通过ActiveXAutomation接口对AutoCAD进行编程控制。AutoCAD、ActiveXAutomation和VBA的这种结合方式不仅为操作AutoCAD对象,而且为向其他应用程序发送或检索数据提供了功能极为强大的接口。以下是AutoCAD中定义ActiveX和VBA编程的三个基本元素。,5.2VBA在AutoCAD中的实现,第一个是AutoCAD本身,它拥有丰富的封装了AutoCAD图元、数据和命令的对象集。因为AutoCAD是一个设计为具有多层接口的开放架构应用程序,因此熟悉AutoCAD编程功能对于有效使用VBA来说是非常必要的。如果使用过AutoLISP编程来控制AutoCAD,就应该已经对AutoCAD的机制有了一定的了解。然而,VBA的基于对象的处理方式和AutoLISP的方式却很不一样。,第二个元素是AutoCADActiveXAutomation接口,它建立与AutoCAD对象的消息传递(通信)。用VBA编程需要对ActiveXAutomation有基本的了解。关于AutoCADActiveXAutomation接口的说明,请参见ActiveXandVBAReferenceGuide。即使是有经验的VB编程人员也会发现要理解和开发AutoCADVBA应用程序,AutoCADActiveXAutomation接口是非常重要的。,第三个元素是VBA编程环境(IDE),它具有自己的对象组、关键词和常量等,能提供程序流、控制、调试和执行等功能。AutoCADVBA包含MicrosoftVBA扩展联机帮助,可以在VBAIDE中通过以下方法访问:按键盘上的F1键从VBAIDE菜单栏中选择“帮助”单击VBAIDE工具栏上的“?”图标,使用VBA管理器可以查看当前AutoCAD任务中加载的所有VBA工程。它是一种AutoCAD工具,可让用户加载、卸载、保存、创建、内嵌和提取VBA工程。打开VBA管理器的步骤从“工具”菜单中选择“宏”“VBA管理器”。在AutoCAD中调用VBAMAN命令。,5.3使用VBA管理器组织工程,加载现有的工程卸载工程将工程嵌入到图形中从图形中提取工程创建新工程保存工程,加载现有的工程,在“VBA管理器”中,使用“加载”选项打开“打开VBA工程”对话框。在“打开VBA工程”对话框中,选择要打开的工程文件。“VBA工程”对话框只能打开有效的DVB文件。如果用户试图打开其他类型的文件,则会收到一条错误信息。选择“打开”。用户还可以使用以下任意方法来加载工程文件:输入VBALOAD命令,打开“打开VBA工程”对话框。将DVB文件从Windows资源管理器拖到AutoCAD窗口中打开的图形中。,卸载VBA工程的步骤,在VBA管理器中选择要卸载的工程。选择“卸载”。或者,使用VBAUNLOAD命令,该命令会提示输入要卸载的工程。,将工程嵌入到AutoCAD图形中的步骤,打开VBA管理器,并选择要嵌入的工程。选择“嵌入”。,创建新工程,新工程会被创建为未保存的全局工程。创建完成后,可将它嵌入到图形中或保存到工程文件中。创建新VBA工程的步骤打开VBA管理器。选择“新建”。新工程将用默认的名称ACADProject创建。要更改工程名,必须进入VBAIDE中。关于在VBAIDE中命名工程的详细信息,请参见命名工程。,更改工程名称的步骤,在VBAIDE的“工程”窗口中,选择要更改的工程。在“特性”窗口中编辑工程的Name特性。,更改工程的文件名的步骤,在VBAIDE中,从“文件”菜单中选择“保存”选项。在“另存为”对话框中输入工程文件的新名称和位置。,使用VBAIDE编辑工程,将工程加载到AutoCAD中后,用户就可以使用VBA交互式开发环境来编辑该工程的代码、窗体和引用,而且还可以在VBAIDE中调试和运行工程。打开之后,使用VBAIDE可以访问所有已加载的工程。,查看工程信息定义工程中的组件输入现有组件编辑组件命名工程保存工程引用其他VBA工程设置VBAIDE选项,5.4使用VBAIDE编辑工程,查看工程信息,VBAIDE包含了“工程”窗口,其中显示了所有已加载的VBA工程列表。这个窗口还显示工程中包含的代码、类和窗体模块,与工程关联的文档,从工程引用的所有其他VBA工程,以及工程的实际位置(路径)。,“工程”窗口有自己的工具栏,可用来打开工程的各个组件进行编辑。使用“查看代码”按钮可打开选定模块的代码。使用“查看对象”按钮可显示选定的对象,例如窗体。,定义工程中的组件,对象窗体标准模块类模块引用添加新组件,对象组件代表VBA代码将访问的对象或文档的类型。对于AutoCADVBA工程,此对象代表当前的AutoCAD图形。窗体组件包含由用户构造的、用于工程的自定义对话框。代码模块组件包含用户的常用过程和函数。标准模块也称为代码模块,或简称为模块。类模块组件包含用户自己定义的所有对象(定义为类)。引用组件包含对其他工程或库的所有引用。,添加新组件,添加新组件将在工程中创建空白组件。用户可以将新的模块、窗体和类模块添加到自己的工程中。用户需要自己更新所有的组件特性(例如组件名称),并填入适当的代码。在命名新组件时,请注意其他开发人员在未来的应用程序中可能会使用您的组件。请遵循开发团队的命名惯例。,向工程中添加新组件的步骤:在VBAIDE的“工程”窗口中选择要向其中添加组件的工程。从“插入”菜单中选择“用户窗体”、“模块”或“类模块”来向工程中添加新组件。新组件将会被添加到工程中并显示在“工程”窗口中。,编辑组件,在VBAIDE中,可以编辑标准模块、类模块和窗体。标准模块和类模块均在“代码”窗口中编辑;窗体则在“用户窗体”窗口中使用特殊的的工具箱进行编辑。由于可以打开的“代码”窗口与拥有的模块数目一致,因此可以很容易地查看不同窗体或模块中的代码,并在它们之间互相复制和粘贴。,编辑工程组件的步骤在VBAIDE的“工程”窗口中,选择要编辑的组件。在“工程”窗口中,选择“查看代码”按钮打开“代码”窗口。在“工程”窗口中,选择“查看对象”按钮打开“用户窗体”窗口和关联的工具箱。,使用“代码”窗口“代码”窗口包含两个下拉列表、一个拆分栏、一个边界指示栏、一个完全视图图标和一个过程视图图标。,使用“用户窗体”窗口通过“用户窗体”窗口,可以在工程中创建自定义对话框自动拥有“最大化”、“最小化”和“关闭”按钮添加控件添加控件代码,访问窗体关联代码的步骤请双击“窗体”窗口中的任意控件。“代码”窗口中将打开与该控件关联的代码。,进行初步练习,上面已经学习了AutoCADVBA编程的基本知识,接下来让我们练习创建简单的“HelloWorld”。在此练习中,将会创建新的AutoCAD图形,并向该图形中添加一行文字,然后保存图形,全部操作均通过VBA来完成。,“HelloWorld”例程从AutoCAD命令行输入以下命令来打开VBAIDE:命令:VBAIDE从VBAIDE的“查看”菜单中选择“代码”选项打开“代码”窗口。从VBAIDE的“插入”菜单中选择“过程”选项,在工程中创建新过程。当提示输入过程信息时,输入名称,例如HelloWorld。请确保选定的“类型”是Sub,选定的“作用域”是Public。选择“确定”。,在行PublicSubHelloWorld()和行EndSub之间输入以下代码。ThisDrawing.Application.Documents.Add(用于打开新图形)紧接代码之后输入以下代码(用于创建字符串和定义其插入位置)。DiminsPoint(0To2)AsDouble声明插入点DimtextHeightAsDouble声明文字高度DimtextStrAsString声明字符串DimtextObjAsAcadText声明文字对象insPoint(0)=2设置插入点的x坐标insPoint(1)=4设置插入点的y坐标insPoint(2)=0设置插入点的z坐标textHeight=1强文字高度设置为1.0textStr=HelloWorld!设置字符串创建Text对象SettextObj=ThisDrawing.ModelSpace.AddText_(textStr,insPoint,textHeight),紧接在步骤7中的输入代码之后输入以下代码(用于保存图形)。ThisDrawing.SaveAs(Hello.dwg)从VBAIDE的“运行”菜单中选择“运行子程序或用户窗体”选项来运行程序。当程序运行完时,AutoCAD应用程序将置于最前。此时应该可以在图形中看到文字“HelloWorld!”。图形的名称应为Hello.dwg。,AutoCADVBA进阶,Add,RetVal=object.Add(Name)Object:Thecollectiontoaddthenewobjectto.Dictionaries,DimStyles,Documents,Groups;Layers,Layouts,Linetypes,PopupMenus,RegisteredApplications,SelectionSets,TextStyles,Toolbars,Views,ViewportsName:Thenameoftheobjecttoaddtothecollection.String;input-only;optionalfortheDocumentscollection.RetVal:Thenewlyaddedobject.Dictionary,DimStyle,Document,Group,Layer,Layout,Linetype,PopupMenu,RegisteredApp,SelectionSet,TextStyle,Toolbar,View,Viewport,ADDLAYER,ThisexamplecreatesanewlayercalledNew_LayerDimlayerObjAsAcadLayerAddthelayertothelayerscollectionSetlayerObj=ThisDrawing.Layers.Add(New_Layer)MakethenewlayertheactivelayerforthedrawingThisDrawing.ActiveLayer=layerObjDisplaythestatusofthenewlayerMsgBoxlayerOinput-onlyThenameofthelinetypetoload.FileNameString;input-onlyThenameofthefilethelinetypeisin.,LinetypeExample,SubExample_Linetype()ThisexamplesearchesforthelinetypeDashDot.Ifitisnotfound,itisaddedfromtheacad.linfile.ThenalineiscreatedandchangedtotheDashDotlinetype.SearchthelinetypescollectionfortheDashDotlinetype.DimentryAsAcadLineTypeDimfoundAsBooleanfound=FalseForEachentryInThisDrawing.LinetypesIfStrComp(,DASHDOT,1)=0Thenfound=TrueExitForEndIfNextIfNot(found)ThenThisDrawing.Linetypes.LoadDASHDOT,acad.lin,CreatethelineDimlineObjAsAcadLineDimstartPoint(0To2)AsDoubleDimendPoint(0To2)AsDoublestartPoint(0)=1#:startPoint(1)=1#:startPoint(2)=0#endPoint(0)=4#:endPoint(1)=4#:endPoint(2)=0#SetlineObj=ThisDrawing.ModelSpace.AddLine(startPoint,endPoint)ChangethelinetypeofthelinelineObj.Linetype=DASHDOTZoomAllEndSub,LineWeightProperty,Specifiesthelineweightofanindividualobjectorthedefaultlineweightforthedrawing.Signatureobject.LineweightObjectAllDrawingobjects;DatabasePreferences,Layer,acLnWtByLayeracLnWtByBlockacLnWtByLwDefaultacLnWt000acLnWt005acLnWt009acLnWt013acLnWt015acLnWt018acLnWt020acLnWt025acLnWt030acLnWt035acLnWt040acLnWt050acLnWt053acLnWt060acLnWt070acLnWt080acLnWt090acLnWt100acLnWt106acLnWt120acLnWt140acLnWt158acLnWt200acLnWt211,AddText,Createsasinglelineoftext.RetVal=object.AddText(TextString,InsertionPoint,Height)Object:Theobjectorobjectsthismethodappliesto.ModelSpaceCollection,PaperSpaceCollection,BlockTextString:Theactualtexttobedisplayed.String;input-onlyInsertionPoint:The3DWCScoordinatesonthedrawingwherethetextisplaced.Variant(three-elementarrayofdoubles);input-onlyHeight:Theheightofthetext.Mustbeapositivenumber.Double;input-onlyRetVal:ThenewlycreatedTextobject.Textobject,SubExample_AddText()Thisexamplecreatesatextobjectinmodelspace.DimtextObjAsAcadTextDimtextStringAsStringDiminsertionPoint(0To2)AsDoubleDimheightAsDoubleDefinethetextobjecttextString=Hello,World.insertionPoint(0)=2:insertionPoint(1)=2:insertionPoint(2)=0height=0.5CreatethetextobjectinmodelspaceSettextObj=ThisDrawing.ModelSpace.AddText(textString,insertionPoint,height)ZoomAllEndSub,GetBoundingBox,Getstwopointsofaboxenclosingthespecifiedobject.object.GetBoundingBoxMinPoint,MaxPointObject:Theobjectorobjectsthismethodappliesto.AllDrawingObjects,AttributeReferenceMinPoint:The3DWCScoordinatesspecifyingtheminimumpointoftheobjectsboundingbox.Variant(three-elementarrayofdoubles);output-onlyMaxPoint:The3DWCScoordinatesspecifyingthemaximumpointoftheobjectsboundingboxVariant(three-elementarrayofdoubles);output-only,SubExample_GetBoundingBox()Thisexamplecreatesalineinmodelspace.Itthenfindstheboundingboxforthelineanddisplaysthecornersofthebox.DimstartPoint(0To2)AsDoubleDimendPoint(0To2)AsDoubleDimlineObjAsAcadLineCreatetheLineobjectinmodelspacestartPoint(0)=2#:startPoint(1)=2#:startPoint(2)=0#endPoint(0)=4#:endPoint(1)=4#:endPoint(2)=0#SetlineObj=ThisDrawing.ModelSpace.AddLine(startPoint,endPoint)ZoomAll,DimminExtAsVariantDimmaxExtAsVariantReturntheboundingboxforthelineandreturntheminimumandmaximumextentsoftheboxintheminExtandmaxExtvariables.lineObj.GetBoundingBoxminExt,maxExtPrinttheminandmaxextentsMsgBoxTheextentsoftheboundingboxforthelineare:input-only;optionalPrompt:Thetextusedtoprompttheuserforinput.Variant(string);input-only;optionalRetVal:The3DWCScoordinatesofthepointtheAutoCADuserhasselected.Variant(three-elementarrayofdoubles),SubExample_GetPoint()Thisexamplereturnsapointenteredbytheuser.DimreturnPntAsVariantReturnapointusingapromptreturnPnt=ThisDrawing.Utility.GetPoint(,Enterapoint:)MsgBoxTheWCSofthepointis:read-writeacAlignmentLeftacAlignmentCenteracAlignmentRightacAlignmentAlignedacAlignmentMiddleacAlignmentFitacAlignmentTopLeftacAlignmentTopCenteracAlignmentTopRightacAlignmentMiddleLeftacAlignmentMiddleCenteracAlignmentMiddleRightacAlignmentBottomLeftacAlignmentBottomCenteracAlignmentBottomRight,RemarksTextalignedtoacAlignmentLeftusestheInsertionPointpropertytopositionthetext.TextalignedtoacAlignmentAligned,oracAlignmentFitusesboththeInsertionPointandTextAlignmentPointpropertiestopositionthetext.TextalignedtoanyotherpositionusestheTextAlignmentPointpropertytopositionthetext.,AutoCADVBA高阶,SaveASMethod,Savesthedocumentormenugrouptoaspecifiedfile.object.SaveAsFileName,FileType,SecurityParamsObject:Document,MenuGroupFileName:String;input-onlyThefullpathandfilename,orvalidURLaddress,forthefile.Theactivedocumentormenugrouptakesonthenewname.,AcSaveAsTypeenum;input-only;optionalforDocumentobjectsac2000_dwgAutoCAD2000DWG(*.dwg)ac2000_dxfAutoCAD2000DXF(*.dxf)ac2000_TemplateAutoCAD2000DrawingTemplateFile(*.dwt)ac2004_dwgAutoCAD2004DWG(*.dwg)ac2004_dxfAutoCAD2004DXF(*.dxf)ac2004_TemplateAutoCAD2004DrawingTemplateFile(*.dwt)acNativeAsynonymforthelatestdrawingrelease.Inthisrelease,thisvalueequalsac2004_dwg.,RemarksThedefaultfiletypefordocumentsisac2004_dwg.Thefollowingvaluesareobsolete:acR14_DWGandacR14_DXF.Documentscanbesavedonlyasfileswiththeextensionsindicatedabove.Tosaveadocumentinadifferentfiletype,usetheExportmethod.,SendCommandMethod,SendsacommandstringfromaVBorVBAapplicationtothedocumentforprocessing.Signatureobject.SendCommand(Command)ObjectDocumentTheobjectorobjectsthismethodappliesto.CommandString;input-onlyThecommandtosendtothedocument.UseaspaceortheASCIIcarriagereturncharacter(vbCr)attheendofthecommandstringtoendthecommand;thisisequivalenttopressingENTERonthekeyboard.,ThismethodprocessesanyAutoCADcommand-linefunctionThismethodisgenerallysynchronous.However,ifthecommandsentwiththismethodrequiresanyuserinteraction(suchaspickingapointonthescreen)thenthismethodwillreturnassoonastheuserinputbegins.Thecommandwillthencontinuetobeprocessedasynchronously.Whenthismethodiscalledfromaneventhandleritisprocessedasynchronously.YoushouldneverusethismethodtoissueacommandforwhichthereisanActiveXmethodavailable.Forexample,donotuseSendCommandVBALOAD.Instead,usetheLoadDVBmethod.,将命令发送到AutoCAD命令行,下面的样例创建一个圆心为(2,2,0)、半径为4的圆,然后将图形缩放至图形中的所有几何图形都可见。注意,字符串末尾有一个空格,表示最后按ENTER键开始执行命令。SubCh3_SendACommandToAutoCAD()ThisDrawing.SendCommand_Circle2,2,04ThisDrawing.SendCommand_zooma“EndSub,将VBA代码转换为VB,要更新代码样例以在VB中使用,必须先引用AutoCAD类型库。要在VB中完成此操作,请从“工程”菜单中选择“引用”选项,启动“引用”对话框。在“引用”对话框中,选择AutoCAD类型库,然后单击“确定”。下一步,将代码样例中所有的ThisDrawing引用替换为引用活动文档的用户指定的变量。要完成这项操作,请为AutoCAD应用程序(acadApp)和当前的文档(acadDoc)定义变量。然后,将应用程序变量设置为当前的AutoCAD应用程序。,要显示AutoCAD图形窗口,需要将AutoCAD应用程序的Visible特性设置为TRUE。如果GetObject创建了一个新的AutoCAD实例(即调用GetObject时AutoCAD没有运行),没有将Visible设置为TRUE会导致AutoCAD应用程序不可见,甚至AutoCAD不显示在Windows任务栏上。,使用VB创建直线,SubCh2_AddLineVB()OnErrorResumeNext连接至AutoCAD应用程序DimacadAppAsAcadApplicationSetacadApp=GetObject_(,AutoCAD.Application.16)IfErrThenErr.ClearSetacadApp=CreateObject_(AutoCAD.Application.16)IfErrThenMsgBoxErr.DescriptionExitSubEndIfEndIf连接至AutoCAD图形,DimacadDocAsAcadDocumentSetacadDoc=acadApp.ActiveDocument创建直线的端点DimlineObjAsAcadLineDimstartPoint(0To2)AsDoubleDimendPoint(0To2)AsDoublestartPoint(0)=1startPoint(1)=1startPoint(2)=0endPoint(0)=5endPoint(1)=5endPoint(2)=0在模型空间中创建Line对象SetlineObj=acadDoc.ModelSpace.AddLine_(startPoint,endPoint)ZoomAllacadApp.visible=TrueEndSub,创建自己的菜单项,Addsapopupmenuitemtoapopupmenu.SeeAlso|ExampleSignatureRetVal=object.AddMenuItem(Index,Label,Macro)ObjectPopupMenuTheobjectorobjectsthismethodappliesto.,Index:Theindexlocationinthepopupmenuwheretheitemistobeadded.Theindexmustbeeitheranintegerorastring.Variant;input-onlyIfanintegerissuppliedtospecifyagivenlocationinthemenu,theindexmustbebetween0andN-1,whereNisthenumberofobjectsinthepopupmenu.Thenewitemwillbeaddedimmediatelybeforethespecifiedindexlocation.Toaddthenewmenuitemtotheendofamenu,settheindextobegreaterthanN.Ifastringisspecifiedandtheindexeditemdoesnotexist,thenthenewmenuitemisaddedattheendofthemenu.,Label:Thelabelforthemenuitem.String;input-onlyMacro:Thenameofthemacroassociatedwiththismenuitem.String;input-onlyRetVal:Thenewlycreatedpopupmenuitem.PopupMenuItemobject,SubExample_AddMenuItem()DimcurrMenuGroupAsacadMenuGroupSetcurrMenuGroup=ThisDrawing.Application.MenuGroups.Item(0)CreatethenewmenuDimnewMenuAsAcadPopupMenuSetnewMenu=currMenuGroup.Menus.Add(TestMenu)AddamenuitemtothenewmenuDimnewMenuItemAsAcadPopupMenuItemDimopenMacroAsStringAssignthemacrostringtheVBequivalentofESCESC_openopenMacro=Chr(3)input-onlyTheindexlocationinthepopupmenuwheretheitemistobeadded.Theindexmustbeeitheranintegerorastring.Ifanintegerisspecified,theindexmustbebetween0andN-1,whereNisthenumberofobjectsinthetoolbar.Thenewitemwillbeaddedimmediatelybeforethespecifiedindexlocation.,NameString;input-onlyThestringthatidentifiesthetoolbarbutton.Thestringmustcomprisealphanumericcharacterswithnopunctuationoth

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论