osmode and looping

osmode and looping

126843
Participant Participant
1,251 Views
3 Replies
Message 1 of 4

osmode and looping

126843
Participant
Participant

Hello

 

whats the difference betwen "code1" and "code2"? Why if i put  "(setvar "osmode" 32)" inside the loop there will be some errors when i press Enter while program will be asking me for an Insertion point?

 

 

 

 ;code1
 
 
(defun c:testprog()

(while

(setvar "osmode" 32)

(setq ip (getpoint "\nInsertion Point : "))  ;when i press enter the program will not quit

(setvar "osmode" 0)

(command "Linia" ip '(0 0 0) "")

)

)

 

 

 

 

 ;code2
 
 
(defun c:testprog()
 
(setvar "osmode" 32)

(while

(setq ip (getpoint "\nInsertion Point : "))  ;now when i press enter, the program will quit properly

(setvar "osmode" 0)

(command "Linia" ip '(0 0 0) "")
 
(setvar "osmode" 32)

)

)
 

 

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

Ranjit_Singh
Advisor
Advisor
Accepted solution

 While loop quits only when the test condition fails. The test condition is the very first statement after (while

Code1 is always setting osmode to 32 (intersection snap).  No matter what you do  inside the while loop; the OS mode test condition is always true and the loop does not quit.

In the second case your test condition is using getpoint. Now, the  test condition will fail if it does not receive a point because that's all getpoint function allows; either a point through mouse click or entering in list format at command prompt, but hitting enter is not an option (unless you change the functions behavior by preceding it with initget) and will return nil.  Hence your condition fails and your while loop quits when you hit enter. 

0 Likes
Message 3 of 4

Ranjit_Singh
Advisor
Advisor
Accepted solution

Looking at code 1 again you are setting the odmode to 0 inside the while loop. However, the test condition resets the odmode to 32. If you want the while loop to quit when osmode is not 32 then change

(while (setvar 'osmode 32)
...........

to

(while (/= 32 (getvar 'osmode))
..........

or some other version where the while loop test will fail at some point. 

0 Likes
Message 4 of 4

ВeekeeCZ
Consultant
Consultant

As the addition to great Ranjit's explanation...

 

This situation is quite common - you have the function which a first line is a test expression but you need to add some predecessor - very often is the (initget), noticed already. So how you gonna do that... Certainly one way is your code 2. But this is not very practical put something, what should be predecessor, at the very end.

 

So other way is wrap those functions together some way, where only last line will count. You can use eg. (progn) or (and)

 

(progn
  (anything)
  (my test expression)
  )

(and (statement which is always T)
     (my test expression)
     )

 

then in your case

(defun c:testprog()
  (while (progn
           (setvar "osmode" 32)
           (setq ip (getpoint "\nInsertion Point: ")))
    (setvar "osmode" 0)
    (command "Linia" ip '(0 0 0) ""))
  )

btw. Proper formatting helps better readability. Blue is wrapped test expression, bold is part that is decisive.

 

 

The second way is avoiding the issue entirely - by using only temporary OSNAP change.

 

(defun c:testprog()
  (setvar "osmode" 32)
  (while (setq ip (getpoint "\nInsertion Point: "))
    (command "_.line "
             "_none" ip
             "_none" '(0 0 0)
             ""))
  )
0 Likes