Jump to content

Lisp xuất-nhập toạ độ

+ Lệnh MEXY chia Polyline/Spline theo phương X/Y thành những đoạn nhỏ bằng nhau có kích thước xác định.

http://img132.imageshack.us/img132/7104/splinexl1.jpg
http://img132.imageshack.us/img132/splinexl1.jpg/1/w949.png
Lisp này do bác Nataca viết:

Cách dùng

  1. Tải tệp 50690_mexy.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 MEXY để chạy lisp.

Mã nguồn 50690_mexy.lsp

;; free lisp from cadviet.com
;;; this lisp was downloaded from https://www.cadviet.com/forum/topic/9019-%C4%91%C3%A3-xong-lisp-xu%E1%BA%A5t-nh%E1%BA%ADp-to%E1%BA%A1-%C4%91%E1%BB%99/
(defun C:MEXY (/ Lent ent vent Fp Ep entL Nent Lstp pInt)
 	(INIT)
 	(SAVE_MODE)
 	(vl-load-com)
 	(setvar "OSMODE" 0)
(setq trucchia 	(NHAPKEY "\n Chia deu doi tuong theo x/y: " "x y" trucchia)
	  huongchia	(NHAPKEY "\n Chon huong chia <1-bat dau phia trai(x)/duoi(y), 2-bat dau phia phai(x)/tren(y): " "1 2" huongchia)
	  Lent		(SS2LST (ESENT "\n Chon doi tuong can chia" "POLYLINE,LWPOLYLINE,SPLINE,ARC,ELLIPSE,CIRCLE,LINE"))
	  kcach		(NHAPREAL "\n Nhap khoang cach chia:" kcach)
 	)
 	(foreach ent Lent
  	(setq 	Fp	(vlax-curve-getStartPoint ent)
			Ep	(vlax-curve-getEndPoint ent)
	)
  	(cond	( 	(= trucchia "x")
  				(command "line" Fp (list (car Ep) (cadr Fp)) "")
			 	(setq Mp	(list (/ (+ (car Fp) (car Ep)) 2) (cadr Fp)))
		 	)
			(	(= trucchia "y")
	  			(command "line" Fp (list (car Fp) (cadr Ep)) "")
			 	(setq Mp	(list (car Fp) (/ (+ (cadr Fp) (cadr Ep)) 2)))
		 	)
  	)

  	(setq	entL 	(entlast)
	  		Nent 	entL
			Lstp	'()
  	)
  	(command "measure" Nent kcach)
  	(while Nent
	  	(setq Nent 	(entnext Nent)
			  Lstp	(if (and	Nent
								(= (cdr (assoc 0 (entget Nent))) "POINT")
						)
						(append Lstp (list Nent))
					  	Lstp
				  	)
	  	)
  	)
  	(if (= huongchia "1")
	  	(foreach p Lstp
		  	(if (= trucchia "x")
	  			(command "mirror" p "" Mp (list (car Mp) (1+ (cadr Mp))) "Y")
			  	(command "mirror" p "" Mp (list (1+ (car Mp)) (cadr Mp)) "Y")
		  	)
 		)
  	)
 		(cond	(	(= trucchia "x")
			 	(foreach d Lstp 
				 	(command "xline" "v" (cdr (assoc 10 (entget d))) "")
				  	(setq pInt (car (GIAODT (entlast) ent)))
				  	(entdel (entlast))
				  	(if pInt
						(entmake (list '(0 . "POINT") (cons 10 pInt)))
			  		)
				)
			 	(entmake (list '(0 . "POINT") (cons 10 Fp)))
			 	(entmake (list '(0 . "POINT") (cons 10 Ep)))
		 	)
			(	(= trucchia "y")
			 	(foreach d Lstp 
				 	(command "xline" "h" (cdr (assoc 10 (entget d))) "")
				  	(setq pInt (car (GIAODT (entlast) ent)))
				  	(entdel (entlast))
				  	(if pInt
						(entmake (list '(0 . "POINT") (cons 10 pInt)))
			  		)
				)
			 	(entmake (list '(0 . "POINT") (cons 10 Fp)))
			 	(entmake (list '(0 . "POINT") (cons 10 Ep)))
		 	)
	)
  	(foreach p Lstp (entdel p))
  	(entdel entL)
 	)
 	(DONE)
 	(RESTORE)
)

;;;Ham con

; HAM BAY LOI
(defun INIT ()
 	(setq 	OLD_ERROR 	*error*
		*error* 	MYERROR
)
  	(command "Undo" "begin")
)1

(defun MYERROR (errmsg)

 	(cond
   		(	(= errmsg "quit / exit abort")
	 	(princ)
	)
   		(	(/= errmsg "Function cancelled")
	 	(princ (strcat "\n Co loi: " errmsg))
	)
 	)
 	;(command "Undo" 1)
 	(setvar "osmode" OLD_OSMODE)
(command "CECOLOR" OLD_CECOLOR)
	(DONE)
 	(prompt "\n Da thuc hien ham error, Reset lai thiet lap ban dau")
  	(command "Undo" "end")

)

(defun DONE ()
 	(if OLD_ERROR (setq *error* OLD_ERROR))
)
;;;;;----------------------------------------------------------
; HAM LUU VA TRA LAI CAC THONG SO BAN DAU
(defun SAVE_MODE()

 	(command "Undo" "begin")
(command "UCS" "W" "")
	(setq   OLD_OSMODE 		(getvar "OSMODE")
			OLD_CECOLOR 	(getvar "CECOLOR")
		OLD_AUTOSNAP	(getvar "AUTOSNAP")
		OLD_ORTHOMODE	(getvar "ORTHOMODE")
)
 	(command "cmdecho" 0)

)
(defun RESTORE()

 	(command "Undo" "end")			
 	(setvar "osmode" 	OLD_OSMODE)
(setvar "AUTOSNAP"	OLD_AUTOSNAP)
(setvar	"ORTHOMODE" OLD_ORTHOMODE)
	(command "CECOLOR" OLD_CECOLOR)
 	(command "cmdecho" 1)
(Grtext -1 "Lisp's written by Nataca - 0983.715.333")
)
;;;NHAP GIA TRI LA SO THUC
(defun NHAPREAL (dongnhac Tso)
(if (null Tso)
	(progn
		(initget (+ 1 2))
		(getreal (strcat dongnhac " <?>:"))
	)
	(progn
		(cond 
		  	(	(progn
					(initget (+ 2))
					(getreal (strcat dongnhac " < "(rtos Tso 2 5) " >:"))
			  	)
			)
			(T Tso)

		)
	)

)
)

;------------------------------------------
;;;NHAP KEY VAO
(defun NHAPKEY	(dongnhac key Text)

(if (null Text)
	(progn
		(initget 1 key)
		(getkword (strcat dongnhac " :"))
	)
	(progn
		(cond
			((progn
				 (initget key)
				 (getkword (strcat dongnhac " < " Text " >:"))
			 )
			)
			(T Text)

		)
	)

)
)
; CHUYEN BIEU DIEN TAP HOP DOI TUONG DUOI DANG LIST CHUA ENAME CUA CAC DOI TUONG
(defun SS2LST (ss / sodt index lstent)
(setq
	sodt (if ss (sslength ss) 0)
	index 0
)
(repeat sodt
	(setq 	ent 	(ssname ss index)
			index 	(1+ index)
			lstent 	(cons ent lstent)
	)
)
(reverse lstent)
)

;;; CHON DOI TUONG THEO NGUOI DUNG KEM DONG NHAC (BAT BUOC CHON)

(defun ESENT (dongnhac Nent / ss LNent Lst SLst)

 	(if (= Nent nil)
  	(while 	(and	(not (prompt dongnhac))
					(not (setq ss	(ssget)))
			)
  	)
  	(while 	(and	(not (prompt dongnhac))
					(not (setq ss	(ssget (list (cons 0 Nent)))))
			)
  	)

 	)
 	ss
)

;;;GIAO CUA 2 DOI TUONG
(defun GIAODT (ent1 ent2 / ob1 ob2 g kq sd)
(setq	ob1 (vlax-ename->vla-object ent1)
		ob2 (vlax-ename->vla-object ent2)
)
(setq	g (vlax-variant-value
			  (vla-IntersectWith ob1 ob2 acExtendNone)
		  )
)
(if (/= (vlax-safearray-get-u-bound g 1) -1)
	(setq g (vlax-safearray->list g))
	(setq g nil)
)
(if g
	(progn
		(setq	kq	nil
				sd	(fix (/ (length g) 3))
		)
		(repeat sd
			(setq	kq	(append kq (list (list (car g) (cadr g) (caddr g))))
					g	(cdddr g)
			)
		)
		kq
	)
	nil
)
)

Thảo luận gốc

Lisp được chia sẻ trong chủ đề [Đã xong] Lisp xuất-nhập toạ độ. Xem đúng bài viết #50690 để đọc hướng dẫn và trao đổi thêm.

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