Troubles with if / cond /else?

Troubles with if / cond /else?

thomas.schive
Enthusiast Enthusiast
848 Views
3 Replies
Message 1 of 4

Troubles with if / cond /else?

thomas.schive
Enthusiast
Enthusiast

Hello!

 

This seem to give me a different result for everytime run the routine:

 

Earlier in the code the variables punkta and punktb are getpoints and the punkty is sat to:

 

(setq vinkelabradianer (angle (trans punkta 1 0) (trans punktb 1 0)))

(setq punkty (polar punkta vinkelabradianer 1000)

 

For the initget it's written like this:

 

(initget 1 "hoyre venstre")
    (setq
        referansespor (getkword "\ Referansespor [hoyre/venstre] <hoyre>: ")
    )

 

 

IF - this could possible be better with some sort of "else"?

 

(if (= referansespor "hoyre")
        (progn
            (setq
                punktz (list (- (car punkta) deltax) (+ (cadr punkta) deltay) 0.0)
                punktx (list (- (car punkty) deltax) (+ (cadr punkty) deltay) 0.0)
            )
            (command "._pline" punktz punktx "")
        )
    )
        ;else
    (if (= referansespor "venstre")    
        (progn
            (setq
                punktz (list (+ (car punkta) deltax) (- (cadr punkta) deltay) 0.0)
                punktx (list (+ (car punkty) deltax) (- (cadr punkty) deltay) 0.0)
            )
            (command "._pline" punktz punktx "")
        )
    )

 

 

Any errors here? Sometimes it works, sometimes it's just getting duplicates of the same polyline.

 

/ts

0 Likes
Accepted solutions (1)
849 Views
3 Replies
Replies (3)
Message 2 of 4

ВeekeeCZ
Consultant
Consultant
Accepted solution

Probably the best is this one...

 

(initget "Hoyre Venstre")
(setq referansespor (cond ((getkword "\nReferansespor [Hoyre/Venstre] <Hoyre>: "))
                      ("Hoere")))								; this catch nil which is returen by hit Enter


(cond ((= referansespor "Hoyre")
       (setq
         punktz (list (- (car punkta) deltax) (+ (cadr punkta) deltay) 0.0)
         punktx (list (- (car punkty) deltax) (+ (cadr punkty) deltay) 0.0)
         )
       (command "._pline" punktz punktx ""))

      ((= referansespor "Venstre")
       (setq
         punktz (list (+ (car punkta) deltax) (- (cadr punkta) deltay) 0.0)
         punktx (list (+ (car punkty) deltax) (- (cadr punkty) deltay) 0.0)
         )
       (command "._pline" punktz punktx "")))

  

Ou, I see you have (initget 1)... but you tell the user to hit <enter> ... you should do one way or another...

 

 

Also possible:

 

(initget "Hoyre Venstre")
(getkword "\nReferansespor [Hoyre/Venstre] <Hoyre>: ")   ; if you hit enter, then it returns nil!

(if (= referansespor "Venstre")
  (progn ); if venstre
  (progn ); all the other cases like "hoyer" or nil (= enter)

 

 

Notes:

/ mean nothing - /n is for new line

Hoyre - capitalized (first) letter allows you to select the option by this letter only. (no need to spell out the whole letter)

cond in this case is probable better, because it take the first True condition and does not test the others. (unlike your multiple ifs...)

Be careful of running OSNAPS when you're using (command). Either turn it off before, or use 

 

(command "._pline" "_non" punktz "_non" punktx "")

 

Message 3 of 4

thomas.schive
Enthusiast
Enthusiast

Splendid, thanks a lot. I think the problem actually was with the command pline part without the "_non", but great to have a better cond than my if's too! /ts

0 Likes
Message 4 of 4

roland.r71
Collaborator
Collaborator

There's a better way to do the getkword too...

You can make it remember your last choice and/or use a default value

& you can make it use this previous/default choice when user hits [enter]

 

I almost exclusively build my 'prompts' this way. (except when it is not desired to use a previous or default choice)

 

; The following will ask user for choice
; If user hits enter, it will use previous choice or default
; If the var Referansespor is kept as a global variable
; it will 'remember' the choice made the previous run 
; (within the same drawing session)
(initget "Hoyre Venstre")
(setq Referansespor
   (cond
      ((getkword 
            (strcat "\nReferansespor [Hoyre/Venstre] <"
               (setq Referansespor 
                  (cond (Referansespor) ("Hoyre")) ; Previous or default
               ) ">: "
            )
         )
      ) 
      (Referansespor) ; in case of [enter]
   )
)
(Cond 
   ((= Referansespor "Hoyre")
      ; Do whatever you wish for choice "Hoyre"
) ((= Referansespor "Venstre") ; Do whatever you wish for choice "Venstre"
) )
0 Likes