1. Trang chủ
  2. » Công Nghệ Thông Tin

autocad 2007 and autocad lt 2007 bible - phần 10 potx

127 311 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 127
Dung lượng 1,25 MB

Nội dung

1128 Part VII ✦ Programming AutoCAD STEPS: Working with Break Points 1. Open AutoCAD and start a drawing using any template. 2. Start the Visual LISP Editor. Open ab36-a.lsp from the CD-ROM. Choose File➪ Save As and save it as ab36-01.lsp in the AutoCAD 2007\Support folder or in another folder that you’ve added to the support file search path. 3. Choose Load Active Edit Window from the Tools toolbar. 4. Read through the code. This routine creates a vertical list of numbers. The new FOREACH function steps through each item in a list. The comments explain why it contains a bug. 5. If you didn’t do the previous exercise, choose Debug ➪ Break on Error. (Don’t do any- thing if the Break on Error item is already checked.) 6. Type the following in the Console window: (list-beautify-with-bug ‘(1 2 3 4 5 )) ↵ The Console returns the following: (1 ; error: bad argument type: FILE 1 7. Choose Last Break on the Debug toolbar to jump to the error in the source code. 8. To place a break point in the code, place the cursor after (princ (chr 40)). Press F9. Visual LISP marks the break with a red box. 9. Place another break point after (princ (car aList)). Finally, place a third break point after the closing parenthesis on the line that follows (princ item 1). Your code should look like Figure 36-3. 10. After the code produces an error, you need to reset. Click Reset on the Debug toolbar. 11. Click the Visual LISP Editor window and reload the function into Visual LISP. (Choose Load Active Edit Window on the Tools toolbar.) 12. Type the same expression that produced the error in the Console: (list-beautify-with-bug ‘(1 2 3 4 5 )) ↵ 13. Visual LISP highlights the expression (princ (chr 40)). Choose Step Into on the Debug toolbar. Visual LISP highlights (princ (car aList)). 14. Choose Step Into. Now only (car aList)) is highlighted. 15. Choose Step Into until you reach the error, (princ item 1). 16. Edit (princ item 1) so that it reads (princ item). 17. Click Reset again. 18. Choose Debug ➪ Clear All Breakpoints. Click Yes to confirm. 19. Activate the Editor window and reload the function. 20. In the Console, type (list-beautify-with-bug '(1 2 3 4 5 )) ↵. 21. Activate the Editor window and save the file. 48_788864 ch36.qxp 5/22/06 7:34 PM Page 1128 1129 Chapter 36 ✦ Exploring Advanced AutoLISP Topics Figure 36-3: Your code should look like this after you place three break points. If you have difficulty in fixing this bug, you can find the “repaired” version in the Results folder on the CD-ROM, with the filename list-beautify-without-bug.lsp. Using the Watch window The Watch window enables you to examine expressions and variable values as they’re being evaluated. To watch a variable, select it in the code and choose Debug ➪ Add Watch, or press Ctrl+W. Visual LISP opens the Watch window listing the expression or vari- able and displaying its value after an equal sign. If you select an expression or nothing is selected and you press Ctrl+W, the Add Watch dialog box opens. If it is not already displayed, enter the expression that you want to watch and click OK. For example, if you add a watch for (princ (chr 40)), the Watch window displays (PRINC (CHR 40)) = “(“ because (princ (chr 40)) is another way of telling AutoLISP to print a left parenthesis. After the Watch win- dow is open, you can add expressions or variables by selecting them and choosing Add Watch on the Watch window toolbar. If you have a routine with arguments, you can execute the function with various arguments and see the results on your watched expressions and variables in the Watch window. Furthermore, you can add any expression to the Watch window and alter the contents of any variable while debugging a function. The file used in the following exercise on using the Watch window, ab36-01.lsp, is in the Results folder on the CD-ROM. On the CD-ROM On the CD-ROM 48_788864 ch36.qxp 5/22/06 7:34 PM Page 1129 1131 Chapter 36 ✦ Exploring Advanced AutoLISP Topics 12. To remove the break point, choose View ➪ Breakpoints Window to open the Breakpoints dialog box. Click Delete All to delete the break point. Visual LISP automatically closes the dialog box. 13. Click Reset on the Debug toolbar. 14. Close the Watch window and the Visual LISP IDE without saving the file. As you can see, Visual LISP is not just an editor; it’s a full-featured, integrated development environment for AutoLISP. Summary In this chapter, you examined some of the advanced features of AutoLISP and Visual LISP. You read about: ✦ Global and local variables ✦ Some of the features of ActiveX ✦ How to use some of Visual LISP’s debugging features, including the Error Trace window, break points, and the Watch window In the next chapter, you read about the basics of Visual Basic for Applications, another pro- gramming language that you can use with AutoCAD. ✦✦✦ 48_788864 ch36.qxp 5/22/06 7:34 PM Page 1131 48_788864 ch36.qxp 5/22/06 7:34 PM Page 1132 37 37 CHAPTER Programming with Visual Basic for Applications V isual Basic for Applications (VBA) is a programming language and environment that is included with many Microsoft applica- tions, such as Word, Excel, PowerPoint, and Access. Since Release 14, VBA has been available with AutoCAD as well. VBA is ideally suited for situations in which you need to work with more than one application at a time. ActiveX, which I discussed in the last chapter in relation to Visual LISP, enables you to access objects in other applications. However, you can also use VBA to program AutoCAD alone. This chap- ter introduces you to VBA and shows how you can start to use this powerful language to customize AutoCAD. AutoCAD LT does not support VBA. This entire chapter applies to AutoCAD only. Visual Basic for Applications is a variation of Visual Basic. Visual Basic is not related to any specific application. Visual Basic code is compiled into an executable file that stands alone, unrelated to any specific document. VBA, on the other hand, is connected to its appli- cation and the document in which you created the code. VBA pro- vides a simple way to customize AutoCAD, automate tasks, and program applications from within the application. VBA in AutoCAD works slightly differently from VBA in most other applications, in that VBA projects are stored in a separate file, with the DVB filename extension, but can also be stored within the draw- ing file. Starting to Work with VBA After you decide to program AutoCAD, the first step is to select a pro- gramming language to use. ✦✦✦✦ In This Chapter Understanding VBA and AutoCAD Writing VBA code Getting user input Creating dialog boxes Modifying objects Creating loops and conditions Debugging and trapping errors ✦✦✦✦ 49_788864 ch37.qxp 5/22/06 7:30 PM Page 1133 1134 Part VII ✦ Programming AutoCAD VBA has the following advantages: ✦ VBA is faster than AutoLISP, even when AutoLISP is compiled. ✦ VBA is common to many other applications. If you’ve used VBA before, you can easily transfer your knowledge to using VBA in AutoCAD. You’re also more likely to find other programmers who know VBA compared to AutoLISP. ✦ VBA is generally easier to learn than AutoLISP because of its syntax. On the other hand, AutoLISP has the advantage of backward compatibility with prior releases of AutoCAD. Of course, if you’re familiar with AutoLISP but not VBA, it’s hard to beat the ease of working with a language that you already know and use. VBA programs are saved in projects. A project contains all of the parts that are needed to exe- cute the function of the program. You can use the VBA Manager to view your VBA projects. The VBA Manager also enables you to load, unload, save, and create VBA projects. To open the VBA Manager, choose Tools ➪ Macro➪ VBA Manager. Opening the VBA environment To start working with VBA, you must open the VBA environment. VBA has its own interface, just like Visual LISP. To open VBA in AutoCAD, choose Tools ➪ Macro➪ Visual Basic Editor (or type vbaide ↵). Like Visual LISP, VBA has its own interface, called an integrated development environment, or IDE. AutoCAD displays the VBA environment window. VBA projects can contain modules. A module is a self-contained piece of programming code. A VBA project can have one or more modules. To add a module, choose Insert ➪ Module, or click the drop-down list to the right of the second button on the VBA IDE Standard toolbar. Then choose Module. AutoCAD opens a module text editor window so that you can start typing code. In the Project window, VBA adds a new module to the list of modules. By default, the first module is called Module1. Figure 37-1 shows the VBA IDE, including the text editor. If you don’t see the Project Explorer or the Properties window, use the View menu of the VBA IDE to choose Project Explorer or Properties Window. You can resize the module text editor as you would any window. As you start adding code to the text editor, you’ll find it easier to work with a larger window. Click the Maximize button to enlarge the text editor to its maximum size. AutoCAD is still running in the background. You can return to it at any time by clicking its button on the Windows task bar, or by clicking View AutoCAD on the VBA IDE Standard toolbar. Getting acquainted with VBA VBA enables you to easily retrieve, create, and manipulate objects. To get a list of objects, open the Object Browser by clicking Object Browser on the Standard toolbar or choosing View➪ Object Browser. Objects are organized into libraries. All of the objects that you work with in AutoCAD are in the AutoCAD library. Tip 49_788864 ch37.qxp 5/22/06 7:30 PM Page 1134 1135 Chapter 37 ✦ Programming with Visual Basic for Applications Figure 37-1: The VBA environment window. To see the list of AutoCAD objects, click the <All Libraries> drop-down list and choose AutoCAD. You can see the result in Figure 37-2. You can resize the panes in the VBA window. Place the mouse cursor on the bar between the panes until you see the double-headed arrow, and drag it either left or right. Figure 37-2: You can use the Object Browser to see the AutoCAD library of objects. Tip 49_788864 ch37.qxp 5/22/06 7:30 PM Page 1135 1136 Part VII ✦ Programming AutoCAD Objects and collections of objects In the left pane, labeled Classes, you see the list of objects. In VBA, you can have both individ- ual objects and collections of objects. For example, AcadLayer would be the layer object, and AcadLayers would be the collection of layers. The purpose of collections is to enable you to work with a group of objects. For example, to add a layer, you add it to the collection of lay- ers; this is because the new layer is not related to any existing layer. Methods and properties What can you do with objects in VBA? First, objects can have properties. For example, you can set an ellipse to the color red because one of the properties of the ellipse object is TrueColor. (Of course, all drawing objects have TrueColor as one of their properties.) Second, objects have methods. A method is an action that you can take on the object. For example, you can delete (erase) an ellipse because Delete is a method of the ellipse object (as well as of all drawing objects). In the Object Browser, the right pane, Members, lists the properties and methods of any object that you choose in the Classes pane. Investigating the hierarchy model Although you might first think that an object in VBA is the same as an object in an AutoCAD drawing, there is more to the story. In VBA, everything is an object. For example, AutoCAD as an application is an object. Your current drawing is also an object. Model space and paper space are also objects. Therefore, to specify an object in your drawing, you need to specify the application, the drawing, and finally the object in the drawing. To do this, VBA works with a hierarchy of objects. For example, the hierarchy makes it possible to distinguish between an object in your drawing and an object in an Excel spreadsheet. Objects are specified from the most general to the most specific, with a period between each part of the definition. You then add the desired method or properties after another period. For example, you can use the following VBA code to add a circle: Application.ActiveDocument.ModelSpace.AddCircle(center, radius) A shortcut for Application.ActiveDocument is ThisDrawing, and so you can also use: ThisDrawing.ModelSpace.AddCircle(center, radius) In order to work with any object, you need to know where it fits in the hierarchy. The quickest way to see the hierarchical structure from the VBA IDE is to choose any method or property in the Object Browser and to choose Help on the Object Browser’s toolbar. On the Contents tab, choose Object Model to see the listing in Figure 37-3. Within AutoCAD, choose Help ➪ Additional Resources ➪ Developer Help. You’re now in a new Help system. On the Contents tab, double-click ActiveX and VBA Reference. Click Object Model to see the hierarchical model shown in Figure 37-3, or double-click Objects to see the alphabetical object list. Note 49_788864 ch37.qxp 5/22/06 7:30 PM Page 1136 1137 Chapter 37 ✦ Programming with Visual Basic for Applications Figure 37-3: The object model shows you the hierarchy of all of the VBA objects so that you can work with them. STEPS: Becoming Acquainted with the VBA Environment 1. With any drawing open in AutoCAD, choose Tools➪ Macro ➪ Visual Basic Editor. AutoCAD opens the VBA IDE. 2. Choose Insert ➪ Module from the menu. The VBA IDE opens the module text editor window. 3. Move down to the Windows task bar and click the AutoCAD button to return to AutoCAD. Now click the Microsoft Visual Basic button to return to the VBA IDE. 4. Click Object Browser on the VBA IDE Standard toolbar. Click the <All Libraries> drop-down list and choose AutoCAD. If necessary, maximize the window by click- ing the Maximize button at the top-right corner of the window. 5. In the Classes pane, click AcadLine. You see the associated properties and methods in the right pane. 6. In the right pane, which is labeled Members of AcadLine, click Delete. You see the fol- lowing at the bottom of the window: Sub Delete() Member of AutoCAD.AcadLine Deletes a specified object 49_788864 ch37.qxp 5/22/06 7:30 PM Page 1137 [...]... to use AutoCAD 2007 or AutoCAD LT 2007, they offer a great deal of information for ongoing use as well Appendix A runs you through the process of installing and configuring AutoCAD and AutoCAD LT to suit your personal needs Many new features appear in the Options dialog box that I cover here Appendix B lists numerous additional resources, where you can find information on AutoCAD and AutoCAD LT, including... information on AutoCAD and AutoCAD LT, including discussion groups, Web sites, and blogs Appendix C explains what is on the CD-ROM and how to use the files there A R T VIII ✦ ✦ ✦ ✦ In This Part Appendix A Installing and Configuring AutoCAD and AutoCAD LT Appendix B AutoCAD and AutoCAD LT Resources Appendix C What’s on the CD-ROM ✦ ✦ ✦ ✦ ... Programming AutoCAD Add a command button To add a command button, click CommandButton on the Toolbox toolbar Move your cursor over the dialog box, and drag to create a button Figure 3 7-8 shows a dialog box with a new command button The selection border and handles indicate that the button is a selected object You can move the button by dragging it You can resize the button by dragging one of the handles... environment ✦ Understanding VBA objects, methods, and properties ✦ Principles of writing VBA code ✦ How to get user input ✦ How to create dialog boxes ✦ How to modify objects ✦ Methods of debugging and trapping errors A Final Word AutoCAD offers almost unlimited potential for the design and drawing of real-world objects I hope that this book helps you to understand the world of AutoCAD and makes it easier... change it to a later date.) Alternatively, it will tell you how many days ago January 1, 2006 occurred (indicated by a negative value) Function DaysTil2007() As Integer ‘the return type of Dim dtToday As Date Dim dt2007 As Date ‘notice “As Integer” tells ‘the function ‘holds todays date ‘holds Jan 1, 2007 dtToday = Now() dt2007 = CDate(“1/1 /2007 ) DaysTil2007 = dt2007 - dtToday ‘return value assigned... of Visual LISP and VBA, and you’ve seen the power that these languages provide for automating your work However, they are not the only options for programming AutoCAD ObjectARX applications share the same memory space as AutoCAD, and are many times faster than routines written in AutoLISP or VBA ObjectARX is based on C++ and enables full object-oriented interfacing with AutoCAD An object-oriented interface... 14 Click Save on the toolbar and save the file in your AutoCAD Bible folder as ab3 7-0 3.dvb 15 Return to your drawing Choose Tools ➪ Macro ➪ Macros In the Macros dialog box, choose DrawArc and click Run 16 Click the large button AutoCAD draws the arc Figure 3 7-9 : A simple dialog box created in VBA 1157 49_788864 ch37.qxp 1158 5/22/06 7:30 PM Page 1158 Part VII ✦ Programming AutoCAD Modifying Objects Modifying... IDE Standard toolbar VBA returns you to AutoCAD and opens the Save As dialog box Type a name for your project, choose a location, and click Save You can save your project in the Sample\VBA subfolder in the AutoCAD 2007 folder, or use another folder that is in AutoCAD s support file search path Loading a VBA routine Before you run a VBA routine, it must be loaded If you’re working on the routine and want... double-click Microsoft Forms Reference, and then Microsoft Forms Object Model Reference From there, you may want to double-click Objects, Collections, and Controls You can double-click a control, such as the ListBox control, and then read more about it The specific controls also offer hyperlinks to examples that you can look at STEPS: Creating a Dialog Box with a Command Button 1 Open a new drawing in AutoCAD. .. ‘draw eyes cen(0) = cen(0) - rad / 4 cen(1) = cen(1) + rad / 4 Set cir = ThisDrawing.ModelSpace.AddCircle(cen, rad / 8) cen(0) = cen(0) + rad / 2 Set cir = ThisDrawing.ModelSpace.AddCircle(cen, rad / 8) 5 Change the module name to HappyFace 6 Choose Save from the VBA IDE Standard toolbar, and save the VBA project as ab3 7-0 2.dvb in your AutoCAD Bible folder 7 Return to your drawing and choose Tools ➪ Macro . environment window. To see the list of AutoCAD objects, click the < All Libraries> drop-down list and choose AutoCAD. You can see the result in Figure 3 7-2 . You can resize the panes in the. can also use VBA to program AutoCAD alone. This chap- ter introduces you to VBA and shows how you can start to use this powerful language to customize AutoCAD. AutoCAD LT does not support VBA. This. the draw- ing file. Starting to Work with VBA After you decide to program AutoCAD, the first step is to select a pro- gramming language to use. ✦✦✦✦ In This Chapter Understanding VBA and AutoCAD Writing

Ngày đăng: 08/08/2014, 23:20

TỪ KHÓA LIÊN QUAN