Vous êtes sur la page 1sur 5

The Basics in a Nutshell - Part 2 | AfraLISP

http://www.afralisp.net/autolisp/tutorials/the-basics-part-2.php

Home Contact Site Map

AutoLISP Beginners' Tutorials Intermediate Tutorials Advanced Tutorials Application Tutorials Tips'n'Tricks Visual LISP Beginners' Tutorials Intermediate Tutorials DCL Beginners' Tutorials Intermediate Tutorials VBA VBA Tutorials VBA Applications Tips'nTricks AutoCAD Customization Tutorials Tips'n'Tricks Reference AutoLISP Functions Visual LISP Sample Files DXF Group Codes The AutoCAD APIs DCL Tile Attributes AutoLISP DCL Functions System Variables AutoCAD Object Model Sin and Cos Functions VLAX Enumeration Download Forum

by Kenny Ramage See also:

1 of 5

1/9/2014 8:40 PM

The Basics in a Nutshell - Part 2 | AfraLISP

http://www.afralisp.net/autolisp/tutorials/the-basics-part-2.php

Part 1 Part 3 Part 4 We now need to be able to "store" our AutoLisp routines in a file. AutoLisp files are simple ASCII text files with the extension "lsp". Open up Notepad or any other simple text editor and type in the following :
(defun testline () ;define the function (setq a (getpoint "\nEnter First Point : ")) ;get the first point (setq b (getpoint "\nEnter Second Point : ")) ;get the second point (command "Line" a b "") ;draw the line ) ;end defun

Now, save this file as "testline.lsp" remembering, to save it as a ASCII Text file and ensuring that it is saved in a directory in your AutoCAD's search path. Now open AutoCAD and type this :
(load "testline")

This will load the function into memory. (Did you notice that you do not have to stipulate the "lsp" extension?) Now type this :
(testline)

Your function should now run. Let's edit this routine so that it acts like a standard AutoCAD command :
(defun c:testline () ;define the function (setq a (getpoint "\nEnter First Point : ")) ;get the first point (setq b (getpoint "\nEnter Second Point : ")) ;get the second point (command "Line" a b "") ;draw the line ) ;end defun

By preceding the function name with c: we do not have to enclose the name with brackets when running. Re-load the function and run it again.
(load "testline") testline

Much better, Hey? We do have one problem though. Did you notice that when we loaded the routine, an annoying "nil" kept on popping up. We also got the same "nil" returned to us when we ran the routine. To suppress this "nil" when

2 of 5

1/9/2014 8:40 PM

The Basics in a Nutshell - Part 2 | AfraLISP

http://www.afralisp.net/autolisp/tutorials/the-basics-part-2.php

loading or running, we can use the (princ) function. Here's what your routine would look like :
(defun c:testline () ;define the function (setq a (getpoint "\nEnter First Point : ")) ;get the first point (setq b (getpoint "\nEnter Second Point : ")) ;get the second point (command "Line" a b "") ;draw the line (princ) ;clean running ) ;end defun (princ) ;clean loading

For more details on the (defun) function, refer to The AfraLisp Tutorial : Define Function (defun). And for a more detailed explanation of loading AutoLisp routines, refer to the AfraLisp tutorial : Loading AutoLisp Files. Now it's time to make AutoLisp do some calculations for us. Let's say we wanted AutoLisp to draw a beam, in elevation, for us. First of all we would start by getting input from the user regarding certain parameters that we would need to draw the beam. Here's what we are trying to draw, along with the values that the user needs to input.

The values that we need to retrieve from the user are as follows :
Insertion Point Length of Beam Height of Beam Flange Thickness End Plate Thickness Length of Notch Depth of Notch ip lb hb wt ep nl nd

Let's write a routine to retrieve these values first.


(defun c:testbeam ()

3 of 5

1/9/2014 8:40 PM

The Basics in a Nutshell - Part 2 | AfraLISP

http://www.afralisp.net/autolisp/tutorials/the-basics-part-2.php

;define the function ;******************************************************** ;Get User Inputs (setq lb (getdist "\nLength of Beam : ")) ;get the length of the beam (setq hb (getdist "\nHeight of Beam : ")) ;get the height of the beam (setq wt (getdist "\nFlange Thickness : ")) ;get the thickness of the flange (setq ep (getdist "\nEnd Plate Thickness : ")) ;get the thickness of the end plate (setq nl (getdist "\nLength of Notch : ")) ;get the length of notch (setq nd (getdist "\nDepth of Notch : ")) ;get the depth of the notch ;End of User Inputs ;********************************************************* ;Get Insertion Point (setq ip (getpoint "\nInsertion Point : ")) ;get the insertion point ;******************************************************** (princ) ;finish cleanly ) ;end of defun

;********************************************************** (princ) ;load cleanly ;**********************************************************

Load and run the routine. You will be prompted for all the values listed above. Enter some numbers and then check the value of all the variables by preceeding the value names with "!" (e.g. !ip). O.K. we've got all the values we need from the user. But first we need to do some calculations to determine the other points required before we can draw the beam. See you in Part 3

AutoLISP Beginners' Tutorials AutoLISP Intermediate Tutorials AutoLISP Advanced Tutorials AutoLISP Application Tutorials AutoLISP Tips'n'Tricks

4 of 5

1/9/2014 8:40 PM

The Basics in a Nutshell - Part 2 | AfraLISP

http://www.afralisp.net/autolisp/tutorials/the-basics-part-2.php

Hey, what's happened to AfraLISP? If you've visited our site before, you'll notice some big changes. We're currently revamping the entire site to bring you updated tutorials and a better user experience. However, if there's something you can't find, the AfraLISP Archive contains a full copy of the original site as originally created by Kenny Ramage.

Lee Mac - AutoLISP Tutorials and Programmes Jeffery P Sanders - AutoLISP Tutorials Ron Leigh - AutoLISP Tutorials Pixel Graphics Inc. - AutoLISP Tutorials A'CAD Solutions - AutoLISP Tutorial CAD Digest - AutoLISP Tutorials Autodesk - Customization Tutorials Daily AutoCAD - AutoLISP Tips & Tricks

The ABC's of AutoLISP The Visual LISP Developer's Bible

CADTutor Autodesk Discussion Groups Autodesk User Group International (AUGI) The Swamp 120 Tutorials and reference documents published at AfraLISP so far. Back to top Home Cared for by David Watson 2014

5 of 5

1/9/2014 8:40 PM

Vous aimerez peut-être aussi