Author Topic: search directorys for text in dwg  (Read 6193 times)

0 Members and 1 Guest are viewing this topic.

tader

  • Guest
search directorys for text in dwg
« on: March 09, 2009, 12:04:19 PM »
Here's a lisp I found on autodesk's website a while back.  It will search a directory of dwgs for text within a drawing.  There are some changes/additions that would be nice if anyone can fix.

It doesn't search subdirectories.

There's no place to change partial word find, right now it seems to only find whole words.

I added the acet-u-pick directory a while ago, I think that was the only change I made, but it would be nice to have the option to default to the previously selected directory.

I've tried the find in vlide by changing the *.lsp to dwg. doesn't work as nice.  Attached is the lisp.


;;;=========================================================================================================================================================
(defun Fndtxtstr_Error (msg)
  (if
    (not
      (member msg
        '("Function cancelled" "console break" "quit / exit abort")
      )
    )
    (princ (strcat "\Error: " msg))
  )
  (Close_Dbx_Document DBX_DOC)
  (setq *error* old_error old_error nil)
)
;;;=========================================================================================================================================================
(defun Open_Dbx_Document (DWG_NAME / DBX_DOCUMENT DBX_OPEN) ;Code provided by James Buzzbee

  (if (/= DWG_NAME (vla-get-fullname (vla-get-activedocument (vlax-get-acad-Object))))
    (progn
      (cond
        ( (= (substr (getvar "ACADVER") 1 5) "15.06")
          (setq DBX_DOCUMENT (vla-GetInterfaceObject (vlax-get-acad-Object) "ObjectDBX.AxDbDocument"))
          (setq DBX_OPEN     (vl-catch-all-apply 'vla-open (list DBX_DOCUMENT DWG_NAME)))
        )
        ( (= (substr (getvar "ACADVER") 1 4) "16.0")
          (setq DBX_DOCUMENT (vla-GetInterfaceObject (vlax-get-acad-Object) "ObjectDBX.AxDbDocument.16"))
          (setq DBX_OPEN     (vl-catch-all-apply 'vla-open (list DBX_DOCUMENT DWG_NAME)))
        )
        ( (= (substr (getvar "ACADVER") 1 4) "16.1")
          (setq DBX_DOCUMENT (vla-GetInterfaceObject (vlax-get-acad-Object) "ObjectDBX.AxDbDocument.16"))
          (setq DBX_OPEN     (vl-catch-all-apply 'vla-open (list DBX_DOCUMENT DWG_NAME)))
        )
        ( (= (substr (getvar "ACADVER") 1 4) "16.2")
          (setq DBX_DOCUMENT (vla-GetInterfaceObject (vlax-get-acad-Object) "ObjectDBX.AxDbDocument.16"))
          (setq DBX_OPEN     (vl-catch-all-apply 'vla-open (list DBX_DOCUMENT DWG_NAME)))
        )
        ( (= (substr (getvar "ACADVER") 1 4) "17.0")
          (setq DBX_DOCUMENT (vla-GetInterfaceObject (vlax-get-acad-Object) "ObjectDBX.AxDbDocument.17"))
          (setq DBX_OPEN     (vl-catch-all-apply 'vla-open (list DBX_DOCUMENT DWG_NAME)))
        )
      )
      (if (vl-catch-all-error-p DBX_OPEN)
        (setq DBX_DOCUMENT nil)
      )
    )
    (alert "Can not open in DBX mode the drawing from which the call is made.")
  )
  DBX_DOCUMENT
)
;;;=========================================================================================================================================================
(defun Close_Dbx_Document  (DBX_DOCUMENT) ;Code provided by James Buzzbee
  (if (= (type DBX_DOCUMENT) 'VLA-OBJECT)(vlax-release-object DBX_DOCUMENT))
)
;;;=========================================================================================================================================================
(defun Find_Text_String (DWG TEXT_STRING CASE_SENSITIVE)

  (if DWG
    (progn
      (setq DBX_DOC (Open_Dbx_Document DWG))
      (vlax-for LAYOUT (vla-get-layouts DBX_DOC)
        (vlax-for OBJECT (vla-get-Block LAYOUT)
          (if
            (and
              (= (vla-get-objectname OBJECT) "AcDbText")
              (if
      (and CASE_SENSITIVE (not flag))
           
                (wcmatch (vla-get-textstring OBJECT) TEXT_STRING)
                (wcmatch (strcase (vla-get-textstring OBJECT)) (strcase TEXT_STRING))
)
            )
            (progn
              (print "===============================")           
              (print (strcat "Drawing " DWG ":"))
              (print (strcat "Found text string <" TEXT_STRING "> in " (vla-get-name LAYOUT)))
              (print "===============================")
            )
          )
        )
      )
      (Close_Dbx_Document DBX_DOC)
    )
    (alert (strcat "Could not find the drawing " DWG " ."))   
  )
  (princ)
)
;;;=========================================================================================================================================================
(defun Find_Files_And_Run_In_Batch_Mode ($DIRECTORY $TEXT_STRING $CASE_SENSITIVE)

  (setq old_error *error*)
  (setq *error* Fndtxtstr_Error)

  (foreach DRAWING_FILE (acad_strlsort (vl-directory-files $DIRECTORY "*.dwg" 1))
    (if
      (/=
        (strcat $DIRECTORY "\\" DRAWING_FILE)
        (strcat (getvar "dwgprefix")(getvar "dwgname"))
      )
      (progn
        (prompt (strcat "\nProcessing file " (strcase DRAWING_FILE) " ..."))
        (princ)
        (Find_Text_String
          (strcat $DIRECTORY "\\" DRAWING_FILE)
          $TEXT_STRING
          $CASE_SENSITIVE
        )
      )
    )
  )
  (setq *error* old_error old_error nil) 
)

;;;=========================================================================================================================================================
(defun c:FTS1 ()

  (Find_Files_And_Run_In_Batch_Mode
    (ACET-UI-PICKDIR "Select Directory to Search" "c:\\"); REPLACE HERE THE FOLDER TO CHECK FOR THE DRAWINGS
    (getstring T "Enter text string to search for: ")                 ; TEXT STRING TO CHECK FOR
    T                                                               ; CASE_SENSITIVE FLAG: IF IS T => MATCH CASE, IF IS NIL => DON'T MATCH THE CASE
  )
  (princ)
)
;;;=========================================================================================================================================================
(defun c:FTS2 () ;doesn't match case
(initget "Y N")
  (setq flag (getstring "Find whole word [Y/N]: <N>"))
  (if (= flag "")
    (setq flag nil))
  (Find_Files_And_Run_In_Batch_Mode
    (ACET-UI-PICKDIR "Select Directory to Search" "c:\\"); REPLACE HERE THE FOLDER TO CHECK FOR THE DRAWINGS

    (getstring T "Enter text string to search for: ")                 ; TEXT STRING TO CHECK FOR
    nil)                                                           ; CASE_SENSITIVE FLAG: IF IS T => MATCH CASE, IF IS NIL => DON'T MATCH THE CASE
 
 
  (princ)
)
;;;=========================================================================================================================================================
(prompt "\n*** FIND TEXT STRING VIA ODBX.LSP loaded. ***")
(prompt "\nType FNDTXTSTR1 to run in Case sensitive mode and FNDTXTSTR2 in Case insensitive mode.")
(princ)
;;;=========================================================================================================================================================

JohnK

  • Administrator
  • Seagull
  • Posts: 10661
Re: search directorys for text in dwg
« Reply #1 on: March 09, 2009, 01:04:46 PM »
Welcome to theSwamp.


> Partial word find

WCMATCH allows for wildcards.

> acet-u-pick directory

ElpanovEvgeniy (A member here) has posted several very good directory search functions you can use instead using those badly written functions ``ACET-...'' functions.

> Previously selected directory

Simple, use an .ini file to store that information and read it upon execution.

Here is a simple .ini file reader for you to use.

Code: [Select]
(defun iniread (str file)
  ;; iniread
  ;;
  ;; Returns all valid entries in a file. (see below)
  ;;
  ;; file to be read must be structured in a way that
  ;; all lines begining with a `` ; '' is considered
  ;; to be a comment.
  ;;
  ;; EX:
  ;; Example file: Example.ini
  ;;
  ;; ; this is an example ini file.
  ;; var1=val1
  ;; var2=val2
  ;; ; var3=val3
  ;; var4=val4
  ;;
  ;;      ( (lambda ( / f my-list )
  ;;          (setq f (open "c:\\Example.ini" "R"))
  ;;          (setq my-list
  ;;              (apply
  ;;                'append
  ;;                   (iniread (read-line f) f)))
  ;;          (close f)
  ;;          my-list ) )
  ;;     
  ;; => ("var1=val1" "var2=val2" "var4=val4")
  ;;
  ;; By: John (7) K
  ;;
  ( (lambda (s f)
      (if s
        (cons
          (if (not (wcmatch s "*;*"))
            (list s))
          (iniread (read-line f) f))))
   str
   file ) )

TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: search directorys for text in dwg
« Reply #2 on: March 09, 2009, 01:55:22 PM »
You may want to take a look at this & another by Tim Willey in this thread.
http://www.theswamp.org/index.php?topic=14703.msg177823#msg177823
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

cadmoogle

  • Guest
Re: search directorys for text in dwg
« Reply #3 on: August 12, 2009, 08:39:08 AM »
When I try to run the code posted above I get this following message

Code: [Select]
Command: fts1
Enter text string to search for: REDRAW
Processing file 100 SCALE.DWG ...Error: bad argument type: VLA-OBJECT nil
Command:

I've tried to add the vl-load, but still received the same message.

Any suggestions?

Thank you,
Daniel

cadmoogle

  • Guest
Re: search directorys for text in dwg
« Reply #4 on: August 12, 2009, 08:41:21 AM »
I'm not looking to 'Find & Replace', but to simply find and note which DWGs contain that word and store it in a .TXT document. I've searched around, but had no luck.

Thank you,
Daniel

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: search directorys for text in dwg
« Reply #5 on: August 12, 2009, 01:30:06 PM »
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

cadmoogle

  • Guest
Re: search directorys for text in dwg
« Reply #6 on: August 12, 2009, 01:51:08 PM »
I tried it and it says this.

Code: [Select]
Command: (LOAD "C:/Documents and Settings/dgj520/Desktop/FTS.lsp") ; error:
misplaced dot on input

I loaded the VLISP editor and is says this is the source of the error. From what I've read on this error it's caused by using .2 instead of 0.2... but this section doesn't contain that type of information.

Code: [Select]
(strcat "Found text string <"> in " (vla-get-name LAYOUT)))
(print "===============================")
)
)
)
)
(Close_Dbx_Document DBX_DOC)
)
(alert (strcat "Could not find the drawing " DWG " ."

any suggestions?

Thank you,
Daniel




cadmoogle

  • Guest
Re: search directorys for text in dwg
« Reply #7 on: August 12, 2009, 01:56:44 PM »
I got it, only one " was shown.
... testing now.

cadmoogle

  • Guest
Re: search directorys for text in dwg
« Reply #8 on: August 12, 2009, 02:43:09 PM »
Still throwing the error  :-o

cadmoogle

  • Guest
Re: search directorys for text in dwg
« Reply #9 on: August 12, 2009, 02:49:20 PM »
I added the version number for 2010(18) and that stopped the error. Now I need to see how it works.  :ugly:

Code: [Select]
;;;=========================================================================================================================================================
(defun Fndtxtstr_Error (msg)
(if
(not
(member msg
'("Function canceled" "console break" "quit / exit abort")
)
)
(princ (strcat "\Error: " msg))
)
(Close_Dbx_Document DBX_DOC)
(setq *error* old_error old_error nil)
)
;;;=========================================================================================================================================================
(defun Open_Dbx_Document (DWG_NAME / DBX_DOCUMENT DBX_OPEN) ;Code provided by James Buzzbee

(if (/= DWG_NAME (vla-get-fullname (vla-get-activedocument (vlax-get-acad-Object))))
(progn
(cond
( (= (substr (getvar "ACADVER") 1 5) "15.06")
(setq DBX_DOCUMENT (vla-GetInterfaceObject (vlax-get-acad-Object) "ObjectDBX.AxDbDocument"))
(setq DBX_OPEN (vl-catch-all-apply 'vla-open (list DBX_DOCUMENT DWG_NAME)))
)
( (= (substr (getvar "ACADVER") 1 4) "16.0")
(setq DBX_DOCUMENT (vla-GetInterfaceObject (vlax-get-acad-Object) "ObjectDBX.AxDbDocument.16"))
(setq DBX_OPEN (vl-catch-all-apply 'vla-open (list DBX_DOCUMENT DWG_NAME)))
)
( (= (substr (getvar "ACADVER") 1 4) "16.1")
(setq DBX_DOCUMENT (vla-GetInterfaceObject (vlax-get-acad-Object) "ObjectDBX.AxDbDocument.16"))
(setq DBX_OPEN (vl-catch-all-apply 'vla-open (list DBX_DOCUMENT DWG_NAME)))
)
( (= (substr (getvar "ACADVER") 1 4) "16.2")
(setq DBX_DOCUMENT (vla-GetInterfaceObject (vlax-get-acad-Object) "ObjectDBX.AxDbDocument.16"))
(setq DBX_OPEN (vl-catch-all-apply 'vla-open (list DBX_DOCUMENT DWG_NAME)))
)
( (= (substr (getvar "ACADVER") 1 4) "17.0")
(setq DBX_DOCUMENT (vla-GetInterfaceObject (vlax-get-acad-Object) "ObjectDBX.AxDbDocument.17"))
(setq DBX_OPEN (vl-catch-all-apply 'vla-open (list DBX_DOCUMENT DWG_NAME)))
)
( (= (substr (getvar "ACADVER") 1 4) "18.0")
(setq DBX_DOCUMENT (vla-GetInterfaceObject (vlax-get-acad-Object) "ObjectDBX.AxDbDocument.18"))
(setq DBX_OPEN (vl-catch-all-apply 'vla-open (list DBX_DOCUMENT DWG_NAME)))
)
)
(if (vl-catch-all-error-p DBX_OPEN)
(setq DBX_DOCUMENT nil)
)
)
(alert "Can not open in DBX mode the drawing from which the call is made.")
)
DBX_DOCUMENT
)
;;;=========================================================================================================================================================
(defun Close_Dbx_Document (DBX_DOCUMENT) ;Code provided by James Buzzbee
(if (= (type DBX_DOCUMENT) 'VLA-OBJECT)(vlax-release-object DBX_DOCUMENT))
)
;;;=========================================================================================================================================================
(defun Find_Text_String (DWG TEXT_STRING CASE_SENSITIVE)

(if DWG
(progn
(setq DBX_DOC (Open_Dbx_Document DWG))
(vlax-for LAYOUT (vla-get-layouts DBX_DOC)
(vlax-for OBJECT (vla-get-Block LAYOUT)
(if
(and
(= (vla-get-objectname OBJECT) "AcDbText")
(if CASE_SENSITIVE
(wcmatch (vla-get-textstring OBJECT) TEXT_STRING)
(wcmatch (strcase (vla-get-textstring OBJECT)) (strcase TEXT_STRING))
)
)
(progn
(print "===============================")
(print (strcat "Drawing " DWG ":"))
(print (strcat "Found text string <""> in " (vla-get-name LAYOUT)))
(print "===============================")
)
)
)
)
(Close_Dbx_Document DBX_DOC)
)
(alert (strcat "Could not find the drawing " DWG " ."))
)
(princ)
)
;;;=========================================================================================================================================================
(defun Find_Files_And_Run_In_Batch_Mode ($DIRECTORY $TEXT_STRING $CASE_SENSITIVE)

(setq old_error *error*)
(setq *error* Fndtxtstr_Error)

(foreach DRAWING_FILE (acad_strlsort (vl-directory-files $DIRECTORY "*.dwg" 1))
(if
(/=
(strcat $DIRECTORY "\\" DRAWING_FILE)
(strcat (getvar "dwgprefix")(getvar "dwgname"))
)
(progn
(prompt (strcat "\nProcessing file " (strcase DRAWING_FILE) " ..."))
(princ)
(Find_Text_String
(strcat $DIRECTORY "\\" DRAWING_FILE)
$TEXT_STRING
$CASE_SENSITIVE
)
)
)
)
(setq *error* old_error old_error nil)
)

;;;=========================================================================================================================================================
(defun c:FTS1 ()
(vl-load-com)

(Find_Files_And_Run_In_Batch_Mode
(ACET-UI-PICKDIR "Select Directory to Search" "X:\Engineering\General\Asbuilts\a-redraws\New Folder"); REPLACE HERE THE FOLDER TO CHECK FOR THE DRAWINGS
(getstring T "Enter text string to search for: ") ; TEXT STRING TO CHECK FOR
T ; CASE_SENSITIVE FLAG: IF IS T => MATCH CASE, IF IS NIL => DON'T MATCH THE CASE
)
(princ)
)
;;;=========================================================================================================================================================
(defun c:FTS2 ()
(vl-load-com)

(Find_Files_And_Run_In_Batch_Mode
(ACET-UI-PICKDIR "Select Directory to Search" "X:\Engineering\General\Asbuilts\a-redraws\New Folder"); REPLACE HERE THE FOLDER TO CHECK FOR THE DRAWINGS
(getstring T "Enter text string to search for: ") ; TEXT STRING TO CHECK FOR
nil ; CASE_SENSITIVE FLAG: IF IS T => MATCH CASE, IF IS NIL => DON'T MATCH THE CASE
)
(princ)
)
;;;=========================================================================================================================================================
(prompt "\n*** FIND TEXT STRING VIA ODBX.LSP loaded. ***")
(prompt "\nType FNDTXTSTR1 to run in Case sensitive mode and FNDTXTSTR2 in Case insensitive mode.")
(princ)
;;;=========================================================================================================================================================
« Last Edit: August 12, 2009, 02:57:22 PM by cadmoogle »

cadmoogle

  • Guest
Re: search directorys for text in dwg
« Reply #10 on: August 12, 2009, 03:00:09 PM »
In the end it did not do anything....

I ran it and at least one of these contains the word "REDRAW", and it just says "Processing", and does not mention finding any text.

Quote
Type FNDTXTSTR1 to run in Case sensitive mode and FNDTXTSTR2 in Case
insensitive mode.

Command: FTS1
Enter text string to search for: REDRAW

Processing file 100 SCALE.DWG ...
Processing file DESK.DWG ...
Processing file DRAWING2.DWG ...
Processing file EH1-12-1.DWG ...
Processing file EH1-20-1.DWG ...
Processing file EH1TREST.TIFF.DWG ...
Processing file FIDDLER RIDGE.DWG ...
Processing file FLEMGROVE.DWG ...
Processing file GOLF CLUB.DWG ...
Processing file H8-1.DWG ...
Processing file H8-2REV.DWG ...
Processing file K12-2.DWG ...
Processing file LOGO.DWG ...
Processing file OPCC1.DWG ...
Processing file PINE.DWG ...
Processing file PLATS SOUTHERN LINKS 2.DWG ...
Processing file PLPRRD01.DWG ...Loading AEC Base...
Loading AECC Base...
Loading AEC Base Extended...
Loading AEC Base UI...

Processing file SEWSTAND.DWG ...
Processing file SHORTCUT TO AS-BUILTS.DWG ...Loading AecCivilBase...

Processing file TEST.DWG ...
Processing file TEST1.DWG ...
Processing file WEST SHORE.DWG ...
Command:

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: search directorys for text in dwg
« Reply #11 on: August 12, 2009, 03:09:01 PM »
What is the word REDRAW in? Text or Mtext or Other, buried in a block or in an attribute or field?
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

cadmoogle

  • Guest
Re: search directorys for text in dwg
« Reply #12 on: August 12, 2009, 03:10:09 PM »
It will either be Text or Mtext

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: search directorys for text in dwg
« Reply #13 on: August 12, 2009, 05:01:40 PM »
Here is a rewrite of the routine.
Code: [Select]
;;  Modified by CAB 08.12.09

;;;===============================================================================================================
(defun Fndtxtstr_Error (msg)
  (if
    (not
      (member msg
        '("Function cancelled" "console break" "quit / exit abort")
      )
    )
    (princ (strcat "\Error: " msg))
  )
  (Close_Dbx_Document DBX_DOC)
  (setq *error* old_error old_error nil)
)
;;;===============================================================================================================
(defun Open_Dbx_Document (DWG_NAME / DBX_DOCUMENT DBX_OPEN)
  (if (= DWG_NAME (vla-get-fullname (vla-get-activedocument (vlax-get-acad-Object))))
    (princ "\nCan not open in DBX mode the drawing from which the call is made.")
      (if (and (setq DBX_DOCUMENT (GetDBXInterface))
               (vl-catch-all-error-p
                 (setq DBX_OPEN (vl-catch-all-apply
                                     '(lambda()(vlax-invoke-method DBX_DOCUMENT 'open DWG_NAME))))
                ))
        (setq DBX_DOCUMENT nil)
    )
  )
  DBX_DOCUMENT
)
;;;=============================================================================================================
;;  CAB 07/15/2008, update 8.11.09
(defun GetDBXInterface (/ Vers)
  (cond 
   ((< (setq Vers (substr (getvar "acadver") 1 2)) "16")
    (vla-GetInterfaceObject (vlax-get-acad-object) "ObjectDBX.AxDbDocument"))
   ((<  (substr (getvar "acadver") 1 4) "17.2")
    (vla-GetInterfaceObject (vlax-get-acad-object) "ObjectDBX.AxDbDocument.16"))
   (t (vla-GetInterfaceObject (vlax-get-acad-object)
         (strcat "ObjectDBX.AxDbDocument."(substr (getvar "acadver") 1 2))))
  )
)

;;;=============================================================================================================
(defun Close_Dbx_Document  (DBX_DOCUMENT) ;Code provided by James Buzzbee
  (if (= (type DBX_DOCUMENT) 'VLA-OBJECT)(vlax-release-object DBX_DOCUMENT))
)
;;;=============================================================================================================
(defun Find_Text_String (DWG TEXT_STRING CASE_SENSITIVE)

  (if (setq DBX_DOC (Open_Dbx_Document DWG))
    (progn
      (vlax-for LAYOUT (vla-get-layouts DBX_DOC)
        (vlax-for OBJECT (vla-get-Block LAYOUT)
          (if
            (and
              (vl-position (vla-get-objectname OBJECT) '("AcDbText" "AcDbMText"))
              (if CASE_SENSITIVE
                (wcmatch (vla-get-textstring OBJECT) (strcat "*" TEXT_STRING "*"))
                (wcmatch (strcase (vla-get-textstring OBJECT))
                         (strcat "*" (strcase TEXT_STRING) "*")
                )
              )
            )
             (progn
               (print "===============================")
               (print (strcat "Drawing " DWG ":"))
               (print
                 (strcat "Found text string <" TEXT_STRING "> in " (vla-get-name LAYOUT))
               )
               (print "===============================")
             )
          )
        )
      )
      (Close_Dbx_Document DBX_DOC)
    )
    (princ
      (strcat "\n*  Could not open drawing " (vl-filename-base DWG) ".DWG")
    )
  )
  (princ)
)
;;;===========================================================================================================
(defun Find_Files_And_Run_In_Batch_Mode ($DIRECTORY $TEXT_STRING $CASE_SENSITIVE)

  (setq old_error *error*)
  (setq *error* Fndtxtstr_Error)
  (textscr)
  (princ "---------------------------------")
  (princ "\n  -=<  Find Text in Progress  >=-")
  (princ "\n---------------------------------")
  (foreach DRAWING_FILE (acad_strlsort (vl-directory-files $DIRECTORY "*.dwg" 1))
    (if (/= (strcat $DIRECTORY "\\" DRAWING_FILE)
            (strcat (getvar "dwgprefix") (getvar "dwgname"))
        )
      (progn
        (prompt (strcat "\n|-> Processing file " (strcase DRAWING_FILE) " ..."))
        (princ)
        (Find_Text_String
          (strcat $DIRECTORY "\\" DRAWING_FILE)
          $TEXT_STRING
          $CASE_SENSITIVE
        )
      )
    )
  )
  (princ "\n-------------------------------")
  (princ "\n  -=<  Find Text Completed  >=-")
  (princ "\n-------------------------------")
  (setq *error* old_error
        old_error nil
  )
)

<edit: Replaced the GetDBXInterface function>
« Last Edit: August 12, 2009, 06:25:26 PM by CAB »
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

cadmoogle

  • Guest
Re: search directorys for text in dwg
« Reply #14 on: August 13, 2009, 07:57:05 AM »
CAB,

Thank you for the code, I modified the existing version with your portion, and it appears to process the same drawing twice.

Quote

Command:
Command: (LOAD "C:/Documents and Settings/dgj520/Desktop/lisp2.lsp")
*** FIND TEXT STRING VIA ODBX.LSP loaded. ***
Type FNDTXTSTR1 to run in Case sensitive mode and FNDTXTSTR2 in Case
insensitive mode.
Command: fts1
Enter text string to search for: REDRAW
---------------------------------
  -=<  Find Text in Progress  >=-
---------------------------------
|-> Processing file B10-1.DWG ...Loading AEC Base...
Loading AECC Base...
Loading AEC Base Extended...
Loading AEC Base UI...
Loading AECC Land...
Loading AECC Subentity Selection...
Loading AEC Project Base...
Loading AEC Schedule Data...
Loading AEC Architectural Base...
Loading AEC Structural Base...
Loading AEC Area Base...
Loading AEC Dimensions Base...
Loading AecCivilBase...
Loading AEC Schedule...

"==============================="
"Drawing C:\\Temp\\Test\\B10-1.dwg:"
"Found text string <REDRAW> in Model"
"==============================="
"==============================="
"Drawing C:\\Temp\\Test\\B10-1.dwg:"
"Found text string <REDRAW> in Model"
"==============================="
-------------------------------
  -=<  Find Text Completed  >=-
-------------------------------

Command:

Also is it normal for it to load the AEC base? Additionally if I process 1,000 drawings will the AutoCAD list window hold all the text from those possible finds or would it be best to export to a .TXT?

Here is the code that I used.

Code: [Select]
;;  Modified by CAB 08.12.09

;;;===============================================================================================================
(defun Fndtxtstr_Error (msg)
  (if
    (not
      (member msg
        '("Function cancelled" "console break" "quit / exit abort")
      )
    )
    (princ (strcat "\Error: " msg))
  )
  (Close_Dbx_Document DBX_DOC)
  (setq *error* old_error old_error nil)
)
;;;===============================================================================================================
(defun Open_Dbx_Document (DWG_NAME / DBX_DOCUMENT DBX_OPEN)
  (if (= DWG_NAME (vla-get-fullname (vla-get-activedocument (vlax-get-acad-Object))))
    (princ "\nCan not open in DBX mode the drawing from which the call is made.")
      (if (and (setq DBX_DOCUMENT (GetDBXInterface))
               (vl-catch-all-error-p
                 (setq DBX_OPEN (vl-catch-all-apply
                                     '(lambda()(vlax-invoke-method DBX_DOCUMENT 'open DWG_NAME))))
                ))
        (setq DBX_DOCUMENT nil)
    )
  )
  DBX_DOCUMENT
)
;;;=============================================================================================================
;;  CAB 07/15/2008, update 8.11.09
(defun GetDBXInterface (/ Vers)
  (cond 
   ((< (setq Vers (substr (getvar "acadver") 1 2)) "16")
    (vla-GetInterfaceObject (vlax-get-acad-object) "ObjectDBX.AxDbDocument"))
   ((<  (substr (getvar "acadver") 1 4) "17.2")
    (vla-GetInterfaceObject (vlax-get-acad-object) "ObjectDBX.AxDbDocument.16"))
   (t (vla-GetInterfaceObject (vlax-get-acad-object)
         (strcat "ObjectDBX.AxDbDocument."(substr (getvar "acadver") 1 2))))
  )
)

;;;=============================================================================================================
(defun Close_Dbx_Document  (DBX_DOCUMENT) ;Code provided by James Buzzbee
  (if (= (type DBX_DOCUMENT) 'VLA-OBJECT)(vlax-release-object DBX_DOCUMENT))
)
;;;=============================================================================================================
(defun Find_Text_String (DWG TEXT_STRING CASE_SENSITIVE)

  (if (setq DBX_DOC (Open_Dbx_Document DWG))
    (progn
      (vlax-for LAYOUT (vla-get-layouts DBX_DOC)
        (vlax-for OBJECT (vla-get-Block LAYOUT)
          (if
            (and
              (vl-position (vla-get-objectname OBJECT) '("AcDbText" "AcDbMText"))
              (if CASE_SENSITIVE
                (wcmatch (vla-get-textstring OBJECT) (strcat "*" TEXT_STRING "*"))
                (wcmatch (strcase (vla-get-textstring OBJECT))
                         (strcat "*" (strcase TEXT_STRING) "*")
                )
              )
            )
             (progn
               (print "===============================")
               (print (strcat "Drawing " DWG ":"))
               (print
                 (strcat "Found text string <" TEXT_STRING "> in " (vla-get-name LAYOUT))
               )
               (print "===============================")
             )
          )
        )
      )
      (Close_Dbx_Document DBX_DOC)
    )
    (princ
      (strcat "\n*  Could not open drawing " (vl-filename-base DWG) ".DWG")
    )
  )
  (princ)
)
;;;===========================================================================================================
(defun Find_Files_And_Run_In_Batch_Mode ($DIRECTORY $TEXT_STRING $CASE_SENSITIVE)

  (setq old_error *error*)
  (setq *error* Fndtxtstr_Error)
  (textscr)
  (princ "---------------------------------")
  (princ "\n  -=<  Find Text in Progress  >=-")
  (princ "\n---------------------------------")
  (foreach DRAWING_FILE (acad_strlsort (vl-directory-files $DIRECTORY "*.dwg" 1))
    (if (/= (strcat $DIRECTORY "\\" DRAWING_FILE)
            (strcat (getvar "dwgprefix") (getvar "dwgname"))
        )
      (progn
        (prompt (strcat "\n|-> Processing file " (strcase DRAWING_FILE) " ..."))
        (princ)
        (Find_Text_String
          (strcat $DIRECTORY "\\" DRAWING_FILE)
          $TEXT_STRING
          $CASE_SENSITIVE
        )
      )
    )
  )
  (princ "\n-------------------------------")
  (princ "\n  -=<  Find Text Completed  >=-")
  (princ "\n-------------------------------")
  (setq *error* old_error
        old_error nil
  )
)
;;;=========================================================================================================================================================
(defun c:FTS1 ()

(Find_Files_And_Run_In_Batch_Mode
(ACET-UI-PICKDIR "Select Directory to Search" "c:\\"); REPLACE HERE THE FOLDER TO CHECK FOR THE DRAWINGS
(getstring T "Enter text string to search for: ") ; TEXT STRING TO CHECK FOR
T ; CASE_SENSITIVE FLAG: IF IS T => MATCH CASE, IF IS NIL => DON'T MATCH THE CASE
)
(princ)
)
;;;=========================================================================================================================================================
(defun c:FTS2 ()

(Find_Files_And_Run_In_Batch_Mode
(ACET-UI-PICKDIR "Select Directory to Search" "c:\\"); REPLACE HERE THE FOLDER TO CHECK FOR THE DRAWINGS
(getstring T "Enter text string to search for: ") ; TEXT STRING TO CHECK FOR
nil ; CASE_SENSITIVE FLAG: IF IS T => MATCH CASE, IF IS NIL => DON'T MATCH THE CASE
)
(princ)
)
;;;=========================================================================================================================================================
(prompt "\n*** FIND TEXT STRING VIA ODBX.LSP loaded. ***")
(prompt "\nType FTS1 to run in Case sensitive mode and FTS2 in Case insensitive mode.")
(princ)
;;;=========================================================================================================================================================

Thank you,
Daniel