Originally Posted by
ticad02
Either I'm missing something or I just can't figure it out. I've tried all the mentioned ideas and haven't come up with anything that works. I can't even trouble shoot my attempts as I don't know if I'm even writing the right code in the right places. I'm pretty much just shooting in the dark (lisp at its finest LOL).
I've attached my three attempts maybe someone can make heads or tails out of what my problem is. Note1.lsp jams up ACAD and I have to go to the task manager to close it. By the way I'm using ACAD 2010 if that changes anything.
I don't want it written for me, I won't learn anything that way, but if I had a little more guidance about where I was going wrong I may be able to get an the right path.
Thanks again for everyones patienece and advice.
What immediately jumps out at me is the nested functions. It looks like you are taking the posted sample code and attempting to just "wrap" it around your real code. Rather, you should modify your function so that it is similar to the posted samples.
The two functions myerror and MyExit are really intended to do the same thing: clean up after a clean exit or an error. However, most of the system variable restores are being done in myerror whereas MyExit is being used too.
I suggest moving all the system variable restores to MyExit and then get rid of the function nesting and unneeded statements as shown here in green. (Any added item will be in red.)
Code:
; This routine draws a leader w/arrow and text.
;
(defun myerror (s)
(if
(or
(= msg "Function cancelled")
(= msg "quit / exit abort")
)
(princ)
(princ (strcat "\nError: " msg))
)
(if (/= s "Function cancelled")
(princ (strcat "\nError: " s)))
(command "layer" "s" userlay "")
(command "linetype" "s" userlt "")
(command "color" "bylayer")
(setvar "cmdecho" userecho)
(setvar "orthomode" userortho)
(setvar "clayer" userla)
(setvar "pickbox" userpick)
(setq *error* olderror)
(princ)
)
(defun secure (/ fp)
(setvar "cmdecho" 0)
(if (= (getvar "USERR3") 0.0)
(progn (print)(print)(print)
(alert "System variables not set, please re-run\nthe SETUP routine to continue...")
(exit)))
)
(defun MyExit (msg)
(cond
((not msg)) ; no error, normal exit
((member msg '("Function cancelled" "quit / exit abort"))) ; <esc> or (quit)
((princ (strcat "\nError: " msg)))) ; error, display it
(setvar "clayer" userlay)
(setvar "celtype" userlt)
(setvar "cecolor" "bylayer")
(setvar "cmdecho" userecho)
(setvar "orthomode" userortho)
(setvar "clayer" userla)
(setvar "pickbox" userpick)
(setvar "polaraddang" "")
(setvar "osmode" useros)
(princ))
(defun C:MainCode (/ *Error*) ; localize error handler
(setq *Error* MyExit) ; redefine local error handler
(defun C:srs (/ *error* dsc da la ls ep ang ang1 p1 p2 th tx txthght olo oln oll ls1 userla useras userpm)
(secure)(print)(print)
(setq olderror *error* *error* myerror)
(setq *error* MyExit)
(setq userpick (getvar "pickbox"))
(setq dsc (getvar "USERR3"))
(setq userecho (getvar "cmdecho"))
(setq userla (getvar "clayer"))
(setq userortho (getvar "orthomode"))
(setq useros (getvar "osmode"))
(setq userlay (getvar "clayer"))
(setq userlt (getvar "celtype"))
(setvar "orthomode" 0)
(setvar "cmdecho" 0)
(setq th (* 9.0 (/ dsc 96.0)))
(if (= 1 (getvar "userr2")) (setq th (* 12.0 (/ dsc 96.0))))
(setvar "osmode" 512)
(setq useras (getvar "autosnap"))
(setq userpm (getvar "polarmode"))
(command "polarmode" "4")
(command "polaraddang" "30;60;120;150;210;240;300;330")
(command "autosnap" "13")
(setq olo (entsel "\nSelect referenced object..."))
;(setq ls (car(cdr olo)))
(setq oln (car olo))
(setq oll (entget oln))
(setq la (cdr (assoc 8 oll)))
(setq la (substr la 1 3))
(setq la (strcat la "TX"))
(setq laY (cdr (assoc 8 oll)))
(setq laY (substr laY 1 7))
(setq laY (strcat laY "TEXT"))
(setq ls (getpoint "\nLeader start <nearest>..."))
(setvar "osmode" 0)
(command "iNSERT" "loop3" ls (/ dsc 96.0) (/ dsc 96.0) da)
(setvar "DIMASZ" 0.046875)
(setvar "DIMCLRD" 3)
(setvar "DIMCLRE" 3)
(setvar "DIMCLRT" 4)
(setvar "DIMscale" (getvar "USERR3"))
(setvar "DIMLDRBLK" "LOOP3")
(setq ep (getpoint ls "\nNext point..."))
(setq ang (angle ep ls))
(setq ang1 pi)
(if (and (> ang (/ pi 2.0)) (< ang (* 1.5 pi))) (setq ang1 0.0))
(setq p1 (polar ep ang1 (* 6.0 (/ dsc 96.0))))
(setq p2 (polar p1 ang1 (* 2.0 (/ dsc 96.0))))
(if (= nil (tblsearch "layer" la))
(command "layer" "m" la "c" "c" la "lt" "continuous" la ""))
(command "layer" "s" la "")
(if (= nil (tblsearch "layer" laY))
(if (= 2 (getvar "userr2"))(command "layer" "m" laY "c" "c" laY "lt" "continuous" laY "")))
(if (= 2 (getvar "userr2"))(command "layer" "s" laY ""))
(command "color" "g")
(command "linetype" "s" "continuous" "")
(setq da (* (+ ang pi) (/ 180.0 pi)))
;(command "insert" "arrow" ls (/ dsc 96.0) (/ dsc 96.0) da)
(command "LEADER" ls ep p1 "F" "A" "" "" "N")
(command "color" "c")
;(setq tx (getstring T "\nEnter text string : "))
(setq txthght (getvar "textsize"))
(if (NOT (= th txthght))
(progn
(command "style" "hand" "" "0" "1" "0" "n" "n" "n")
(if (= 1 (getvar "userr2")) (command "style" "lwa8g" "" "0" "0.8" "0" "n" "n" "n"))
(if (= 2 (getvar "userr2")) (command "style" "lwa8g" "" "0" "0.8" "0" "n" "n" "n"))
)
)
(command "style" "" "" "0" "" "0" "n" "n" "n")
(princ "\nInput Text (2 enters to quit):")
(if (> (car ep) (car p1))
(command "Dtext" "j" "mr" p2 th 0)
(command "Dtext" "j" "ml" p2 th 0)
)
(setvar "polarmode" userpm)
(setvar "autosnap" useras)
(command "polaraddang" ".")
(command "layer" "s" userlay "")
(command "color" "bylayer")
(command "linetype" "s" userlt "")
(setvar "cmdecho" userecho)
(setvar "orthomode" userortho)
(setvar "pickbox" userpick)
(setvar "clayer" userla)
(setvar "osmode" useros)
(setq *error* olderror)
(MyExit nil)) ; normal, clean exit
(print)(print)
)