Jump to content

Lisp lệnh BFIND%3B trong chủ đề “Find và Replace trong nhiều bản vẽ”

hôm trước lang thang trên mạng, tìm được cái Lisp của Lee McDonnell, thấy hay quá, tuy nhiên, do bản vẽ nơi mình làm việc, dung toàn tiếng anh nên chưa gặp vấn đề gì. còn với các bản vẽ có tiếng việt, các bạn thử test xem nhé. Lisp có rất nhiều tùy chọn.

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 100317_bfind%3B...............................................................................%3B.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 BFIND%3B để chạy lisp.

Mã nguồn 100317_bfind%3B...............................................................................%3B.lsp (trích từ bài viết gốc)

;;;¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,;;;
;;;ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,¤º°`°º¤;;;
;;																			 ;;
;;																			 ;;
;;					   --=={  Batch Find & Replace  }==--					;;
;;																			 ;;
;;  Will Find and Replace strings entered by the user within Text, MText, Block;;
;;  Attributes, MLeader Text, Table Text and Dimensions within the current	 ;;
;;  drawing and/or a directory (and subdirectories) of drawings.			;;
;;																			 ;;
;;  The user can choose which objects to search, whether the find string is	;;
;;  case sensitive, and whether to search for whole words only, i.e. strings;;
;;  that match the Find String exactly in length.							  ;;
;;																			 ;;
;;  Example:   Find  ~  AutoCAD  =>  Replace With  ~  CAD					  ;;
;;																			 ;;
;;  With 'Match Case' ON:   AutoCAD  =>  CAD								;;
;;																			 ;;
;;  With 'Match Case' OFF:  AutoCAD  =>  CAD								;;
;;						  AUTOCAD  =>  CAD								;;
;;						  AuToCaD  =>  CAD								;;
;;																			 ;;
;;																			 ;;
;;  The user can also choose to 'Ignore Locked Layers', with this option set,  ;;
;;  all objects on Locked Layers are not searched for the Find String, else all;;
;;  objects are searched.													  ;;
;;																			 ;;
;;																			 ;;
;;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=;;
;;																			 ;;
;;  FUNCTION SYNTAX:  BFind													;;
;;																			 ;;
;;  Notes:-																	;;
;;  ---------																  ;;
;;  As usual the known bug with ObjectDBX arises when it comes to attribute	;;
;;  alignment after modification. A sub-function has been included to try to;;
;;  allow for the shift in position, however it is not 100% foolproof, and a;;
;;  slight shift in position may be noticed. The attribute is realigned when the;;
;;  block is moved manually.												;;
;;																			 ;;
;;  As expected, when using the Batch Find and Replace, drawing thumbnails are ;;
;;  lost upon saving with ObjectDBX. These return when the drawing is saved	;;
;;  manually.																  ;;
;;																			 ;;
;;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=;;
;;																			 ;;
;;  AUTHOR:																	;;
;;																			 ;;
;;  Copyright © Lee McDonnell, March 2010. All Rights Reserved.				;;
;;																			 ;;
;;	  { Contact: Lee Mac @ TheSwamp.org, CADTutor.net }					  ;;
;;																			 ;;
;;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=;;
;;																			 ;;
;;  VERSION:																;;
;;																			 ;;
;;  ø 1.0   ~¤~	24th March 2010   ~¤~									;;
;;																			 ;;
;;  º First Release															;;
;;..............................................................................

.;;
;;  ø 1.1   ~¤~	26th March 2010   ~¤~									;;
;;																			 ;;
;;  º Added Options Button.													;;
;;  º Updated String Replacement functions to allow for case sensitivity	;;
;;	options.																 ;;
;;  º Upgraded StringRep function to allow replacement string to contain find  ;;
;;	string.																  ;;
;;  º Fixed bug causing an unsaved drawing to be saved.						;;
;;..............................................................................

.;;
;;  ø 1.2   ~¤~	29th March 2010   ~¤~									;;
;;																			 ;;
;;  º Added ability to perform multiple replacements.						  ;;
;;..............................................................................

.;;
;;  ø 1.3   ~¤~	31st March 2010   ~¤~									;;
;;																			 ;;
;;  º Added ability to edit find and replace entry by double-clicking.		 ;;
;;  º Added 'Table Text' object to Options Dialog.							 ;;
;;  º Added 'Find Whole Words Only' option to Options Dialog.				  ;;
;;  º Redesigned Options Dialog to accommodate new toggles, added mnemonics.;;
;;..............................................................................

.;;
;;																			 ;;
;;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=;;
;;																			 ;;
;;;¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,;;;
;;;ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,¤º°`°º¤;;;

(defun c:BFind

;............................................................................
...;

 (   /

;;		 --=={ Local Functions }==--		;

  *error*
  CALCINSPT
  DIR_MODE DIR_TEXT DCL_WRITE DIRDIALOG
  ENTRY_EDIT
  GET_SUBS
  L->PTR LOGO
  OBJECTDBXDOCUMENT OBJRELEASE OPTIONS_DIALOG
  POPULATE_LISTBOX POPUP PTR->L
  READ_CONFIG REMOVE_ITEMS
  SORTBYFIRST STRINGREP SUBST_AT_N
  WRITE_CONFIG

;;		 --=={ Local Variables }==--		;

  *ACAD *ADOC
  *BFIND_CUR* *BFIND_LST* *BFIND_OPT* *BFIND_PAT* *BFIND_SUB*
  ATT
  CASEFLAG CFGFNAME COL
  DBX DCEDITITLE DCFLAG DCFNAME DCOPTTITLE DCTAG DCTITLE DOCLST DOUBLECLICKTIME DWLST
  EXPRESS
  FILE FILEPATH FLAG FOLDER FS FSTR
  I ITEMS
  LOCKED LOCKEDLAYERS LOCKFLAG
  MODE
  NSTR
  ODOC OFILE ONAME OV
  PROGBAR
  ROW RS RSTR
  SHELL
  TMP
  UFLAG
  VAL VERSIONNUMBER VL
  WHOLE WSHSHELL

;;		 --=={ Global Variables }==--	;

; --None--

 )

 (vl-load-com)


;;;¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,;;;

;;						   --=={  Preliminaries  }==--					;;

;;;¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,;;;


 (setq VersionNumber "1.3")

 (setq dcfname	(strcat "LMAC_BFind_V" VersionNumber ".dcl")

	cfgfname   (strcat "LMAC_BFind_V" VersionNumber ".cfg")

	dctitle	(strcat "Batch Find & Replace V" VersionNumber)

	dcopttitle "Options"

	dcedititle "Edit Entry")

 (setq DoubleClickTime 0.0000011667);; Increase to allow for slower double-click

 (setq *acad (vlax-get-acad-object)
	*adoc (vla-get-ActiveDocument *acad))

 (setq Express
(and (vl-position "acetutil.arx" (arx))
	 (not
	   (vl-catch-all-error-p
		 (vl-catch-all-apply
		   (function (lambda nil (acet-sys-shift-down))))))))


;;;¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,;;;

;;						   --=={  Local Functions  }==--					 ;;

;;;¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,;;;


;;  --=={  Error Handler  }==--

 (defun *error* (msg)

(and ofile  (close ofile))
(and dcTag  (unload_dialog dcTag))
(and Express ProgBar (acet-ui-progress))
(and uFlag  (vla-EndUndomark *adoc))
(and ov	 (mapcar (function setvar) vl ov))

(ObjRelease (list *acad *adoc Shell WSHShell dbx oDoc))

(mapcar
  (function (lambda (symbol) (set symbol nil)))

  '(*acad *adoc *BFind_fnd* *BFind_rep* *BFind_pat* *BFind_cur* *BFind_sub* *BFind_opt*))

(or (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*")
	(princ (strcat "\n** Error: " msg " **")))

(princ))

;............................................................................

.....;

 (defun write_config (fname lst / wPath ofile)

(if (setq wPath (findfile "ACAD.PAT"))
  (progn
	(setq wPath (vl-filename-directory wPath))

	(or (eq "\\" (substr wPath (strlen wPath)))
		(setq wPath (strcat wPath "\\")))

	(setq ofile (open (strcat wPath fname) "w"))

	(foreach x lst
	  (write-line (vl-prin1-to-string x) ofile))

	(setq ofile (close ofile))

  t)))

;............................................................................

.....;

 (defun read_config  (fname lst / ofile)

(if (and (setq fname (findfile fname))
		 (setq ofile (open fname "r")))
  (progn

	(foreach x lst (set x (read (read-line ofile))))
	(setq ofile (close ofile))

  t)))

;............................................................................

.....;

 (defun ObjRelease (lst)
(mapcar
  (function
	(lambda (x)
	  (if (and (eq (type x) 'VLA-OBJECT)
			   (not (vlax-object-released-p x)))
		(vl-catch-all-apply
		  (function vlax-release-object) (list x))))) lst))

;............................................................................

.....;

 (defun DirDialog (msg dir flag / Shell Fold Path)

(setq *acad (cond (*acad) ((vlax-get-acad-object))))

(setq Shell (vla-getInterfaceObject *acad "Shell.Application")
	  Fold  (vlax-invoke-method Shell 'BrowseForFolder
			  (vla-get-HWND *acad) msg flag dir))
(vlax-release-object Shell)

(if Fold
  (progn
	(setq Path (vlax-get-property
				 (vlax-get-property Fold 'Self) 'Path))
	(vlax-release-object Fold)

	(and (= "\\" (substr Path (strlen Path)))
		 (setq Path (substr Path 1 (1- (strlen Path)))))))

Path)

;............................................................................

.....;

 (defun Get_Subs (folder / file);; CAB
(mapcar
  (function
	(lambda (x) (setq file (strcat folder "\\" x))
				(cons file (apply (function append) (get_subs file)))))
	(cddr (vl-directory-files folder nil -1))))

;............................................................................

.....;

 (defun ObjectDBXDocument (/ acVer)

(setq *acad (cond (*acad) ((vlax-get-acad-object))))

(vla-GetInterfaceObject *acad
  (if (< (setq acVer (atoi (getvar "ACADVER"))) 16) "ObjectDBX.AxDbDocument"
	(strcat "ObjectDBX.AxDbDocument." (itoa acVer)))))

;............................................................................

.....;

 (defun Popup (title flags msg / WSHShell result)

(setq WSHShell (vlax-create-object "WScript.Shell"))
(setq result   (vlax-invoke WSHShell 'Popup msg 0 title flags))
(vlax-release-object WSHShell)

result)

;............................................................................

.....;

 (defun DCL_Write (fname / wPath ofile)

(if (not (findfile fname))

  (if (setq wPath (findfile "ACAD.PAT"))
	(progn
	  (setq wPath (vl-filename-directory wPath))

	  (or (eq "\\" (substr wPath (strlen wPath)))
		  (setq wPath (strcat wPath "\\")))

	  (setq ofile (open (strcat wPath fname) "w"))

	  (foreach str

		'("//;¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,;//"
		  "//;ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,¤º°`°º¤;//"
		  "//																			   //"
		  "//																			   //"
		  "//					   --=={  Batch Find & Replace  }==--					  //"
		  "//																			   //"
		  "//  BFind.dcl to be used in conjunction with BFind.lsp.						  //"
		  "//																			   //"
		  "//  Copyright © Lee McDonnell, March 2010. All Rights Reserved.				  //"
		  "//																			   //"
		  "//																			   //"
		  "//;¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,;//"
		  "//;ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,¤º°`°º¤;//"
		  ""
		  "//  --=={ Sub-Assembly Definitions }==--"
		  ""
		  "boxcol : boxed_column {  width =  60; fixed_width  = true; alignment = centered; }"
		  "edit60 :	 edit_box {  width =  60;  fixed_width = true; alignment = left;	 }"
		  "space1 :	   spacer { height = 0.1; fixed_height = true;					   }"
		  "butt10 :	   button {  width =  10; fixed_width  = true; alignment = centered; }"
		  "butt12 :	   button {  width =  12; fixed_width  = true; alignment = centered; }"
		  "butt20 :	   button {  width =  20; fixed_width  = true; alignment = centered; }"
		  ""
		  "//;¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,;//"
		  ""
		  "						//  --=={ Main Dialog Definition }==--  //"
		  ""
		  "//;¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,;//"
		  ""
		  "bfind : dialog { key = \"dcl_title\";"
		  "  spacer;"
		  "  : text { label = \"Copyright (c) 2010 Lee McDonnell\"; alignment = right; }"
		  ""
		  "  : column {"
		  "  "
		  "	: text   { key = \"fw\"; label = \"Find &What:\"; }"
		  "	: edit60 { key = \"fstr\"; mnemonic = \"W\"; }"
		  "	"
		  "	spacer;"
		  "	"
		  "	: text   { key = \"rw\"; label = \"&Replace With:\"; }"
		  "	: edit60 { key = \"rstr\"; mnemonic = \"R\"; }"
		  "	"
		  "	spacer;"
		  "  }"
		  "  "
		  "  spacer;"
		  ""
		  "  : row {"
		  ""
		  "	spacer;"
		  "	: butt20 { key = \"add\"; label = \"Add\"; }"
		  "	: butt10 { key = \"clr\"; label = \"Clear\"; }"
		  "	: butt20 { key = \"rem\"; label = \"Remove\"; }"
		  "	spacer;"
		  ""
		  "  }"
		  ""
		  "  spacer;"
		  ""
		  "  : list_box { key = \"rep_list\"; multiple_select = true;"
		  "			   fixed_width = false; alignment = centered; tabs = \"30\"; }"
		  ""
		  "  : text   { label = \"Double-Click to Edit Entry   \"; alignment = right; }"
		  ""
		  "  : boxcol { label = \"Drawing Directory\";"
		  ""
		  "	: row {"
		  ""
		  "	  : column {"
		  ""
		  "		space1;"
		  "		: text   { key = \"dir_text\"; alignment = left; }"
		  "		space1;"
		  ""
		  "	  }"
		  ""
		  "	  : butt10 { label = \"Directory...\"; key = \"dir\"; }"
		  ""
		  "	}"
		  ""
		  "	: row {"
		  ""
		  "	  : toggle { key = \"sub_dir\"; label = \"Include Sub-Directories\"; }"
		  "	  : toggle { key = \"cur_dwg\"; label = \"Current Drawing Only\" ; }"
		  "	  "
		  "	}"
		  ""
		  "	spacer;"
		  "  }"
		  ""
		  "  spacer;"
		  ""
		  "  : row {"
		  ""
		  "	: butt12 { key = \"option\"; label = \"Options\"; }"
		  ""
		  "	: butt12 { key = \"accept\"; label = \"OK\"; is_default = true; }"
		  ""
		  "	: butt12 { key = \"cancel\"; label = \"Cancel\"; is_cancel = true; }"
		  "	"
		  "	: image  { key = \"logo\"; alignment = centered;"
		  "			   width = 16.06; fixed_width  = true;"
		  "			   height = 2.06; fixed_height = true; color = -15; }"
		  "  }"
		  "  "
		  "}"
		  ""
		  ""
		  "//;¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,;//"
		  ""
		  "					//  --=={ Options Dialog Definition }==--  //"
		  ""
		  "//;¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,;//"
		  ""
		  "bfind_opt : dialog { key = \"dcl_title_opt\";"
		  "  spacer;"
		  ""
		  "  : row {"
		  ""
		  "	spacer;"
		  ""
		  "	: column {"
		  ""
		  "	  : row {"
		  "	"
		  "		: toggle { key = \"case\"; label = \" Match &Case\"		  ; mnemonic = \"C\"; }"
		  "		: toggle { key = \"whol\"; label = \" Find &Whole Words Only\"; mnemonic = \"W\"; }"
		  ""
		  "	  }"
		  "	"
		  "	  : toggle { key = \"lock\"; label = \" &Ignore Objects on Locked Layers\"; mnemonic = \"I\"; }"
		  ""
		  "	}"
		  ""
		  "  }	"
		  "  "
		  "  spacer;"
		  ""
		  "  : boxed_column { label = \"Objects to Search\";"
		  ""
		  "	: row {"
		  ""
		  "	  : column {"
		  ""
		  "		: toggle { key = \"dtxt\"; label = \" &Single-line Text\"; mnemonic = \"S\"; }"
		  "		: toggle { key = \"mtxt\"; label = \" &Multiline Text\" ; mnemonic = \"M\"; }"
		  "		: toggle { key = \"att\"; label = \" &Block Attributes\"; mnemonic = \"B\"; }"
		  ""
		  "	  }"
		  ""
		  "	  : column {"
		  "	  "
		  "		: toggle { key = \"dim\"; label = \" &Dimension Text\" ; mnemonic = \"D\"; }"
		  "		: toggle { key = \"mld\"; label = \" M&ultileader Text\"; mnemonic = \"u\"; }"
		  "		: toggle { key = \"tab\"; label = \" &Table Text\"	 ; mnemonic = \"T\"; }"
		  ""
		  "	  }"
		  "	  "
		  "	}"
		  ""
		  "	spacer;"
		  ""
		  "  }  "
		  "  spacer;"
		  ""
		  "  ok_cancel;"
		  "}"
		  ""
		  ""
		  "//;¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,;//"
		  ""
		  "				 //  --=={ Entry Editing Dialog Definition }==--  //"
		  ""
		  "//;¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,;//"
		  ""
		  "bfind_edit : dialog { key = \"dcl_title_edi\";"
		  "  spacer;"
		  ""
		  "  : column {"
		  "  "
		  "	: text   { key = \"fw\"; label = \"Find &What:\"; }"
		  "	: edit60 { key = \"fstr\"; mnemonic = \"W\"; }"
		  "	"
		  "	spacer;"
		  "	"
		  "	: text   { key = \"rw\"; label = \"&Replace With:\"; }"
		  "	: edit60 { key = \"rstr\"; mnemonic = \"R\"; }"
		  "	"
		  "	spacer;"
		  "  }"
		  ""
		  "  spacer;"
		  ""
		  "  ok_cancel;"
		  "}"
		  ""
		  ""
		  "//;¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,;//"
		  "//																			   //"
		  "//						   End of Dialog Definition							//"
		  "//																			   //"
		  "//;ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,¤º°`°º¤;//"
		 )

	   (write-line str ofile))

	(setq ofile (close ofile))

	t); File written successfully

nil); Filepath not Found

 t)); DCL file already exists

;............................................................................

.....;

 (defun logo (key)

(start_image key)
(mapcar 'vector_image
		'(22 21 1 0 0 0 0 7 0 0 0 0 1 6 6 6 6 7 43 36 27 36 30 21 21 21 22 22 22
		  22 21 21 21 28 28 28 27 27 30 29 29 30 52 43 43 43 44 44 46 46 45 45 45
		  45 52 52 52 51 51 51 51 51 52 62 65 66 68 68 68 68 67 67 75 75 75 74 74
		  73 66 58 58 59 59 59 59 52 57 57 56 56 56 56 57 58 65 65 65 65 66 95 94
		  94 92 91 91 91 90 89 89 88 87 86 85 74 74 75 75 76 77 78 79 80 81 82 83
		  84 85 86 87 88 88 89 90 91 92 93 94 95 74 73 73 72 72 71 71 71 71 71 71
		  71 72 72 72 73 84 83 82 81 80 79 79 78 77 77 76 76 76 76 76 77 77 78 79
		  79 80 81 82 83 94 94 95 83 83 82 81 80 79 78 77 76 75 74 84 85 86 87 88
		  89 89 90 91 91 91 91 92 95 94 93 92 91 90 89 89 88 87 86 85 84)

		'(20 20 23 23 23 24 24 0 0 0 0 1 1 20 1 1 1 0 2 24 7 15 0 0 0 0 1 1 23 23
		  23 24 24 24 24 24 23 23 2 1 1 0 0 0 0 0 1 1 7 23 23 23 24 24 24 24 24 23
		  23 1 1 1 0 10 16 19 21 22 23 24 24 24 24 24 24 23 23 22 4 4 5 5 6 6 7 24
		  24 24 24 23 23 22 19 16 7 7 6 5 5 22 22 22 17 17 18 18 19 20 20 20 21 21
		  21 21 22 23 23 23 24 24 24 25 25 25 25 25 25 25 25 24 24 24 23 23 22 22
		  22 22 7 8 8 9 10 11 12 13 14 15 16 17 18 19 19 20 21 21 21 21 20 20 19 19
		  18 17 16 15 14 13 12 12 11 10 9 9 8 8 8 7 7 7 7 4 4 4 4 4 4 4 5 5 6 6 7 7
		  8 8 8 9 9 9 10 11 11 11 11 7 7 7 6 6 5 5 4 4 4 4 4 4)

		'(21 6 0 0 0 0 21 0 0 0 0 1 1 6 6 6 7 7 36 46 36 30 21 21 21 22 22 22 22 21
		  21 21 28 28 28 27 27 27 29 29 30 30 43 43 43 44 44 43 46 45 45 45 45 52 52
		  52 51 51 51 51 51 52 52 65 58 68 68 68 68 67 67 75 75 75 74 74 73 65 58 58
		  59 59 59 59 51 57 57 56 56 56 56 57 66 62 65 65 65 66 66 94 94 95 91 91 91
		  90 89 89 88 87 86 85 84 74 75 75 76 77 78 79 80 81 82 83 84 85 86 87 88 88
		  89 90 91 92 93 94 95 92 73 73 72 72 71 71 71 71 71 71 71 72 72 72 73 74 83
		  82 81 80 79 79 78 77 77 76 76 76 76 76 77 77 78 79 79 80 81 82 83 84 94 95
		  94 83 82 81 80 79 78 77 76 75 74 74 85 86 87 88 89 89 90 91 91 91 91 92 95
		  94 93 92 91 90 89 89 88 87 86 85 84 83)

		'(20 20 23 23 24 24 24 0 0 0 1 1 23 1 1 1 0 0 15 7 24 2 0 0 0 1 1 23 23 23 24
		  24 24 24 24 23 23 7 1 1 0 0 0 0 0 1 1 2 23 23 23 24 24 24 24 24 23 23 1 1 1
		  0 0 16 16 21 22 23 24 24 24 24 24 24 23 23 22 7 4 5 5 6 6 7 22 24 24 24 23
		  23 22 19 19 10 7 6 5 5 4 22 22 22 17 18 18 19 20 20 20 21 21 21 21 22 23 23
		  23 24 24 24 25 25 25 25 25 25 25 25 24 24 24 23 23 22 22 22 22 17 8 8 9 10 11
		  12 13 14 15 16 17 18 19 19 20 21 21 21 21 20 20 19 19 18 17 16 15 14 13 12 12
		  11 10 9 9 8 8 8 7 7 7 7 7 4 4 4 4 4 4 5 5 6 6 7 7 8 8 8 9 9 9 10 11 11 11 11
		  7 7 7 6 6 5 5 4 4 4 4 4 4 4)

		'(178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178
		  178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178
		  178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178
		  178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178
		  178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178
		  178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178
		  178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178
		  178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178
		  178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178
		  178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178
		  178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178 178
		  178 178 178 178 178 178 178 178 178))

 (end_image))

;............................................................................

.....;

 (defun Dir_Text (key str)
(set_tile key
  (if str
	(if (< 45 (strlen str))
	  (strcat (substr str 1 42) "...") str) "")))

;............................................................................

.....;

 (defun Dir_Mode (val)
(mapcar
  (function
	(lambda (x)
	  (mode_tile x (atoi val)))) '("sub_dir" "dir" "dir_text")))

;............................................................................

.....;

 (defun SortByFirst (lst)
(if lst
  (vl-sort lst
	(function
	  (lambda (a b) (< (car a) (car b)))))))

;............................................................................

.....;

 (defun Remove_Items (items lst / i)
(setq i -1)
(vl-remove-if
  (function
	(lambda (x) (vl-position (setq i (1+ i)) items))) lst))

;............................................................................

.....;

 (defun StringRep  (lst str case whole / i nl ol old new x)

(foreach x lst
  (setq old (car x) new (cdr x))

  (setq i 0 nl (strlen new)
			ol (strlen old) old (if case (strcase old) old))

  (cond (whole

		   (if (eq old (if case (strcase str) str))
			 (setq str new)))

		(t

		   (while (and (< i (strlen str))
					   (setq i (vl-string-search old (if case (strcase str) str) i)))

			 (setq str (strcat (substr str 1 i) new (substr str (+ i 1 ol))) i (+ i nl))))))

str)

;............................................................................

.....;

 (defun Populate_ListBox (key lst)
(start_list key)

(mapcar (function add_list)
		(mapcar
		  (function
			(lambda (x)
			  (strcat (if (< 29 (strlen (car x)))
						(strcat (substr (car x) 1 26) "...")

						(car x)) "\t" (cdr x))))
		  lst))

(end_list)

lst)

;............................................................................

.....;

 (defun CalcInsPt (obj str / eLst Alig)
;; Modification of VovKa's Routine

 (setq eLst (entget (vlax-vla-object->ename obj))
	Alig (cdr (assoc 72 eLst)))

 (polar (vlax-get obj 'InsertionPoint)
	 (vla-get-Rotation obj)

	 (* (apply (function +)
			   (mapcar
				 (function
				   (lambda (e1 e2) (- (car e1) (car e2))))

				 (textbox eLst)
				 (textbox (subst (cons 1 str) (assoc 1 eLst) eLst))))

		(cond (  (or (= Alig 1) (= Alig 4)) 0.5)

			  (  (= Alig 2) 1.0)

			  (t 0.0)))))

;............................................................................

.....;

 (defun Options_Dialog (tag mode / i tmp)

(cond (  (not (new_dialog "bfind_opt" tag))

		 (Popup "Warning" 16 "Batch Find Options Dialog could not be Loaded")
		 (princ "\n** DCL could not be Loaded **"))

	  (t (set_tile "dcl_title_opt" dcopttitle)

		 (  (lambda (mode / i)
			  (setq i 1)

			  (mapcar
				(function
				  (lambda (tile)
					(set_tile tile
							  (if (= i (logand i mode)) "1" "0"))

					(setq i (lsh i 1))))

				'("case" "lock" "whol" "dtxt" "mtxt" "att" "dim" "mld" "tab")))

		   (setq tmp mode))

		 (setq i 1)

		 (mapcar
		   (function
			 (lambda (tile)
			   (action_tile tile
				 (strcat "(setq tmp ((if (eq \"1\" $value) + -) tmp " (itoa i) "))"))

			   (setq i (lsh i 1))))

		   '("case" "lock" "whol" "dtxt" "mtxt" "att" "dim" "mld" "tab"))

		 (action_tile "accept" "(setq mode tmp) (done_dialog)")

		 (action_tile "cancel" "(done_dialog)")

		 (start_dialog)))

mode)

;............................................................................

.....;

 (defun Entry_Edit (tag entry / fs rs)

(cond (  (not (new_dialog "bfind_edit" tag))

		 (Popup "Warning" 16 "Batch Find Edit Dialog could not be Loaded")
		 (princ "\n** DCL could not be Loaded **"))

	  (t (set_tile "dcl_title_edi" dcedititle)

		 (mapcar (function set) '(fs rs)
				 (mapcar (function set_tile) '("fstr" "rstr")
						 (list (car entry) (cdr entry))))

		 (mode_tile "fstr" 3)

		 (action_tile "fstr" "(setq fs $value)")

		 (action_tile "rstr" "(setq rs $value)")

		 (action_tile "accept"
		   (vl-prin1-to-string
			 (quote
			   (progn
				 (cond (  (or (not fs) (eq "" fs))

						  (popup "Information" 64 "Please Enter a String to Find"))

					   (  (setq entry (cons fs rs))

						  (done_dialog)))))))

		 (action_tile "cancel" "(done_dialog)")

		 (start_dialog)))
entry)

;............................................................................

.....;

 (defun Ptr->L (ptr)
(read (strcat "(" ptr ")")))

;............................................................................

.....;

 (defun L->Ptr (lst)
(vl-string-trim "()"
  (vl-princ-to-string lst)))

;............................................................................

.....;

 (defun Subst_at_n (item lst n)
(if lst
  (cons (if (zerop n) item (car lst))
		(Subst_at_n item (cdr lst) (1- n)))
  (car lst)))

;............................................................................

.....;


;;;¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,;;;

;;						   --=={  Main Function  }==--					;;

;;;¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,;;;


;; Option Bit-Codes

;  Match Case			=   1
;  Ignore Locked Layers  =   2
;  Whole Words Only	  =   4
;  Single-line Text	  =   8
;  Multiline Text		=  16
;  Block Attributes	  =  32
;  Dimension Text		=  64
;  Multileader Text	  = 128
;  Table Text			= 256

 (setq vl '("DIMZIN") ov (mapcar (function getvar) vl))
 (mapcar (function setvar) vl '(0))

;;  ¤º°`°º¤ø,¸¸,ø¤º°`°º¤  --=={  Setup Defaults  }==--  ¤º°`°º¤ø,¸¸,ø¤º°`°º¤

 (or (findfile cfgfname)
  (write_config cfgfname (list 'nil (getvar 'DWGPREFIX) "0" "1" (+ 1 8 16 32 64 128 256))))

 (read_config cfgfname '(*BFind_lst* *BFind_pat* *BFind_cur* *BFind_sub* *BFind_opt*))

 (or *BFind_pat*  (setq *BFind_pat* (getvar 'DWGPREFIX)))
 (or *BFind_cur*  (setq *BFind_cur* "0"))
 (or *BFind_sub*  (setq *BFind_sub* "1"))
 (or *BFind_opt*  (setq *BFind_opt* (+ 1 8 16 32 64 128 256)))

;;  ¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤


 (cond (  (not (DCL_Write dcfname))

	   (Popup "Warning" 16 "Dialog Definition File could not be Written")
	   (princ "\n** DCL File Could not be Written **"))

	(  (<= (setq dcTag (load_dialog dcfname)) 0)

	   (Popup "Warning" 16 "Dialog Definition File could not be Found")
	   (princ "\n** DCL File could not be Found **"))

	(  (not (new_dialog "bfind" dcTag))

	   (Popup "Warning" 16 "Batch Find Dialog could not be Loaded")
	   (princ "\n** DCL could not be Loaded **"))

	(t

;............................................................................

.....;

	   (Dir_Text  "dir_text"   *BFind_pat*)
	   (set_tile  "sub_dir"	*BFind_sub*)
	   (set_tile  "dcl_title"	  dcTitle)

	   (Logo "logo")

	   (Dir_Mode (set_tile "cur_dwg" *BFind_cur*))

	   (Populate_Listbox "rep_list"  *BFind_lst*)

	   (action_tile "cur_dwg"  "(Dir_Mode (setq *BFind_cur* $value))")

	   (action_tile "fstr"	 "(setq fstr $value)")

	   (action_tile "rstr"	 "(setq rstr $value)")

	   (action_tile "option"   "(setq *BFind_opt* (Options_Dialog dcTag *BFind_opt*))")   

	   (action_tile "add"
		 (vl-prin1-to-string
		   (quote
			 (progn
			   (cond (  (or (not fstr) (eq "" fstr))

						(popup "Information" 64 "Please Enter a String to Find"))

					 (  (or rstr (setq rstr ""))
						(mapcar (function set_tile) '("fstr" "rstr") '("" ""))

						(setq *BFind_lst*
						  (Populate_Listbox "rep_list"
							(SortByFirst
							  (cons
								(cons fstr rstr) *BFind_lst*))))

						(setq fstr nil rstr nil)))))))		 

	   (action_tile "rem"
		 (vl-prin1-to-string
		   (quote
			 (progn
			   (if *BFind_lst*
				 (if (and ptr (listp (setq items (Ptr->L ptr))))

				   (setq *BFind_lst*
					 (Populate_Listbox "rep_list" 
					   (SortByFirst
						 (Remove_Items items *BFind_lst*))) ptr nil)

				   (popup "Information" 48 "Please Select an Entry from the List to Remove"))

				 (popup "Information" 64 "No Entries Found to Remove"))))))

	   (action_tile "rep_list"
		 (vl-prin1-to-string
		   (quote
			 (progn
			   (setq st (getvar "DATE") ptr $value)

			   (if (and (eq dclkptr $value)
						(< (abs (read (rtos (- en st) 2 10))) DoubleClickTime))
				 (progn

				   (setq n (nth (atoi ptr) *BFind_lst*))

				   (Populate_Listbox "rep_list"
					 (setq *BFind_lst* (SortByFirst
										 (Subst_at_n
										   (Entry_Edit dcTag n) *BFind_lst* (atoi ptr)))))

				   (setq dclkptr nil))

				 (setq en (getvar "DATE") dclkptr $value))))))

	   (action_tile "clr"
		 (vl-prin1-to-string
		   (quote
			 (progn
			   (Populate_Listbox "rep_list" (setq *BFind_lst* nil))))))		 

	   (action_tile "dir"
		 (vl-prin1-to-string
		   (quote
			 (progn
			   (if (setq tmp (DirDialog "Select Directory of Drawings to Process..." nil 0))

				 (Dir_Text "dir_text" (setq *BFind_pat* tmp)))))))		 

	   (action_tile "sub_dir"	"(setq *BFind_sub* $value)")

	   (action_tile "accept"
		 (vl-prin1-to-string
		   (quote
			 (progn
			   (cond (  (not *BFind_lst*)

						(popup "Information" 48 "Please Add an Entry to the List"))

					 (t (done_dialog 1)))))))

	   (action_tile "cancel"	 "(done_dialog 0)")

	   (setq dcFlag (start_dialog))

	   (setq dcTag  (unload_dialog dcTag))

;............................................................................

.....;

	   (if (= 1 dcFlag)
		 (progn
		   (setq uFlag	(not (vla-StartUndomark *adoc))

				 CaseFlag (zerop (logand 1 *BFind_opt*))

				 LockFlag (= 2 (logand 2 *BFind_opt*))

				 Whole	(= 4 (logand 4 *BFind_opt*)))

		   (vlax-for doc (vla-get-Documents *acad)
			 (setq DocLst
			   (cons
				 (cons
				   (strcase
					 (vla-get-fullname doc)) doc) DocLst)))

		   (setq dbx (ObjectDBXDocument))

		   (setq dwLst (cond (  (eq "1" *BFind_cur*)

								(list
								  (cond (  (eq "" (vla-get-FullName *adoc))

										   (strcat (vla-get-Path *adoc) (vla-get-Name *adoc)))

										(t (vla-get-FullName *adoc)))))

							 (t (apply (function append)

									   (vl-remove 'nil
										 (mapcar
										   (function
											 (lambda (Filepath)

											   (mapcar
												 (function
												   (lambda (Filename)
													 (strcat Filepath "\\" Filename)))

												 (vl-directory-files Filepath "*.dwg" 1))))

										   (append (list *BFind_pat*)
												   (apply (function append)
														  (if (= "1" *BFind_sub*)
															(Get_Subs *BFind_pat*))))))))))

		   (if Express (setq ProgBar (acet-ui-progress "Performing Replacement..." (length dwLst))))

		   (foreach dwg dwLst

			 (if Express (acet-ui-progress -1))

			 (cond (  (setq flag (eq "1" *BFind_cur*))

					  (setq oDoc *adoc))

				   (  (setq flag (and (setq oDoc (cdr (assoc (strcase dwg) DocLst))))))

				   (t (setq flag (not (vl-catch-all-error-p
										(vl-catch-all-apply
										  (function vla-open) (list dbx dwg)))))
					  (setq oDoc dbx)))

			 (if flag
			   (progn
				 (setq Locked nil LockedLayers nil)					 

				 (vlax-for lay (vla-get-Layers oDoc)
				   (if (eq :vlax-true (vla-get-Lock lay))
					 (progn
					   (vla-put-lock lay :vlax-false)
					   (setq Locked	   (cons lay Locked)
							 LockedLayers (cons (strcase (vla-get-name lay)) LockedLayers)))))					 

				 (vlax-for lay (vla-get-Layouts oDoc)

				   (vlax-for obj (vla-get-Block lay)

					 (setq oName (vla-get-Objectname obj))

					 (cond (  (not (and LockFlag
										(vl-position
										  (strcase (vla-get-layer obj)) LockedLayers)))

							  (cond (  (and (eq "AcDbBlockReference" oName)
											(eq :vlax-true (vla-get-HasAttributes obj))
											(= 32 (logand 32 *BFind_opt*)))

									   (foreach att (vlax-invoke obj 'GetAttributes)

										 (if (vl-some
											   (function
												 (lambda (str)
												   (vl-string-search str
													 (if CaseFlag (strcase (vla-get-TextString att))
													   (vla-get-TextString att)))))

											   (mapcar (function car) *BFind_lst*))
										   (progn

											 (vla-put-InsertionPoint att
											   (vlax-3D-point
												 (CalcInsPt att (setq nStr (StringRep *BFind_lst*
																			 (vla-get-TextString att) CaseFlag Whole)))))

											 (vla-put-TextString att nStr)))))

									(  (and (wcmatch (strcase oName) "*DIMENSION*")
											(= 64 (logand 64 *BFind_opt*)))

									   (if (not (eq "" (vla-get-TextOverride obj)))
										 (vla-put-TextOverride obj (StringRep *BFind_lst*
																	 (vla-get-TextOverride obj) CaseFlag Whole))))

									(  (or (and (eq "AcDbMText" oName)
												(= 16 (logand 16 *BFind_opt*)))

										   (and (eq "AcDbText" oName)
												(= 8 (logand 8 *BFind_opt*)))

										   (and (eq "AcDbMLeader" oName)
												(= 128 (logand 128 *BFind_opt*))))

									   (vla-put-TextString obj (StringRep *BFind_lst*
																 (vla-get-TextString obj) CaseFlag Whole)))

									(  (and (eq "AcDbTable" oName)
											(= 256 (logand 256 *BFind_opt*)))

									   (setq row (vla-get-Rows obj))
									   (while (not (minusp (setq row (1- row))))

										 (setq col (vla-get-Columns obj))
										 (while (not (minusp (setq col (1- col))))

										   (vla-SetText obj row col (StringRep *BFind_lst*
																	  (vla-GetText obj row col) CaseFlag Whole))))))))))

				 (mapcar
				   (function
					 (lambda (x) (vla-put-lock x :vlax-true))) Locked)

				 (if (vlax-property-available-p oDoc 'FullName)

				   (if (not (eq "" (vla-get-FullName oDoc)))
					 (vla-saveas oDoc dwg))

				   (vla-saveas oDoc dwg)))

			   (princ (strcat "\n** Error Opening File: " (vl-filename-base dwg)  ".dwg **"))))

		   (if Express (setq ProgBar (acet-ui-progress)))

		 ;............................................................................

.....;

		   (princ (strcat "\n<< " (itoa (length dwLst)) " Drawings Processed >>"))

		   (write_config cfgfname (list *BFind_lst* *BFind_pat* *BFind_cur* *BFind_sub* *BFind_opt*))

		   (setq uFlag (vla-EndUndomark *adoc))

		   ); Progn

		 (princ "\n*Cancel*"))

	 (ObjRelease (list dbx oDoc)) (gc) (gc)))

 (mapcar (function setvar) vl ov)
 (princ))


;...............................................................................

..;


(princ "\nø¤º°`°º¤ø  BFind.lsp ~ Copyright © by Lee McDonnell  ø¤º°`°º¤ø")
(princ "\n   ~¤~		  ...Type \"BFind\" to Invoke...			~¤~   ")
(princ)

;;;¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,;;;
;;																			 ;;
;;							 End of Program Code							 ;;
;;																			 ;;
;;;ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,¤º°`°º¤;;;

Thảo luận gốc

Lisp được chia sẻ trong chủ đề Find và Replace trong nhiều bản vẽ. Xem đúng bài viết #100317 để đọc hướng dẫn và trao đổi thêm.

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