Jump to content

Lisp lệnh BLKQTY trong chủ đề “Viết lisp theo yêu cầu [phần 2]”

Vụ chèn hình là “chuyện nhỏ” cũng không phải "giữ bí mật" nhưng vì muốn giải quyết rốt ráo nên chậm Update cho anh em.

Nhưng nay thấy "AKA buông súng" nên Post lên anh em sài thử!

 

...........

 

Sau  02 bà rồi sẽ là cái này :333.jpg

 

Bổ sung tùy chọn : nhập ký hiệu Block

Code :

(defun c:BlkQty (/ blk_id blk_len blk_name cur_var ent h height i ins len0 lst_blk msp pt row 
ss str tblobj width width1 width2 x y)
;;  By : Gia Bach, gia_bach @  www.CadViet.com             ;;
(defun TxtWidth (val h msp / txt minp maxp)
 (setq	txt (vla-AddText msp val (vlax-3d-point '(0 0 0)) h))
 (vla-getBoundingBox txt 'minp 'maxp )
 (vla-Erase txt)
 (-(car(vlax-safearray->list maxp))(car(vlax-safearray->list minp)))  )

(defun GetOrCreateTableStyle (tbl_name / name namelst objtblsty objtblstydic tablst txtsty)
 (setq objTblStyDic (vla-item (vla-get-dictionaries *adoc) "ACAD_TABLESTYLE") )  
 (foreach itm (vlax-for itm objTblStyDic
	 (setq tabLst (append tabLst (list itm))))
   (if (not
  (vl-catch-all-error-p
    (setq name (vl-catch-all-apply 'vla-get-Name (list itm)))))
     (setq nameLst (append nameLst (list name)))  )  )
 (if (not (vl-position tbl_name nameLst))
   (vla-addobject objTblStyDic tbl_name "AcDbTableStyle"))
 (setq objTblSty (vla-item objTblStyDic tbl_name)
TxtSty (variant-value (vla-getvariable *adoc "TextStyle")))
 (vla-settextstyle objTblSty acTitleRow  TxtSty)
 (vla-settextstyle objTblSty acHeaderRow TxtSty)
 (vla-settextstyle objTblSty acDataRow   TxtSty)
 (vla-setvariable *adoc "CTableStyle" tbl_name) )
;main  
 (if (setq ss (ssget (list (cons 0 "INSERT"))))
   (progn
     (vl-load-com)
     (setq i -1 len0 8)
     (while (setq ent (ssname ss (setq i (1+ i))))
(setq blk_name (vla-get-name (vlax-Ename->Vla-Object ent)))
(if (> (setq blk_len (strlen blk_name)) len0)
  (setq str blk_name len0 blk_len) )	
(if (not (assoc blk_name lst_blk))
  (setq lst_blk (cons (cons blk_name 1) lst_blk))
  (setq lst_blk (subst (cons blk_name (1+ (cdr (assoc blk_name lst_blk))))
		       (assoc blk_name lst_blk) lst_blk)))	    )
     (setq lst_blk (vl-sort lst_blk '(lambda (x y) (      (setq cur_var (mapcar 'getvar '("DYNMODE" "DYNPROMPT")))
     (mapcar 'setvar '("DYNMODE" "DYNPROMPT") '(1 1))
     (initget "Yes No")
     (setq ins (getkword "\nChen ki hieu Block  : ") )
     (or ins (setq ins "Yes"))
     (mapcar 'setvar '("DYNMODE" "DYNPROMPT") cur_var)      
     (or *h* (setq *h* (* (getvar "dimtxt")(getvar "dimscale"))))
     (initget 6)
     (setq h (getreal (strcat "\nChieu cao chu  :")))      
     (if h (setq *h* h) (setq h *h*) )
     (setq *adoc (vla-get-ActiveDocument (vlax-get-acad-object))
    msp (vla-get-modelspace *adoc))      
     (setq width1 (* 2 (TxtWidth "STT" h msp))
    width (* 2 (TxtWidth "So luong" h msp))
    height (* 2 h))
     (if str
(setq width2 (* 1.5 (TxtWidth (strcase str) h msp)))
(setq width2 width))
     (if (> h 3)
(setq width (* (fix (/ width 10))10)
      width1 (* (fix (/ width1 10))10)
      width2 (* (fix (/ width2 10))10)
      height (* (fix (/ height 5))5)))
     (GetOrCreateTableStyle "CadViet")
     (setq pt (getpoint "\nDiem dat Bang :")
    TblObj (vla-addtable msp (vlax-3d-point pt) (+ (length lst_blk) 2) 5 height width))
     (vla-put-regeneratetablesuppressed TblObj :vlax-true)
     (vla-SetColumnWidth TblObj 0 width1)
     (vla-SetColumnWidth TblObj 1 width2)
     (vla-put-vertcellmargin TblObj (* 0.75 h))
     (vla-put-horzcellmargin TblObj (* 0.75 h))
     (mapcar '(lambda (x)(vla-setTextHeight TblObj x h))
      (list acTitleRow acHeaderRow acDataRow) )
     (mapcar '(lambda (x)(vla-setAlignment TblObj x 2))
      (list acTitleRow acHeaderRow acDataRow))      
     (vla-MergeCells TblObj 0 0 0 3)
     (vla-setText TblObj 0 0 "Bang thong ke")
     (mapcar '(lambda (x y)(vla-setText TblObj 1 x y))
      (list 0 1 2 3 4)
      (list "STT" "Ten" "Don vi" "So luong" "Ky hieu"))      
     (setq row 2 i 1)    
     (foreach pt lst_blk
(setq blk_name (car pt) )
(mapcar '(lambda (x y)(vla-setText TblObj row x y))
      (list 0 1 2 3)
      (list i blk_name "cai" (cdr pt)))
(if (= ins "Yes")
  (vla-SetBlockTableRecordId TblObj row 4 (vla-get-objectID (vla-item (vla-get-blocks *adoc) blk_name))
:vlax-true))
(vla-SetCellAlignment TblObj row 1 7)
(vla-SetCellAlignment TblObj row 3 9)
(setq row (1+ row) i (1+ i))	)
     (vla-put-regeneratetablesuppressed TblObj :vlax-false)
     (vlax-release-object TblObj) )  )
 (princ))

Code quá tuyệt, gãi đúng ngay chổ ngứa :cheers: . Tick thanks không chưa đã, phải nói lời cảm ơn mới được.

Tue_NV chân thành cảm ơn anh gia bach thật nhiều

Cách dùng

  1. Tải tệp 94042_blkqty.lsp về máy.
  2. Trong AutoCAD gõ lệnh APPLOAD, chọn tệp vừa tải rồi bấm Load. Cũng có thể kéo thả tệp .lsp vào cửa sổ bản vẽ.
  3. Gõ lệnh BLKQTY để chạy lisp.

Mã nguồn 94042_blkqty.lsp

;; free lisp from cadviet.com
;;; this lisp was downloaded from https://www.cadviet.com/forum/topic/13203-vi%E1%BA%BFt-lisp-theo-y%C3%AAu-c%E1%BA%A7u-ph%E1%BA%A7n-2/?page=46&tab=comments#comment-94041
(defun c:BlkQty (/ blk_id blk_len blk_name cur_var ent h height i ins len0 lst_blk msp pt row 
ss str tblobj width width1 width2 x y)
;;  By : Gia Bach, gia_bach @  www.CadViet.com             ;;
(defun TxtWidth (val h msp / txt minp maxp)
 (setq	txt (vla-AddText msp val (vlax-3d-point '(0 0 0)) h))
 (vla-getBoundingBox txt 'minp 'maxp )
 (vla-Erase txt)
 (-(car(vlax-safearray->list maxp))(car(vlax-safearray->list minp)))  )

(defun GetOrCreateTableStyle (tbl_name / name namelst objtblsty objtblstydic tablst txtsty)
 (setq objTblStyDic (vla-item (vla-get-dictionaries *adoc) "ACAD_TABLESTYLE") )  
 (foreach itm (vlax-for itm objTblStyDic
	 (setq tabLst (append tabLst (list itm))))
   (if (not
  (vl-catch-all-error-p
    (setq name (vl-catch-all-apply 'vla-get-Name (list itm)))))
     (setq nameLst (append nameLst (list name)))  )  )
 (if (not (vl-position tbl_name nameLst))
   (vla-addobject objTblStyDic tbl_name "AcDbTableStyle"))
 (setq objTblSty (vla-item objTblStyDic tbl_name)
TxtSty (variant-value (vla-getvariable *adoc "TextStyle")))
 (vla-settextstyle objTblSty acTitleRow  TxtSty)
 (vla-settextstyle objTblSty acHeaderRow TxtSty)
 (vla-settextstyle objTblSty acDataRow   TxtSty)
 (vla-setvariable *adoc "CTableStyle" tbl_name) )
;main  
 (if (setq ss (ssget (list (cons 0 "INSERT"))))
   (progn
     (vl-load-com)
     (setq i -1 len0 8)
     (while (setq ent (ssname ss (setq i (1+ i))))
(setq blk_name (vla-get-name (vlax-Ename->Vla-Object ent)))
(if (> (setq blk_len (strlen blk_name)) len0)
  (setq str blk_name len0 blk_len) )	
(if (not (assoc blk_name lst_blk))
  (setq lst_blk (cons (cons blk_name 1) lst_blk))
  (setq lst_blk (subst (cons blk_name (1+ (cdr (assoc blk_name lst_blk))))
		       (assoc blk_name lst_blk) lst_blk)))	    )
     (setq lst_blk (vl-sort lst_blk '(lambda (x y) (      (setq cur_var (mapcar 'getvar '("DYNMODE" "DYNPROMPT")))
     (mapcar 'setvar '("DYNMODE" "DYNPROMPT") '(1 1))
     (initget "Yes No")
     (setq ins (getkword "\nChen ki hieu Block [Yes/No ] : ") )
     (or ins (setq ins "Yes"))
     (mapcar 'setvar '("DYNMODE" "DYNPROMPT") cur_var)      
     (or *h* (setq *h* (* (getvar "dimtxt")(getvar "dimscale"))))
     (initget 6)
     (setq h (getreal (strcat "\nChieu cao chu  :")))      
     (if h (setq *h* h) (setq h *h*) )
     (setq *adoc (vla-get-ActiveDocument (vlax-get-acad-object))
    msp (vla-get-modelspace *adoc))      
     (setq width1 (* 2 (TxtWidth "STT" h msp))
    width (* 2 (TxtWidth "So luong" h msp))
    height (* 2 h))
     (if str
(setq width2 (* 1.5 (TxtWidth (strcase str) h msp)))
(setq width2 width))
     (if (> h 3)
(setq width (* (fix (/ width 10))10)
      width1 (* (fix (/ width1 10))10)
      width2 (* (fix (/ width2 10))10)
      height (* (fix (/ height 5))5)))
     (GetOrCreateTableStyle "CadViet")
     (setq pt (getpoint "\nDiem dat Bang :")
    TblObj (vla-addtable msp (vlax-3d-point pt) (+ (length lst_blk) 2) 5 height width))
     (vla-put-regeneratetablesuppressed TblObj :vlax-true)
     (vla-SetColumnWidth TblObj 0 width1)
     (vla-SetColumnWidth TblObj 1 width2)
     (vla-put-vertcellmargin TblObj (* 0.75 h))
     (vla-put-horzcellmargin TblObj (* 0.75 h))
     (mapcar '(lambda (x)(vla-setTextHeight TblObj x h))
      (list acTitleRow acHeaderRow acDataRow) )
     (mapcar '(lambda (x)(vla-setAlignment TblObj x 2))
      (list acTitleRow acHeaderRow acDataRow))      
     (vla-MergeCells TblObj 0 0 0 3)
     (vla-setText TblObj 0 0 "Bang thong ke")
     (mapcar '(lambda (x y)(vla-setText TblObj 1 x y))
      (list 0 1 2 3 4)
      (list "STT" "Ten" "Don vi" "So luong" "Ky hieu"))      
     (setq row 2 i 1)    
     (foreach pt lst_blk
(setq blk_name (car pt) )
(mapcar '(lambda (x y)(vla-setText TblObj row x y))
      (list 0 1 2 3)
      (list i blk_name "cai" (cdr pt)))
(if (= ins "Yes")
  (vla-SetBlockTableRecordId TblObj row 4 (vla-get-objectID (vla-item (vla-get-blocks *adoc) blk_name))
:vlax-true))
(vla-SetCellAlignment TblObj row 1 7)
(vla-SetCellAlignment TblObj row 3 9)
(setq row (1+ row) i (1+ i))	)
     (vla-put-regeneratetablesuppressed TblObj :vlax-false)
     (vlax-release-object TblObj) )  )
 (princ))

Thảo luận gốc

Lisp được chia sẻ trong chủ đề Viết lisp theo yêu cầu [phần 2]. Xem đúng bài viết #94042 để đọc hướng dẫn và trao đổi thêm.

Về Thư viện AutoLisp (trang 247)