@kajanthangavel Here is your code. It should work OK according to my tests, but you newer know.Since you know some tricks with autolisp you should add to my code writing out to output file however you like it.
Also, rename your blocks so there is no empty space in block name i.e. replace " " with "_" or "-", and don't change MH-ID.
Code uses vector algebra, if you need some explanation just ask. At the moment I'm not having time to do so.
(defun c:mhout
( / take take2 pointlist2d create_value_vector unit_vect vect_dot mod_vect m*v
rot_vect acos asin angle_strike getattributevalue cable cable_points ss i eo
vec_list ip mh_name cnt cnt_prev cnt_next vec_in vec_out in out
)
(defun take (amount lst / ret)(repeat amount (setq ret (cons (car lst) (take (1- amount) (cdr lst))))))
(defun take2 (lst)(take 2 lst))
(defun pointlist2d (lst / ret) (while lst (setq ret (cons (take 2 lst) ret) lst (cddr lst))) (reverse ret))
(defun create_value_vector (n val / r ) (repeat n (setq r (cons val r))) r)
(defun unit_vect (v)(mapcar '* v (create_value_vector (length v) (/ 1 (mod_vect v)))))
(defun vect_dot (v1 v2)(apply '+ (mapcar '* v1 v2)))
(defun mod_vect (v)(sqrt(vect_dot v v)))
(defun m*v (mat vect / r i )
(if
(and (eq (length(car mat)) (length vect)))
(progn
(setq i 0)
(repeat (length mat)
(setq r (append r (list(list (apply '+ (mapcar '* vect (nth i mat)))))) i (+ i 1))
)
r
)
)
)
(defun rot_vect (vect ang / mat)
(setq mat
(list
(list (cos ang) (* -1.0 (sin ang)))
(list (sin ang) (cos ang)))
)
(apply 'append (m*v mat vect))
)
(defun acos (x)
(cond
((and(>= x -1.0)(<= x 1.0)) (-(* pi 0.5) (asin x)))
)
)
(defun asin (x)
(cond
((and(> x -1.0)(< x 1.0)) (atan (/ x (sqrt (- 1.0 (* x x))))))
((= x -1.0) (* -1.0 (/ pi 2)))
((= x 1) (/ pi 2))
)
)
(defun angle_strike (un_vect_dir un_vec_pt / ang)
(acos (/ (vect_dot un_vect_dir un_vec_pt) (* (mod_vect un_vect_dir)(mod_vect un_vec_pt))))
)
(defun getattributevalue ( blk tag )
(setq tag (strcase tag))
(vl-some '(lambda ( att ) (if (= tag (strcase (vla-get-tagstring att))) (vla-get-textstring att))) (vlax-invoke blk 'getattributes))
)
(cond
((setq cable (car(entsel "\nSelect optic cable >")))
(setq cable_points (pointlist2d (vlax-get (vlax-ename->vla-object cable) 'coordinates)))
(setq ss (ssget "_F" cable_points '((0 . "INSERT")(2 . "Manhole*"))))
(setq i -1)
(while (< (setq i (1+ i))(sslength ss))
(setq eo (vlax-ename->vla-object (ssname ss i)))
(setq vec_list
(list
(rot_vect '(0 1) (vlax-get eo 'rotation))
(rot_vect '(1 0) (vlax-get eo 'rotation))
(rot_vect '(0 -1) (vlax-get eo 'rotation))
(rot_vect '(-1 0) (vlax-get eo 'rotation))
)
)
(setq mh_name (getAttributeValue eo "MH-ID"))
(setq ip (take2(vlax-get eo 'insertionPoint)))
(setq ip (car (vl-sort cable_points '(lambda (x y)(< (distance ip x)(distance ip y))))))
(setq cnt (vl-position ip cable_points))
(setq cnt_prev (1- cnt) cnt_next (1+ cnt))
(setq vec_in (unit_vect(mapcar '- (nth cnt_prev cable_points)(nth cnt cable_points))))
(setq vec_out (unit_vect(mapcar '- (nth cnt_next cable_points)(nth cnt cable_points))))
(setq in (car(vl-sort-i vec_list '(lambda (x y) (< (angle_strike vec_in x)(angle_strike vec_in y))))))
(setq in (nth in '("A" "B" "C" "D")))
(setq out (car(vl-sort-i vec_list '(lambda (x y) (< (angle_strike vec_out x)(angle_strike vec_out y))))))
(setq out (nth out '("A" "B" "C" "D")))
;add code for autput to file as you like
(princ (strcat "\n" mh_name " " in " "out))
)
)
)
(princ "\nDone!")
(princ)
)
Miljenko Hatlak

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.