Online Tutorial
Total Page:16
File Type:pdf, Size:1020Kb
Purpose of course: The student will: • Execute hands-on exercises, such as using AutoLISP for mathematical calculations, AutoCAD shortcuts and text manipulation. • Learn to create new LISP commands, and develop skill in managing lisp programming. • Learn to use available tools for AutoLISP creation. • Learn to locate alternate resources for this craft. • Discover error management and learn beginner techniques for resolving error reports. Introduction: When I first began learning AutoLISP eighteen years ago, I was awestruck at how easy it was to learn. I was also awestruck at what a powerful a skill it was to have within my AutoCAD arsenal of tools. I buried my nose in every book and magazine I could locate, with a deep thirst for whatever new ideas and routines I could put to use. Over time I found that the value was not so much in the software others had authored, but rather the techniques they used, and more importantly, the fact that I could create new ones of my own. Rare it is that you will be able to create something that can be sold on the market. While this tool is extremely powerful, you will likely be using it just as an enhancement to your own capabilities. However, if you have an office with well versed management, you may be called upon to create productivity tools. Perhaps someone may ask you to DEBUG existing company programs. On the very rare occasion, you might even encounter employment specifically for creation of AutoLISP. Simply know that no matter how you use it, AutoLISP knowledge is very rewarding. Investing in learning and writing code will pay for itself very quickly, and will serve you for years to come. You are wise to be here learning this new “toy”. Commit yourself to gaining a fully fluent knowledge; hunt for new sources of this skill. Know that you will always be a more valuable AutoCAD user once you have mastered AutoLISP. Right this very moment you have the opportunity to expand your horizons in surprisingly powerful ways. This exciting adventure is about to begin... • What is AutoLISP? AutoLISP is a high level programming language developed as an enhancement tool integrated into AutoCAD. It is the singular most powerful tool in the AutoCAD arsenal. “High Level” means that it is generally NOT compiled (such as C++, Pascal or Assembly languages), but instead utilizes an Interpreter, built into AutoCAD itself. AutoLISP is a dialect (or derivation) of “Common LISP”, which is a language created for Artificial Intelligence (AI). This means it can be written to modify itself, and create & execute its own new functions. LISP itself is an acronym for LISt Processing. We will examine that soon enough. • Is AutoLISP easy to learn? Generally YES. Functions & syntax are easy, and help is easy to find. Most concepts are intuitive to anyone who understands AutoCAD. There are three concepts that might be a bit complex: • List elements • AutoCAD Database • Error Management This course will gently introduce these concepts by examining them from various views before showing the depth of definition. Our second course will study advanced techniques of manipulating these concepts. We will begin with easy fundamentals and build upon them. You will have the capacity to write your own code (or functions or routines) before the end of this course. • What will I need to write AutoLISP? AutoCAD. You will need a working knowledge of AutoCAD concepts such as entities, command-line dialogue, layers, dimensioning, text, and AutoCAD variables. It would be helpful if you have a working knowledge of your Operating System (Windows?) and file management. Generally, AutoLISP files have an LSP extension and are saved as “Text Only” files, editable with Notepad, WordPad and most word processors. AutoCAD itself has its own AutoLISP editor. This is discussed briefly in the Appendix of Module I, however, to begin with we will simply be using AutoLISP directly on the Command line. Module I Lesson 1: FUNDAMENTAL MATH Math in AutoCAD is a good place to begin learning AutoLISP. It is easy to learn, you already know it, and it can be used right away within a drawing. It also demonstrates the format that other AutoLISP commands use. First you must learn about parentheses. All AutoLISP FUNCTIONS are surrounded in parentheses. The function itself is always the first item or ATOM within the parentheses. The remaining required items (ATOMS) follow, separated by spaces. In the mathematical STATEMENT: 4 + 5 = 9 the addition is represented by the "+". This mathematical operation symbol is called a FUNCTION. The "4" and the "5", the necessary operands, are called ARGUMENTS. The "=" shows equality and, of course the "9" is the result (or RETURN). In AutoLISP "4+5" would be written: (+ 4 5) This RETURNS 9. Notice the function is isted first (always), followed by the necessary ATOMS to complete the LIST. For now, we shall say that math in AutoLISP takes on the general LIST form: (<function> <argument> <argument>...) NOTE: This LIST has three ATOMS. From this we can logically conclude that “5 - 4" would be written: (- 5 4) Notice that the ARGUMENTS appear in the order that you would expect in subtraction. Other math OPERATIONS take the same format: 9 * 3 and 9 / 3 and 3 * 3 * 4 would be written: (* 9 3) and (/ 9.0 3.0) and (* 3 3 4) We will discuss the decimal point and trailing zero later. Given this much, you can actually use AutoLISP while in a drawing. Suppose you needed to offset a polyline 3/32". Try this: Command: OFFSET Offset distance or Through <Through>: (/ 3.0 32.0) In this sequence we are actually able to use AutoLISP's math as a response to an AutoCAD question at the Command Line! In fact, anytime AutoCAD is requesting a distance, you may substitute your response with a math statement written in AutoLISP. (Note: It is understood that AutoLISP in the OFFSET command is not practical in this case, but it is important to know that AutoLISP can be used at any AutoCAD Command- line query). Lesson 1: Summary AutoLISP comprises of LISTS interpreted by AutoCAD. These lists are always surrounded by matching parentheses. The FUNCTION is always the first ATOM in the list followed by the other needed atoms, called ARGUMENTS. The result of a lists function is called a RETURN. Lesson 1: Questions and Exercises 1. What is the RETURN of: (+ 0 pi) ? 2. Are these RETURNS equal: (- 12 5) ; (- 12 5.0) ? 3. Are these RETURNS equal: (/ 12 5) ; (/ 12.0 5.0) ? 4. How many ARGUMENTS are in: (* 1 1) ? 5. How many ATOMS are in this LIST: (* 1 1) ? For your first exercises in AutoLISP try your own math at the Command Line or try converting these: 278 + 1211 - 111 55 * 100 144 / 12 2 * 2 * pi If, at the command line, you see “>1" or “>2" or “>” followed by any number it means that you are not matching parentheses. Use only one open parenthesis and one closing parenthesis for each function. Every leading parenthesis requires a closing parenthesis. It is important to place the closing one in its proper place. We will discuss this in the next lesson: NESTING. Module I Lesson 2a: NESTING MATH OPERATIONS In normal mathematics the following two statements are equal: 4 + 5 5 + 4 as well as are: 3 * 4 4 * 3 however these two are not: 3 * 4 + 5 3 * 5 + 4 The reason is that multiplication TAKES PRECEDENCE over the addition. Multiplication is done first, then the addition. If, there is need for the addition to be done first, then in mathematics one must force it like this: 3 * (4 + 5) This would actually force the three to be multiplied by the result of the addition The operation in the parentheses come first. In other words, one operand of the multiplication is actually an addition operation. In AutoLISP this would appear as: (* 3.0 (+ 4.0 5.0)) The concept of substituting a LIST for an ARGUMENT is called NESTING. This can be done to both arguments: (3 + 2) * (4 + 5) becomes: (* (+ 3 2) (+ 4 5) ) The use of three lines and indentation here is not necessary but it is common practice to make AutoLISP as readable as possible. This concept of NESTING can be carried to several levels and is fundamental to ALL AutoLISP programming. 3 * ((5 + 4) / (6 – 3)) becomes: (* 3 (/ (+ 4 5) (- 6 3) ) ) Practice these ideas several times until you feel comfortable doing math in AutoLISP. Soon you will find yourself doing it naturally. The concept of nesting is pervasive in programming. It is a relatively simple concept that you will see again so be sure you are comfortable doing it. I use closing parentheses on seperate lines here. In easy lines of code this is probably less useful for reading, but more complex lines may need the practice for clarity. Hereon, this tutorial will use seperate lines for closing parenthesis only when clarity calls for it. Module I Lesson 2b: VARIABLE ASSIGNMENT In the mathematical statement “X = 5", the VARIABLE X is ASSIGNED the VALUE 5. In AutoLISP this is done using the SETQ FUNCTION [say SET eQual]: (SETQ X 5.0) The SETQ FUNCTION takes the general form: (SETQ <variable> <expression>...) If either the variable or the expression is missing (or any an odd number of arguments) you will get an ERROR message. SETQ is valuable because it is the only easy way to save the result of an expression. Otherwise, to the program the expression would have no purpose. The algebraic expression “X = 5 * (4 + 5)” becomes: (SETQ X (* 5.0 (+ 4.0 5.0))) Given this step, we can use the assigned variables to generate something new: (SETQ A 4.0) (SETQ B 5.0) (SETQ C (+ A B)) This sequence returns 9.0.