Jump to content

Repeat Loop


harilalmn

Recommended Posts

Hi All,

I am trying to write a program to loop the insertion of a block untill I press escape. I wrote the code like shown below, but for some reason it stops after the first insertion of the block. It doesn't loop any further. What is wrong?

 

(defun c:IGB()
(setq p 0)
 (while (/= p 1)
(command "INSERT" "Grid_Bubble" )
  )
)

Link to comment
Share on other sites

Two comments: (1) you have to provide the insertion point, scale factors and angle to that command call and (2) your counter isn't modified in the WHILE cycle. In fact if the command syntax was respected that excerpt of code should run infinitely.

 

(defun c:IGB()
(setq p 0)

(while (/= p 1)
 (command "INSERT" "Grid_Bubble" [color=red]InsertionPoint ScaleOnX ScaleOnY InsertionAngle[/color])

 [color=red](setq p (getint "Continue?"))[/color]
)
)

 

 

Regards,

Mircea

Edited by MSasu
Spelling
Link to comment
Share on other sites

Using the vl-cmdf function in place of command will return T or nil depending upon whether the command was successful since vl-cmdf evaluates the given parameters before submitting the command.

 

So, for a non-attributed block it could be something like:

 

(defun c:IGB nil
 (while (vl-cmdf "_.-insert" "Grid_Bubble" "_S" 1 "_R" 0 pause))
 (princ)
)

To account for attributes you could use:

 

(defun c:IGB nil
 (while (vl-cmdf "_.-insert" "Grid_Bubble" "_S" 1 "_R" 0 pause)
   (while (= 1 (logand 1 (getvar 'CMDACTIVE))) (command ""))
 )
 (princ)
)

To submit the default values.

 

Note that the above code is very basic with no error trapping to check for existence of the block, etc. Also, since the above code uses Visual LISP functions, you may need to add :

 

(vl-load-com)

To either the code or your ACADDOC.lsp.

Link to comment
Share on other sites

This would offer more error trapping:

 

(defun c:IGB ( / *error* blk old var )

 (setq blk "Grid_Bubble") ;; Block to Insert

 (defun *error* ( msg )
   (if old (mapcar 'setvar var old))
   (or (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*")
       (princ (strcat "\n** Error: " msg " **")))
   (princ)
 )

 (cond
   ( (not
       (or
         (tblsearch "BLOCK" blk)
         (setq blk (findfile (strcat blk ".dwg")))
       )
     )
     (princ "\n--> Block not Found.")
   )
   ( t
     (setq var '("CMDECHO" "ATTREQ")
           old  (mapcar 'getvar var)
     )
     (mapcar 'setvar var '(0 0))
     (princ "\nSpecify Point for Block: ")
     (while (vl-cmdf "_.-insert" blk "_S" 1 "_R" 0 pause))
     (mapcar 'setvar var old)
   )
 )
 (princ)
)

Link to comment
Share on other sites

Well... I am happy... I got a lot of options now..

thanks everyone..

 

With all these chit-chats, I landed up on this and this is what i wanted;

 

(defun c:IGB()
(setq p 0)

(while (/= p 1)
 (setq Insertionpoint (getpoint "\nBlock Insertion Point : "))
 (Setq ScaleOnX 1)
 (Setq ScaleOnY 1)
 (Setq InsertionAngle 0)
 (Setq GridNo (Getstring "\nGrid Number"))
 (command "INSERT" "Grid_Bubble" InsertionPoint ScaleOnX ScaleOnY InsertionAngle GridNo)
;  (setq p (getint "Continue?"))
)
)

 

But one thing I still not understand.

Why doesn't it work with my first code...!!?

Is it because I was lacking the arguments to the insert command?

Link to comment
Share on other sites

It was answered in the first reply but also why would you not add a counter so as you add bubbles the grid number increases.

 

put (Setq GridNo (Getstring "\nGrid Number")) at top

 

Then replace in while with (Setq GridNo (+ Gridno 1)) if you want alphas a-z then you need to play with (chr x) search here sure there are lots of grid lsp's that do num & alpha.

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