Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations IDS on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

LISP ROUTINE 3

Status
Not open for further replies.

tendai1

Structural
Mar 29, 2003
24
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 &quot;.line&quot; pt1 pt2 &quot;&quot;)
(command &quot;.text&quot; pt4 250 Ang3 &quot;Eddie&quot;))

 
Replies continue below

Recommended for you

I see one way your routine could have errors-in the &quot;text&quot; command. AutoCAD prompts for text height if your style height is set to 0, but does not prompt if it is a set height.

You should not &quot;hard enter&quot; pi or its ratios; instead use the lisp constant &quot;pi&quot;. And to simplify your routine, instead of 2 sets of nested &quot;if&quot; statements for setting ang2 and ang3, you can do it in one set, and the use of &quot;cond&quot; is easier to follow and faster. Cond evalutes each expression until a &quot;true&quot; is encountered and skips the rest.

Applicable system variables to this routine-hmmm, maybe &quot;textsize&quot; 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 &quot;\nFirst Point:&quot;))
(setq pt2 (getpoint pt1 &quot;\nSecond Point:&quot;))
(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 &quot;.line&quot; pt1 pt2 &quot;&quot;)
(command &quot;.text&quot; pt4 250 Ang3 &quot;Eddie&quot;)
(princ);clean exit
)
 
I tried your routine several times and observed the following: (DO YOU KNOW WHATS CAUSING THAT)

The value for pt3 is inconsistent (&quot;NITTY GRIT&quot; 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)
 
Looks like round-off errors. Pretty small - 10 x 10 to the -13 is pretty small!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor