Jump to content

Recommended Posts

Posted

Hey guys kinda new to this whole thing. And, well this is my first code I made by my self. A friend was telling me about error trapping and i was Planning on using this as a time saver later on.

 

;; Set var 


(defun setvars (vl nlv inout)
 (if(= inout 0)
       (progn
   (setq vlv (mapcar 'getvar vl))
   (mapcar 'setvar vl nlv)
   )
     )
 (if(= inout 1)
      (progn
      (mapcar 'setvar vl vlv)
      )
     )
)

;;Refrence code for above

(setvars '();|var names|; '();|var values|; 0;|0: save old vars and Input new Vars 1: restore old vars|



;; quick test
(defun c:qc ()
(setvars '(ORTHOMODE OSMODE) '(0 16383) 0)
 (command "line")
(setvars '(ORTHOMODE OSMODE) '(0 16383) 1)
 )

the only issue when i use this it doesnt use the (setvars) command it seemed like it would work to me but i cant figure out why its not. I can load 1 defun setvars in "VLIDE" then copy each line from defun c:qc and it works the way i want it to any help would be great.

Posted

Welcome to CADTutor :)

 

I'm not sure why your function doesn't work, beware of using 'qc' as a function syntax, as you should not use AutoCAD aliases as function syntax.

 

This works for me:

 

(defun setvars ( vl nlv inout / vlv )
 (if (= inout 0)
   (progn
     (setq vlv (mapcar 'getvar vl))
     (mapcar 'setvar vl nlv)
   )
   (mapcar 'setvar vl nlv)
 )
 vlv
)


(defun c:test1 (/ *error* lst vlv )

 (defun *error* ( msg )
   (and vlv (setvars lst vlv 1))
   (or (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*")
       (princ (strcat "\n** Error: " msg " **")))
   (princ)
 )

 (setq lst '("ORTHOMODE" "OSMODE")
       vlv  (setvars lst '(0 0) 0))

 (command "_.line")
 (while (= 1 (logand 1 (getvar 'CMDACTIVE)))
   (command pause)
 )

 (setvars lst vlv 1)
 (princ)
)

 

But you could also approach it this way:

 

(defun setvars ( vl nlv / ol )
 (setq ol (mapcar 'getvar vl))
 (mapcar 'setvar vl nlv)
 ol
)

(defun c:test2 (/ *error* lst vlv )

 (defun *error* ( msg )
   (and vlv (setvars lst vlv))
   (or (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*")
       (princ (strcat "\n** Error: " msg " **")))
   (princ)
 )

 (setq lst '("ORTHOMODE" "OSMODE")
        vlv (setvars lst '(0 0)))

 (command "_.line")
 (while (= 1 (logand 1 (getvar 'CMDACTIVE)))
   (command pause)
 )

 (setvars lst vlv)
 (princ)
)

 

Lee

Posted

Thank you very much Lee. I was reading through your code and found out why mine was not working. My lisp was working as intended but it had no pause for user input. It was finishing the lisp before i could even draw the line

 

 

My new code

 

(defun setvars (vl nlv inout)
 (if(= inout 0)
   (progn
     (setq vlv (mapcar 'getvar vl))
     (mapcar 'setvar vl nlv)
   )
 )
 (if(= inout 1)
   (progn
     (mapcar 'setvar vl vlv)
    )
 )
)



;; quick test
(defun c:test3 (/)
 (setq svin '(setvars '(ORTHOMODE OSMODE) '(1 16383) 0))
 (setq svout '(setvars '(ORTHOMODE OSMODE) '(1 16383) 1))
 (eval svin)
   (command "_.line")
   (while (= 1 (logand 1 (getvar 'CMDACTIVE)))
     (command pause)
   )
 (eval svout)
)


(defun *error* ( msg )
 (eval svout)
   (or (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*")
     (princ (strcat "\n** Error: " msg " **")))
 (princ)
)

 

Now anytime some one "tries to escape" it will reset all the values.

Posted
I'm not sure why your function doesn't work, beware of using 'qc' as a function syntax, as you should not use AutoCAD aliases as function syntax.

FYI, it doesn't hurt anything to use command aliases as command names. A command will always have priority over aliases defined in the acad.pgp and won't hurt any calls to the original, since they are always called with the full command name, not the alias. However, the problem resides in trying to define a command with the same name as an existing command.

 

eg.

Command: (defun c:L (/) (princ "HEY") (princ))
C:L

Command: l
HEY

Command:
Command: (defun c:Line (/) (princ "HEY") (princ))
C:LINE

Command: line
Specify first point:

Specify next point or [undo]:

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...