Jump to content

Lisp lệnh EP MEP trong chủ đề “Viết Lisp theo yêu cầu”


Đây có thể là lisp bạn cần: lệnh ep để nhập 1 cao độ cho nhiều đường, lệnh mep để nhập cao độ khác nhau cho nhiều đường (chọn tất cả và lệnh sẽ zoom từng đường để bạn vào số liệu, hoặc nhập theo thứ tự tăng dần cao độ - phải chọn polyline theo đúng thứ tự: pick hoặc dùng fence line)

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 34480_ep_mep.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 EP MEP để chạy lisp.

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

(defun myerror (s)
 (cond
   ((= s "quit / exit abort") (princ))
   ((/= s "Function cancelled") (princ (strcat "\nError: " s)))
 )
 (setvar "cmdecho" CMD)		; Restore saved modes
 (setvar "osmode" OSM)
 (setq *error* OLDERR)			; Restore old *error* handler
 (princ)
)

;;;=========================================================================
(defun bocchu (ss1 c)
 (setq ob (entget (ssname ss1 c)))
 (setq ts (assoc 1 ob))
 (setq a (cdr ts))
)

(defun boc1chu (ob)
 (setq ts (assoc 1 ob))
 (setq a (cdr ts))
)


(defun Txtnum (num)
 (if (> num 0)
   (strcat "+" (rtos num 2 0))
   (rtos num 2 0)
 )
)
;;;=================================
(defun Txtint (num)
 (rtos num 2 0)

)

(defun Txtreal (num) (rtos num 2 2))
(defun Txtreal1 (num) (rtos num 2 0))

(defun thaychu (Ob newstr / obtmp)
 (setq txtstr (assoc 1 Ob))
 (setq newstr (cons 1 newstr)
obtmp (entmod (subst newstr txtstr Ob))
)
 (entupd (cdr (assoc -1 obtmp)))
)
(defun chonchu (dongnhac)
 (prompt dongnhac)
 (ssget
   '((-4 . "<OR") (0 . "text") (0 . "mtext") (0 . "ATTRIB") (-4 . "OR>"))
 )
)

(defun chon1chu	(dongnhac / obj objtype)
 (if (setq obj (nentsel dongnhac))
   (setq obj (entget (car obj))
  objtype (cdr (assoc 0 obj))
  )
   )
 (if (member objtype '("ATTRIB" "MTEXT" "TEXT"))
   (setq obj obj)
   )
)




(defun chon (str) (ssget '((cons (0 str)))))
(defun bamchon (st) (entget (car (entsel st))))
(defun bocdt (ss1 c) (entget (ssname ss1 c)))



;;;=============================================================================

=================
(Defun PlMake (Plist)			;  Create polyline entities
 (entmake '((0 . "POLYLINE")))
 (setq	n  (length Plist)
ic 0
 )
 (while (< ic n)
   (entmake (list (cons 0 "VERTEX") (cons 10 (nth ic Plist))))
   (setq ic (1+ ic)
   )
 )
 (entmake '((0 . "SEQEND")))

)

;;;===========================================================
;;;                 Tao cac layer
(Defun Laymake ()
 (command "regenauto" "off")
 (if (= (tblsearch "LAYER" "Kyhieuga") nil)
   (progn
     (command "layer" "n" "Kyhieuga" "")
     (princ)
     (command "layer" "c" 8 "Kyhieuga" "")
     (princ)
   )
 )
 (if (= (tblsearch "LAYER" "Kyhieuganb") nil)
   (progn
     (command "layer" "n" "Kyhieuganb" "")
     (princ)
     (command "layer" "c" 8 "Kyhieuganb" "")
     (princ)
   )
 )


 (if (= (tblsearch "LAYER" "TxtGT") nil)
   (progn
     (command "layer" "n" "TxtGT" "")
     (princ)
     (command "layer" "c" 7 "TxtGT" "")
     (princ)
   )
 )
 (if (= (tblsearch "LAYER" "TxtCDMG") nil)
   (progn
     (command "layer" "n" "TxtCDMG" "")
     (princ)
     (command "layer" "c" 1 "TxtCDMG" "")
     (princ)
   )
 )
 (if (= (tblsearch "LAYER" "TxtCDDC") nil)
   (progn
     (command "layer" "n" "TxtCDDC" "")
     (princ)
     (command "layer" "c" 2 "TxtCDDC" "")
     (princ)
   )
 )
 (if (= (tblsearch "LAYER" "TxtCDDG") nil)
   (progn
     (command "layer" "n" "TxtCDDG" "")
     (princ)
     (command "layer" "c" 3 "TxtCDDG" "")
     (princ)
   )
 )
 (command "regenauto" "on")
 (princ)
)

;;;=========================================================
(defun arrow (P1 P2 Htxt / Pm wid)
 (setq Plw (getvar "PLineWid"))
 (Setq	wid (/ Htxt 4)
Pm  (list (/ (+ (car P1) (car P2)) 2)
	  (/ (+ (cadr P1) (cadr P2)) 2)
    )
 )
 (command "Pline" P1 "W" 0 wid Pm "W" 0 0 P2 "")
)
;;;=======================================================
;;;	 Ham nhan list dinh tu Polyline								
;;;
(defun Getvpl (ename / Elist rep e1 dp c)
 (setq	rep "C"
Elist '()
c 1
 )
 (setq
   ename (entnext ename))
 (while (/= rep "SEQEND")
   (setq e1 (entget ename))
   (setq rep (cdr (assoc 0 e1)))
   (if	(/= rep "VERTEX")
     (princ)
     (progn
(setq dp (cdr (assoc 42 e1)))
(setq
  elist	(cons (cons c
		    (reverse (cdr (reverse (cdr (assoc 10 e1))
				  )
			     )
		    )
	      )
	      elist
	)
)
     )
   )
   ;;end if
   (setq ename	(entnext ename)
  c	(1+ c)
   )
 )
 ;;end while
 (setq elist (reverse elist))
 ;;kthuc 
)
;;;
;;;	 Ham nhan list dinh tu LWPolyline								
;;;
(defun Getvlw (ename / di i e1 elist dp c)
 (setq e1 (entget ename))
 (setq	elist '()
c 1
c1 1
 )
 (while e1
   (setq di (car e1))
   (if	(/= 10 (car di))
     (princ)
     (progn
(setq vlap 1
      dp nil
)
(while (and e1 (/= vlap 42))
  ;;never null
  (setq i (car e1))
  (setq vlap (car i))
  (if (/= vlap 42)
    (setq e1 (cdr e1))
    (setq dp (cdr i))
  )
)
;;end while
(if (null dp)
  (alert "Error bulge in the LWPOLYLINE")
  ;;nerver appear 
  (setq	elist (cons (cons c1 (cdr di)) elist)
	c1    (1+ c1)
  )
)
;;end if
     )
   )
   ;;end if
   (setq e1 (cdr e1)
  c  (1+ c)
   )
 )
 ;;end while
 (setq elist (reverse elist))
)

;;;================================================Nhap gia tri cao do cho nhieu polyline
(defun C:ep (/ sspl evl)
 (command "undo" "begin")
 (setvar "cmdecho" 0)
 (setq	sspl (ssget '((-4 . "<OR")
	      (0 . "Polyline")
	      (0 . "LWpolyline")
	      (-4 . "OR>")
	     )
     )
cter 0
 )
;;; Nhap du lieu (lay gia tri mac dinh la gia tri da nhap vao truoc do), neu su dung lan dau, mac dinh la 0
 (if oldstr
   (if	(= nil (setq evl (getreal oldstr)))
     (if oldevl
(setq evl oldevl)
(setq evl 0.00)
     )
   )
   (progn
     (setq oldstr "\nNhap vao gia tri cao do: <Enter for 0.00> ")
     (if (= nil
     (setq evl (getreal oldstr))
  )
(setq evl 0.00)
     )
   )
 )
 (setq	oldevl evl
oldstr (strcat "\nNhap vao gia tri cao do: <Enter for "
	       (rtos oldevl 2 2)
	       "> "
       )
 )
;;; ket thuc nhap du lieu
 (while (< cter (sslength sspl))
   (setq objpl	 (ssname sspl cter)
  entpl	 (entget objpl)
  pltype (cdr (assoc 0 entpl))
  cter	 (1+ cter)
   )
   (cond
     ((= pltype "POLYLINE")
      (setq evlpl  (cdr (assoc 10 entpl))
     newevl (cons 10 (list (car evlpl) (cadr evlpl) evl))
     entpl  (entmod (subst newevl (assoc 10 entpl) entpl))
      )
      (entupd objpl)
     )
     ((= pltype "LWPOLYLINE")
      (setq evlpl  (cdr (assoc 38 entpl))
     newevl (cons 38 evl)
     entpl  (entmod (subst newevl (assoc 38 entpl) entpl))
      )
      (entupd objpl)
     )
   )
 )
 (command "undo" "end")
 (setvar "cmdecho" 1)
 (princ)
)
;;;=============================================================================

============
;;; Nhap gia tri cao do cho nhieu polyline (moi polyline co mot gia tri cao do khac nhau
(defun C:mep (/ sspl evl)
 (command "undo" "begin")
 (setvar "cmdecho" 0)
 (setq	sspl (ssget '((-4 . "<OR")
	      (0 . "Polyline")
	      (0 . "LWpolyline")
	      (-4 . "OR>")
	     )
     )
cter 0
 )
 (initget "Y N")
 (if (= nil
 (setq res
	(getkword
	  "\nCo nhap buoc nhay cho duong dong muc ko? <Y/N - Enter for none>: "
	)
 )
     )
   (setq step 0)
   (if
     (= nil
 (setq
   step	(getreal "\nNhap gia tri buoc nhay <Enter for 0.1>: ")
 )
     )
      (setq step 0.1)
   )
 )
 (while (< cter (sslength sspl))
   (setq objpl	 (ssname sspl cter)
  entpl	 (entget objpl)
  pltype (cdr (assoc 0 entpl))
  cter	 (1+ cter)
   )
   (cond
     ((= pltype "POLYLINE")
      (setq
 polist	(getvpl objpl)
      )
     )
     ((= pltype "LWPOLYLINE")
      (setq polist (getvlw objpl)
      )
     )
   )

   (setq Xmax (car (cdr (nth 0 polist)))
  Xmin (car (cdr (nth 0 polist)))
  Ymax (cadr (cdr (nth 0 polist)))
  Ymin (cadr (cdr (nth 0 polist)))
  ctmax 0)
   (repeat (length polist)
     (if (<= xmax (car (cdr (nth ctmax polist))))
(setq xmax (car (cdr (nth ctmax polist))))
)
     (if (>= xmin (car (cdr (nth ctmax polist))))
(setq xmin (car (cdr (nth ctmax polist))))
)
     (if (<= Ymax (cadr (cdr (nth ctmax polist))))
(setq Ymax (cadr (cdr (nth ctmax polist))))
)
     (if (>= Ymin (cadr (cdr (nth ctmax polist))))
(setq Ymin (cadr (cdr (nth ctmax polist))))
)
     (setq ctmax (1+ ctmax))
     )
   (setq ppl  (list Xmax Ymax)
  ppl1 (list Xmin Ymin ) 
   )
   (command "zoom" "W"	ppl ppl1)

   (redraw objpl 3)
;;; Nhap du lieu (lay gia tri mac dinh la gia tri da nhap vao truoc do), neu su dung lan dau, mac dinh la 0
   (if	oldstr
     (if (= nil (setq evl (getreal oldstr)))
(if oldevl
  (setq evl oldevl)
  (setq evl 0.00)
)
     )
     (progn
(setq oldstr "\nNhap vao gia tri cao do: <Enter for 0.00> ")
(if (= nil
       (setq evl (getreal oldstr))
    )
  (setq evl 0.00)
)
     )
   )
   (setq oldevl (+ evl step)
  oldstr (strcat "\nNhap vao gia tri cao do: <Enter for "
		 (rtos oldevl 2 2)
		 "> "
	 )
   )
;;; ket thuc nhap du lieu

   (cond
     ((= pltype "POLYLINE")
      (setq evlpl  (cdr (assoc 10 entpl))
     newevl (cons 10 (list (car evlpl) (cadr evlpl) evl))
     entpl  (entmod (subst newevl (assoc 10 entpl) entpl))
     polist (getvpl objpl)
      )
      (entupd objpl)
     )
     ((= pltype "LWPOLYLINE")
      (setq evlpl  (cdr (assoc 38 entpl))
     newevl (cons 38 evl)
     entpl  (entmod (subst newevl (assoc 38 entpl) entpl))
     polist (getvlw objpl)
      )
      (entupd objpl)
     )
   )
   (redraw objpl 4)
 )
 (command "undo" "end")
 (setvar "cmdecho" 1)
 (princ)
)

Thảo luận gốc

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

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