Cours AutoCAD 2006 VBA, tutoriel & guide de travaux pratiques en pdf.
Visual Basic Concepts
Since VBA is a Microsoft Windows development environment, you’ll find developing in VBA easiest if you have some knowledge of Windows. If you are new to Windows, you’ll find fundamental differences between programming in Windows and programming in other environments, such as Visual LISP. The next sections outline concepts of Windows programming that may be new to you.
Windows, Events, and Messages
Explaining the inner workings of Windows requires much more space than is available in this book. But you don’t need an extensive knowledge of the Windows workings to create useful applications. The Windows operating system can be simplified to three basic concepts: windows, events, and messages. A window is a rectangular region on the screen that has its own border. The AutoCAD drawing window, Notepad, a Word document, and the place where you compose an e-mail areall windows. Windows can have hierarchy. A dialog form, which is a window within a distinct application, contains relevant ActiveX components and code. A drawing window is the parent of adialog form window, and AutoCAD is the parent of the drawing windows within it. The operating system is the parent “window” of all applications running in it, including AutoCAD. Each window recognizes and controls the programs that execute inside them or their subordinate windows. To manage windows, Windows assigns a unique ID known as a handle, or hWndin programming jargon, to each window. Windows uses eventsto constantly monitor each window for signs of activity. Events change the application environment or system state. They occur when a user acts, such as by clicking the mouse or pressing a key; programmatically; or by another window through system processes. Each time an event is triggered, Windows sends a message to the hosting application. Windows processes the message and broadcasts it to the windows. Then, based on its own instructions, each window can take appropriate action such as repainting itself when uncovered by another window. In the case of VBA in AutoCAD, the VBA work space intercepts event messages. VBA programs can then respond directly or pass the event up to AutoCAD or to Windows if necessary. VBA provides a controlling environment within AutoCAD in which to execute and respond to events, either directly or by allowing AutoCAD or Windows to respond. Although this seems like a lot of work, VBA hides most of the low-level details from you and exposes event procedures, which are routines that execute when a particular event occurs, for your convenience. You can quickly create very powerful applications without being concerned with low-level details.
Event-Driven vs. Procedural Programming
When a traditional procedural application runs, it follows a predetermined path that controls the portions and sequence of code executed. It starts with the first line of code and progresses from the top down, calling each procedure when needed, until reaching the end of the code. This predetermined path is the major difference between procedural and eventdriven applications. Event-driven applications do not have a predetermined destiny. Different sections of code are executed based upon the events triggered in whatever order they occur. Depending on what events occur when the application runs, some sections of code may not get executed at all. You can’t predict the sequence of events, so you must make assumptions about the application’s “state” at any moment. This seems like it would be difficult, but it’s really not. Typically, you have a set of possibilities to work with, such as the Click, DblClick, KeyPress, and LostFocus events. For example, you might require the user to type a value in a TextBox before enabling a CommandButton that allows further processing. The TextBox control’s Change event would contain code that enables the CommandButton control, as shown in this sample code:
Private Sub TextBox1_Change() If Len(TextBox1.Text) > 0 Then CommandButton1.Enabled = True
Else CommandButton1.Enabled = False End If End Sub.