Jump to content

Lisp lệnh COPY2 PASTE2 trong chủ đề “Sử dụng ClipBoard trong LISP : Copy và Paste dữ liệu kiểu Text”

Giới thiệu với các bạn đọan LISP (vừa sưu tầm đuợc) lưu và lấy dữ liệu kiểu Text từ ClipBoard
Hàm SetClipBoardText By XShrimp : lưu dữ liệu kiểu Text vào ClipBoard
Hàm GetClipBoardText By Patrick_35 : lấy dữ liệu kiểu Text từ ClipBoard

Ứng dụng :
Trong CAD
- trên bản vẽ A gọi lệnh copy2 -> chọn Text nguồn,
- trên bản vẽ B, C ... gọi lệnh paste2 -> chọn các đối tựong đích >enter :bigsmile:

Với các Text từ nguồn khác như : Word, Excel , trình duyệt, … : hàm GetClipBoardText bỏ qua các đối tuợng đồ họa (image), chỉ chọn ra Text. Đặc biệt trong Excel : cho phép copy dữ liệu trong nhiều cell.

Cách dùng

  1. Tải tệp 75960_copy2_paste2.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 COPY2 PASTE2 để chạy lisp.

Mã nguồn 75960_copy2_paste2.lsp

;; free lisp from cadviet.com
;;; this lisp was downloaded from https://www.cadviet.com/forum/topic/14099-s%E1%BB%AD-d%E1%BB%A5ng-clipboard-trong-lisp-copy-v%C3%A0-paste-d%E1%BB%AF-li%E1%BB%87u-ki%E1%BB%83u-text/
(vl-load-com)
(defun c:copy2 (/ ent str); Copy to ClipBoard
 (if
   (and
     (setq ent (car (entsel (strcat"\nChon Text de luu vao ClipBoard <<" (if (setq str (GetClipBoardText)) (Trim_Str str 15) "nil") ">> :"))))
     (wcmatch (cdr (assoc 0 (entget ent))) "*TEXT") )
   (princ (strcat"\nDa luu <<" (SetClipBoardText (cdr (assoc 1 (entget ent)))) ">> vao ClipBoard." ))
   (alert "Chon doi tuong khong hop le.")
   )
 (princ)
 )

(defun c:paste2 (/ obj str ss); Paste from ClipBoard
 (if (setq str (GetClipBoardText))
   (progn
     (princ (strcat "\nChon text de gan gia tri tu ClipBoard <<" (Trim_Str str 15) ">> :"))
     (setq ss (ssget (list (cons 0 "*TEXT")) ))
     (foreach e (vl-remove-if 'listp (mapcar 'cadr (ssnamex ss)))
(vla-put-TextString (vlax-ename->vla-object e) str)     )
     )
   (alert (strcat "Gia tri ClipBoard hien hanh khong phai kieu String."
	   "\nGoi lenh Copy2 de luu gia tri vao ClipBoard."))
   )
 (princ)
)

(defun Trim_Str(Str len)
 (if(and Str (< len (strlen Str)))
   (strcat(substr Str 1 len)"...")
   Str
   ))
(defun SetClipBoardText (text / htmlfile result ) ; By XShrimp
 (if (= 'STR (type text))
   (progn
     (setq htmlfile (vlax-create-object "htmlfile")
    result (vlax-invoke (vlax-get (vlax-get htmlfile 'ParentWindow ) 'ClipBoardData) 'SetData "Text" text ) )
     (vlax-release-object htmlfile)
     text
     ))
 )
(defun GetClipBoardText( / htmlfile result ) ; By Patrick_35
 (setq htmlfile (vlax-create-object "htmlfile")
result (vlax-invoke (vlax-get (vlax-get htmlfile 'ParentWindow ) 'ClipBoardData) 'GetData "Text" ) )
 (vlax-release-object htmlfile)
 result
)

Thảo luận gốc

Lisp được chia sẻ trong chủ đề Sử dụng ClipBoard trong LISP : Copy và Paste dữ liệu kiểu Text. Xem đúng bài viết #75960 để đọc hướng dẫn và trao đổi thêm.

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