AUGIWorld Magazine - January/February 2008 - (Page 19) Not every project needs a form, but every project will have at least one code module. The one code module that all projects have by default is a special module called ThisDrawing. The ThisDrawing module represents the current open drawing. Creating your first VBA project To create your first VBA project, you will need to use the Microsoft Visual Basic Editor. To display the Microsoft Visual Basic Editor, follow these steps. 1. In AutoCAD at the Command prompt, enter vbaman and press ENTER. 2. In the VBA Manager, click Visual Basic Editor. 3. In the Microsoft Visual Basic Editor, click View menu > Project Explorer and click View menu > Properties Window. 4. In the Project Explorer, expand ACADProject and AutoCAD Objects if they are collapsed. Figure 2 shows the Project Explorer. VBA inside of AutoCAD has been a welcomed feature for those who develop applications professionally and even for those are just discovering their desire to write custom programs. Figure 2: Project Explorer 5. Double-click the ThisDrawing module. A new code window is opened to the right of the Project Explorer in the document area. 6. In the code window, enter the following: Public Sub DrawLine() Dim objLine As AcadLine Dim dStartPoint(0 to 2) As Double, dEndPoint(0 to 2) As Double ’ Define the coordinates points for the line 0,0,0 and 5,5,0 dStartPoint(0) = 0#: dStartPoint(1) = 0#: dStartPoint(2) = 0# dEndPoint(0) = 5#: dEndPoint(1) = 5#: dEndPoint(2) = 0# ’ Add the line to Model space Set objLine = ModelSpace.AddLine(dStartPoint, dEndPoint) ’ Assign the color blue to the line Dim objColor As New AcadAcCmColor objColor.ColorIndex = acBlue objLine.TrueColor = objColor ’ Zoom to the extents of the drawing Application.ZoomExtents Set objColor = Nothing Set objLine = Nothing End Sub Figure 3: Macros dialog box 7. In the Microsoft Visual Basic Editor, click File > Save. 8. In the Save As dialog box, browse to the location in which you want to save the file to and enter the name DrawLine.dvb. Click Save. 9. In the Microsoft Visual Basic Editor, click View menu > AutoCAD. 10. In AutoCAD at the Command prompt, enter vbaman and press ENTER. 11. In the VBA Manager dialog box, click Macros. 12. In the Macros dialog box (see Figure 3), click Run. The Macros dialog box closes and a blue line is added to the drawing. If you had more than one macro in the project, you would select the name of the procedure to run from the list in the Macros dialog box before you click Run. Using VBA to create custom programs might be intimidating at first, but it allows you to automate a variety of different tasks that might normally require a number of manual steps. While I have only scratched the surface of how to use VBA with AutoCAD in this article, you can find out more about using VBA with AutoCAD in the Online Help system that comes with AutoCAD. To launch the Online Help system, click Help menu > Additional Resources > Developer Help within AutoCAD. Then open the ActiveX and VBA Developer’s Guide and ActiveX and VBA Reference books to access the topics on using VBA with AutoCAD. Lee Ambrosius is a consultant for HyperPics, LLC an AutoCAD Consulting/Training company located in Wisconsin, USA. He specializes in AutoCAD Customization/Programming with AutoCAD R12 through 2008. He is one of the co-authors of the book AutoCAD & AutoCAD LT All-in-One Desk Reference For Dummies (based on AutoCAD 2007) and is the author of the book AutoCAD 2008 3D Modeling Workbook For Dummies. Lee can be reached via e-mail at lee_ambrosius@hyperpics.com; other contact information can be found on his Website at http://www.hyperpics.com or his Blog at http://hyperpics.blogs.com. 19 Conclusion Jan/Feb 2008 http://www.hyperpics.com http://hyperpics.blogs.com
For optimal viewing of this digital publication, please enable JavaScript and then refresh the page. If you would like to try to load the digital publication without using Flash Player detection, please click here.