(= A nil) and at same time (= B nil) ... stop the while XX

(= A nil) and at same time (= B nil) ... stop the while XX

Anonymous
Not applicable
1,781 Views
26 Replies
Message 1 of 27

(= A nil) and at same time (= B nil) ... stop the while XX

Anonymous
Not applicable

Good afternoon everybody.
I have a routine that writes the value of getpoin "A" and getpoint "B", and now I need it when (= A nil) and at the same time (= B nil), the routine leaves the cycle and stops.
someone tell me how do i stop when two entrances (getpoint) are invalid, o (while xx, stop?

 

Thanks

 

0 Likes
Accepted solutions (2)
1,782 Views
26 Replies
Replies (26)
Message 2 of 27

pbejse
Mentor
Mentor

@Anonymous wrote:

Good afternoon everybody.
I have a routine that writes the value of getpoin "A" and getpoint "B", and now I need it when (= A nil) and at

... 

Thanks


 

(while
  (And (setq a (Getpoint "\Pick first point:"))
       (setq b (Getpoint a "\Pick next point:"))
  )
   (print a)
   (print b)
   (princ)
)

 

once A is nil there is no point in testing B yes"?

use OR  

 

(while
  (or (setq a (Getpoint "\Pick first point:"))
       (setq b (Getpoint "\Pick next point:"))
  )

   (and a (print a))
   (and b (print b))
   (princ)
)

 

 

HTH

 

EDIT: Now I'm confused 😄

0 Likes
Message 3 of 27

Anonymous
Not applicable

I am sorry, I did not express myself well.

What my routine does is:

 

get the value of getpoint "A" and getpoint "B".

 

and what I need is that when both (at the same time) are (invalid) "nil" the routine ends and leaves the rest of the operations.

 

sorry I didn't explain myself well

0 Likes
Message 4 of 27

pbejse
Mentor
Mentor

@Anonymous wrote:

get the value of getpoint "A" and getpoint "B".

 


 

(While
  (progn
  	(setq a (Getpoint "\nPick first point:"))
	(setq b (Getpoint "\nPick next point:"))
  	(or a b)
    )
   (and a (print "A"))
   (and b (print "B"))
   (princ)
  )

 

HTH

 

 

Message 5 of 27

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

....get the value of getpoint "A" and getpoint "B".

.... when both (at the same time) are (invalid) "nil" the routine ends and leaves the rest of the operations.

....


(if (and (not A) (not B))

  (quit); then

  (progn ; else

    ... other operations ...

  ); progn

); if

 

Or, to turn it around the other way:

 

(if (or A B)

  (progn ; then

    ... other operations ...

  ); progn

); if

 

No need for an 'else' argument to (quit) in that case.

 

The (exit) function can be used instead of (quit) [they're equivalent].

Kent Cooper, AIA
Message 6 of 27

ronjonp
Advisor
Advisor

Another:

 

(while (and a b)
       ;; Do your stuff
)

Another example with same logic

 

;; Set variables outside of loop
(setq a	1
      b	1
      c	0
)
(while (and a b)
  ;; If 'c' = 100 reset 'a' & 'b' to exit while loop
  (if (= (setq c (1+ c)) 100)
    ;; print nil
    (print (setq a nil
		 b nil
	   )
    )
    (progn ;; Print incremented 'c'
	   (print c)
	   ;; Do your stuff
    )
  )
)
Message 7 of 27

Kent1Cooper
Consultant
Consultant

@ronjonp wrote:

Another:

(while (and a b)
       ;; Do your stuff
)

....


If I understand correctly, that should be an (or) function, not (and):

(while (or a b)
       ;; Do your stuff
)

because they only want to give up when both A and B at the same time are nil [see Message 3], which means that if either of them exists, they want to keep going.

Kent Cooper, AIA
0 Likes
Message 8 of 27

ronjonp
Advisor
Advisor

@Kent1Cooper  Ah yes .. you are correct. Not enough coffee this morning 🙂 Now if only I could go edit my incorrect post... 🤓

 

ronjonp_0-1616089973907.png

 

Message 9 of 27

ВeekeeCZ
Consultant
Consultant

You can simply (if (= a b nil) (exit))

Message 10 of 27

pbejse
Mentor
Mentor

@ronjonp wrote:

Now if only I could go edit my incorrect post... 🤓


And you already know the answer to that.

 

I also suggested both options  (and a b) and (or a b) , then i was told "thats not it". my post after should satisfy the requirement

 

(Defun c:foo ()
  (While
    (progn
      (setq a (Getpoint "\nPick first point:"))
      (setq b (Getpoint "\nPick next point:"))
      (or a b)
    )
     (and a (print "A"))
     (and b (print "B"))
     (princ)
  )
)

 

 

Command: FOO
Pick first point: < user press enter >
Pick next point: < user pick point > 
"B"
Pick first point: < user pick point > 
Pick next point: < user press enter >
"A"
Pick first point: < user pick point > 
Pick next point: < user pick point > 
"A"
"B"
Pick first point: < user press enter >
Pick next point: < user press enter >

exit

 

But hey, It appears that the OP doesn't agree with my suggestion 😊

 

 

 

0 Likes
Message 11 of 27

Anonymous
Not applicable
thanks to everyone, this part I already managed to solve.

now I lack another part, which is:

when a point (getpoint a) has been accepted and has value, but the second (getpoint b) does not have is (nil), I need this to result in a string that can then be written in a block with attributes in which there are two fields, the first and a second, and so in this case the two would be pre-filled, but the first with the value of the getpoint and the second with the string ""

I don't know if it's very explicit, I'll send the routine to understand.
0 Likes
Message 12 of 27

Anonymous
Not applicable

(defun C:CT ()
(setvar "cmdecho" 0)

(setvar "DIMZIN" 0)
(COMMAND "LAYER" "m" "A-00-COTA_NIV" "T"
"A-00-COTA_NIV" "S" "A-00-COTA_NIV" ""
)
(setq sc (getreal "\nscale ? "))
(setq P (getint "\nhow many decimal ? "))
(setq a "T")
(setq r 0)

(while a
(setq pt1 (getpoint "\npoint (NA) ? : "))
(setq pt2 (getpoint "\npoint (NO) ? : "))
(if (= pt1 pt2 nil) (set a nil))
(progn
(setq y1 (cadr pt1))
(setq y2 (cadr pt2))
(setq aa (atof (rtos y1 2 P)))
(setq aa1 (atof (rtos y2 2 P)))

(if (> aa 0)
(progn
(setq yy (strcat "+" (rtos y1 2 P) " (NA)"))
)
)
(if (> aa1 0)
(progn
(setq yy1 (strcat "+" (rtos y2 2 P) " (NO)"))
)
)
(if (< aa 0)
(progn
(setq yy (strcat (rtos y1 2 P) " (NA)"))
)
)
(if (< aa1 0)
(progn
(setq yy1 (strcat (rtos y2 2 P) " (NO)"))
)
)

(if (= pt1 nil) (setq yy " "))
(if (= pt2 nil) (setq yy1 " "))
);progn
(command "insert" "nivel_corte_na+no" pt1 sc "" r yy yy1)
(setvar "cmdecho" 1)
);while
);defun

 

______________________________________________

but the part of just writing one of the two pt1 whether it be pt1 or pt2 is not working. 

someone can help me, I think it must be a simple thing, but I'm not getting it

 

 

0 Likes
Message 13 of 27

pbejse
Mentor
Mentor
Accepted solution

On your first post, you want the loop to end when both is nil. The way have it right now, regardless if both pt1 and pt2 is nil it will still continue,  you are evaluating variable a as a condition for while. the only way to exit is to press esc. as the value for variable a remains the same never change.

 

Do you need it to exit  and still insert the block? You cant because there is no pt1 or pt2 for insertion, unless you use pause for the insertion and leave the two attribtue values as default or " " .

 

...
(while 
    (progn
	    (setq pt1 (getpoint "\npoint (NA) ? : "))
	    (setq pt2 (getpoint "\npoint (NO) ? : "))
	    (or pt1 pt2)
    )
    (cond
	((And pt1
	      (setq y1 (cadr pt1))
	      (setq yy (strcat  "+" (rtos y1 2 P)  " (NA)"))
	      pt2)
		(setq y2 (cadr pt2))
       		(setq yy1 (strcat "+" (rtos y2 2 P)  " (NO)"))  
	 )
	( pt1
	 	(setq yy1 " ")
	 )
	( pt2
	 	(setq yy " " pt1 pt2)
	 	(setq y2 (cadr pt2))
	 	(setq yy1 (strcat "+" (rtos y2 2 P)  " (NO)"))
		)
	)
    
	(command "insert" "nivel_corte_na+no" pt1 sc "" r yy yy1)
    )
(command "insert" "nivel_corte_na+no" pause sc "" r "" "")
)

 

HTH

 

 

Message 14 of 27

ronjonp
Advisor
Advisor

Looking at the code you posted I think you want to check that pt1 and pt2 are valid. Excuse me if I'm off base .. didn't look much past (while...

 

(defun c:ct (/ aa aa1 p pt1 pt2 r sc y1 y2 yy yy1)
  (setvar "cmdecho" 0)
  (setvar "DIMZIN" 0)
  (command "LAYER" "m" "A-00-COTA_NIV" "T" "A-00-COTA_NIV" "S" "A-00-COTA_NIV" "")
  (if (and (setq sc (getreal "\nscale ? ")) (setq p (getint "\nhow many decimal ? ")) (setq r 0))
    (while
      (and (setq pt1 (getpoint "\npoint (NA) ? : ")) (setq pt2 (getpoint "\npoint (NO) ? : ")))
       (setq y1 (cadr pt1))
       (setq y2 (cadr pt2))
       (setq aa (atof (rtos y1 2 p)))
       (setq aa1 (atof (rtos y2 2 p)))
       (if (> aa 0)
	 (progn (setq yy (strcat "+" (rtos y1 2 p) " (NA)")))
       )
       (if (> aa1 0)
	 (progn (setq yy1 (strcat "+" (rtos y2 2 p) " (NO)")))
       )
       (if (< aa 0)
	 (progn (setq yy (strcat (rtos y1 2 p) " (NA)")))
       )
       (if (< aa1 0)
	 (progn (setq yy1 (strcat (rtos y2 2 p) " (NO)")))
       )
       (if (= pt1 nil)
	 (setq yy " ")
       )
       (if (= pt2 nil)
	 (setq yy1 " ")
       )				;progn
       (command "insert" "nivel_corte_na+no" pt1 sc "" r yy yy1)
       (setvar "cmdecho" 1)
    )
  )					;while
)					;defun

 

Message 15 of 27

Anonymous
Not applicable

thank you

0 Likes
Message 16 of 27

Anonymous
Not applicable

thank you very much, it is working now !

thanks

JM

0 Likes
Message 17 of 27

ronjonp
Advisor
Advisor

Glad to help 🍻

Message 18 of 27

Anonymous
Not applicable

I really appreciate the help, after modifying the routine, it works well in that aspect of skipping the first input if there is no valid value and the second, all right ...
now I just need it, and I just saw it now, what I need is the part of:

 

(setq aa (atof (rtos y1 2 p)))
(setq aa1 (atof (rtos y2 2 p)))
(if (> aa 0) (progn (setq yy (strcat "+" (rtos y1 2 p) "(NA)"))))
(if (<aa 0) (progn (setq yy (strcat (rtos y1 2 p) "(NA)"))))
(if (> aa1 0) (progn (setq yy1 (strcat "+" (rtos y2 2 p) "(NO)"))))
(if (<aa1 0) (progn (setq yy1 (strcat (rtos y2 2 p) "(NO)"))))

 

If i use (if ...) gives an error.

 

0 Likes
Message 19 of 27

ВeekeeCZ
Consultant
Consultant

This is what would I do. Dig the help first, then ask if you need some explanation.

 

(defun c:ct_5Set ()
  
  (or *ct-s*
      (setq *ct-s* 1)) ; default scale
  (initget 6)
  (setq *ct-s* (cond ((getreal (strcat "\nScale <" (vl-princ-to-string *ct-s*) ">: ")))
		     (*ct-s*)))
  (or *ct-d*
      (setq *ct-d* 3)) ; default no of decimals
  (initget 6)
  (setq *ct-d* (cond ((getint (strcat "\nDecimals <" (vl-princ-to-string *ct-d*) ">: ")))
		     (*ct-d*)))
  (princ)
  )



(defun c:ct_5 ( / *error* cmd dzn cly arq na no)
  
  (defun *error* (errmsg)
    (if (not (wcmatch errmsg "Function cancelled,quit / exit abort,console break,end"))
      (princ (strcat "\nError: " errmsg)))
    (if cmd (setvar 'cmdecho cmd))
    (if dzn (setvar 'dimzin dzn))
    (if cly (setvar 'clayer cly))
    (if arq (setvar 'attreq arq))
    (princ))
  
  ; -----------------------------------------------------------------------------------------------------------
  
  (setq cmd (getvar 'cmdecho)) 	(setvar 'cmdecho 0)
  (setq dzn (getvar 'dimzin)) 	(setvar 'dimzin 0)
  (setq cly (getvar 'clayer))   (command  "_.layer" "_t" "A-00-COTA_NIV" "_m" "A-00-COTA_NIV" "")
  (setq arq (getvar 'attreq))   (setvar 'attreq 0)
  
  (if (not (and *ct-s* *ct-d*))
    (c:ct_5Set))
  
  (setq na (cadr (getpoint "\nPoint (NA): " ))
	na (if na
	     (strcat (if (minusp na) "" "+") (rtos na 2 *ct-d*))
	     "")
	no (cadr (getpoint "\nPoint (NO): " ))
	no (if no
	     (strcat (if (minusp no) "" "+") (rtos no 2 *ct-d*))
	     ""))
  
  (command "_.insert" "nivel_corte_na+no" "_s" *ct-s* "_r" 0 pause)
  (setpropertyvalue (entlast) "000,00_(NA)" na)
  (setpropertyvalue (entlast) "000,00_(NO)" no)
  
  (*error* "end")
  )

 

 
0 Likes
Message 20 of 27

Anonymous
Not applicable

  

0 Likes