Você está na página 1de 3

Home

Contact
Site Map

Custom Search Search

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

SET and SETQ


by Kenny Ramage

In AutoLISP, the process of establishing a value for a symbol is called assignment. The common way to do assignment is to
use SETQ. An assignment causes the value of its second argument to become the value of its first argument, which has to be
a symbol. A symbol is a lexical variable, declared implicitly.

SET is somewhat like SETQ except that SET does evaluate its first argument. Therefore the first argument must evaluate to
the name of a dynamic variable (one whose value is to be created dynamically). The only other difference is that SET takes
only two arguments.

Consider the following assignments:

(setq a var) Sets symbol A to VAR. Symbol VAR is not defined.

(set a 2) Evaluates A and sets the result of this evaluation to 2.

(princ var) Returns 2. Symbol VAR is now defined and set to 2.

A common use of the SET assignment is to define symbols at runtime, and is sometimes more efficient than list construction
when the number of items is not known in advance. As an illustration of this, the following lisp routine example takes a
variable number of points, sorts them by their Y coordinate and draws a line using the sorted points.
(defun c:test ( / j lpt symb)

(setq j 1)
(while (set (read (strcat "point_" (itoa j)))
(getpoint "\nPoint : ")
)
(setq j (1+ j))
)
;; Last point is nil.
(setq j (1- j))
;; Sort the points. See function "sortpoints" below.
(sortpoints j)
;; Initialize the list of points to be passed to (command "_line")
(setq lpt (list ""))
;; Construct the list and set each of our
;; "on the fly" symbols to nil
(while (> j 0)
(setq
symb (read (strcat "point_" (itoa j)))
lpt (cons (eval symb) lpt)
)
(set symb nil)
(setq j (1- j))
)
;; draw the line
(command "_line")
(mapcar 'command lpt)
(princ)
)

;; This function sorts the points by their Y coordinate


(defun sortpoints (j / i cur_y prev_y cur_symb prev_symb point)
(setq i 2)
(while (<= i j)
(setq
cur_symb (read (strcat "point_" (itoa i)))
prev_symb (read (strcat "point_" (itoa (1- i))))
cur_y (cadr (eval cur_symb))
prev_y (cadr (eval prev_symb))
)
(if (> cur_y prev_y)
(progn
(setq point (eval prev_symb))
(set prev_symb (eval cur_symb))
(set cur_symb point)
(if (/= i 2) (setq i (1- i)))
)
(setq i (1+ i))
)
)
)

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

AfraLISP Archive
‘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.

AutoLISP Learning Resources


Lee Mac - AutoLISP Tutorials and Programmes
Draftsperson.net - AutoLISP Tutorials
Jeffery P Sanders - AutoLISP Tutorials
Ron Leigh - AutoLISP Tutorials
Pixel Graphics Inc. - AutoLISP Tutorials
A'CAD Solutions - AutoLISP Tutorial
CAD Digest - AutoLISP Tutorials

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

AutoLISP Forums
CADTutor
Autodesk Discussion Groups
Autodesk User Group International (AUGI)
The Swamp

122 Tutorials and reference documents published at AfraLISP so far.

API Example Price My Car 10

Back to top
Home
Cared for by David Watson © 2018

Você também pode gostar