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”

Các bác cho em hỏi.

Nếu em quét chọn 1 tập đối tượng Text chẳng hạn. Em muốn lấy tọa độ của Text, nội dung Text ( X Y Z ND) cho vào Clipboard được không ạ?

Sau khi quét chọn Text, kết thúc lệnh thì có thể paste được vào Notepad hoặc Word  hoặc Excel ...

X1 Y1 Z1 MD

X2 Y2 Z2 NHA

................

Lisp trên trình bày hơi rối tý nên em chỉnh sửa lại cho đẹp mắt

(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
)

Cảm ơn các anh, các bác nhiều

Cách dùng

  1. Tải tệp 320510_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 320510_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 #320510 để đọc hướng dẫn và trao đổi thêm.

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