Jump to content

Recommended Posts

Posted

Sometimes I need to repeatedly use same autolisp routine many times. Each time I have to press space bar to reinvoke the same command. Is it possible to tell autocad to continueuly repeat the command unless we press ESC or repeat the autolisp command by a specified number of times.Thanks.

Posted

Type Multiple, then the command.

  • 7 years later...
Posted

Same doubt. The "while" loop worked. But I'm confused about the syntaxe.

I tried

(while (T)'infinite loop, exitted by pressing space, enter or esc
(commands))

and the CAD didn't buy it. Then I tried changing to

(while(= 1 1)'same thing, written differently
(commands))

and it worked. But it looks a little dumb.

Is there a good-looking way to put this into code?

Posted
Same doubt. The "while" loop worked. But I'm confused about the syntaxe.

I tried

(while (T)'infinite loop, exitted by pressing space, enter or esc
(commands))

and the CAD didn't buy it. Then I tried changing to

(while(= 1 1)'same thing, written differently
(commands))

and it worked. But it looks a little dumb.

Is there a good-looking way to put this into code?

 

It all depends on what you are doing in the loop. Here's a simple example that will place points repeatedly until a point is not picked.

(while (setq p (getpoint "\nPick a point: "))
 (entmakex (list '(0 . "point") '(8 . "point") (cons 10 p)))
)

Posted

Your first example should be:

(while t
   (command ...)
   ...
)

But using an infinite loop should be avoided where possible.

Posted

Re a specified number of times You can use repeat as well as a while

 

(repeat 3 
... your code
)
or
(setq x 1)
(while (< x 4)
...... your code
(setq x (+ x 1))
)

  • Thanks 1
  • 1 month later...
Posted
It all depends on what you are doing in the loop. Here's a simple example that will place points repeatedly until a point is not picked.

(while (setq p (getpoint "\nPick a point: "))
 (entmakex (list '(0 . "point") '(8 . "point") (cons 10 p)))
)

 

Really beginner, here, please comment everything.

Although I assume the second line has nothing to do with the loop, it seems pretty interesting. I can't make sense of any of it and will search for those commands.

But the point inquestion, here, is the first line. How should I read it? Because right now, I'm reading: "while (command)", which doesn't make sense to me.

Should I read "While (whathever the command returns)" and understand that when the user does not click it'll return nill and end the loop?

Wow, it DOES make sense! Just confirm, please, if that's that!

Posted
Really beginner, here, please comment everything.

...

Should I read "While (whathever the command returns)" and understand that when the user does not click it'll return nill and end the loop?

Wow, it DOES make sense! Just confirm, please, if that's that!

Exactly :)

Posted
Should I read "While (whathever the command returns)" and understand that when the user does not click it'll return nill and end the loop?

not exactly, but close. Whathever the evaluation of the first expression returns. (it could be a variable, a subfunction, or a function, but not really a command, since on top of my head a command never returns "nil").

 

if we look at ronjp's example, the part that make the while go on or stop is the (setq p (getpoint "\nPick a point: ")). LEts look at it on its own.

Command: (setq p (getpoint "\nPick a point: "))

Pick a point: (6.95981 2.86731 0.0)

Command: !p

(6.95981 2.86731 0.0)

The point you pick (if you pick a point) is stored in the variable P. As long as you pick a point, P is non nil, and the second part gets evaluated. It (the enmakex) then creates a graphical point, using the coordinates stored in the variable P. If you were to hit return or spacebar instead,

Command: (setq p (getpoint "\nPick a point: "))

Pick a point: (hit enter or space bar instead of clicking here)

nil

Command: !p

nil

P becomes nil, and the while's evaluation stops.

Posted

You could create a button on a toolbar put “*” at the beginning the macro and that will cause it to loop.

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