Jump to content

Running lisp over multiple items


jnky

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

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