harilalmn Posted June 22, 2011 Share Posted June 22, 2011 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" ) ) ) Quote Link to comment Share on other sites More sharing options...
MSasu Posted June 22, 2011 Share Posted June 22, 2011 (edited) 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 June 22, 2011 by MSasu Spelling Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted June 22, 2011 Share Posted June 22, 2011 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. Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted June 22, 2011 Share Posted June 22, 2011 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) ) Quote Link to comment Share on other sites More sharing options...
harilalmn Posted June 22, 2011 Author Share Posted June 22, 2011 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? Quote Link to comment Share on other sites More sharing options...
BIGAL Posted June 23, 2011 Share Posted June 23, 2011 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. Quote Link to comment Share on other sites More sharing options...
harilalmn Posted June 23, 2011 Author Share Posted June 23, 2011 Excellnt Ideas...!!! Also I understood where I went wrong.. Thanks to all of you who advised....!!! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.