Jump to content

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


Chào svba, A Thiep thấy có sự khác nhau về câu hỏi: lisp của anh hỏi: "Chon do cao text :"
Còn lisp svba chạy thì hỏi: "Chon do cao text : " là sao vậy sao kà!
Thôi thì svba tạm thử lại lisp svb.lsp này xem. Nên nhớ cài Express tool nhé

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

  1. Chép mã nguồn, lưu thành tệp 89747_svb.lsp.
  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 SVB để chạy lisp.

Mã nguồn 89747_svb.lsp (trích từ bài viết gốc)

;| Lisp thong ke duong ong nuoc
copyright by thiep 03/2010|;
;;;-------------------------------------------
(defun SS-entlst (ss / c L)
 (setq c -1)
 (repeat (sslength ss)
   (setq L (cons (ssname ss (setq c (1+ c)))
	  L
    )
   )
 )
 (reverse L)
)
;;;----------------------------------
(defun filter (lstent otype olayer / kq)
 (foreach pp lstent
    (setq tt (entget pp))
    (if (and
   (member (cons 0 otype) tt)
   (member (cons 8 olayer) tt)
 )
      (setq kq (append kq (list pp)))
    )
 )
 kq
)
;;;======================
(defun timgan (p lst / dmin ppluu)
 (foreach pp lst
   (setq d (distance p pp))
   (if	(or (not dmin) (> dmin d))
     (setq dmin d
    ppluu pp
     )
   )
 )
 ppluu
)
;;;======================
(defun dxf (code ent)
 (cdr (assoc code (entget ent)))
)
;;;;;;;;;;;;;;;;;;;;;;;;;
(defun SAVE_MODE ()
 (setq	OLD_OSMODE    (getvar "OSMODE")
OLD_ORTHOMODE (getvar "ORTHOMODE")
 )
 (setvar "cmdecho" 0)
 (setvar "OSMODE" 0)
)
(defun RESTORE ()
 (setvar "osmode" OLD_OSMODE)
 (setvar "ORTHOMODE" OLD_ORTHOMODE)
)
;|===========================================================
                     MAIN LISP                              
===========================================================|;
(defun c:svb (/ lstent	   lstLWP_tn lstLWP_vh lsttextt	 tsi
	 Tstyle	   lstpoCP   lstenttext		 lstLWP_vh_text
	 TblObj	   p1	     rows      nRow	 nCol
	 objLwp	   lstpoF    len       midpo	 param
	 ang	   mfen1     mfen2     ss	 lstLWP2
	 lsttext_ong	     po	       str	 val
	 lstval	   lstvalpo  doan      pogan	 tenkhu
	 strso	   lstpo     p2	       en
	)
 (or ActDoc
     (setq ActDoc (vla-get-ActiveDocument (vlax-get-acad-object)))
 )
 (or *Model* (setq *Model* (vla-get-ModelSpace ActDoc)))
 (vla-StartUndoMark ActDoc)
 (SAVE_MODE)
 (setq	lstent	  (SS-entlst (ssget '((0 . "LWPOLYLINE,TEXT")
			      (8 . "vh,thoatnuoc,sttnut,sttkhu")
			     )
		     )
	  )
lstLWP_tn (filter lstent "LWPOLYLINE" "thoatnuoc")
lstLWP_vh (filter lstent "LWPOLYLINE" "vh")
lsttextt  (filter lstent "TEXT" "sttnut")
 )
 (setq	p1   (append (acet-list-remove-nth 2 (getvar "extmin")) (list 0.0))
rows (length lstLWP_tn)
 )
 (setq	tsi    (acet-dxf 40 (entget (nth 0 lsttextt)))
Tstyle (acet-dxf 7 (entget (nth 0 lsttextt)))
 )
 (setq oldtsi tsi)
 (setq	tsi (getreal
      (strcat "\nChon do cao text <" (rtos oldtsi 2 0) "> : ")
    )
 )
 (or tsi (setq tsi oldtsi))
;;;------------------
 (foreach entLWP2 lstLWP_vh
;;;    (redraw entLWP2 3)
   (setq lstpoCP (ACET-GEOM-VERTEX-LIST entLWP2))
   (if	(or (vlax-curve-isClosed entLWP2)
    (eq (car lstpoCP) (last lstpoCP))
)
     (progn
(setq ss3 (ssget "CP"
		 lstpoCP
		 '((0 . "TEXT") (8 . "sttkhu"))
	  )
)
(if ss3
  (progn
    (setq lstenttext (SS-entlst ss3))
    (setq lstLWP_vh_text
	   (cons (cons entLWP2 lstenttext) lstLWP_vh_text)
    )
  )
)
     )
   )
;;;    (redraw entLWP2 4)
 )
;;;---------------

 (if (> (atof (substr (getvar "ACADVER") 1 4)) 16.0)
   (progn
     (setq TblObj (vla-addtable
	     *Model*
	     (vlax-3d-point p1)
	     (1+ rows)
	     ;;the number of rows 
	     3
	     ;;the number of columns 
	     tsi
	     ;;the height of the rows
	     (* 6 tsi)
	     ;;the width of the columns 
	   )
     )
     (vla-UnMergeCells TblObj 0 0 0 2)
     (vla-MergeCells TblObj 0 0 1 2)
     (vla-put-vertcellmargin TblObj (* 0.5 tsi))
     (mapcar '(lambda (x) (vla-setTextHeight TblObj x tsi))
      (list acTitleRow acHeaderRow acDataRow)
     )
     (mapcar
'(lambda (x) (vla-setAlignment TblObj x acMiddleCenter))
(list acTitleRow acHeaderRow acDataRow)
     )
     (mapcar
'(lambda (x) (vla-SetTextStyle TblObj x Tstyle))
(list acTitleRow acHeaderRow acDataRow)
     )
     (vla-setText TblObj 0 0 "§O¹N èNG")
     (vla-setText TblObj 0 1 "TIÓU KHU")
     (vla-SetColumnWidth TblObj 0 (* 8.4 tsi))
     (setq nRow 1)

;;;  ===================;tung duong ong
     (foreach entLWP lstLWP_tn
(setq objLwp  (vlax-ename->vla-object entLWP)
      lstpoF (ACET-GEOM-VERTEX-LIST entLWP)
      len     (vla-get-Length objLwp)
      midpo   (vlax-curve-getPointAtDist objLwp (/ len 2))
      param   (vlax-curve-getParamAtPoint objLwp midpo)
      ang      (angle '(0 0 0) (vlax-curve-getFirstDeriv objLwp param))
      mfen1   (polar midpo (+ ang (/ pi 2)) 75)
      mfen2   (polar midpo (- ang (/ pi 2)) 75)
      ss      (ssget "F" lstpoF '((0 . "TEXT") (8 . "sttnut")))
      lstLWP2 (SS-entlst (ssget	"F"
				(list mfen1 mfen2)
				'((0 . "LWPOLYLINE") (8 . "vh"))
			 )
	      )
)
(if ss
  (progn
    (setq lsttext_ong (SS-entlst ss))
;;;    ==============;tung text chu dau ong sttnut
    (foreach ent lsttext_ong
      (redraw ent 3)
      (setq po	   (dxf 10 ent)
	    str	   (dxf 1 ent)
	    val	   (read str)
	    lstval (append (list str) lstval)
      )
      (redraw ent 4)
    )
    (setq doan (strcat (nth 1 lstval) "-" (nth 0 lstval)))
    (and doan (vla-setText TblObj nRow 0 doan))
    (setq nCol 1)
  )
)
;;;    =========================;tung duong bao / 2 
(foreach entLWP2 lstLWP2
  (setq	lstenttext
	 (acet-dxf entLWP2 lstLWP_vh_text)
	lstpo nil
	lstvalpo nil
  )
;;;      ====================;tung text trong duong bao
  (foreach ent lstenttext
    (setq po (dxf 10 ent))
    (if	(distof (dxf 1 ent))
      (setq strso (dxf 1 ent))
      (progn
	(setq str      (dxf 1 ent)
	      lstpo    (cons po lstpo)
	      lstvalpo (cons (cons po str) lstvalpo)
	)
      )
    )
  )
  (if lstvalpo
    (progn
      (setq pogan  (timgan midpo lstpo)
	    tenkhu (strcat strso (acet-dxf pogan lstvalpo))
      )
      (and tenkhu (vla-setText TblObj nRow nCol tenkhu))
      (setq nCol (1+ nCol))
    )
  )
)
(setq nRow (1+ nRow))
     )
   )					;END progn
 )					;END IF
 (vla-Update TblObj)
 (setq	en  (entlast)
ss (acet-list-to-ss (list en))
 )
 (setq
   p2 (acet-ss-drag-move ss p1 "\n<<< Place Table >>> ")
 )
 (vla-put-InsertionPoint TblObj (vlax-3d-point p2))
 (vla-Update TblObj)
 (vlax-release-object TblObj)
 (RESTORE)
 (vla-StartUndoMark ActDoc)
 (princ "\nThank you for use my lisp: svb.lsp!")
 (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 #89747 để đọc hướng dẫn và trao đổi thêm.

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