Lisp lệnh MEXY trong chủ đề “Viết Lisp theo yêu cầu”
Bạn xem cái này đã đạt yêu cầu chưa. Lệnh MEXY
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 49974_mexy.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 MEXY để chạy lisp.
Mã nguồn 49974_mexy.lsp (trích từ bài viết gốc)
(defun C:MEXY (/ Lent ent vent Fp Ep entL Nent Lstp pInt)
(INIT)
(SAVE_MODE)
(vl-load-com)
(setq trucchia (NHAPKEY "\n Chia deu doi tuong theo x/y: " "x y" trucchia)
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 vent (vlax-ename->vla-object ent)
Fp (vlax-curve-getStartPoint vent)
Ep (vlax-curve-getEndPoint vent)
)
(command "line" Fp (list (car Ep) (cadr Fp)) "")
(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
)
)
)
(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)))
)
)
)
( (= 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)))
)
)
)
)
(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)))
)
)
(Progn
(setq LNent (vl-string->list Nent)
Lst '()
SLst (List (cons -4 " )
(Foreach e LNent
(if (/= e 44)
(setq Lst (append Lst (list e)))
(setq Lst (vl-list->string Lst)
SLst (append SLst (list (cons 0 Lst)))
Lst '()
)
)
)
(setq SLst (append SLst (list (cons 0 (vl-list->string Lst))))
SLst (append SLst (list (cons -4 "OR>")))
)
(while (and (not (prompt dongnhac))
(not (setq ss (ssget SLst)))
)
)
)
)
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ủ đề Viết Lisp theo yêu cầu. Xem đúng bài viết #49974 để đọc hướng dẫn và trao đổi thêm.