Lisp đếm block BLKQTY
@vandv:
+ Description phải dùng hexa.
+ Cách dùng đơn giản nhất để nhập mã hexa: https://www.cadviet.com/forum/topic/65354-thao-luan-go-tieng-viet-unicode-hexadecimal-ngay-trong-trinh-soan-thao-code-lisp/
Đây là lisp:
(defun c:BlkQty (/ blk_id blk_len blk_name blks cur_var ent h header_lsp height i ins j len0 lst_blk msp pt row ss str tblobj width width1 width2 x y blk_obj defx blk_def)
;; 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")))
(mapcar '(lambda (x) (vla-settextstyle objTblSty x TxtSty)) (list actitlerow acheaderrow acdatarow))
(vla-setvariable *adoc "CTableStyle" tbl_name))
(defun GetObjectID (obj)
(if (vl-string-search "64" (getenv "PROCESSOR_ARCHITECTURE"))
(vlax-invoke-method (vla-get-utility *adoc) 'GetObjectIdString obj :vlax-false)
(vla-get-objectid obj))) ;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)))
;; ------------- Lay Description -------------------
(setq blk_obj (vlax-ename->vla-object ent)
blk_def (vla-item
(vla-get-blocks (vla-get-activedocument (vlax-get-acad-object)))
(vla-get-effectivename blk_obj))
defx (vl-catch-all-apply (function (lambda () (vla-get-comments blk_def)))))
;;-------------Thay doi Len-------------------------
(if (> (setq blk_len (strlen defx)) len0)
(setq str defx
len0 blk_len))
(setq blk_name (list blk_name defx)) ; Them Description
(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) (< (car (car x)) (car (car y)))))) ; Them car
(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 <" (rtos *h*) "> :")))
(if h (setq *h* h)
(setq h *h*))
(setq *adoc (vla-get-activedocument (vlax-get-acad-object))
msp (vla-get-modelspace *adoc)
blks (vla-get-blocks *adoc))
(setq width1 (* 2 (TxtWidth "STT" h msp))
width (* 2 (TxtWidth "So luong" h msp))
height (* 2 h))
(if str
(setq width2 (* 1.25 (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))
(vl-catch-all-error-p (vl-catch-all-apply (function (lambda () (vla-mergecells TblObj 0 0 0 3)))))
(vla-settext TblObj 0 0 "B\U+1EA2NG TH\U+1ED0NG K\U+00CA")
(vla-setcelltextheight TblObj 0 0 (* h 2))
(setq j -1
header_lsp (list "STT" "T\U+00CAN G\U+1ECCI" "\U+0110\U+01A0N V\U+1ECA" "S\U+1ED0 L\U+01AF\U+1EE2NG" "K\U+00DD HI\U+1EC6U"))
(repeat (length header_lsp) (vla-settext TblObj 1 (setq j (1+ j)) (nth j header_lsp)))
(setq row 2
i 1)
(foreach
pt lst_blk
(setq blk_name (car pt)
j -1)
(mapcar '(lambda (x) (vla-settext TblObj row (setq j (1+ j)) x))
(list i (cadr blk_name) "C\U+00C1I" (cdr pt))) ; Them cadr
(if (= ins "Yes")
(vlax-for
blk blks
(if (= (vla-get-name blk) (car blk_name)) ; Them car
(vla-setblocktablerecordid TblObj row 4 (GetObjectID blk) :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))
Tệp .lsp gốc không còn trên máy chủ. Hãy chép mã nguồn bên dưới (lấy từ bài viết gốc) vào một tệp văn bản, lưu với đuôi .lsp.
Cách dùng
- Chép mã nguồn, lưu thành tệp 355158_blkqty.lsp.
- 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ẽ.
- Gõ lệnh BLKQTY để chạy lisp.
Mã nguồn 355158_blkqty.lsp (trích từ bài viết gốc)
(defun c:BlkQty (/ blk_id blk_len blk_name blks cur_var ent h header_lsp height i ins j len0 lst_blk msp pt row ss str tblobj width width1 width2 x y blk_obj defx blk_def)
;; 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")))
(mapcar '(lambda (x) (vla-settextstyle objTblSty x TxtSty)) (list actitlerow acheaderrow acdatarow))
(vla-setvariable *adoc "CTableStyle" tbl_name))
(defun GetObjectID (obj)
(if (vl-string-search "64" (getenv "PROCESSOR_ARCHITECTURE"))
(vlax-invoke-method (vla-get-utility *adoc) 'GetObjectIdString obj :vlax-false)
(vla-get-objectid obj))) ;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)))
;; ------------- Lay Description -------------------
(setq blk_obj (vlax-ename->vla-object ent)
blk_def (vla-item
(vla-get-blocks (vla-get-activedocument (vlax-get-acad-object)))
(vla-get-effectivename blk_obj))
defx (vl-catch-all-apply (function (lambda () (vla-get-comments blk_def)))))
;;-------------Thay doi Len-------------------------
(if (> (setq blk_len (strlen defx)) len0)
(setq str defx
len0 blk_len))
(setq blk_name (list blk_name defx)) ; Them Description
(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) (< (car (car x)) (car (car y)))))) ; Them car
(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 <" (rtos *h*) "> :")))
(if h (setq *h* h)
(setq h *h*))
(setq *adoc (vla-get-activedocument (vlax-get-acad-object))
msp (vla-get-modelspace *adoc)
blks (vla-get-blocks *adoc))
(setq width1 (* 2 (TxtWidth "STT" h msp))
width (* 2 (TxtWidth "So luong" h msp))
height (* 2 h))
(if str
(setq width2 (* 1.25 (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))
(vl-catch-all-error-p (vl-catch-all-apply (function (lambda () (vla-mergecells TblObj 0 0 0 3)))))
(vla-settext TblObj 0 0 "B\U+1EA2NG TH\U+1ED0NG K\U+00CA")
(vla-setcelltextheight TblObj 0 0 (* h 2))
(setq j -1
header_lsp (list "STT" "T\U+00CAN G\U+1ECCI" "\U+0110\U+01A0N V\U+1ECA" "S\U+1ED0 L\U+01AF\U+1EE2NG" "K\U+00DD HI\U+1EC6U"))
(repeat (length header_lsp) (vla-settext TblObj 1 (setq j (1+ j)) (nth j header_lsp)))
(setq row 2
i 1)
(foreach
pt lst_blk
(setq blk_name (car pt)
j -1)
(mapcar '(lambda (x) (vla-settext TblObj row (setq j (1+ j)) x))
(list i (cadr blk_name) "C\U+00C1I" (cdr pt))) ; Them cadr
(if (= ins "Yes")
(vlax-for
blk blks
(if (= (vla-get-name blk) (car blk_name)) ; Them car
(vla-setblocktablerecordid TblObj row 4 (GetObjectID blk) :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ủ đề [Nhờ giúp đỡ] Lisp đếm block BLKQTY. Xem đúng bài viết #355158 để đọc hướng dẫn và trao đổi thêm.