本帖最后由 hb198075 于 2012-3-31 13:57 编辑
对选择的图块进行连线,要求获得最短的一种自身不交叉的连线方式。
已完成如下代码,感觉不理想,图块少时还不错,图块多了就乱了。望高手们赐教!
- ;;;测试程序
- (defun c:tt1 ( / e10 ent itm n na ptlst ss)
- (if (setq ss (ssget '((0 . "INSERT"))))
- (progn
- (setq n 0)
- (repeat (sslength ss)
- (setq na (ssname ss n)
- ent (entget na)
- e10 (cdr (assoc 10 ent))
- ptlst (cons e10 ptlst)
- n (1+ n)
- )
- )
- (setq ptlst (bz-dianjuLst ptlst))
- (command "_.PLINE")
- (foreach itm ptlst
- (command itm)
- )
- (command "")
- )
- )
- (princ)
- )
- ;;;获得最短的总点距
- (defun bz-dianjuLst (lst / a all b jd jl n pt tglst)
- (setq n 0)
- (while (< n (length lst))
- (setq pt (nth n lst)
- tglst (bz-getNextPoint pt lst)
- jl (HB_GETdist tglst)
- )
- (setq jd(bz-getPlistInsert tglst)
- jl(* jl jd)
- );_根据交点数设置优先级,每多一个交点,则距加一倍
- (setq all (cons (cons jl tglst) all)
- n (1+ n)
- )
- )
- (setq all(vl-sort all '(LAMBDA (a b)(< (car a)(car b)))))
- (cdar all)
- )
- ;;;获得离当前点最近的下一点
- (defun bz-getNextPoint (pt lst / a b rel tmplst)
- (setq tmplst lst)
- (while tmplst
- (setq tmplst (vl-sort tmplst
- '(LAMBDA (a b) (< (DISTANCE pt a) (DISTANCE pt b)))
- )
- pt (car tmplst)
- )
- (setq rel (cons pt rel)
- tmplst (cdr tmplst)
- )
- )
- (REVERSE rel)
- )
- ;;;判断当前连线自身的交点数
- (defun bz-getPlistInsert (lst / jd len m n p1 p2 pt1 pt2 rel)
- (setq n 0
- len (length lst)
- rel 1
- )
- (repeat (- len 2)
- (setq p1 (nth n lst)
- p2 (nth (1+ n) lst)
- n (1+ n)
- m (1+ n)
- )
- (repeat (- len n 2)
- (setq pt1 (nth m lst)
- pt2 (nth (1+ m) lst)
- jd (INTERS p1 p2 pt1 pt2)
- m (1+ m)
- )
- (if jd
- (setq rel (1+ rel))
- )
- )
- )
- rel
- )
- ;;;获得点连线的总长度
- (defun HB_GETdist (lst / rel n p1 p2 tmp)
- (setq rel 0.0)
- (if (> (length lst) 1)
- (progn
- (setq n 0)
- (repeat (1- (length lst))
- (setq p1 (nth n lst)
- p2 (nth (1+ n) lst)
- n (1+ n)
- tmp (DISTANCE p1 p2)
- rel (+ tmp rel)
- )
- )
- )
- )
- rel
- )
下面是测试图
|