Docsity
Docsity

Prepare-se para as provas
Prepare-se para as provas

Estude fácil! Tem muito documento disponível na Docsity


Ganhe pontos para baixar
Ganhe pontos para baixar

Ganhe pontos ajudando outros esrudantes ou compre um plano Premium


Guias e Dicas
Guias e Dicas


AutoCAD 2000 Visual LISP Tutorial, Notas de estudo de Engenharia Mecânica

AutoLISP - AutoLISP

Tipologia: Notas de estudo

Antes de 2010

Compartilhado em 30/12/2009

william-macgyver-5
william-macgyver-5 🇧🇷

4.8

(24)

54 documentos

1 / 140

Toggle sidebar

Esta página não é visível na pré-visualização

Não perca as partes importantes!

bg1
VISUAL LISP™ TUTORIAL
00120-010000-5080 January 29, 1999
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61
pf62
pf63
pf64

Pré-visualização parcial do texto

Baixe AutoCAD 2000 Visual LISP Tutorial e outras Notas de estudo em PDF para Engenharia Mecânica, somente na Docsity!

VISUAL LISP™ TUTORIAL

00120-010000-5080 January 29, 1999

1 2 3 4 5 6 7 8 9 10

Copyright © 1999 Autodesk, Inc.

All Rights Reserved

AUTODESK, INC. MAKES NO WARRANTY, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, REGARDING THESE MATERIALS AND MAKES SUCH MATERIALS AVAILABLE SOLELY ON AN “AS-IS” BASIS.

IN NO EVENT SHALL AUTODESK, INC. BE LIABLE TO ANYONE FOR SPECIAL, COLLATERAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES IN CONNECTION WITH OR ARISING OUT OF PURCHASE OR USE OF THESE MATERIALS. THE SOLE AND EXCLUSIVE LIABILITY TO AUTODESK, INC., REGARDLESS OF THE FORM OF ACTION, SHALL NOT EXCEED THE PURCHASE PRICE OF THE MATERIALS DESCRIBED HEREIN.

Autodesk, Inc. reserves the right to revise and improve its products as it sees fit. This publication describes the state of this product at the time of its publication, and may not reflect the product at all times in the future. Autodesk Trademarks The following are registered trademarks of Autodesk, Inc., in the USA and/or other countries: 3D Plan, 3D Props, 3D Studio, 3D Studio MAX, 3D Studio VIZ, 3D Surfer, ADE, ADI, Advanced Modeling Extension, AEC Authority (logo), AEC-X, AME, Animator Pro, Animator Studio, ATC, AUGI, AutoCAD, AutoCAD Data Extension, AutoCAD Development System, AutoCAD LT, AutoCAD Map, Autodesk, Autodesk Animator, Autodesk (logo), Autodesk MapGuide, Autodesk University, Autodesk View, Autodesk WalkThrough, Autodesk World, AutoLISP, AutoShade, AutoSketch, AutoSolid, AutoSurf, AutoVision, Biped, bringing information down to earth, CAD Overlay, Character Studio, Design Companion, Drafix, Education by Design, Generic, Generic 3D Drafting, Generic CADD, Generic Software, Geodyssey, Heidi, HOOPS, Hyperwire, Inside Track, Kinetix, MaterialSpec, Mechanical Desktop, Multimedia Explorer, NAAUG, Office Series, Opus, PeopleTracker, Physique, Planix, Rastation, Softdesk, Softdesk (logo), Solution 3000, Tech Talk, Texture Universe, The AEC Authority, The Auto Architect, TinkerTech, WHIP!, WHIP! (logo), Woodbourne, WorkCenter, and World-Creating Toolkit. The following are trademarks of Autodesk, Inc., in the USA and/or other countries: 3D on the PC, ACAD, ActiveShapes, Actrix, Advanced User Interface, AEC Office, AME Link, Animation Partner, Animation Player, Animation Pro Player, A Studio in Every Computer, ATLAST, Auto-Architect, AutoCAD Architectural Desktop, AutoCAD Architectural Desktop Learning Assistance, AutoCAD DesignCenter, Learning Assistance, AutoCAD LT Learning Assistance, AutoCAD Simulator, AutoCAD SQL Extension, AutoCAD SQL Interface, AutoCDM, Autodesk Animator Clips, Autodesk Animator Theatre, Autodesk Device Interface, Autodesk PhotoEDIT, Autodesk Software Developer’s Kit, Autodesk View DwgX, AutoEDM, AutoFlix, AutoLathe, AutoSnap, AutoTrack, Built with ObjectARX (logo), ClearScale, Concept Studio, Content Explorer, cornerStone Toolkit, Dancing Baby (image), Design Your World, Design Your World (logo), Designer’s Toolkit, DWG Linking, DWG Unplugged, DXF, Exegis, FLI, FLIC, GDX Driver, Generic 3D, Heads-up Design, Home Series, Kinetix (logo), MAX DWG, ObjectARX, ObjectDBX, Ooga-Chaka, Photo Landscape, Photoscape, Plugs and Sockets, PolarSnap, Powered with Autodesk Technology, Powered with Autodesk Technology (logo), ProConnect, ProjectPoint, Pro Landscape, QuickCAD, RadioRay, SchoolBox, SketchTools, Suddenly Everything Clicks, Supportdesk, The Dancing Baby, Transforms Ideas Into Reality, Visual LISP, and Volo. Third Party Trademarks Élan License Manager is a trademark of Élan Computer Group, Inc. Microsoft, Visual Basic, Visual C++, and Windows are registered trademarks and Visual FoxPro and the Microsoft Visual Basic Technology logo are trademarks of Microsoft Corporation in the United States and other countries. All other brand names, product names or trademarks belong to their respective holders. Third Party Software Program Credits ACIS® Copyright © 1994, 1997, 1999 Spatial Technology, Inc., Three-Space Ltd., and Applied Geometry Corp. All rights reserved. Copyright © 1997 Microsoft Corporation. All rights reserved. International CorrectSpell™ Spelling Correction System © 1995 by Lernout & Hauspie Speech Products, N.V. All rights reserved. InstallShield™ 3.0. Copyright © 1997 InstallShield Software Corporation. All rights reserved. Portions Copyright © 1991-1996 Arthur D. Applegate. All rights reserved. Portions of this software are based on the work of the Independent JPEG Group. Typefaces from the Bitstream® typeface library copyright 1992. Typefaces from Payne Loving Trust © 1996. All rights reserved. The license management portion of this product is based on Élan License Manager © 1989, 1990, 1998 Élan Computer Group, Inc. All rights reserved. Autodesk would like to acknowledge and thank Perceptual Multimedia, Inc., for the creative and technical design and the development of the Visual LISP Garden Path tutorial. GOVERNMENT USE Use, duplication, or disclosure by the U. S. Government is subject to restrictions as set forth in FAR 12.212 (Commercial Computer Software-Restricted Rights) and DFAR 227.7202 (Rights in Technical Data and Computer Software), as applicable.

In This Chapter

Introduction

This tutorial is designed to demonstrate several

powerful capabilities of the Visual LISP™^ programming

environment for AutoCAD®^ and introduce features of

the AutoLISP ®^ language that may be new to you.

The purpose of the tutorial is to draw a garden path

using an automated drawing tool that minimizes

drafting time and shows the power of parametric

programming. You will learn how to create a drawing

routine that automates the generation of a complex

shape—the kind of drafting operation you do not want

to draw over and over again, if it means starting from

scratch each time.

n The Garden Path Revisited: Working in Visual LISP n Tutorial Overview

2 | I Introduction

The Garden Path Revisited: Working in Visual

LISP

This tutorial is intended for experienced AutoCAD users and assumes you have some familiarity with either LISP or AutoLISP. It also assumes you understand basic Windows ®^ file management tasks such as creating direc- tories, copying files, and navigating through the file system on your hard disk or network. If you are familiar with AutoLISP and have used earlier versions of the Garden Path tutorial, you will notice several differences:

n The Visual LISP (VLISP™^ ) environment is introduced. This environment provides you with editing, debugging, and other tools specific to the cre- ation of AutoLISP applications. Previous versions of the Garden Path tutorial taught AutoLISP language concepts—not VLISP development tools. n New ActiveX™^ and Reactor functions of AutoLISP are demonstrated, as well as several other extensions to the AutoLISP language provided with VLISP. n The tutorial has been thoroughly redesigned. Even if you are familiar with the previous version, you will encounter entirely different source code and a much more extensive tutorial. n There are two possible execution contexts for the Garden Path tutorial. The application may be run as interpreted LISP in piecemeal files and/or functions that are loaded into a single document. Alternately, the program code can be compiled into a VLX application, denoted by a *.vlx execut- able. A VLX operates from a self-contained namespace that can interact with the application-loading document.

In This Lesson

Designing and Beginning

the Program

In this first lesson, you'll begin by defining what the

application will do. Using the Visual LISP (VLISP) devel-

opment environment, you'll create a LISP file and begin

writing AutoLISP code to support your application.

In the process, you'll begin to discover how VLISP

facilitates application development.

n Defining Overall Program Goals n Getting Started with Visual LISP n Looking at Visual LISP Code Formatting n Analyzing the Code n Filling the Gaps in the Program n Letting Visual LISP Check Your Code n Running the Program with Visual LISP n Wrapping Up Lesson 1

Getting Started with Visual LISP | 7

To run the supplied example 1 From the AutoCAD Tools menu, choose Load Application. 2 Select gardenpath.vlx from the Tutorial\VisualLISP directory, and choose Load. 3 Press Close. 4 At the Command prompt, enter gpath. 5 Respond to the first two prompts by picking a start point and an endpoint in the AutoCAD graphics window. 6 Enter 2 at the Half Width of Path prompt. 7 Choose OK when prompted by the Garden Path Tile Specifications dialog box.

Getting Started with Visual LISP

Now that you’ve seen how the application is supposed to work, you can begin developing it with VLISP. But first, it helps to demonstrate what can happen when VLISP is waiting for control to return from AutoCAD. You may have already encountered this.

To see Visual LISP wait for control to return from AutoCAD 1 At the AutoCAD Command prompt, enter vlisp^ to start Visual LISP. 2 Switch back to the AutoCAD window (either select AutoCAD from the task- bar or press ALT^ +^ TAB^ and choose AutoCAD), and enter^ gpath^ at the AutoCAD Command prompt. 3 Before responding to the prompts from gpath , switch back to the VLISP window. In the VLISP window, the mouse pointer appears as a VLISP symbol and you cannot choose any commands or enter text anywhere in the VLISP window. The pointer symbol is a reminder that there is an activity you must complete in AutoCAD before resuming work with VLISP. Remember this whenever you see the VLISP pointer. 4 Return to the AutoCAD window and respond to all the prompts from gpath.

Now you are ready to begin building the garden path application.

8 | Lesson 1 Designing and Beginning the Program

To begin application development with Visual LISP 1 From the VLISP File menu, choose New File. 2 Enter the following code in the text editor window (it is the window titled “<Untitled-0>”); you can omit the comments, if you wish: ;;; Function C:GPath is the main program function and defines the ;;; AutoCAD GPATH command. (defun C:GPath () ;; Ask the user for input: first for path location and ;; direction, then for path parameters. Continue only if you have ;; valid input. (if (gp:getPointInput) ; (if (gp:getDialogInput) (progn ;; At this point, you have valid input from the user. ;; Draw the outline, storing the resulting polyline ;; "pointer" in the variable called PolylineName. (setq PolylineName (gp:drawOutline)) (princ "\nThe gp:drawOutline function returned <") (princ PolylineName) (princ ">") (Alert "Congratulations - your program is complete!") ) (princ "\nFunction cancelled.") ) (princ "\nIncomplete information to draw a boundary.") ) (princ) ; exit quietly ) ;;; Display a message to let the user know the command name. (princ "\nType gpath to draw a garden path.") (princ) 3 Choose File ä Save As from the menu, and save the code in the new file as \Tutorial\VisualLISP\MyPath\gpmain.lsp. 4 Review your work.

Looking at Visual LISP Code Formatting

VLISP recognizes the various types of characters and words that make up an AutoLISP program file and highlights the characters in different colors. This makes it easier for you to spot something incorrect quickly. For example, if you miss a closing quotation mark following a text string, everything you type continues to display in magenta, the color denoting strings. When you enter the closing quotation mark, VLISP correctly colors the text following the string, according to the language element it represents.

10 | Lesson 1 Designing and Beginning the Program

For now, you will just write stubbed-out function definitions. A stubbed-out function serves as a placeholder for the complete function that is to follow. It allows you to try out pieces of your code before adding all the detail needed to complete the application.

To define stubbed-out functions for the application 1 Position your cursor at the top of the program code in the text editor window and press ENTER^ a couple of times to add blank lines. 2 Enter the following code, beginning where you inserted the blank lines: ;;; Function gp:getPointInput will get path location and size (defun gp:getPointInput() (alert "Function gp:getPointInput will get user drawing input" ) ;; For now, return T, as if the function worked correctly. T ) ;;; Function gp:getDialogInput will get path parameters (defun gp:getDialogInput () (alert "Function gp:getDialogInput will get user choices via a dialog" ) ;;For now, return T, as if the function worked correctly. T ) ;;; Function gp:drawOutline will draw the path boundary (defun gp:drawOutline () (alert (strcat "This function will draw the outline of the polyline" "\nand return a polyline entity name/pointer." ) ) ;; For now, simply return a quoted symbol. Eventually, this ;; function will return an entity name or pointer. 'SomeEname ) Right before the end of each input function is a line of code that contains only a T. This is used as a return value to the calling function. All AutoLISP functions return a value to the function that called them. The letter T^ is the symbol for “true” in AutoLISP, and adding it causes the function to return a true value. The way gpmain.lsp is structured, each input function it calls must return a value other than nil (which indicates “no value”) for the program to proceed to the next step. An AutoLISP function will, by default, return the value of the last expression evaluated within it. In the stubbed-out functions, the only expression is a call to the alert function. But alert always returns nil. If this is left as the last

Letting Visual LISP Check Your Code | 11

expression in gp:getPointInput , it will always return nil, and you will never pass through the if^ to the^ gp:getDialogInput^ function. For a similar reason, the end of the gp:DrawOutline function returns a quoted symbol ('SomeEname) as a placeholder. A quoted symbol is a LISP con- struct that is not evaluated. (If you are curious about how the LISP language works, there are a number of good books available, mentioned at the end of this tutorial.)

Letting Visual LISP Check Your Code

VLISP has a powerful feature for checking your code for syntactical errors. Use this tool before trying to run the program. You can catch common typing errors such as missing parentheses or missing quotation marks, and other syntactical problems.

To check the syntax of your code 1 Make sure the text editor window containing gpmain.lsp is the active win- dow. (Click in the title bar of the window to activate it.) 2 From the VLISP menu, choose Tools ä Check Text in Editor. 3 The Build Output window appears with the results of the syntax check. If VLISP did not detect any errors, the window contains text similar to the following: [CHECKING TEXT GPMAIN.LSP loading...] ...... ; Check done. If you have problems and need help, refer to the “Developing Programs with Visual LISP” chapter of the Visual LISP Developer’s Guide. See if you can deter- mine where the problem is located. If you are spending too much time locating the problem, use the sample gpmain.lsp file provided in the lesson directory to continue with the tutorial.

To use the supplied gpmain.lsp program (if necessary) 1 Close the text editor window containing the gpmain.lsp code you entered. 2 Choose File ä Open File from the VLISP menu, and open the gpmain.lsp file in the \Tutorial\VisualLISP\lesson1 directory. 3 Choose File ä Save As and save the file in your \Tutorial\VisualLISP\MyPath directory as gpmain.lsp , replacing the copy you created.

In This Lesson

Using Visual LISP

Debugging Tools

This lesson teaches you how to use several valuable

VLISP debugging tools that speed up the development

of AutoLISP programs. You will also learn the difference

between local and global variables, and when to use

them. Your program will become more active—prompt-

ing users to enter some information. The information

will be stored in a list and you’ll begin to understand

the power of using lists within your AutoLISP programs.

After all, LISP got its name because it is a LISt Processing

language.

n Differentiating between Local and Global Variables n Using Association Lists to Bundle Data n Examining Program Variables n Revising the Program Code n Commenting Program Code n Setting a Breakpoint and Using More Watches n Wrapping Up Lesson 2

14 | Lesson 2 Using Visual LISP Debugging Tools

Differentiating between Local and Global

Variables

This lesson discusses the use of local variables versus global document variables. Global variables are accessible by all functions loaded within a doc- ument (an AutoCAD drawing). These variables may retain their value after the program that defined them completes. Sometimes, this is what you want. You’ll see an example of this later in the tutorial. Local variables retain their value only as long as the function that defined them is running. After the function finishes running, the local variable val- ues are automatically discarded, and the system reclaims the memory space the variable used. This is known as automatic garbage collection, and is a fea- ture of most LISP development environments, such as VLISP. Local variables use memory more efficiently than global variables.

Another big advantage is that local variables make it easier to debug and maintain your applications. With global variables, you are never sure when or in which function the variable’s value might be modified; with local vari- ables you don’t have as far to trace. You usually end up with fewer side effects (that is, one part of the program affecting a variable from another part of the program). Because of the advantages cited, this tutorial uses local variables almost exclusively.

FunA (setq localvara "boo") (setq localvara "hoo") (setq globalvara "foo") (funB) (print localvara) "boo" (print globalvara) "whenever"

FunB (print localvara) nil (print localvarb) nil (print globalvara) "foo" (setq globalvara "whatever) (setq localvara "fie") (funC) FunC (print localvara) nil (print globalvara) "whatever" (setq globalvara) "whenever"