Results 1 to 4 of 4

Thread: Create Selected Layers from Excel

  1. #1
    Active Member
    Join Date
    2005-08
    Posts
    55
    Login to Give a bone
    0

    Default Create Selected Layers from Excel

    So i know everyone has done the, create list of layers based on an excel file. I have a slightly different task.

    I have a .csv file that i use with the csvlayers.vlx that imports them all and it works perfectly.
    However what i'd like to do is to be able to just load single layers or even groups of 2 or 3 layers from this same list.

    Is it possible to just call this same list and have it search for the layer name in it and create a layer based on that layers parameters?

    My goal/idea here is to just manage this one csv file for all the layers and use different routines to load different groups/single layers from it.

    Thanks everyone, you've all been a huge help.

    John Gray.

  2. #2
    Administrator BlackBox's Avatar
    Join Date
    2009-11
    Posts
    5,714
    Login to Give a bone
    0

    Default Re: Create Selected Layers from Excel

    Here's a quick adaptation of this old post, which accepts a WCMATCH string (i.e., the layers you want to match), and a file path for the desired CSV as arguments.

    Please note that the 'extract' and 'trim' sub-functions use "," (comma) as delineator; be sure to change to "/t" if you use tab delineation instead.

    Code:
    (vl-load-com)
    
    (defun MatchCSV->Layers (char path / f *error* extract trim acDoc l
                             layerTable layerName layerItem layerDescription
                             layerColor layerLinetype layerLineweight
                             layerPlottable layerFreeze
                            )
      ;; Exampe: (MatchCSV->Layers "C-ROAD*" "Z:\\my_layers_folder\\my_layers.csv")
    
      ;; Error handler
      (defun *error* (msg)
        (if f
          (close f)
        )
        (if acDoc
          (vla-endundomark acDoc)
        )
        (cond
          ((not msg))                                                       ; Normal exit
          ((member msg '("Function cancelled" "quit / exit abort")))        ; <esc> or (quit)
          ((princ (strcat "\n** Error: " msg " ** ")))
        )                                                                   ; Fatal error, display it
        (princ)
      )
    
      ;; Line extraction sub-function
      (defun extract (l /)
        (substr l 1 (vl-string-search "," l))
      )
    
      ;; Line trim sub-function
      (defun trim (v /)
        (vl-string-subst "" (strcat v ",") l)
      )
    
    
      ;; Main code
      (if (and (findfile path)
               (setq f (open path "r"))
          )
        (progn
          (vla-startundomark
            (setq acDoc (vla-get-activedocument (vlax-get-acad-object)))
          )
          (read-line f)                                                     ; <- Skip first line (headers)
          (setq layerTable (vla-get-layers acDoc))
    
          ;; Linetype check
          ;; <- Add linetype import code here, to avoid errors
    
          (while (/= nil (setq l (read-line f)))
            (if (wcmatch (setq layerName (extract l)) char)
              (progn
    
                ;; Get or create layer
                (setq layerItem (vla-add layerTable layerName))
    
    
                ;; Layer settings
                (setq l (trim layerName))
                (vla-put-description
                  layerItem
                  (setq layerDescription (extract l))
                )
                (setq l (trim layerDescription))
                (if (/= 7 (setq layerColor (extract l)))
                  (vla-put-color layerItem layerColor)
                )
                (setq l (trim layerColor))
                (vla-put-linetype
                  layerItem
                  (setq layerLinetype (extract l))
                )
                (setq l (trim layerLinetype))
                (if
                  (= "BYLAYER" (strcase (setq layerLineweight (extract l))))
                   (vla-put-lineweight layerItem aclnwtbylayer)
                )
                (setq l (trim layerLineweight))
                (if (/= "YES" (strcase (setq layerPlottable (extract l))))
                  (vla-put-plottable layerItem :vlax-false)
                )
                (setq l (trim layerPlottable))
                (if (/= "NO" (strcase (setq layerFreeze (extract l))))
                  (vla-put-freeze layerItem :vlax-true)
                )
              )
            )
          )
          (setq f (close f))
        )
      )
    
      (*error* nil)
    )


    Some examples of how one might use this:

    Code:
    (defun c:ImportRoadLayers () (MatchCSV->Layers "C-ROAD*" "Z:\\my_layers_folder\\my_layers.csv"))
    (defun c:ImportUtilLayers () (MatchCSV->Layers "C-UTIL*" "Z:\\my_layers_folder\\my_layers.csv"))
    (defun c:ImportWaterLayers () (MatchCSV->Layers "C-UTIL-WATR*" "Z:\\my_layers_folder\\my_layers.csv"))

    Cheers
    "How we think determines what we do, and what we do determines what we get."

    Sincpac C3D ~ Autodesk Exchange Apps

    Computer Specs:
    Dell Precision 3660, Core i9-12900K 5.2GHz, 64GB DDR5 RAM, PCIe 4.0 M.2 SSD (RAID 0), 16GB NVIDIA RTX A4000

  3. #3
    Active Member
    Join Date
    2005-08
    Posts
    55
    Login to Give a bone
    0

    Default Re: Create Selected Layers from Excel

    This is wonderful

    OK so the 3 different code example that you've given at the end. Do these go in the main body of code at the top where you have

    ;; Exampe: (MatchCSV->Layers "C-ROAD*" "Z:\\my_layers_folder\\my_layers.csv")

    And i'm assuming i can have as many functions defined here as i want?

    Thanks again.

  4. #4
    Administrator BlackBox's Avatar
    Join Date
    2009-11
    Posts
    5,714
    Login to Give a bone
    0

    Default Re: Create Selected Layers from Excel

    Quote Originally Posted by jgray View Post
    This is wonderful

    <snip>

    Thanks again.
    That is kind of you to say, John; I'm happy to help.


    Quote Originally Posted by jgray View Post
    OK so the 3 different code example that you've given at the end. Do these go in the main body of code at the top where you have

    Code:
     ;; Exampe: (MatchCSV->Layers "C-ROAD*" "Z:\\my_layers_folder\\my_layers.csv")
    And i'm assuming i can have as many functions defined here as i want?
    Correct; you can setup any number of custom routines, that each use the code above to cull the desired layers.

    For example, instead of the 'Road, Utilities, and Water' Layer sub-sets, you might instead have a custom routine for the various sheets in your plan set, such as 'Existing Conditions, Master Site Plan, and Plan and Profiles', etc (one for each sheet type).


    As for 'where they go', that depends on your setup... Each LISP routine you load slows down the opening of a drawing, as LISP must be loaded into each-and-every-single drawing... For this reason, I prefer to demand-load my routines, but load a single routine which contains myriad AUTOLOAD statements.

    That said, sub-functions (such as MatchCSV->Layers above) cannot be demand-loaded directly; the simplest approach might be to include all of your custom routines in the same .LSP file as the code above, then setup an AUTOLOAD statement in your AcadDoc.lsp file that loads said .LSP file when one of the custom routines is invoked... Something like this (using my above examples), assuming the .LSP file is located within SFSP:

    Code:
    (autoload "MatchCSV->Layers.lsp" '("IMPORTROADLAYERS" "IMPORTUTILLAYERS" "IMPORTWATERLAYERS"))

    Separately, if like me, you prefer to manage CSV files using Excel, but find the task of manually exporting to CSV to be a PITA:

    Quote Originally Posted by BlackBox View Post

    If you need to quickly convert XLS/XLSx --> CSV, this Right Click Excel to CSV Windows Shell Context Menu might help.



    Cheers
    "How we think determines what we do, and what we do determines what we get."

    Sincpac C3D ~ Autodesk Exchange Apps

    Computer Specs:
    Dell Precision 3660, Core i9-12900K 5.2GHz, 64GB DDR5 RAM, PCIe 4.0 M.2 SSD (RAID 0), 16GB NVIDIA RTX A4000

Similar Threads

  1. Need to create points at endpoints for selected lines with the same layers
    By brahmanandam.thadikonda762224 in forum AutoLISP
    Replies: 6
    Last Post: 2019-02-22, 05:40 PM
  2. Excel selected range
    By carmine.iannotta342739 in forum AutoLISP
    Replies: 11
    Last Post: 2014-07-02, 04:13 AM
  3. Replies: 2
    Last Post: 2013-05-23, 01:26 PM
  4. I want to Export the Selected Text and Mtext to Excel
    By avinash00002002 in forum VBA/COM Interop
    Replies: 4
    Last Post: 2011-03-31, 02:19 PM
  5. Layers that are Visible, but cannot be Selected or Snapped To
    By autocad.wishlist1734 in forum AutoCAD Wish List
    Replies: 9
    Last Post: 2008-07-01, 08:18 PM

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
  •