(DEFUN c:ZHL_Df ( / ss)
  (prompt "\n请选择需要自动调整文字位置的一组标注尺寸【DF】：")
(defun yad-angformat(ang pat / val)
  (setq val (rem (+ (* 2 pi) ang) pat))
  (if (equal val pat 0.001)
    0.0
    val
  )
) 
(defun yad-midpt(pt1 pt2)
  (polar pt1 (angle pt1 pt2) (/ (distance pt1 pt2) 2))
) 
(defun yad-dxf(en n)
  (if (not (listp en)) (setq en (entget en)))
  (cdr (assoc n en))
)
(defun yad-chgent(en n val / m)
  (if (not (listp en)) (setq en (entget en)))
  (if (assoc n en)
    (setq en (subst (cons n val) (assoc n  en) en))
    (setq en (append en (list (cons n val))))
  )
  (entmod en)
  nil
)
;;尺寸文字自动避让[ss-尺寸选择集 ==>返回：nil]
(defun yad-adjustdim(ss / scl dis nblen s n ent en p21 p22 p23 p24 ent1 pt1 m ent2 en2 mov pt2 pc2 len1 len2)
  (setq scl (getvar "DIMSCALE"))
  (cond
    ((= scl 10)(setq dis 50)(setq nblen 50))
    ((= scl 20)(setq dis 100)(setq nblen 100))
    ((= scl 50)(setq dis 500)(setq nblen 300))
    ((= scl 100)(setq dis 1100)(setq nblen 700))
    ((= scl 150)(setq dis 2000)(setq nblen 1150))
    ((= scl 200)(setq dis 2700)(setq nblen 1600))
    ((= scl 250)(setq dis 3400)(setq nblen 2100))
    ((= scl 300)(setq dis 4100)(setq nblen 2650))
    ((= scl 400)(setq dis 5400)(setq nblen 3500))
    ((= scl 500)(setq dis 7000)(setq nblen 4400))
    ((= scl 750)(setq dis 11200)(setq nblen 6900))
    ((= scl 1000)(setq dis 16000)(setq nblen 9700))
    ((= scl 2000)(setq dis 34000)(setq nblen 22000))
    (T (setq dis 86000)(setq nblen 56000))
  )
  (setq s (ssadd))
  (vl-cmdf "_.dimedit" "_h" ss "")
  (setq n -1)
  (repeat (sslength ss)
    (if (< (yad-dxf (setq ent (ssname ss (setq n (1+ n)))) 42) dis)
      (ssadd ent s)
    )
  )
  (repeat (sslength s)
    (setq ent1 (ssname s 0)
          pt1 (yad-dxf ent1 11)
    )
    (ssdel ent1 s)
    (setq m 0)
    (while (setq ent2 (ssname s m))
      (setq en2 (entget ent2)
            mov (yad-dxf en2 70)
            pt2 (yad-dxf en2 11)
            p21 (yad-dxf en2 13)
            p22 (yad-dxf en2 14)
            p23 (yad-dxf en2 10)
            p24 (polar p21 (angle p22 p23) (* (distance p21 p23) (cos (- (angle p22 p23) (angle p21 p23)))))
            pc2 (yad-midpt p23 p24)
      )
      (if (and (equal (yad-angformat (angle pt2 pt1) pi) (yad-angformat (angle p23 p24) pi) 0.001) (< (distance pt2 pt1) nblen))
        (progn
          (if (< mov 128)
            (progn
              (setq len1 (distance pt2 pc2))
              (setq en2 (subst (cons 11 (polar pt2 (angle pt2 pc2) (* len1 2))) (assoc 11 en2) en2))
            )
            (progn
              (setq len2 (distance pt2 pc2))
              (if (not len1)
                (setq len1 len2)
              )
              (if (< len2 (* len1 2))
                (setq en2 (subst (cons 11 (polar pt2 (angle pt2 pc2) (+ (* len1 2) (* 4 scl)))) (assoc 11 en2) en2))
                (setq en2 (subst (cons 11 (polar pt2 (angle pt2 pc2) (* len2 2))) (assoc 11 en2) en2))
              )
            )
          )
          (yad-chgent en2 70 (+ 128 (rem (yad-dxf en2 70) 128)))
        )
      )
      (setq m (1+ m))
    )
  )
  nil
)
(setq ss (ssget '((0 . "DIMENSION"))))

(IF SS
(yad-adjustdim ss)
(PRINC)
)
)