Author Topic: INITIALIZE NONE EXISTANT VAR  (Read 3523 times)

0 Members and 1 Guest are viewing this topic.

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4076
INITIALIZE NONE EXISTANT VAR
« on: June 21, 2012, 12:56:43 PM »
I dont use LISP very often, so this might be really simple.  What Im trying to do is check a var for valid number, and if it doesnt exist, initialize it.  Im using (IF (not CMDRUN )(SETQ CMDRUN 0)).  When I open a new dwg, CMDRUN is nil by default.  Any ideas?
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4076
Re: INITIALIZE NONE EXISTANT VAR
« Reply #1 on: June 21, 2012, 01:02:53 PM »
I forgot to add this, the error message:   error: bad argument type: numberp: nil
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: INITIALIZE NONE EXISTANT VAR
« Reply #2 on: June 21, 2012, 01:09:46 PM »
Simplest way I usually use for global variables:
Code - Auto/Visual Lisp: [Select]
  1. (or CMDRUN (setq CMDRUN 0))
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: INITIALIZE NONE EXISTANT VAR
« Reply #3 on: June 21, 2012, 01:12:02 PM »
Not too sure why you're getting numberp:nil error in your code. It should work fine.
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

efernal

  • Bull Frog
  • Posts: 206
Re: INITIALIZE NONE EXISTANT VAR
« Reply #4 on: June 21, 2012, 01:13:08 PM »
Code - Auto/Visual Lisp: [Select]
  1. ;; example
  2. ;; set value
  3. (IF (NOT cmdrun)
  4.   (VL-BB-SET 'cmdrun 0)
  5. )
  6. ;; get value
  7. (SETQ var (VL-BB-REF 'cmdrun))
  8.  
e.fernal

Lee Mac

  • Seagull
  • Posts: 12922
  • London, England
Re: INITIALIZE NONE EXISTANT VAR
« Reply #5 on: June 21, 2012, 01:16:34 PM »
I dont use LISP very often, so this might be really simple.  What Im trying to do is check a var for valid number, and if it doesnt exist, initialize it.  Im using (IF (not CMDRUN )(SETQ CMDRUN 0)).  When I open a new dwg, CMDRUN is nil by default.  Any ideas?

Is CMDRUN local to a function and you are testing the value outside of such function?

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4076
Re: INITIALIZE NONE EXISTANT VAR
« Reply #6 on: June 21, 2012, 01:17:13 PM »
Could it be because of a later test?  This was the rest of it
Code: [Select]
  (IF (not CMDRUN )(SETQ CMDRUN 0))

  (COND
    ((= CMDRUN 0)
     (PROGN
       (alert "Draw Cables from Breaker to Switch/Bus")
       (SETQ CMDRUN (1+ CMDRUN))
     )
    )
    ((< CMDRUN 5)
     (SETQ CMDRUN (1+ CMDRUN))
    )
    ((>= CMDRUN 5)
     (SETQ CMDRUN nil)
    )
  )
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4076
Re: INITIALIZE NONE EXISTANT VAR
« Reply #7 on: June 21, 2012, 01:18:18 PM »
Here is the whole thing
Code: [Select]
(defun DrawCable ()

  (IF (not CMDRUN )(SETQ CMDRUN 0))

  (COND
    ((= CMDRUN 0)
     (PROGN
       (alert "Draw Cables from Breaker to Switch/Bus")
       (SETQ CMDRUN (1+ CMDRUN))
     )
    )
    ((< CMDRUN 5)
     (SETQ CMDRUN (1+ CMDRUN))
    )
    ((>= CMDRUN 5)
     (SETQ CMDRUN nil)
    )
  )
  (setq clay (getvar "clayer"))
  (setq osm (getvar "osmode"))
  (setq oth (getvar "orthomode"))
  (setvar "orthomode" 0)
  (setvar "osmode"
  (setq   pt1 (getpoint "\nPick Cable Start: ")
   pt2 (getpoint pt1 "\nPick Cable Endpoint: ")
   ANG (ANGLE pt1 pt2)
  )
  (setvar "osmode" 0)
  (SETQ mpt (POLAR pt1 (ANGLE pt1 pt2) (/ (DISTANCE pt1 pt2) 2)))
  (cond
    ((AND (<= (R->D ANG) 90) (> (R->D ANG) 0))
     (setq pt3 (polar mpt (+ ang (d->r 90)) (/ (DISTANCE pt1 pt2) 5)))
    )
    ((AND (<= (R->D ANG) 180) (> (R->D ANG) 90))
     (setq pt3 (polar mpt (- ang (d->r 90)) (/ (DISTANCE pt1 pt2) 5)))
    )
    ((AND (<= (R->D ANG) 270) (> (R->D ANG) 180))
     (setq pt3 (polar mpt (+ ang (d->r 90)) (/ (DISTANCE pt1 pt2) 5)))
    )
    ((AND (<= (R->D ANG) 360) (> (R->D ANG) 270))
     (setq pt3 (polar mpt (- ang (d->r 90)) (/ (DISTANCE pt1 pt2) 5)))
    )
    ((= (R->D ANG) 0)
     (setq pt3 (polar mpt (- ang (d->r 90)) (/ (DISTANCE pt1 pt2) 5)))
    )
  )
  (command "-layer" "m" "SITE-BUSS" "C" 3 "SITE-BUSS" "")
  (command "pline" pt1 "w" 2 2 "a" "s" pt3 pt2 "")
  (setvar "orthomode" oth)
  (setvar "osmode" osm)
  (setvar "clayer" clay)
)
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4076
Re: INITIALIZE NONE EXISTANT VAR
« Reply #8 on: June 21, 2012, 01:22:53 PM »
Im using this to draw arcs from the breaker (in the center) to the switch



Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4076
Re: INITIALIZE NONE EXISTANT VAR
« Reply #9 on: June 21, 2012, 01:29:11 PM »
Simplest way I usually use for global variables:
Code - Auto/Visual Lisp: [Select]
  1. (or CMDRUN (setq CMDRUN 0))
I thought about this, but would that set it to 0 every time?
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

Lee Mac

  • Seagull
  • Posts: 12922
  • London, England
Re: INITIALIZE NONE EXISTANT VAR
« Reply #10 on: June 21, 2012, 01:34:00 PM »
Simplest way I usually use for global variables:
Code - Auto/Visual Lisp: [Select]
  1. (or CMDRUN (setq CMDRUN 0))
I thought about this, but would that set it to 0 every time?

No, since the second expression will only be evaluated if the first evaluates to nil.

OR : Evaluates expressions until an expression returns a non-nil value (at which OR returns T), or until there are no more expressions (at which OR returns nil)

AND: Evaluates expressions until an expression returns a nil value (at which AND returns nil), or until there are no more expressions (at which AND returns T)

That said, although the use of or is more concise, I personally think an if statement preserves readability and intent.
« Last Edit: June 21, 2012, 01:37:01 PM by Lee Mac »

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4076
Re: INITIALIZE NONE EXISTANT VAR
« Reply #11 on: June 21, 2012, 01:36:21 PM »
so any idea why NOT didnt work
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

dgorsman

  • Water Moccasin
  • Posts: 2437
Re: INITIALIZE NONE EXISTANT VAR
« Reply #12 on: June 21, 2012, 01:37:39 PM »
Maybe it wasn't localized in a previously loaded/run LISP function, so you've got dangling variables?
If you are going to fly by the seat of your pants, expect friction burns.

try {GreatPower;}
   catch (notResponsible)
      {NextTime(PlanAhead);}
   finally
      {MasterBasics;}

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4076
Re: INITIALIZE NONE EXISTANT VAR
« Reply #13 on: June 21, 2012, 01:45:00 PM »
Not sure what you mean.  If i open a dwg, and !cmdrun it come back nil, which i was trying to trap.
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

Lee Mac

  • Seagull
  • Posts: 12922
  • London, England
Re: INITIALIZE NONE EXISTANT VAR
« Reply #14 on: June 21, 2012, 01:54:17 PM »
Do you have any other routines that use the variable as a global variable and are possibly setting it to nil?