Chapters 1 and 2. Introduction to AutoLISP/Visual LISP

Chapter 1. AutoLISP/Visual LISP

LISP syntax is none other than that of mathematical functions, already familiar from our secondary education. We all know how to solve an expression such as (8 x ((17 + 3) / 4)). First we would add 17 + 3, obtaining a number which we would then divide by 4, to finally multiply this new result by 8. That is, we solve the innermost parentheses passing the resulting value to the operations represented in the containing parentheses
The figures and the sign of the mathematical operations to be performed are grouped within parentheses. A data sequence between parentheses is what in LISP we call a list. The solution of the previous expression consisted, as we have seen, in the successive processing of each one of these lists. Hence the name of LISP, derived from LISt Processing. In LISP notation the operator always appears at the beginning of the list.
(* 8 (/ (+ 3 17) 4)) would be the equivalent LISP expression. This typed directly into the AutoCAD command line would return 40.

We’re already using LISP. As simple as that. AutoCAD Has interpreted the function received and has returned the result of its evaluation, which is printed on the screen. Thus we have accessed LISP’s fundamental mechanism. Expressions like the one just written are the basic units of the language, which operates through repeated reading, evaluating and printing a result, which in the terminology of the language is known as the read-eval-print loop.
An additional advantage, at least from the standpoint of the group of users to which this book is addressed: other programming languages ​​demand a series of arrangements related to memory management. We would have to foresee the types of data to employ and define in advance the suitable containers for these data types (declaration of variables, etc.). In LISP memory is managed automatically, and only some minimum cautions should be taken by the programmer regarding possible interferences between the names of variables.
Thus, the success of AutoLISP as a means for extending the capabilities of AutoCAD was overwhelming. Perhaps today we are not aware that some of the tools that we consider essential were not available in the original program, being developed as AutoLISP macros and programs, primarily for the personal use of their creators.

What does Visual LISP offer?

The two most pressing demands of the expert user, speed and safety are largely taken care of. Besides programming will be easier with these new tools. But there’s still a question to be answered: Is AutoLISP/Visual LISP the programming language we need? Back in 1986 there was no choice. But today you can create applications that manage AutoCAD with almost any modern programming language. To a programmer who wishes to engage in building applications that extend the features of AutoCAD we would certainly recommend the use of languages ​​such as C#, Visual Basic or VC++ on the .NET platform.
This is not the user profile to which Visual LISP is oriented. Visual LISP is the ideal language for anyone who makes a living day by day using AutoCAD as a tool. For those looking to increase their productivity by automating everyday tasks. For those who want something more than "what comes out of the box". In short, to become a real expert, what we usually deem as an AutoCAD "Power-user". Visual LISP provides them, with respect to classical AutoLISP, a number of advantages:

New LISP functions.

First, Visual LISP significantly increases the number of available functions, including many which have been for quite a long time part of the Common LISP standard. These functions are distinguished from the primitive AutoLISP functions by the VL- prefix totaling about 77, including several functions for reading and writing in the Windows registry or to manage storage in files and folders, some that can be used for a more effective treatment of run-time errors and others which deal with LISP data treatment (symbols, strings, lists, etc.) and type conversion.

Visual LISP ActiveX Extensions.

ActiveX extensions supplied with Visual LISP include functions for the measurement of curves, data conversion, and objects, properties, collections and dictionaries manipulation. These features provide a number of advantages, among which are greater execution speed and wider functionality. They also allow us to use data types that are not available in AutoLISP, such as arrays. These functions are distinguished, like the other new features built into Visual LISP, by their prefixes. We have them in two types:
  • VLA- Functions that directly correspond to the native AutoCAD ActiveX methods.
  • VLAX- Functions specialized functions that are unique to Visual LISP.
This ActiveX interface allows Visual LISP to act as a client and a server in the Windows environment, allowing AutoCAD to control or be controlled from other applications. This way the ability to interact with other applications is enabled, something almost impossible for the old AutoLISP. VLA- and VLAX- functions are not available in AutoCAD for Mac.

Reactors

Other new features group, identified by the VLR- prefix, aim at implementing reactors. Reactors make it possible for a specific AutoCAD application to receive notifications when certain actions occur, making it possible to schedule a response to that event. There are four types of reactors that correspond to the general categories of events to which an application can respond:
  • Editor reactors, which notify each time an AutoCAD command is called.
  • Database Reactors, linked to specific entities or objects in a drawing's database.
  • Object reactors that notify the program if certain actions are performed on an AutoCAD entity.
  • Link Reactors, which notify the application when loading or unloading an ARX application.
This is a complex matter which we will address in Chapter 21. Callback routines invoked from the reactors must necessarily use the new Visual LISP ActiveX functions instead of the AutoLISP command function.

The Development Environment.

Finally we have the programming tools included in the Visual LISP Integrated Development Environment (IDE). Among the components of the IDE we find:
  • A syntax checker that recognizes erroneous expressions and inappropriate arguments within calls to primitive AutoLISP functions.
  • A file compiler that increases the speed of execution and provides a safe and efficient platform for application delivery. The compiler is able to process files both isolated or grouped as projects
  • A source code debugger designed specifically for AutoLISP, which allows stepping through the code in the Editor window while showing the results of its execution in the AutoCAD viewport.
  • A text editor which enables color coding for AutoLISP and DCL sources, while providing assistance in syntax checking.
  • An AutoLISP formatter which restructures the program code to improve readability.
  • Inspection and Watch windows which provide access to the values ​​assumed by variables and expressions allowing their review and modification. They can be used to examine AutoLISP data and AutoCAD entities.
  • A context-sensitive help providing information about AutoLISP functions and an efficient Apropos feature for searching symbol names.
  • A project management system that facilitates the maintenance of applications that span multiple archives.
  • The possibility of packing compiled AutoLISP files in a single module, together with the corresponding DCL dialog definitions.
  • An intelligent LISP console for immediate code evaluation that provides a series of other features useful in the process of program development.

Summary.

Today the applications that manage AutoCAD drawings and can be developed in such different programming languages ​​as Visual BASIC, Delphi, C++, C # Java and even other LISP implementations. For a programmer with experience in any of these environments to learn Visual LISP would be useful but not essential. But in the same manner, an expert AutoCAD user, not a professional programmer, but with knowledge of AutoLISP/Visual LISP need not miss, in order to streamline his daily work, the features offered by those languages. Hopefully this will be demonstrated in the following chapters.
AutoLISP / Visual LISP is also the programming interface for a large number of alternative CAD programs, most of them based on the IntelliCAD technology.

This Chapter includes the following sections:

1.1. Visuual LISP
2.2. New LISP functions
2.3. Summary.


Chapter 2. A Visual LISP Project, Step by Step

As an introduction to both the programming language and its Development Environment this Chapter follows the process that leads from the initial statement of the problem to solve to its completion in the form of a new AutoCAD application. This is the spirit that this book will maintain hereafter: addressing such problems as any user can find in his daily work.
We hope that this book in addition to being a learning tool will provide a library of programs to which the reader may resort, adapting them to his needs. For readers with previous experience in AutoLISP programming we hope to give in this chapter a glimpse of the power available through the new tools. And from here on the more impatient readers will surely start to use immediately and with benefit tools like the Programming Editor. The rest of the book will be a reference and indispensable guide in their daily work. Those without such experience will surely find more questions than answers. These answers will be found in the following chapters. To become a true expert always requires effort and dedication. We hope this tutorial will set the mood for this endeavor.
Some time ago, a friend from Colombia posed the following problem:
I must identify the buildings on a site with a number which increases by 10 each time. I would like that if I placed a number x in the first house of a block, I could just click on this first number and by clicking on the second house I would have placed the previous number increased by 10, and so on. Right now what I have to do is to copy the same text on all houses and then change them one by one, procedure which I find to be very slow.
This is a typical example of how a few lines of code will increase our productivity. Despite its simplicity, we will establish in the development of this program a series of work habits essential in more complex applications.
Our friend tells us that he wants to select a text that contains a figure and thereafter place similar text entities in which that figure is increased by 10 each time. This tells us that we need a set of functions for:
  • Reading a text in the drawing and checking that its value is a number.
  • If it is a number, increasing it in the desired amount.
  • Requesting a location for the new text.
  • Drawing in that location a text containing the new figure.
In order to make it a more general purpose tool, it is also desirable that:
  • We can type an initial figure as an alternative.
  • Make it possible to change the increment value.
  • That this value and the last number written shall be retained between drawing sessions.
We could also store other information related to text formatting. In order not to complicate this tutorial we shall memorize, for the time being, only the text height.
To save data between sessions and retrieve them when we reopen the drawing will use a DICTIONARY object. Dictionaries are objects that the user can create at will to keep non-graphical information in them.
This tutorial will introduce us to some of the key tools of the Visual Lisp Development Environment (VLISP IDE):
  • The Project
  • The Editor
  • The Visual LISP Console
  • The Apropos utility
  • The Error Trace window
  • The Compiler
For those familiar with classical AutoLISP, using the Dictionary object as a means of preserving the values ​​of the program's parameters will have surely revealed the new possibilities that Visual LISP ActiveX extensions provide.

This Chapter includes the following sections:

2.1. Work Space and Project Structure.
2.2. A custom dictionary.
2.3. The calculus Function.
2.4. The drawing function.
2.5. The user interface.
2.6. Placing the labels.
2.7. Updating the dictionary.
2.8. On error.
2.9. Compiling the program.
2.10. Demand loading the program.
2.11. Summary.


Your questions or comments about these Chapters' contents are welcome!

Buy from CreateSpace eStore

4 comments:

  1. I'm using AutoCAD more frequently in my job- so I'm looking for more knowledge to improve productivity/standards...hence considering my first dip into using AutoLisp. Do I need to get an old AutoLisp book before attempting the Visual Lisp in your new book ? or would the book make sense without any AutoLisp knowledge (from books/web) ?

    ReplyDelete
  2. Part 2 of this book covers both the original AutoLISP programming language and the new possibilities introduced with Visual LISP. Classic AutoLISP is still very powerful, sometimes even more than the new ActiveX methods introduced by VLISP. In this book I try to offer examples of how to reach the same goal using one and the other so the reader can decide on which way he feels more comfortable with. And AutoLISP is being enriched in the new versions, with the introduction of a non ActiveX property management system which will greatly extend its possibilities. In brief, with this book you can start from scratch!

    ReplyDelete
  3. In Figure 2.11 NUM-READ
    There is a symbol after (assoc
    What is it?
    I cannot tell.

    ReplyDelete
    Replies
    1. Sorry for the delay in answering. I've been far away from home this summer. The symbol after the first assoc in Figure 2.11 is a zero. This is a screen capture from the VLISP IDE and I can see it may be confusing. this zero indicates the very first element in the list passed as second argument.

      Delete