CAD Forum - Database of tips, tricks and utilities for AutoCAD, Inventor and other Autodesk products [www.cadforum.cz]
CZ | EN | DE
Login or
registration
  Visitors: 5303
RSS channel - CAD tips RSS tips
RSS discussions

Discussion Discussion forum

 

HelpCAD discussion

 
CAD Forum - Homepage CAD discussion forum - ask any CAD-related questions here, share your CAD knowledge on AutoCAD, Inventor, Revit and other Autodesk software with your peers from all over the world. To start a new topic, choose an appropriate forum.

Please abide by the rules of this forum.

How to post questions: register or login, go to the specific forum and click the NEW TOPIC button.
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

How find a drawing with a specific layer name?

 Post Reply Post Reply
Author
remcokoedoot View Drop Down
Groupie
Groupie


Joined: 16.Oct.2013
Location: Netherlands
Using: AutoCAD
Status: Offline
Points: 35
Post Options Post Options   Thanks (0) Thanks(0)   Quote remcokoedoot Quote  Post ReplyReply Direct Link To This Post Topic: How find a drawing with a specific layer name?
    Posted: 11.Nov.2022 at 20:34
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.


Edited by remcokoedoot - 11.Nov.2022 at 20:35
With kind regards,

Remco Koedoot
Back to Top
remcokoedoot View Drop Down
Groupie
Groupie


Joined: 16.Oct.2013
Location: Netherlands
Using: AutoCAD
Status: Offline
Points: 35
Post Options Post Options   Thanks (1) Thanks(1)   Quote remcokoedoot Quote  Post ReplyReply Direct Link To This Post Posted: 13.Nov.2022 at 12:08
I have a 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.

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



Edited by remcokoedoot - 13.Nov.2022 at 12:11
With kind regards,

Remco Koedoot
Back to Top
remcokoedoot View Drop Down
Groupie
Groupie


Joined: 16.Oct.2013
Location: Netherlands
Using: AutoCAD
Status: Offline
Points: 35
Post Options Post Options   Thanks (0) Thanks(0)   Quote remcokoedoot Quote  Post ReplyReply Direct Link To This Post Posted: 17.Nov.2022 at 09:52
Originally posted by remcokoedoot remcokoedoot wrote:

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 thats results all layer names?

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

With kind regards,

Remco Koedoot
Back to Top
remcokoedoot View Drop Down
Groupie
Groupie


Joined: 16.Oct.2013
Location: Netherlands
Using: AutoCAD
Status: Offline
Points: 35
Post Options Post Options   Thanks (0) Thanks(0)   Quote remcokoedoot Quote  Post ReplyReply Direct Link To This Post Posted: 02.Feb.2023 at 13:28
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.
With kind regards,

Remco Koedoot
Back to Top
Vladimir Michl View Drop Down
Moderator Group
Moderator Group

Arkance Systems CZ

Joined: 26.Jul.2007
Location: Czech Republic
Using: Autodesk software
Status: Offline
Points: 1921
Post Options Post Options   Thanks (0) Thanks(0)   Quote Vladimir Michl Quote  Post ReplyReply Direct Link To This Post Posted: 02.Feb.2023 at 14:07
I would recommend to stay inside AutoCAD (or ScriptPro) to process a batch of DWGs. Opening AutoCAD drawings has some specifics (delays, xrefs missing, fon missing dialogs...) that an OLE object launched from Excel won't handle.
Vladimir Michl (moderator)
Arkance Systems - arkance-systems.cz - Autodesk reseller
Back to Top
remcokoedoot View Drop Down
Groupie
Groupie


Joined: 16.Oct.2013
Location: Netherlands
Using: AutoCAD
Status: Offline
Points: 35
Post Options Post Options   Thanks (0) Thanks(0)   Quote remcokoedoot Quote  Post ReplyReply Direct Link To This Post Posted: 04.Feb.2023 at 09:30
The lisp routine works but only for one drawing. Is it possible even as in dbxscanblock to execute a directory of selected files to write the information to Excel?

;;;; LAYERS2XLS.LSP - AutoCAD lisp routine for writing the names and properties of AutoCAD layers to an Excel .xls file
;;;; Start commando: l2x

(defun c:l2x ()
  (graphscr)
  (setvar "cmdecho" 0)
  (vl-load-com)
  (setq *modelspace*
(vla-get-modelspace
   (vla-get-activedocument
     (vlax-get-acad-object)
   )
)
  )
  (setq *layers*
(vla-get-layers
   (vla-get-activedocument (vlax-get-acad-object))
)
  )

  (setq chekCountProperty
(vlax-property-available-p *layers* 'count)
  )

  (if (= chekCountProperty T)
    (progn
      (setq numbLayers (vlax-get-property *layers* 'count))

      (setq chekItemMethod (vlax-method-applicable-p *layers* 'item))

      (if (= chekItemMethod T)
(progn
  (init-excel)

  (init-app)
  (setq count 0)
  (while (< count numbLayers)
    (progn
      (setq *layerobject*
     (vlax-invoke-method *layers* 'item count)
      )
      (setq newrow (+ count 2))
      (setq *name* (vlax-get-property *layerobject* 'name))
      (write-row-column newrow 1 *name*)
      (setq
*layeron* (vlax-get-property *layerobject* 'layeron)
      )
      (write-row-column newrow 2 (true-or-false *layeron*))
      (setq *freeze* (vlax-get-property *layerobject* 'freeze))
      (write-row-column newrow 3 (true-or-false *freeze*))
      (setq *lock* (vlax-get-property *layerobject* 'lock))
      (write-row-column newrow 4 (true-or-false *lock*))
      (setq
*color* (vlax-get-property
  (vlax-get-property *layerobject* 'truecolor)
  'colorindex
)
      )
      (write-row-column newrow 5 (itoa *color*))
      (setq *linetype*
     (vlax-get-property *layerobject* 'linetype)
      )
      (write-row-column newrow 6 *linetype*)
      (setq *lineweight*
     (vlax-get-property
       *layerobject*
       'lineweight
     )
      )
      (write-row-column newrow 7 (itoa *lineweight*))
      (setq *plotstylename*
     (vlax-get-property
       *layerobject*
       'plotstylename
     )
      )
      (write-row-column newrow 8 *plotstylename*)
      (setq *plottable*
     (vlax-get-property *layerobject* 'plottable)
      )
      (write-row-column newrow 9 (true-or-false *plottable*))
      (setq *viewportdefault*
     (vlax-get-property
       *layerobject*
       'viewportdefault
     )
      )
      (write-row-column
newrow
10
(true-or-false *viewportdefault*)
      )
      (setq *description*
     (vlax-get-property
       *layerobject*
       'description
     )
      )
      (write-row-column newrow 11 *description*)
      (setq ssobjects (ssget "x" (list (cons 8 *name*))))
      (if (/= ssobjects nil)
(setq numbObjects (sslength ssobjects))
(setq numbObjects 0)
      )
      (write-row-column newrow 12 (itoa numbObjects))
      (setq count (1+ count))
    )
  )
)
      )
    )
  )
)

(defun init-excel (/ excel-app wb-collection workbook sheets sheet1)
  (setq excel-app (vlax-get-or-create-object "excel.application"))
  (setq wb-collection (vlax-get excel-app "workbooks"))
  (setq workbook (vlax-invoke-method wb-collection "add"))
  (setq sheets (vlax-get workbook "sheets"))
  (setq sheet1 (vlax-get-property sheets "item" 1))
  (setq *excel-cells* (vlax-get sheet1 "cells"))
  (vlax-put excel-app "visible" 1)
)

(defun write-row-column (row col x)
  (vlax-put-property
    *excel-cells*
    "item"
    row
    col
    (vl-princ-to-string x)
  )
)

(defun init-app ()
  (write-row-column 1 1 "Layer Name")
  (write-row-column 1 2 "Layer On")
  (write-row-column 1 3 "Layer Freeze")
  (write-row-column 1 4 "Layer Lock")
  (write-row-column 1 5 "Layer Color")
  (write-row-column 1 6 "Layer Linetype")
  (write-row-column 1 7 "Layer Lineweight")
  (write-row-column 1 8 "Layer Plot Style")
  (write-row-column 1 9 "Layer Plottable")
  (write-row-column 1 10 "Layer New VP Freeze")
  (write-row-column 1 11 "Layer Description")
  (write-row-column 1 12 "Number of Objects in this Layer")
)

(defun true-or-false (bool)
  (if (= bool :vlax-true)
    (setq bool "True")
    (setq bool "False")
  )
)

(princ)



Edited by remcokoedoot - 04.Feb.2023 at 09:31
With kind regards,

Remco Koedoot
Back to Top
CAO Expert AI View Drop Down
Newbie
Newbie


Joined: 11.Mar.2023
Location: Morocco
Using: AutoCAD2023
Status: Offline
Points: 3
Post Options Post Options   Thanks (0) Thanks(0)   Quote CAO Expert AI Quote  Post ReplyReply Direct Link To This Post Posted: 11.Mar.2023 at 23:58
Of course, here is a Lisp routine that should help you? search folder designs for a specific layer name and ? save the results in an Excel file:
___________________________________________________________________
 
(defun search-for-layer (layer-name folder-path excel-file)
  (setq excel (vlax-create-object "Excel.Application"))
  (setq workbooks (vlax-get-property excel "Workbooks"))
  (setq workbook (vlax-invoke-method "Add" workbooks)
  (feuilles setq (classeur vlax-get-property "Sheets"))
  (feuille setq (feuilles vlax-get-property "Item" 1))
  (vlax-invoke "Enable" method sheet)
  (vlax-invoke-method sheet "Cells" 1 1 :value "File name")
  (vlax-invoke-method sheet "Cells" 1 2 :value "Layer Found")
  (setq line 2)
  (foreach file (vl-directory-files folder-path "*.dwg" 1)
    (setq doc (vla-open (vla-get-documents (vlax-get-property excel "Application")) fichier))
    (and doc
      (prog
        (setq layer found null)
        (couches setq (doc vla-get-layers))
        (for each layer of lyr
          (if (string= layer-name (vla-get-name lyr))
            (setq layer found t)
          )
        )
        (if layer found
          (prog
            (vlax-invoke-method sheet "Cells" line 1: value file)
            (vlax-invoke-method sheet "Cells" row 2 :value layer-name)
            (setq row (+ row 1))
          )
        )
        (vla-close the doc)
      )
    )
  )
  (vlax-invoke-method "SaveAs" workbook excel file)
  (vlax-invoke-method "Close" workbook: vlax-false)
  (vlax-invoke-method excel "Quitter")
)
____________________________________________________
To use this routine, you must supply three arguments: 
The name of the layer you are looking for. 
The path to the folder where you want to search for DWG files. 
The full path and filename to save the results to an Excel file. 
For example, if you want to search for layer "LAYER1" in all DWG files in the "C:\Drawings" folder, and save the results to an Excel file named "Search LAYER1.xlsx" in the "C:\Results" folder , you can use this Lisp command To use this routine, you must provide three arguments: The name of the layer you are looking for. The path to the folder where you want to search for DWG files. The full path and filename to save the results to an Excel file. For example, if you want to search for layer "LAYER1" in all DWG files in the "C:\Drawings" folder, and save the results to an Excel file named "Search LAYER1.xlsx" in the "C:\Results" folder ,

(search-layer "LAYER1" "C:\\Drawings\\" "C:\\Results\\Search LAYER1.xlsx")


Edited by CAO Expert AI - 12.Mar.2023 at 00:01
CAO Expert
Back to Top

Related CAD tips:


 Post Reply Post Reply
  Share Topic   

Forum Jump Forum Permissions View Drop Down



This page was generated in 0,109 seconds.