;;;--- global variable.
;;;---------------------------------------
;;; 读取数据文件--getdat(wjm)
;;; read data from /ktcad/supp/wjm
;;;---------------------------------------
(defun getdat(wjm / ff temp ft sbb)

  (setq ff (open wjm "r"))
  (if ff
    (progn
      (setq ft t)
      (setq sbb nil)
      (setq temp nil)
      (while ft
        (setq temp (read-line ff))
        (if (or (= temp nil) (= temp "") (= temp "#"))
          (setq ft nil)
          (progn
            (setq temp (read temp))
            (setq sbb (cons temp sbb))
          )
        )
      )  ;----end of while ft
      (close ff)
      (setq sbb (reverse sbb))
    )
    (setq sbb nil)
  )
  sbb
)
;;;------------------------------------
(defun qxdata(ename cc / q1)
  (setq q1 (HY_QQ10xx ename cc 1000))
  (if (/= q1 nil) (setq q1 (read q1)))
)
;;-------------------------------------
(defun q1040(ent app) (HY_QQ10xx ent app 1040))
;;;------------------------------
;;; 1995.09.23
;;; 把表转换为 string --- list2s()
;;;------------------------------
(defun list2s(ss) (HY_Lst2Str ss))
;;;========================================
;;---------------------------------
;; 转换视图
;;---------------------------------
(defun c:v0()   (command "vpoint" '(1 0 0))  (princ))
(defun c:v45()  (command "vpoint" '(1 1 1))  (princ))
(defun c:v90()  (command "vpoint" '(0 1 0))  (princ))
(defun c:v135() (command "vpoint" '(-1 1 1)) (princ))
(defun c:v180() (command "vpoint" '(-1 0 0)) (princ))
(defun c:v225() (command "vpoint" '(-1 -1 1))(princ))
(defun c:v270() (command "vpoint" '(0 -1 0)) (princ))
(defun c:v315() (command "vpoint" '(1 -1 1)) (princ))
(defun c:pm()   (command "vpoint" '(0 0 1))  (princ))
(defun c:xt()   (command "vpoint" '(-1 -1 1))(princ))
(defun c:v000() (command "vpoint" '(0 0 1))  (princ))
;;----------------------------------------
;; 1996.02.12 mon
;; 取POLYLINE's VERTEX --- getvex(e)
;; e --- POLYLINE
;; fhb ==> (((x y z) kd) ((x y z) kd) ...)
;;----------------------------------------
(defun getvex(e / e e1 e1b lx pt kd fhb fftt ff70 entb n i etype)
  (setq entb (entget e)
        etype (cdr (assoc 0 entb))
        ff70 (cdr (assoc 70 entb))
  )
  (setq ff70 (rem ff70 2))
;
  (if (= etype "POLYLINE")
    (progn
      (setq fftt t)
      (while fftt
        (setq e1 (entnext e))
        (setq e1b (entget e1))
        (setq lx (cdr (assoc 0 e1b)))
        (if (= lx "VERTEX")
          (progn
            (setq pt (cdr (assoc 10 e1b)))
            (setq kd (cdr (assoc 40 e1b)))
            (setq fhb (append (list (list pt kd)) fhb))
            (setq e e1)
          )
          (setq fftt nil) ;***** lx = "SEQEND"
        )
      )
    )
    (progn
;;;modi 1998.05.29 LWPOLYLINE
      (setq n  (cdr (assoc 90 entb))
            ;i (##getindex (assoc 10 entb) entb)
      )
      (repeat n
        (setq pt (cdr (assoc 10 entb))
              pt (list (car pt) (cadr pt) 0.0)
              kd (cdr (assoc 40 entb))
              fhb (append (list (list pt kd)) fhb)
             ;i (+ i 4)
             entb (cdr (member (assoc 10 entb) entb))
        )
      )
    )
  )
;;;-------

  (if (= ff70 1) (setq fhb (cons (last fhb) fhb))) ;*** closed.
  (setq fhb (reverse fhb))
)
;;求圆弧的两个端点
;;2005-4-8
(defun getArcVetex(e / entb etype ff10 ff40 ff50 ff51 fhb pt0 pt1)
  (setq entb (entget e)
        etype (cdr (assoc 0 entb));;实体类型
  )
  (if (= etype "ARC")
   (progn
      (setq   ff10 (cdr (assoc 10 entb));;中心点
              ff40 (cdr (assoc 40 entb));;半径
              ff50 (cdr (assoc 50 entb));;开始端点角度
              ff51 (cdr (assoc 51 entb));;结束端点角度 
       )
      (setq pt0 (polar ff10 ff50 ff40)
            pt1 (polar ff10 ff51 ff40)
      )
      (setq fhb (list pt0 pt1))
   )
   (setq fhb nil)
  )
)
;;-------------------------------------------
;; 1996.08.01 thu
;; 取POLYLINE中间某段宽度 --- getpkd(e pt)
;; e --- POLYLINE
;; pt -- point In Polyline
;; fhb - (flag kd gd p01 p02 ang bg)
;;       flag = T/nil 成功时T
;;-------------------------------------------
(defun getpkd(e pt / flag eb bg kd gd fhb ll ii b1 b2 ft ff70
                     pt1 pt2 kds ang1 ang2 angc p01 p02)

  (setq flag nil)

  (setq eb (entget e))
  (setq ff70 (cdr (assoc 70 eb)))   ;*** close or not.
  (setq bg (caddr (cdr (assoc 10 eb))))
  (setq gd (cdr (assoc 39 eb)))
  (setq kd (cdr (assoc 40 eb)))
;----- pt 为POLYLONE内顶点时不处理 ----------
  (setq fhb (getvex e)) ;***** In @fggycx.lsp
  (setq ft t)
  (foreach pp fhb
    (setq pp (car pp))
    (if (<= (HY_PtPMDist pp pt) 0.1) (setq ft nil))
  )
  (if ft
    (progn
      (setq ll (length fhb))
      (setq ii 0)
      (while (< ii (- ll 1))
        (setq b1 (nth ii fhb))
        (setq b2 (nth (+ ii 1) fhb))
        (setq pt1 (car b1))
        (setq kds (cadr b1))
        (setq pt2 (car b2))
        (setq ang1 (angle pt1 pt))
        (setq ang2 (angle pt2 pt))
        (setq angc (abs (- ang1 ang2)))
        (if (equal angc pi 0.1)
          (progn
            (setq flag t) ;***** OK! *****
            (setq kd kds)
            (setq ang ang1)
            (setq p01 pt1 p02 pt2)
            (setq ii ll)
          )
        )
        (setq ii (1+ ii))
      )
    )
  )
  (if (= flag nil)
    (setq fhb (list flag))
    (setq fhb (list flag kd gd p01 p02 ang bg))
  )
  fhb
)
;;---------------------------------
;; 1996.08.21 wed
;; 点表X向排序  ptbxpx(ptb)
;; (对表的第一位元素排序(car))
;; ptb : 点表
;; return : X递增
;;---------------------------------
(defun ptbxpx(ptb / xb xmin xmax b1 b2 bt)

  (setq xb (mapcar 'car ptb)
        xmin (1- (apply 'min xb))
  ) ;***** xmin 比min 还小! *****
  (while (< xmin (setq xmax (apply 'max xb)))
    (setq b1 ptb
          xb (subst xmin xmax xb)
    )
    (while (setq b2 (assoc xmax b1))
      (setq b1 (cdr (member b2 b1))
            bt (cons b2 bt)
      )
    )
  )
  bt
)
;;---------------------------------
;; 1996.08.21 wed
;; 点表Y向排序  ptbypx(ptb)
;; (对表的第二位元素排序(cadr))
;; ptb : 点表
;; return : Y递增
;;---------------------------------
(defun ptbypx(ptb / ptby)

  (setq ptby (mapcar '(lambda (pt)
                        (list (cadr pt) (car pt) (cddr pt))
                      )
                      ptb))
  (setq ptby (ptbxpx ptby))
  (setq ptby (mapcar '(lambda (pt)
                        (cons (cadr pt) (cons (car pt) (caddr pt)))
                      )
                      ptby))

  ptby

)

;;------------------------------
(defun mk_lay(lay)
  (if (null (DL_MakeLayer lay))
    (progn
      (beep)
      (prompt (strcat "\n图层" lay "没有打开, 程序退出."))
      (exit)
  ))
)

;;-----------------------------
;; 取最后一个实体
;; e0 --- entity name / nil
;;-----------------------------
(defun ent_last(/ e0 et)
  (setq e0 (entlast))
  (if e0 (while (setq et (entnext e0)) (setq e0 et)))
  e0
)
;;-----------------------------
;; 取e0之后所有Entity(不含e0)
;; e0 --- entity name / nil
;; return ==> ss(>0) / nil
;;-----------------------------
(defun get_last(e0 / ss et)

  (if e0
    (progn
      (setq ss (ssadd))
      (while (setq et (entnext e0))
        (setq ss (ssadd et ss) e0 et)
      )
      (if (= (sslength ss) 0) (setq ss nil))
    )
    (setq ss (ssget "x"))
  )
  ss
)
(princ)
