jnky Posted December 6, 2013 Share Posted December 6, 2013 Hi, Im a bit of a lisp novice and am struggling with expending on a lisp routine I have been trying to write. The routine takes a piece of text which is a number, stores the x,y and z (which is the number) and then inserts a block to those coordinates. What I wrote so far works for a single piece of text but I've been scratching my head about how I can get it to work with multiple items, I've had a look at the 'while' function but I'm not getting it. Does anyone have any tips to share to get me moving again? ;Get text points GB 06.12.13 (defun c:GTP () (prompt "\nSelect text entities : ") ;Create windowed selection set (setq a (ssget)) ;Store (setq b (entlast)) ;Store entitys (setq c (entget b)) ;Store Item (setq xyzval (cdr (assoc 10 c))) ;Store xyz of text (setq zval (cdr (assoc 1 c))) ;Extract Text value and store as z (setq xval (nth 0 xyzval)) ;Seperate x (setq xval (rtos xval)) ;Store x as string (setq yval (nth 1 xyzval)) ;Seperate y (setq yval (rtos yval)) ;Store y as string (setq xyzval (strcat xval "," yval "," zval)) ;Create x,y,z (command "-insert" "I:/Users/Graham/Documents/work/Carriage/Blocks/m10_nut" xyzval "1" "1" "0") ;Insert block (princ) ) Quote Link to comment Share on other sites More sharing options...
MSasu Posted December 6, 2013 Share Posted December 6, 2013 Please take care that ENTLAST is taking the last entity added to database and had nothing to do with the selection performed on previous statement. (setq a (ssget)) ;Store (setq b (entlast)) ;Store entitys (setq c (entget b)) ;Store Item Quote Link to comment Share on other sites More sharing options...
jnky Posted December 6, 2013 Author Share Posted December 6, 2013 I see what your saying, that has just made my issue worse Quote Link to comment Share on other sites More sharing options...
MSasu Posted December 6, 2013 Share Posted December 6, 2013 On parsing the selection set created by SSGET, I suggest you to read this tutorial written by LeeMac. May want also to take a look in help for SSGET filters. Quote Link to comment Share on other sites More sharing options...
CheSyn Posted December 6, 2013 Share Posted December 6, 2013 Would something like this work for you? If so, I would be happy to explain what is going on. (defun c:GTP ( / ss no p1) (prompt "\nSelect text entities : ") (setq ss (ssget '( (0 . "TEXT") )) no 0 ) (while (< no (sslength ss) ) (setq p1 (cdr (assoc 10 (entget (ssname ss no)))) ) (command "_.-insert" "I:/Users/Graham/Documents/work/Carriage/Blocks/m10_nut" p1 "1" "1" "0") (setq no (1+ no) ) ) (princ) ) Quote Link to comment Share on other sites More sharing options...
jnky Posted December 7, 2013 Author Share Posted December 7, 2013 Thanks CheSyn the 'while\repeat' function worked great, but it didn't put the block on the z value depicted in the text value, Ill try and fathom it, thanks a lot for answering Quote Link to comment Share on other sites More sharing options...
jnky Posted December 7, 2013 Author Share Posted December 7, 2013 I have managed to get it working but I'm sure the code is not efficient: Thanks a lot for your help CheSyn and MSasu not only for the assistance in this application but in helping me understand looping better. ;Get text points GB 06.12.13 (defun c:GTP () (prompt "\nSelect text entities : ") (setq a (ssget '( (0 . "TEXT") )) no 0 ) (while (< no (sslength a)) (setq xyzval (cdr (assoc 10 (entget (ssname a no)))) ) (setq zval (cdr (assoc 1 (entget (ssname a no)))) ) (setq xval (nth 0 xyzval)) (setq xval (rtos xval)) (setq yval (nth 1 xyzval)) (setq yval (rtos yval)) (setq xyzval (strcat xval "," yval "," zval)) (command "-insert" "I:/Users/Graham/Documents/work/Carriage/Blocks/m10_nut" xyzval "1" "1" "0") (setq no (1+ no) ) ) (princ) ) Quote Link to comment Share on other sites More sharing options...
MSasu Posted December 7, 2013 Share Posted December 7, 2013 I have tweaked your routine with some comments: (defun c:GTP( / selSet entity pointText pointBlock no ) [color=blue];don't forget to localize variables[/color] (prompt "\nSelect text entities : ") (setq no 0 ) [color=blue];initiate counter here to avoid usage of PROGN[/color] (if (setq selSet (ssget '((0 . "TEXT") ))) [color=blue];make sure user is making a valid selection[/color] (while (< no (sslength selSet)) (setq entity (entget (ssname selSet no)) [color=blue];not need to retrieve the entity twice from selection set[/color] pointText (cdr (assoc 10 entity))) [color=blue];a point may be a list, may avoid inputting it as string[/color] (setq pointBlock (list (nth 0 pointText) [color=blue];there is no need for those intermediate variables[/color] (nth 1 pointText) (distof (cdr (assoc 1 entity))))) [color=blue];use an underscore on command call to take care of localized AutoCADs[/color] (command "_-insert" "I:/Users/Graham/Documents/work/Carriage/Blocks/m10_nut" pointBlock "1" "1" "0") (setq no (1+ no)) ) ) (princ) ) Please try to use relevant names for variables, it will be easier later to revise the code; also avoid using the same variable to store different types (i.e. xyzval was used for a point, then a string). Quote Link to comment Share on other sites More sharing options...
Tharwat Posted December 7, 2013 Share Posted December 7, 2013 Mircea , The name of the selection set with the sslength function is wrong . Quote Link to comment Share on other sites More sharing options...
MSasu Posted December 7, 2013 Share Posted December 7, 2013 Thanks for observation Tharwat; I believe that you noticed one of the intermediate "releases" (I edited the code here). Quote Link to comment Share on other sites More sharing options...
Tharwat Posted December 7, 2013 Share Posted December 7, 2013 Thanks for observation Tharwat; I believe that you noticed one of the intermediate "releases" (I edited the code here). Correct , and I have been in these situation before . Quote Link to comment Share on other sites More sharing options...
jnky Posted December 7, 2013 Author Share Posted December 7, 2013 Excellent MSasu, Looks like you took pity on me in the end, I appreciate you breaking the code down for me and making it more efficient and I shall endeavor to understand it. Thanks a lot Quote Link to comment Share on other sites More sharing options...
MSasu Posted December 7, 2013 Share Posted December 7, 2013 You're entirely welcome! I'm happy to see people struggling to learn, so hope that that will be useful to your progress. 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.