getting error "foreach function"

getting error "foreach function"

thsa2501
Enthusiast Enthusiast
453 Views
4 Replies
Message 1 of 5

getting error "foreach function"

thsa2501
Enthusiast
Enthusiast

hi everyone

i want to make the text for the vertex of the polyline as the same point of pline but does not work "foreach" function.it is getting nil. what is wrong with this lisp code?

 

(defun c:7 ()

(setq sel (car (entsel "\npick the pline:")))

(setq db (entget sel))

(foreach var db
(setq pt (cdr var))
(setq xcor (car pt))
(setq ycor (cadr pt))

(entmake (list (cons 0 "text")(cons 10 pt)(cons 40 0.2)(cons 50 0)(cons 1 (strcat "xcor=" rtos xcor 2 2 "," "ycor=" rtos ycor 2 2))))

)
)

0 Likes
Accepted solutions (1)
454 Views
4 Replies
Replies (4)
Message 2 of 5

ВeekeeCZ
Consultant
Consultant
Accepted solution

You can't use all items from (entget). Vertices are just whose with 10 code. '(10 x y z)

You also had missing parents for (rtos) funcs.

 

 

(defun c:7 ( / sel db pt xcor ycor)
  
  (setq sel (car (entsel "\npick the pline:")))
  (setq db (entget sel))
  
  (foreach var db
    (if (= (car var) 10)
      (progn
	(setq pt (cdr var))
	(setq xcor (car pt))
	(setq ycor (cadr pt))
	(entmake (list (cons 0 "text") (cons 40 0.2) (cons 50 0)
		       var
		       (cons 1 (strcat "xcor=" (rtos xcor 2 2) "," "ycor=" (rtos ycor 2 2))))))))
  (princ)
  )

 

Message 3 of 5

thsa2501
Enthusiast
Enthusiast

thank you so much for reply.

0 Likes
Message 4 of 5

calderg1000
Mentor
Mentor

Saludos  @thsa2501 

Aquí, primero obteniendo la lista de coordenadas de los vértices.

 

 

 

 

(defun c:7 ()
  (setq sel (car (entsel "\npick the pline:")))
  (setq db (mapcar 'cdr (vl-remove-if-not '(lambda (x) (= (car x) 10)) (entget sel))))
  (foreach var db
    (setq xcor (car var))
    (setq ycor (cadr var))
    (entmake
      (list (cons 0 "text")
            (cons 10 var)
            (cons 40 0.2)
            (cons 50 0)
            (cons 1 (strcat "xcor=" (rtos xcor 2 2) "," "ycor=" (rtos ycor 2 2))))))
)

 

 

 

 

 


Carlos Calderon G
EESignature
>Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

0 Likes
Message 5 of 5

Kent1Cooper
Consultant
Consultant

@ВeekeeCZ wrote:

....

....
  (setq db (entget sel))
  
  (foreach var db
    (if (= (car var) 10)
      (progn
	(setq pt (cdr var))
	(setq xcor (car pt))
	(setq ycor (cadr pt))
	(entmake (list (cons 0 "text") (cons 40 0.2) (cons 50 0)
		       var
		       (cons 1 (strcat "xcor=" (rtos xcor 2 2) "," "ycor=" (rtos ycor 2 2))))))))
  (princ)
  )

One thing I like about the approach above is that it can just use the 'var' variable [a list with a 10 and the vertex coordinates] directly as the insertion point for the Text.  The approach used in Message 4 pulls the vertex coordinates out of their relationship to a 10, and then needs to join them back with a 10 to make the insertion point entry.  But that's a coincidence of the fact that vertex entries in Polyline entity data are under DXF code 10, the same as the insertion point of left-justified Text.  If a different justification is desired, using DXF 72 & 73 entries, it would be necessary to pull the coordinates away from the 10 and put them into a DXF 11 entry instead.

 

But given left-justification, just for fun, this can be done without three of the variables, and without the (progn) wrapper for the 'then' argument in the (if) function, but can go directly into the Text creation:

....
(foreach var db
  (if (= (car var) 10)
    (entmake ; then
      (list
        (cons 0 "text") (cons 40 0.2) (cons 50 0) var
        (cons 1 (strcat "xcor=" (rtos (cadr var) 2 2) "," "ycor=" (rtos (caddr var) 2 2)))
      ); list
    ); entmake
  ); if
); foreach

....

Kent Cooper, AIA