Jump to content

Question about exiting a while loop


MarcoW

Recommended Posts

Hi there,

 

In a piece of code of mine I use this:

 


(princ "\nSpecify insertion point")

(while
 (not
   (command "_.insert" BlockName "_s" 
(getvar "DIMSCALE") pause pause)
 ) 
;_not
      ) ;_while

 

It works nice, however I have two questions of wich I believe I should be able to answer myself but I just don't get it.

 

1. What is the use of NOT in the code? I know I need it in there but how do I read it in a way that I can understand.

ie.: what is there to "not" do? It feels like it does not make sense wich of course is stupid for without it it does not stay in the loop at all.

 

2. I can only exit by wich is fine since my error trapping works and saves me when it comes to restoring variables. (Thanks to Lee Macs tutorial on error trapping).

But I find it more natural to exit on wich is my right mousebutton.

 

Any help is appreciated as always!

Link to comment
Share on other sites

1) (command) always returns nil. (not) returns T if the test returns nil. So as long as the test returns nil or the esc key hadn't been used, the loop continues

 

2) You will need to overhaul the entire snippet to have the enter key exit

 

Maybe:

 

(while (getpoint "\nInsert Point: ") ....

 

-David

Link to comment
Share on other sites

Hi Marco,

 

The command function always returns nil, so, by using the logical not function, the while test condition is always satisfied, hence you remain in the loop.

 

Another way to approach this might be something like:

 

 (setq LastEntity T
       Scale      (getvar 'DIMSCALE)
       BlockName  "MyBlock.dwg"
 )

 (while (not (equal LastEntity ThisEntity))
   (setq LastEntity (entlast))
   (command "_.-insert" BlockName "_S" scale pause pause)
   (setq ThisEntity (entlast))
 )

 

However, because you are using the INSERT command to insert your block, this doesn't allow a null input when prompting for a point, so again, Esc is required to exit the loop.

 

A possible alternative to avoid the use of Esc would be to use getpoint / getangle prompts:

 

 (setq Scale     (getvar 'DIMSCALE)
       BlockName "test.dwg"
 )

 (while
   (and
     (setq pt (getpoint "\nSpecify Point: "))
     (setq an (getangle "\nSpecify Angle: " pt))
   )
   (command "_.-insert" BlockName "_S" scale pt (angtos an))
 )

 

 

Lee

Link to comment
Share on other sites

@ Lee & David

 

The command function always returns nil, so, by using the logical not function, the while test condition is always satisfied, hence you remain in the loop.

 

Thanks for the replies! Now I know... it is easy once you know :)

 

However, because you are using the INSERT command to insert your block, this doesn't allow a null input when prompting for a point, so again, Esc is required to exit the loop.

 

A possible alternative to avoid the use of Esc would be to use getpoint / getangle prompts:

 

 (setq Scale     (getvar 'DIMSCALE)
       BlockName "test.dwg"
 )

 (while
   (and
     (setq pt (getpoint "\nSpecify Point: "))
     (setq an (getangle "\nSpecify Angle: " pt))
   )
   (command "_.-insert" BlockName "_S" scale pt (angtos an))
 )

 

I had it running with grread (something like (= (cadr gr) 13) but changed it back to regular inserting because of the osnaps I want to be able to use.

 

The other part of using the getpoint is what I had also but this does not give you the preview of a block while dragging the mouse around the screen.

 

Time for coffee.

Link to comment
Share on other sites

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...