See the top rated post in this thread. Click here

Results 1 to 7 of 7

Thread: How find in a directory of dwg drawings as result a drawing with a specific layer name?

  1. #1
    Member
    Join Date
    2010-01
    Posts
    18
    Login to Give a bone
    0

    Default How find in a directory of dwg drawings as result a drawing with a specific layer name?

    I'm looking for a way through a lisp routine to search various folders drawings (.dwg files) for a specific layer name.
    The result in an excel list of the found dwg files where the specific layer is located.

    I have this lisp routine that will scan in a folder of drawings for all layer names. It must be in a atnother solution. To specify to search various folders drawings (.dwg files) for a specific layer name. The result in an excel list of the found dwg files where the specific layer is located.
    By running the lisp, how to wtite the input (layer name) in a shell-window and not by the routine?

    Code:
    (defun c:CheckLayers ( / *error* DBX DOCLST FILES FLAG LAYER_LIST ODOC OFILE OUTFILE SHELL )
     (vl-load-com)
    
     (defun *error* (msg)
       (ObjRelease (list Shell dbx))
       (and ofile (= (type ofile) 'FILE) (close ofile))
       
       (or (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*")
           (princ (strcat "\n** Error: " msg " **")))
       (princ)
     )
    
     (setq *acad (cond (*acad) ((vlax-get-acad-object)))
           *doc  (cond (*doc ) ((vla-get-ActiveDocument *acad))))
    
     (if (and (setq Files   (GetAllFiles nil t "*.dwg"))
              (setq outfile (getfiled "Output File" "" "csv" 1)))
       (progn
         
         (vlax-for doc (vla-get-Documents *acad)
           (setq DocLst
             (cons
               (cons (strcase (vla-get-FullName doc)) doc) DocLst
             )
           )
         )      
    
         (setq dbx (ObjectDBXDocument))
         
         (foreach dwg Files
    
           (cond
             (  (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)
             )
           )
    
           (setq Layer_List
             (if flag
               (cons (cons dwg (GetLayerProperties oDoc)) Layer_List)
               (cons (cons dwg '(("**Error Opening this Drawing **"))) Layer_List)
             )
           )
         )
    
         (princ (strcat "\n<< " (itoa (length Files)) " Drawings Processed >>"))
       )    
       (princ "*Cancel*")
     )
    
     (vlax-release-object dbx) (gc) (gc)
    
     (if (and Layer_List (setq ofile (open outfile "w")))
       (progn      
         (mapcar
           (function
             (lambda (x)
               (write-line (car x) ofile)
               (write-line (MakeString '("Name" "Colour" "LineType" "LineWeight") (chr 32)) ofile)
               (mapcar
                 (function
                   (lambda (y)
                     (write-line
                       (MakeString y (chr 32)) ofile
                     )
                   )
                 )
                 (cdr x)
               )            
               (write-line "\n" ofile)
             )
           )
           Layer_List
         )
         (close ofile)
       )
       (princ "\n*Cancel*")
     )
     (princ)
    )
    
    (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 GetAllFiles ( Dir Subs Filetype / GetSubFolders Shell Fold Dir )
     (vl-load-com)
     
     (defun GetSubFolders ( folder / _f )
       (mapcar
         (function
           (lambda ( f ) (setq _f (strcat folder "" f))
             (cons _f (apply (function append)
                             (GetSubFolders _f)))
           )
         )
         (cddr (vl-directory-files folder nil -1))
       )
     )
    
     (cond
       ( (not
           (or
             (and Dir (vl-file-directory-p Dir))
             (progn
               (setq Shell (vla-getInterfaceObject
                             (setq acad (vlax-get-acad-object)) "Shell.Application")
                     Fold  (vlax-invoke-method Shell 'BrowseForFolder
                             (vla-get-HWND acad) "Select Directory" 512))
               (vlax-release-object Shell)
               
               (if Fold
                 (progn
                   (setq Dir (vlax-get-property
                               (vlax-get-property Fold 'Self) 'Path))
                   (vlax-release-object Fold)
                   
                   (and (= "" (substr Dir (strlen Dir)))
                        (setq Dir (substr Dir 1 (1- (strlen Dir)))))
                   
                   Dir
                 )
               )
             )
           )
         )
       )
       ( (apply (function append)
           (vl-remove (quote nil)
             (mapcar
               (function
                 (lambda (Filepath)
                   (mapcar
                     (function
                       (lambda (Filename)
                         (strcat Filepath "" Filename)
                       )
                     )
                     (vl-directory-files Filepath Filetype 1)
                   )
                 )
               )
               (append (list Dir)
                 (apply (function append)
                   (if subs (GetSubFolders Dir))
                 )
               )
             )
           )
         )
       )
     )
    )
    
    (defun GetLayerProperties ( doc / lst )
     (vlax-for lay (vla-get-Layers doc)
       (setq lst
         (cons
           (mapcar
             (function
               (lambda ( property )
                 (vl-princ-to-string
                   (vlax-get-property lay property)
                 )
               )
             )
             '(Name Color Linetype LineWeight)
           )
           lst
         )
       )
     )  
     (vl-sort lst
       (function
         (lambda (a b) (< (car a) (car b)))
       )
     )
    )
    
    (defun MakeString  ( lst del / Pad str x i )
     (setq i 10)
     
     (defun Pad ( Str Del Len )
       (while (>= (strlen Str) Len) (setq Len (+ Len 5)))
       (while (< (strlen Str) Len)
         (setq Str (strcat Str Del))
       )
       Str
     )
     
     (apply (function strcat)
       (reverse
         (cons (last lst)
           (mapcar
             (function
               (lambda ( $str )
                 (Pad $str del (setq i (abs (- 40 i))))
               )
             )          
             (cdr (reverse lst))
           )
         )
       )
     )
    )
    Last edited by Remco Koedoot; 2022-11-17 at 08:53 AM. Reason: [code] tags added

  2. #2
    Administrator Opie's Avatar
    Join Date
    2002-01
    Location
    jUSt Here (a lot)
    Posts
    9,100
    Login to Give a bone
    0

    Default Re: How find in a directory of dwg drawings as result a drawing with a specific layer name?

    Are you needing work on this solution? If so, where is the current code you have posted fail? That is a lot to review to help you diagnose your code.
    If you have a technical question, please find the appropriate forum and ask it there.
    You will get a quicker response from your fellow AUGI members than if you sent it to me via a PM or email.
    jUSt

  3. #3
    Member
    Join Date
    2010-01
    Posts
    18
    Login to Give a bone
    0

    Default Re: How find in a directory of dwg drawings as result a drawing with a specific layer name?

    You can read the current code in the topic. And that code works. I would like an extension to the existing module with function as described in the topic. What's your solution?
    Last edited by Remco Koedoot; 2022-11-16 at 09:23 PM.

  4. #4
    Member
    Join Date
    2010-01
    Posts
    18
    Login to Give a bone
    0

    Default Re: How find in a directory of dwg drawings as result a drawing with a specific layer name?

    Is it possible in a lisp routine?

    I read this on internet:
    Since Accoreconsole has no graphical interface, its much faster to process drawings than ObjectDBX.
    Certainly worth looking into. My only experience with Accoreconsole was batch processing dwg to pdfs' running in command line (cmd).
    I was looking for a solution for loading the database or DWG into memory. I know it's possible via .NET by utilizing 'side loading' or 'side database' workflows, and I know Autodesk's own tools like the reference editor can read into a file with opening it.


    I did find Lee-Mac's layer extractor lisp routine, which does exactly what I want, I just tears through files, seemingly without opening them. I believe it's utilizing ObjectDBX but I haven't dug into the code yet. I hope to modify it to fire directly from Excel bypass the interface.
    Regarding the layers, I completely agree. Unfortunately our projects are big (think major civil infrastructure), and have hundreds of people working across a dozen disciplines from dozens of companies. We only have control over very small portions of the CAD work that ends up on our sheets.

    Any text based format is fine, plain text, CSV, XML. I can read or parse through it later. After getting the layers I will plug them into my VBA layer manager to track/find/update everything so there are no surprises during my next bi-weekly plot.
    I'm using ObjectDBX and controlling any Autocad instance from Excel, cutting out the middle man.

    Set acad = CreateObject("AutoCAD.Application")
    acad.Visible = False

    Set doc = acad.getinterfaceobject("ObjectDBX.AxDbDocument.20")

    doc.Open ("C:\....dwg")

    For Each iLayer In doc.Layers
    Debug.Print iLayer.Name
    Next iLayer

    {etc.}
    I'm getting thousands of layers from hundreds of files in seconds, straight into my Excel manager.

  5. #5
    Administrator Ed Jobe's Avatar
    Join Date
    2000-11
    Location
    Turlock, CA
    Posts
    6,409
    Login to Give a bone
    1

    Default Re: How find in a directory of dwg drawings as result a drawing with a specific layer name?

    If you're already using VBA, why do you need it in lisp? VBA manages ObjectDBX just fine, as your sample shows.

    So, if you're getting thousands of layer names, what else do you need?
    C:> ED WORKING....


    LinkedIn

  6. #6
    Member
    Join Date
    2010-01
    Posts
    18
    Login to Give a bone
    0

    Default Re: How find in a directory of dwg drawings as result a drawing with a specific layer name?

    Because I can not install VBA on every machine.

    - - - Updated - - -

    Quote Originally Posted by Ed Jobe View Post
    If you're already using VBA, why do you need it in lisp? VBA manages ObjectDBX just fine, as your sample shows.

    So, if you're getting thousands of layer names, what else do you need?
    Because I can not install VBA on every machine.

  7. #7
    Administrator Opie's Avatar
    Join Date
    2002-01
    Location
    jUSt Here (a lot)
    Posts
    9,100
    Login to Give a bone
    0

    Default Re: How find in a directory of dwg drawings as result a drawing with a specific layer name?

    You state the code you posted extracts layers from a folder of drawings and places the layer properties and drawing name to a CSV file.

    This routine does use ObjectDBX to open drawings without displaying the graphics. Since the graphics are not being displayed, the routine should be fairly quick.

    I believe you are wanting a similar routine that would prompt the user for a layer name and folder to search. You want the results to output to a shell-window, but I am not certain what that is, instead of a CSV.

    You will need to modify your c:CheckLayers routine to prompt the user with the layer name. You would still want to prompt for the folder to search. After those two things are known, you would still use ObjectDBX to open all of the files (one at a time) found in the specified folder. From there, you would process the layers for each found file to see if the user specified layer is found. If the layer is found, add the drawing name to a list. After all drawings are processed, you can decide how to display the drawings that contain the specified layer. I would still recommend exporting to CSV, but that is not what you want.

    It also appears the posted code is cobbled together with code posted by other people. Please leave a note of the original author and a link back to where those parts of the code can be found again. There may be updates due to bugs that may need to be incorporated in the future.

    Also, if you are using Excel as the controlling code, I believe VBA is included in the installation of Excel. I could be wrong as I do not currently have access to the latest version of Excel to verify.
    If you have a technical question, please find the appropriate forum and ask it there.
    You will get a quicker response from your fellow AUGI members than if you sent it to me via a PM or email.
    jUSt

Similar Threads

  1. Search various folders drawings (.dwg files) for a specific block name.
    By remcokoedoot in forum CAD Management - General
    Replies: 5
    Last Post: 2022-10-04, 08:20 AM
  2. Replies: 1
    Last Post: 2021-03-24, 12:24 AM
  3. Which variables are Drawing Specific and Which are Session Specific?
    By stusic in forum CAD Management - General
    Replies: 4
    Last Post: 2013-09-10, 01:28 PM
  4. Replies: 12
    Last Post: 2010-01-08, 07:49 PM
  5. Dim Text on Specific Layer instead of having Specific Color
    By Masroor Javid in forum ACA Wish List
    Replies: 0
    Last Post: 2009-02-23, 09:34 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •