Recursive function

Recursive function

rajeshpatnaik2001
Advocate Advocate
1,281 Views
3 Replies
Message 1 of 4

Recursive function

rajeshpatnaik2001
Advocate
Advocate

Hi,

 

I have a drawing with network of lines and points. I am writing a program to select all points at the end of lines and do some processing.
- select a start point object
- function will do processing on the point object

- function select the lines connecting the point object
- function select the points at the ends of these lines and do processing

- so on till all points are processed in all the branches

I have written the following code. This is not working properly. It process points in one branch and then stops.

 

(defun c:run( / ent)
(setq line_last nil)
(setq ent (car(entsel)))
(find_point ent)
)

(defun find_point (ent / pt sst sst_line ent_line other_end ent_point cn)
(process_point ent)
(setq sst (ssget "_cp" (get-rect-points (cdr (assoc 10 (entget ent))) 0.1) '((0 . "LWPOLYLINE")) ));;select lines connecting to the point
(if line_last (setq sst_line (ssdel line_last sst)) (setq sst_line sst));;incoming line is removed from selection set
(if (/= (sslength sst_line) 0)
(progn
(setq len (sslength sst_line))
(setq cn 0)
(while (< cn len)
(setq ent_line (ssname sst_line cn))
(setq line_last ent_line)

;;select point on the other end of line
(setq other_end (cdr (assoc 10 (reverse(entget ent_line)))))
(setq ent (ssname (ssget "_cp" (get-rect-points other_end 0.1) '((0 . "POINT")) ) 0))
(if ent (find_point ent))
(setq cn (+ 1 cn))
);while
));if
)

(defun process_point(ent)
(command "change" ent "" "p" "color" "red" "")
)

;;get points of 4 sides at specified distance
(defun get-rect-points (pt dist / pt0$ pt1$ pt2$ pt3$ pt4$)
(setq pt0$ (polar pt 0 dist))
(setq pt1$ (polar pt0$ (/ pi 2) dist))
(setq pt2$ (polar pt1$ pi (* 2 dist)))
(setq pt3$ (polar pt2$ (/ (* 3 pi) 2) (* 2 dist)))
(setq pt4$ (polar pt3$ 0 (* 2 dist)))
(list pt1$ pt2$ pt3$ pt4$ pt1$)
)


Capture.PNG

 

Any help would be highly appreciated.

 

Thanks in advance.

0 Likes
Accepted solutions (1)
1,282 Views
3 Replies
Replies (3)
Message 2 of 4

rajeshpatnaik2001
Advocate
Advocate
Accepted solution

Thanks all for viewing.

I found the bug... 😌
the variable "len" was open and so it was creating the issue.

Thanks 😊

0 Likes
Message 3 of 4

_gile
Consultant
Consultant

Hi,

 

Here's a way.

Instead of using selections which may not be reliable according to the zoom factor, I've rather use a list of points and a list of polylines.

(defun c:run (/ procesPoint getPoints mainrec sslist point points plines)

  (defun processPoint (pt / elst pair)
    (setq elst (entget pt))
    (if	(setq pair (assoc 62 elst))
      (entmod (subst '(62 . 1) pair elst))
      (entmod (append elst '((62 . 1))))
    )
    (vl-remove pt points)
  )

  (defun getPoints (point / ss i)
    (setq pos (cdr (assoc 10 (entget point))))
    (vl-remove
      nil
      (mapcar
	'(lambda (pl)
	   (setq pt	(vlax-curve-getEndPoint pl)
		 plines	(vl-remove pl plines)
	   )
	   (vl-some
	     '(lambda (pt)
		(if (equal (vlax-curve-getEndPoint pl)
			   (cdr (assoc 10 (entget pt)))
		    )
		  pt
		)
	      )
	     points
	   )
	 )
	(vl-remove-if-not
	  '(lambda (pl)
	     (equal (vlax-curve-getStartPoint pl) pos)
	   )
	  plines
	)
      )
    )
  )

  (defun mainrec (point)
    (processPoint point)
    (foreach pt	(getPoints point)
      (mainrec pt)
    )
  )

  (defun sslist	(ss / i lst)
    (repeat (setq i (sslength ss))
      (setq lst (cons (ssname ss (setq i (1- i))) lst))
    )
  )

  (if
    (and
      (setq point (car (entsel "\nSelect start point: ")))
      (= (cdr (assoc 0 (entget point))) "POINT")
    )
     (progn
       (setq points (sslist (ssget "_X" '((0 . "POINT"))))
	     plines (sslist (ssget "_X" '((0 . "LWPOLYLINE"))))
       )
       (mainrec point)
     )
  )
  (princ)
)


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 4 of 4

rajeshpatnaik2001
Advocate
Advocate

Thanks @_gile  for the excellent idea.

0 Likes