Author Topic: LISP routine to overwrite layer descriptions...  (Read 3852 times)

0 Members and 1 Guest are viewing this topic.

dortega4269

  • Guest
LISP routine to overwrite layer descriptions...
« on: July 21, 2014, 12:42:55 PM »
I've done several searches for a commands and/or LISP routine to overwrite an existing layer description with the same layer and description found in another file.

My process:
I have several files containing standard layers that are being updated as information is processed (Casework, Equipment, Furniture, etc.).  I insert these files, when updated, into several main files (Level 1, 2, 3, etc.) to add, delete or replace layers.  The use of the layers is to control my blocks and their visibility through the layer manager; hide all Casework or hide all base cabinets that are 30" tall, etc.  The layer description is important because it gives the user an understanding of what the layer is specifically used for; layer 'CW-30-BC  B3' has a layer description of 'Casework: 30" - Base Cabinet - (11" & 30")'.

Should this layer description need to be updated from 'Casework: 30" - Base Cabinet - (11" & 30")' to 'Casework: 30" - Base Cabinet - (12" & 28")' I would like to insert the file containing the correct layer descriptions and overwrite the current layer descriptions.  The current 'insert' function only overwrites the layer name, however, the layer description remains unchanged.

Note: Using the Design Center to add or overwrite layers, using Layer Translator (laytrans), or using Layer Standards (.dws) have only edited the layer name, and layer properties, these commands have not edited the layer description.

Anything you can offer to get me started is greatly appreciated.


BlackBox

  • King Gator
  • Posts: 3770
"How we think determines what we do, and what we do determines what we get."

JohnK

  • Administrator
  • Seagull
  • Posts: 10637
Re: LISP routine to overwrite layer descriptions...
« Reply #2 on: July 21, 2014, 02:38:53 PM »
I rolled my own a few years back but be warned I was beginning to become a little rusty at my lisp skills at the time but I used it for years with no problems.

Hope it helps.

Code - Auto/Visual Lisp: [Select]
  1. ;; NAME
  2. ;;         Layer Settings
  3. ;;
  4. ;; SYNOPSIS
  5. ;;         Load a previous saved layer file.
  6. ;;
  7. ;; DESCRIPTION
  8. ;;         This application will allow you to load previous saved layers
  9. ;;         (name, color, line type, and state) to the current drawing. If
  10. ;;         the layers do not exist they will be created using the
  11. ;;         settings in the file. If the layer name already exists in the
  12. ;;         drawing, those settings will not be updated -- this is to
  13. ;;         allow for custom, per drawing, layer overrides-.
  14. ;;              //-- SEE CHANGE LOG --//
  15. ;;
  16. ;;         To save the current drawings layers use:
  17. ;;              (process-on-drawing save-layer-list-to-file)
  18. ;;         A file with the current drawings file name, with the `.la'
  19. ;;         suffix will be saved to the current drawings location.
  20. ;;
  21. ;; NOTES
  22. ;;         My overall objective was to create a fast way for me to load
  23. ;;         up layer settings for my CAD users.
  24. ;;
  25. ;; AUTHOR
  26. ;;         John Kaul
  27. ;;
  28. ;; VERSION
  29. ;;         1.0.3
  30. ;;
  31. ;; CHANGE LOG
  32. ;;         1.0.0        Orig issue
  33. ;;                      05.20.08
  34. ;;         1.0.1        Revised to handle plot/noplot setting
  35. ;;                      (dxf code 290)
  36. ;;                      Closed layer file once done reading
  37. ;;                      05.27.08
  38. ;;         1.0.2        Removed parsing of drawings layer list to
  39. ;;                      names ...making it faster.
  40. ;;                      New time for creating 10,000 layers was 1.36 seconds
  41. ;;                      05.28.08
  42. ;;         1.0.3        Added the ability to modify layers already in dwg which
  43. ;;                      are different the ones found in the layer file.
  44. ;;         1.0.4        Added the ability to load and handle linetypes not in
  45. ;;                      drawing or located in .lin file in search path
  46. ;;                      07.22.08
  47. ;;         1.0.5        Added the abililty to set a layer description (assoc 777)
  48. ;;                      layer file (.la)
  49. ;;                      03.11.09
  50. ;;         1.0.6        Added the ability to add comments to the .la files
  51. ;;                      comments begin with a semi colon ( ; )
  52. ;;                      03.17.09
  53. ;;
  54. ;; //-- BEGIN SUPPORT PROCEDURES --//
  55.  
  56. (defun process-on-drawing ( process )
  57.   ;; a simple `hook' to run a process
  58.         (eval process) )
  59.  
  60. (defun save-layer-list-to-file ( / x f fp )
  61.   ;; retrieve all layers from dwg dict and save
  62.   ;; to a file.  file saved where drawing is located
  63.       (setq f (strcat (getvar 'DWGPREFIX) (getvar 'DWGNAME) ".la")
  64.             fp (open f "A"))
  65.       (while (setq x (tblnext "LAYER" (not x)))
  66.              (cond
  67.                ((not (>= (cdr (assoc 70 x)) 16))
  68.                 (prin1 x fp)
  69.                 (princ "\n" fp)) )
  70.              )
  71.       (close fp)
  72.       (princ) )
  73.  
  74. (defun get-list-from-file ( name / fp lst read-file-into-list )
  75.   ;; general file reader
  76.   ;; given a file name this procedure will read the contents
  77.   ;; into a list
  78.          (defun read-file-into-list ( str file )
  79.            (if str
  80.              (cons
  81.                (if (not (wcmatch str "*;*")) str "")
  82.                ;; get string from file; if it is a comment, return an empty string.
  83.                (read-file-into-list (read-line file) file))))
  84.   (setq fp (open name "R"))
  85.   (setq lst (read-file-into-list (read-line fp) fp))
  86.   (close fp)
  87.  lst
  88. )
  89.  
  90. (defun get-drawing-dictionary-list ( what / x lst)
  91.   ;; retrieve a list of drawing dictionary entries
  92.        (while (setq x (tblnext what (not x)))
  93.           (setq lst (cons x lst))) )
  94.  
  95. (defun build-name-list ( lst )
  96.   ;; given a list of raw dic entries this proced
  97.   ;; will report entity names
  98.          (if (null lst)
  99.            nil
  100.            (cons (cdr (assoc 2 (car lst)))
  101.                 (build-name-list (cdr lst)))) )
  102.  
  103. (defun build-modify-list ( ls )
  104.   ;; return list of items found in dwg but different from list
  105.           (mapcar
  106.             '(lambda ( x )
  107.                (if (not (member x dwg-layer-list))
  108.                  x))
  109.             ls) )
  110.  
  111. (defun get-value ( val lst )
  112.   ;; shortcut for returning value in assoc list
  113.        (cdr (assoc val lst)) )
  114.  
  115. (defun strParse (aStr delim / strList pos)
  116.   (while
  117.     (setq pos (vl-string-search delim aStr 0))
  118.     ;; Find the postition where the delimiter first shows up.
  119.     (setq strList (cons (substr aStr 1 POS) strList)
  120.     ;; create a list of the fist substring (up untill the delimiter)
  121.           strList (cons (substr aStr (1+ POS) 1) strList)
  122.           aStr (substr aStr (+ pos 2)))
  123.     ;; Skip over the delimiter and grab the rest of the string,
  124.     ;; Set that as the new string
  125.     )
  126.   (reverse (cons aStr strList))
  127.  )
  128.  
  129. (defun lt-find ( x )
  130.   (if (member lt (car (cdr x)))
  131.     (progn
  132.       (command "_.linetype" "_load" lt (car x) "")
  133.       (setq dwg-ltype-name-list
  134.             ;; remake the drawing ltype list
  135.             (build-name-list
  136.               (process-on-drawing
  137.                 '(get-drawing-dictionary-list "LTYPE"))))
  138.       );_ end progn
  139.     );_ end if
  140.   )
  141.  
  142.  
  143.  ;; //-- END SUPPORT PROCEDURES --//
  144.  
  145. (defun LayerSetup ( layers-to-load-from-file / dwg-layer-list dwg-layer-name-list
  146.                                                todo-layer-list timer layers-to-modify
  147.                                                ltype-file-locations file-layer-list
  148.                                                ltype-locations dwg-ltype-name-list
  149.                                                )
  150.  
  151.     (setq
  152.       dwg-layer-list (process-on-drawing '(get-drawing-dictionary-list "LAYER"))
  153.       ;; build a list of layers in current drawing
  154.  
  155.       dwg-layer-name-list (build-name-list dwg-layer-list)
  156.       ;; build a list of layer names (names only) in current drawing
  157.  
  158.       dwg-ltype-name-list
  159.                 (build-name-list (process-on-drawing '(get-drawing-dictionary-list "LTYPE")))
  160.       ;; build a list of litetype names loaded in dwg
  161.  
  162.       ltype-file-locations
  163.                    (apply
  164.                      ;; itterate thru the entire search path to look for .lin files
  165.                      'append
  166.                      (mapcar
  167.                        '(lambda ( x / tmp-str)
  168.                           (setq tmp-str (vl-directory-files x "*.lin" 1))
  169.                           (if (and tmp-str (not (eq ";" x)))
  170.                             (list
  171.                               (strcat
  172.                                 x
  173.                                 "\\"
  174.                                 (car tmp-str)))))
  175.                        (strparse (getvar "ACADPREFIX") ";") ))
  176.       ltype-locations
  177.        ;; a list of lists for the lietypes and their locations
  178.        ;; ( <path>+<file> ( <ltype> <ltype> ... ))
  179.                    (mapcar
  180.                     '(lambda ( x / f lt-list)
  181.                        (setq lt-list
  182.                           (mapcar
  183.                              '(lambda ( x )
  184.                                 (if x
  185.                                   (substr (car (strparse x ",")) 2)))
  186.                                 (get-list-from-file x)
  187.                              )
  188.                           )
  189.                        (cons x (list lt-list))
  190.                        )
  191.                     ltype-file-locations
  192.                     )
  193.  
  194.       file-layer-list (mapcar
  195.                         'read (get-list-from-file
  196.                           (findfile layers-to-load-from-file)))
  197.       ;; get the layers from the file (layers to impliment)
  198.  
  199.       todo-layer-list (mapcar
  200.                       '(lambda ( x )
  201.                               (if
  202.                                  (not (member (cdr (assoc 2 x)) dwg-layer-name-list))
  203.                                x))
  204.                       file-layer-list)
  205.       )
  206.          ;; parse out the already defined common layers
  207.  
  208.    (if todo-layer-list
  209.      (mapcar
  210.        ;;
  211.        ;; create layers not in drawing already
  212.        ;;
  213.        '(lambda (x / lt plot)
  214.           ;; create the layers
  215.           (if (not (null x))
  216.             ;; do not opperate on null entries.
  217.             (progn
  218.               (setq lt (get-value 6 x)
  219.                     lt
  220.                     (cond
  221.                       ((member lt dwg-ltype-name-list)
  222.                        (cons 6 lt))
  223.                       ((and
  224.                          (not (tblsearch "LTYPE" (get-value 6 x)))
  225.                          (not (member (get-value 6 x) dwg-ltype-name-list)))
  226.                        (mapcar
  227.                          ;; itterate thru the ltype file list to see which
  228.                          ;; file contains the missing linetype, then load it
  229.                          'lt-find        
  230.                          ltype-locations )
  231.                        (if (not (tblsearch "LTYPE" lt))
  232.                          ;; another search level incase we still coulnt find lt.
  233.                          (progn
  234.                            (mapcar
  235.                              'princ
  236.                              (list
  237.                                "\nLinetype for Layer: "
  238.                                (get-value 2 x)
  239.                                " Not found, using continuous instead."))
  240.                            (cons 6 "Continuous")
  241.                            )
  242.                          (cons 6 (get-value 6 x))
  243.                          );_ end if
  244.                        )
  245.                       );_ end cond
  246.                     plot
  247.                     (cond
  248.                       ((get-value 290 x)
  249.                          (cons 290 (get-value 290 x)))
  250.                       (t (cons 290 1))));_ end setq
  251.               (entmake (list
  252.                          '(0 . "LAYER")
  253.                          '(100 . "AcDbSymbolTableRecord")
  254.                          '(100 . "AcDbLayerTableRecord")
  255.                          (cons 2 (get-value 2 x))
  256.                          (cons 70 (get-value 70 x))
  257.                          (cons 62 (get-value 62 x))
  258.                          plot
  259.                          lt
  260.                          );_ end list
  261.                        );_ end entmake
  262.              
  263.               ;; add some description
  264.               ;; Descriptions are assoc code 777 in .la file
  265.               (if (get-value 777 x)
  266.                   (vla-put-description
  267.                     (vlax-ename->vla-object (tblobjname "LAYER" (get-value 2 x)))
  268.                     (get-value 777 x)) )
  269.               );_ end progn
  270.             );_ end if
  271.           )
  272.        todo-layer-list
  273.        )
  274.      )
  275.  
  276.  (setq  dwg-layer-list (process-on-drawing '(get-drawing-dictionary-list "LAYER"))
  277.         layers-to-modify (build-modify-list file-layer-list)
  278.         ;; build a list of layers that need to be modified.
  279.         )
  280.      
  281.     (if layers-to-modify
  282.       (mapcar
  283.         ;;
  284.         ;; Update or change layers that do not have properties
  285.         ;; listed in layer file
  286.         ;;
  287.         ;; TODO: Add linetype support -- inline
  288.         '(lambda ( x / lt plot layer )
  289.            (if (not (null x ))
  290.              ;; do not opperate on null entries in list
  291.              (progn
  292.                (setq layer (entget (tblobjname "LAYER" (cdr (assoc 2 x)))))
  293.                ;; get the layer from the dwg dict.
  294.                (mapcar
  295.                  '(lambda ( key / plot )
  296.                     ;; we are going to modify several of the drawing layer props at once
  297.                     ;; the dxf codes can be found at the end of the mapcar function call
  298.                     (cond
  299.                       ((eq key 290)
  300.                        ;; if we are on key 290 (ploting flag)
  301.                        (set 'plot (if (get-value key x) 0 1))
  302.                        ;; determine if the layer value has the 290 entry;
  303.                        ;; if not return 1 (plot able) if it does return 0
  304.                        (setq layer (append layer (list (cons 290 plot))))
  305.                        ;; mod the layer list
  306.                        )
  307.                       ;;                       ((eq key 6)
  308.                       ;;                        (mapcar 'lt-find
  309.                       ((get-value key layer)
  310.                        ;; other wise just mod the layer list
  311.                        (setq layer (subst (assoc key x) (assoc key layer) layer)))) )
  312.                  '(6 62 70 290) )
  313.  
  314.                (entmod layer)
  315.                ;; make modifications
  316.  
  317.                ;; add some description
  318.               (if (get-value 777 x)
  319.                   (vla-put-description
  320.                     (vlax-ename->vla-object (tblobjname "LAYER" (get-value 2 x)))
  321.                     (get-value 777 x)) )
  322.                );_ end progn
  323.              );_ end if
  324.            )
  325.         layers-to-modify
  326.         )
  327.       )
  328.  
  329.     (princ "\n ==> Layers created/modified")
  330.  
  331.   )
  332.  
  333. ;; Example Ussage:
  334. ;;
  335. ;; (defun MyLayerSetup ( / )
  336. ;;   (LayerSetup "\\SERVER\\DIR\\Lisp\\MyLayers.la")
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org