PDA

View Full Version : While Loop



ImaJayhawk
12th Nov 2003, 09:34 pm
I wrote a quick autolisp routine to replace one block with another. It works fine except I tried to add a not statement after the while statement so that if I miss clicking on a block it won't stop the command. Now it won't loop or even work one time through. Does anyone see what I did wrong? Thanks.



--ImaJayhawk


----------------------Code Before Change-----------------------------

(defun C:blocky()

;turn the system echo off
(setvar "cmdecho" 0)

(setq blocker (getstring "\nEnter Block Name: "))

(While

(setq enty (entsel "\nSelect Block: "))

(progn

(setq e (entget (car enty)))

(setq xcorr (cadr (assoc 10 e)))

(setq ycorr (caddr (assoc 10 e)))

(setq zcorr (cadddr (assoc 10 e)))

(setq n (list xcorr ycorr zcorr))

(setq angler (cdr (assoc 50 e)))

(command "erase" enty "")

(command "-insert" blocker n "" "" (strcat (rtos angler 2 6) "r"))

) ;progn


) ;while

(setvar "cmdecho" 1)

(princ)

)

----------------------Code After Change--------------------------

(defun C:blocky()

;turn the system echo off
(setvar "cmdecho" 0)

(setq blocker (getstring "\nEnter Block Name: "))

(While (not (setq enty (entsel "\nSelect Block: ")))

(progn

(setq e (entget (car enty)))

(setq xcorr (cadr (assoc 10 e)))

(setq ycorr (caddr (assoc 10 e)))

(setq zcorr (cadddr (assoc 10 e)))

(setq n (list xcorr ycorr zcorr))

(setq angler (cdr (assoc 50 e)))

(command "erase" enty "")

(command "-insert" blocker n "" "" (strcat (rtos angler 2 6) "r"))

) ;progn


) ;while

(setvar "cmdecho" 1)

(princ)

)

fuccaro
13th Nov 2003, 09:22 am
Hello ImaJayhawk!
Here is my attempt. Is it what do you wish?

(defun C:blocky ( / blocker continue enty e)
(setvar "cmdecho" 0)
(setq blocker (getstring "\nEnter Block Name: ")
continue T)
(While continue
(if (setq enty (car (entsel "\nSelect Block: ")))
(progn
(setq e (entget enty))
(command "-insert" blocker (cdr (assoc 10 e)) "" ""
(strcat (rtos (cdr (assoc 50 e)) 2 6) "r"))
(entdel enty)
)
&#40;if &#40;= &#40;strcase &#40;getstring "\nQUIT? y/<n>"&#41;&#41; "Y"&#41; &#40;setq continue nil&#41;&#41;
&#41;
&#41;
&#40;princ&#41;
&#41;
After you ask the user to enter a block name, you should verify that input if it is a valid block name, do not you think?

ImaJayhawk
13th Nov 2003, 08:26 pm
Hey that's what I needed. Thanks.

---ImaJayhawk

fuccaro
14th Nov 2003, 07:02 am
ImaJayhawk
I am glad that it is what you need! If you will stay around here, I am sure you will find more interresting things in this nice Forum.

gcp310
14th Nov 2003, 11:37 pm
Fuccaro is being modest,

So a search on fuccaro postings, you will see his work with LISP is outstanding.

specially in the area of 3d object creation.

G

fuccaro
15th Nov 2003, 06:47 am
I am not modest, G. I sincerly think that this Forum is a nice place! :)

Rbtdanforth
29th Mar 2006, 07:16 pm
I set up an attredef routine a while back but the Battman command has gone way beyond what I did.

For all other cases I have found a much slicker way to change blocks rather than erasing and replacing. :twisted:





&#40;DEFUN C&#58;CHB&#40;/ A B C D X EN&#41;
&#40;SETQ A &#40;PROMPT "GET BLOCKS"&#41;
A &#40;SSGET&#41;
X &#40;ENTSEL " \nPICK BLOCK..."&#41;
B &#40;IF X &#40;SETQ B &#40;CDR &#40;ENTGET &#40;CAR X&#41;&#41;&#41;&#41;
&#40;SETQ B &#40;GETSTRING "BLOCKNAME"&#41;&#41; &#41;
C &#40;SSLENGTH A&#41;
D &#40;IF X &#40;CDR&#40;ASSOC 2 B&#41;&#41; B&#41;
&#41;
&#40;WHILE &#40;> C 0&#41;
&#40;SETQ C &#40;1- C&#41;
EN &#40;ENTGET&#40;SSNAME A C&#41;&#41;
EN &#40;SUBST &#40;CONS 2 D&#41;&#40;ASSOC 2 EN&#41;EN&#41;
&#41;&#40;ENTMOD EN&#41;&#40;ENTUPD &#40;cdar en&#41;&#41;
&#41;
&#41;




All that defines the geometry of a block is the name in Assoc 2 code, change that and the geometry changes but all other codes are the same, so it is the same scale, layer linetype etc. Even the attributes will stay even if they are defined for the previous block.

Interestingly this confuses the Edit command that will insist the block cannot be edited but not the Attedit command that will be normal.

You need not worry about what would happen if the user types the wrong name, because what will happen is nothing.