;;; ------------------------------------------------------------------
;;; DNH-IIC.LSP - dim nhanh mot chuoi diem trong AutoCAD
;;; Vien Tin hoc Xay dung (IIC) - Truong Dai hoc Xay dung Ha Noi
;;; https://iic.huce.edu.vn  -  Tel: 0989 427 809
;;;
;;; HAI LENH:
;;;   DNH  - chon mot chuoi diem roi dat MOT muc duong kich thuoc:
;;;          may ghi lien tiep tung doan, va hoi co ghi them kich thuoc tong khong
;;;   DNHP - lam cung viec ay nhung lay san moi dinh cua mot duong da tuyen
;;;
;;; Khac voi QDIM co san: QDIM lay hinh hoc ban chon, con hai lenh nay lay
;;; DUNG thu tu ban chi, nen chuoi kich thuoc ra dung thu tu doc ban ve.
;;; Tep luu bang ma UTF-8. Da chay thu tren AutoCAD 2027 truoc khi phat hanh.
;;; ------------------------------------------------------------------

(vl-load-com)

(defun IIC-GIOI-THIEU ()
  (princ "\nLisp được lập trình bởi RDSIC - Viện Tin học Xây dựng")
  (princ "\nwebsite: https://iic.huce.edu.vn  -  Tel: 0989 427 809")
)

;; Ghi mot kich thuoc thang giua hai diem, duong kich thuoc di qua pd.
(defun IIC:DIM1 (a b pd)
  ;; Tien to _non: ep bo qua bat diem dang bat. Thieu no thi tren ban ve that
  ;; (nhieu net, nhieu diem bat) lenh an vao diem khac va kich thuoc ra khong chu.
  (command "_.DIMLINEAR" "_non" a "_non" b "_non" pd)
)

;; Khoang cach giua hai lop kich thuoc: lay theo DIMDLI nhan DIMSCALE cua ban ve.
(defun IIC:BUOC ( / d s )
  (setq d (getvar "dimdli") s (getvar "dimscale"))
  (if (or (null d) (<= d 0)) (setq d 3.75))
  (if (or (null s) (<= s 0)) (setq s 1.0))
  (* d s)
)

(defun c:DNH ( / e-cu os-cu ce-cu ds p pd i n buoc tong pd2 dai )
  (IIC-GIOI-THIEU)
  (setq e-cu *error* os-cu (getvar "osmode") ce-cu (getvar "cmdecho"))
  (defun *error* (m)
    (setvar "osmode" os-cu) (setvar "cmdecho" ce-cu)
    (command "_.UNDO" "_End")
    (setq *error* e-cu)
    (if (and m (not (wcmatch (strcase m) "*CANCEL*,*QUIT*")))
      (princ (strcat "\nIIC loi: " m)))
    (princ)
  )
  (command "_.UNDO" "_Begin")
  (setq ds nil)
  (while (setq p (getpoint "\nChon diem theo thu tu (Enter de dung): "))
    (setq ds (cons p ds))
  )
  (setq ds (reverse ds) n (length ds))
  (if (< n 2)
    (princ "\nCan it nhat hai diem.")
    (progn
      (setq pd (getpoint "\nVi tri duong kich thuoc: "))
      (if pd
        (progn
          (initget "Co Khong")
          (setq tong (getkword "\nGhi them kich thuoc tong? [Co/Khong] <Co>: "))
          (setvar "cmdecho" 0)
          (setvar "osmode" 0)
          (setq i 0)
          (while (< i (1- n))
            (IIC:DIM1 (nth i ds) (nth (1+ i) ds) pd)
            (setq i (1+ i))
          )
          (if (/= tong "Khong")
            (progn
              (setq buoc (IIC:BUOC)
                    dai  (distance (car ds) (last ds)))
              ;; day lop tong ra xa them mot buoc, theo huong tu diem dau toi pd
              (setq pd2 (polar pd (angle (car ds) pd) buoc))
              (IIC:DIM1 (car ds) (last ds) pd2)
            )
          )
          (princ (strcat "\nDa ghi " (itoa (1- n)) " doan."))
        )
      )
    )
  )
  (setvar "osmode" os-cu) (setvar "cmdecho" ce-cu)
  (command "_.UNDO" "_End")
  (setq *error* e-cu)
  (princ)
)

;; DNHP - lay san moi dinh cua mot duong da tuyen, theo dung thu tu dinh.
(defun c:DNHP ( / e-cu os-cu ce-cu es el ds i n pd tong buoc pd2 )
  (IIC-GIOI-THIEU)
  (setq e-cu *error* os-cu (getvar "osmode") ce-cu (getvar "cmdecho"))
  (defun *error* (m)
    (setvar "osmode" os-cu) (setvar "cmdecho" ce-cu)
    (command "_.UNDO" "_End")
    (setq *error* e-cu)
    (if (and m (not (wcmatch (strcase m) "*CANCEL*,*QUIT*")))
      (princ (strcat "\nIIC loi: " m)))
    (princ)
  )
  (command "_.UNDO" "_Begin")
  (setq es (entsel "\nChon duong da tuyen: "))
  (if es
    (progn
      (setq el (entget (car es)) ds nil)
      (foreach x el
        (if (= (car x) 10) (setq ds (cons (cdr x) ds)))
      )
      (setq ds (reverse ds) n (length ds))
      (if (< n 2)
        (princ "\nDuong nay khong du hai dinh.")
        (progn
          (setq pd (getpoint "\nVi tri duong kich thuoc: "))
          (if pd
            (progn
              (initget "Co Khong")
              (setq tong (getkword "\nGhi them kich thuoc tong? [Co/Khong] <Co>: "))
              (setvar "cmdecho" 0)
              (setvar "osmode" 0)
              (setq i 0)
              (while (< i (1- n))
                (IIC:DIM1 (nth i ds) (nth (1+ i) ds) pd)
                (setq i (1+ i))
              )
              (if (/= tong "Khong")
                (progn
                  (setq buoc (IIC:BUOC)
                        pd2 (polar pd (angle (car ds) pd) buoc))
                  (IIC:DIM1 (car ds) (last ds) pd2)
                )
              )
              (princ (strcat "\nDa ghi " (itoa (1- n)) " doan tu " (itoa n) " dinh."))
            )
          )
        )
      )
    )
  )
  (setvar "osmode" os-cu) (setvar "cmdecho" ce-cu)
  (command "_.UNDO" "_End")
  (setq *error* e-cu)
  (princ)
)
