LISP ROUTINE
LISP ROUTINE
(OP)
I am a beginner in LISP. I wrote a short lisp routine which does not produce results consistently. The routine is supposed to draw a line in any quadrant and place text at the mid point of the line. I tried to simplify the routine as a biginner. Could you please test my routine (listed below) and tell me whats wrong with it? I didn't use any AutoCAD System variables, could you please refer me to some documentation (other than AutoCAD Guides) that may help me to understand which system variables to use & when to use them in LISP.
;| Eddies' Routine. This program draws a line between 2 points, then insert text
on top of the line.
ROUTINE
|;
(Defun c:st ()
(setq pt1 (getpoint "First Point:"))
(setq pt2 (getpoint "Second Point:"))
(setq ang1 (angle pt1 pt2))
(setq pt3 (polar pt1 (angle pt1 pt2) (/ (distance pt1 pt2) 2)))
(if (<= ang1 1.570796327) (setq ang2 (+ ang1 1.570796327))
(if (<= ang1 3.14159) (setq ang2 (- ang1 1.570796327))
(if (<= ang1 4.71238898) (setq ang2 (- ang1 1.570796327))
(if (<= ang1 6.283185307) (setq ang2 (+ ang1 1.570796327)) (setq ang2 0)))))
(if (<= ang1 1.570796327) (setq ang3 ang1)
(if (<= ang1 3.14159) (setq ang3 (+ 3.14159 ang1))
(if (<= ang1 4.71238898) (setq ang3 (- ang1 3.14159))
(if (<= ang1 6.283185307) (setq ang3 ang1) (setq ang2 ang1)))))
(setq pt4 (polar pt3 ang2 200))
(setq ang2 (* 57.29577951 ang2))
(setq ang3 (* 57.29577951 ang3))
(command ".line" pt1 pt2 "")
(command ".text" pt4 250 Ang3 "Eddie"))
;| Eddies' Routine. This program draws a line between 2 points, then insert text
on top of the line.
ROUTINE
|;
(Defun c:st ()
(setq pt1 (getpoint "First Point:"))
(setq pt2 (getpoint "Second Point:"))
(setq ang1 (angle pt1 pt2))
(setq pt3 (polar pt1 (angle pt1 pt2) (/ (distance pt1 pt2) 2)))
(if (<= ang1 1.570796327) (setq ang2 (+ ang1 1.570796327))
(if (<= ang1 3.14159) (setq ang2 (- ang1 1.570796327))
(if (<= ang1 4.71238898) (setq ang2 (- ang1 1.570796327))
(if (<= ang1 6.283185307) (setq ang2 (+ ang1 1.570796327)) (setq ang2 0)))))
(if (<= ang1 1.570796327) (setq ang3 ang1)
(if (<= ang1 3.14159) (setq ang3 (+ 3.14159 ang1))
(if (<= ang1 4.71238898) (setq ang3 (- ang1 3.14159))
(if (<= ang1 6.283185307) (setq ang3 ang1) (setq ang2 ang1)))))
(setq pt4 (polar pt3 ang2 200))
(setq ang2 (* 57.29577951 ang2))
(setq ang3 (* 57.29577951 ang3))
(command ".line" pt1 pt2 "")
(command ".text" pt4 250 Ang3 "Eddie"))





RE: LISP ROUTINE
You should not "hard enter" pi or its ratios; instead use the lisp constant "pi". And to simplify your routine, instead of 2 sets of nested "if" statements for setting ang2 and ang3, you can do it in one set, and the use of "cond" is easier to follow and faster. Cond evalutes each expression until a "true" is encountered and skips the rest.
Applicable system variables to this routine-hmmm, maybe "textsize" for last used text height?
A suggestion on your text placement - may want to center justify your text.
Following routine does the same as yours and incorporates the items I mentioned.
Enjoy your lisping!
Carl
---------------------------------------------------
(Defun c:stt ()
(setq hpi (/ pi 2) 15pi (* 1.5 pi) 2pi (* 2 pi))
(setq pt1 (getpoint "\nFirst Point:"))
(setq pt2 (getpoint pt1 "\nSecond Point:"))
(setq ang1 (angle pt1 pt2))
(setq pt3 (polar pt1 (angle pt1 pt2) (/ (distance pt1 pt2) 2)))
(cond
((<= ang1 hpi) (setq ang2 (+ ang1 hpi) ang3 ang1))
((<= ang1 pi) (setq ang2 (- ang1 hpi) ang3 (- ang1 pi)))
((<= ang1 15pi) (setq ang2 (- ang1 hpi) ang3 (- ang1 pi)))
((<= ang1 2pi) (setq ang2 (+ ang1 hpi) ang3 ang1))
);cond
(setq pt4 (polar pt3 ang2 200));;txt start point
(setq ang2 (/ (* 180 ang2) pi))
(setq ang3 (/ (* 180 ang3) pi))
(command ".line" pt1 pt2 "")
(command ".text" pt4 250 Ang3 "Eddie")
(princ);clean exit
)
RE: LISP ROUTINE
The value for pt3 is inconsistent ("NITTY GRIT" though very close to the answer) as shown by sample of log watch results below;
LOG Watch
...............
ANG1 = 1.5708
ANG2 = 180.0
ANG3 = 90.0
PT1 = (0.0 0.0 0.0)
PT2 = (0.0 6000.0 0.0)
PT3 = (1.83691e-013 3000.0 0.0) (I EXPECT X = 0.0)
PT4 = (-200.0 3000.0 0.0)
...............
...............
LOG Watch
...............
ANG1 = 0.0
ANG2 = 90.0
ANG3 = 0.0
PT1 = (0.0 0.0 0.0)
PT2 = (7000.0 0.0 0.0)
PT3 = (3500.0 0.0 0.0)
PT4 = (3500.0 200.0 0.0)
...............
...............
LOG Watch
...............
ANG1 = 3.92699
ANG2 = 135.0
ANG3 = 45.0
PT1 = (0.0 0.0 0.0)
PT2 = (-4000.0 -4000.0 0.0)
PT3 = (-2000.0 -2000.0 0.0)
PT4 = (-2141.42 -1858.58 0.0)
...............
...............
LOG Watch
...............
ANG1 = 2.35619
ANG2 = 45.0
ANG3 = -45.0
PT1 = (0.0 0.0 0.0)
PT2 = (-4000.0 4000.0 0.0)
PT3 = (-2000.0 2000.0 0.0)
PT4 = (-1858.58 2141.42 0.0)
...............
...............
LOG Watch
...............
ANG1 = 5.49779
ANG2 = 405.0
ANG3 = 315.0
PT1 = (0.0 0.0 0.0)
PT2 = (4000.0 -4000.0 0.0)
PT3 = (2000.0 -2000.0 0.0)
PT4 = (2141.42 -1858.58 0.0)
...............
...............
LOG Watch
...............
ANG1 = 4.71239
ANG2 = 180.0
ANG3 = 90.0
PT1 = (0.0 0.0 0.0)
PT2 = (0.0 -5000.0 0.0)
PT3 = (-4.59227e-013 -2500.0 0.0)(I EXPECT X = 0.0)
PT4 = (-200.0 -2500.0 0.0)
...............
...............
LOG Watch
...............
ANG1 = 3.14159
ANG2 = 90.0
ANG3 = 0.0
PT1 = (0.0 0.0 0.0)
PT2 = (-5000.0 0.0 0.0)
PT3 = (-2500.0 3.06152e-013 0.0)(I EXPECT y = 0)
PT4 = (-2500.0 200.0 0.0)
RE: LISP ROUTINE