Author Topic: _add layer defun fails...missing something  (Read 1024 times)

0 Members and 1 Guest are viewing this topic.

jlogan02

  • Bull Frog
  • Posts: 327
_add layer defun fails...missing something
« on: May 13, 2021, 12:58:32 PM »
I tested this smaller piece of code and it worked initially. Put it into a larger code and it failed. Now this smaller piece fails too and generates...

Error: bad DXF group: (6 .

Code - Auto/Visual Lisp: [Select]
  1. (defun _Ltypeld (/ lin)
  2.        
  3.     (progn
  4.       (command "cmdecho" 0 "expert" 5 ".linetype" "_L" "*" "acad" "" "expert" 0 "cmdecho" 1)
  5.       (while (> (getvar 'cmdactive) 0) (command ""))
  6.     )
  7.  
  8.   (progn
  9.       (command "cmdecho" 0 "expert" 5 ".linetype" "_L" "*" "ipco" "" "expert" 0 "cmdecho" 1)
  10.       (while (> (getvar 'cmdactive) 0) (command ""))
  11.     )
  12.  
  13.   (princ)
  14. )
  15.  
  16. (defun _addlayer (name color ltype plot)
  17.     (cond ((not (tblsearch "layer" name))
  18.            (entmakex (list '(0 . "LAYER")
  19.                            '(100 . "AcDbSymbolTableRecord")
  20.                            '(100 . "AcDbLayerTableRecord")
  21.                            '(70 . 0)
  22.                            (cons 2 name)
  23.                            (cons 62 color)
  24.                            (cons 6
  25.                                  (cond ((tblobjname "ltype" ltype))
  26.                                        ("CONTINUOUS")
  27.                                  )
  28.                            )
  29.                            (cons 290 plot)
  30.                            ;;1 = plottable 0 = not=plottable
  31.                      )
  32.            )
  33.           )
  34.           ((tblobjname "layer" name))
  35.     );; cond
  36.   );;   end _addlayer
  37.  
  38.  
  39.  
  40. ;;;----------------------------------- Line0 Colors ------------------------------
  41.  
  42. (defun c:Line0_ByLayer ( / )
  43.   (_ltypeld)
  44.   (_addlayer "LINE0" 7 "CENTER" 1)
  45.   (command "layer" "set" "LINE0" "" "")
  46.   (Command "color" "Bylayer")
  47.   (command "line")
  48. )

It loads the linetypes but won't build the layer
J. Logan
ACAD 2018

I am one with the Force and the Force is with me.
AutoCAD Map 2018 Windows 10

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: _add layer defun fails...missing something
« Reply #1 on: May 13, 2021, 01:07:55 PM »
Code: [Select]
((tblobjname "ltype" ltype))should be:
Code: [Select]
((tblobjname "ltype" ltype) ltype)

JohnK

  • Administrator
  • Seagull
  • Posts: 10605
Re: _add layer defun fails...missing something
« Reply #2 on: May 13, 2021, 01:25:31 PM »
I see the answer is posted already but...

I wrote a robust layer creating routine many years ago that I still use. It does many things so I wont post it all. I'll post enough for you to glean from; I went through a lot of checks and list iterations because of all the different problems (-i.e. searching and loading linetypes and what happens if they cannot be found, current layer, etc, etc).  I hope someone can glean something from my old code.
Code - Auto/Visual Lisp: [Select]
  1. (defun process-on-drawing ( process )
  2.   ;; a simple `hook' to run a process on a saved drawing only
  3.   (if (= (getvar 'DWGTITLED) 1)
  4.     (eval process)
  5.     "Drawing not saved") )
  6.  
  7. (defun get-value ( val lst )
  8.   ;; shortcut for returning value in assoc list
  9.        (cdr (assoc val lst)) )
  10.  
  11. (defun build-name-list ( lst )
  12.   ;; given a list of raw dic entries this proced
  13.   ;; will report entity names
  14.          (if (null lst)
  15.            nil
  16.            (cons (cdr (assoc 2 (car lst)))
  17.                 (build-name-list (cdr lst)))) )
  18.  
  19. (defun get-drawing-dictionary-list ( what / x lst)
  20.   ;; retrieve a list of drawing dictionary entries
  21.        (while (setq x (tblnext what (not x)))
  22.           (setq lst (cons x lst))) )
  23.  
  24. (defun strParse (aStr delim / strList pos)
  25.   (while
  26.     (setq pos (vl-string-search delim aStr 0))
  27.     ;; Find the postition where the delimiter first shows up.
  28.     (setq strList (cons (substr aStr 1 POS) strList)
  29.     ;; create a list of the fist substring (up untill the delimiter)
  30.           strList (cons (substr aStr (1+ POS) 1) strList)
  31.           aStr (substr aStr (+ pos 2)))
  32.     ;; Skip over the delimiter and grab the rest of the string,
  33.     ;; Set that as the new string
  34.     )
  35.   (reverse (cons aStr strList))
  36.  )
  37.  
  38. (defun get-list-from-file ( name / fp lst read-file-into-list )
  39.   ;; general file reader
  40.   ;; given a file name this procedure will read the contents
  41.   ;; into a list
  42.          (defun read-file-into-list ( str file )
  43.            (if str
  44.              (cons
  45.                (if (not (wcmatch str "*;*")) str "")
  46.                ;; get string from file; if it is a comment, return an empty string.
  47.                (read-file-into-list (read-line file) file))))
  48.   (setq fp (open name "R"))
  49.   (setq lst (read-file-into-list (read-line fp) fp))
  50.   (close fp)
  51.  lst
  52. )
  53.  
  54. (defun lt-find ( x )
  55.   (if (member lt (car (cdr x)))
  56.     (progn
  57.       (command "_.linetype" "_load" lt (car x) "")
  58.       (setq dwg-ltype-name-list
  59.             ;; remake the drawing ltype list
  60.             (build-name-list
  61.               (process-on-drawing
  62.                 '(get-drawing-dictionary-list "LTYPE"))))
  63.       );_ end progn
  64.     );_ end if
  65.   )
  66.  
  67.  
  68. ;;; setup...
  69.  
  70.   ltype-file-locations
  71.   (apply
  72.     ;; itterate thru the entire search path to look for .lin files
  73.     'append
  74.     (mapcar
  75.       '(lambda ( x / tmp-str)
  76.          (setq tmp-str (vl-directory-files x "*.lin" 1))
  77.          (if (and tmp-str (not (eq ";" x)))
  78.            (list
  79.              (strcat
  80.                x
  81.                "\\"
  82.                (car tmp-str)))))
  83.       (strparse (getvar "ACADPREFIX") ";") ))
  84.   ltype-locations
  85.   ;; a list of lists for the lietypes and their locations
  86.   ;; ( <path>+<file> ( <ltype> <ltype> ... ))
  87.   (mapcar
  88.     '(lambda ( x / f lt-list)
  89.        (setq lt-list
  90.              (mapcar
  91.                '(lambda ( x )
  92.                   (if x
  93.                     (substr (car (strparse x ",")) 2)))
  94.                (get-list-from-file x)
  95.                )
  96.              )
  97.        (cons x (list lt-list))
  98.        )
  99.     ltype-file-locations
  100.     )
  101.   )
  102.  
  103. ;;; more setup...(creating lists).
  104.  
  105.   '(lambda (x / lt plot)
  106.      ;; create the layers
  107.      (if (not (null x))
  108.        ;; do not opperate on null entries.
  109.        (progn
  110.          (setq lt (get-value 6 x)
  111.                lt
  112.                (cond
  113.                  ((member lt dwg-ltype-name-list)
  114.                   (cons 6 lt))
  115.                  ((and
  116.                     (not (tblsearch "LTYPE" (get-value 6 x)))
  117.                     (not (member (get-value 6 x) dwg-ltype-name-list)))
  118.                   (mapcar
  119.                     ;; itterate thru the ltype file list to see which
  120.                     ;; file contains the missing linetype, then load it
  121.                     'lt-find        
  122.                     ltype-locations )
  123.                   (if (not (tblsearch "LTYPE" lt))
  124.                     ;; another search level incase we still coulnt find lt.
  125.                     (progn
  126.                       (mapcar
  127.                         'princ
  128.                         (list
  129.                           "\nLinetype for Layer: "
  130.                           (get-value 2 x)
  131.                           " Not found, using continuous instead."))
  132.                       (cons 6 "Continuous")
  133.                       )
  134.                     (cons 6 (get-value 6 x))
  135.                     );_ end if
  136.                   )
  137.                  );_ end cond
  138.                plot
  139.                (cond
  140.                  ((get-value 290 x)
  141.                   (cons 290 (get-value 290 x)))
  142.                  (t (cons 290 1))));_ end setq
  143.          (entmake (list
  144.                     '(0 . "LAYER")
  145.                     '(100 . "AcDbSymbolTableRecord")
  146.                     '(100 . "AcDbLayerTableRecord")
  147.                     (cons 2 (get-value 2 x))
  148.                     (cons 70 (get-value 70 x))
  149.                     (cons 62 (get-value 62 x))
  150.                     plot
  151.                     lt
  152.                     );_ end list
  153.                   );_ end entmake
  154.          )
  155.        )
  156.      )
  157.   layers-to-make-list
  158.   )
  159.  
  160.  
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

jlogan02

  • Bull Frog
  • Posts: 327
Re: _add layer defun fails...missing something
« Reply #3 on: May 13, 2021, 02:00:30 PM »
Thanks to you both.

Can't recall who showed me that _addlayer defun, but it's worked as posted in several routines. I assume the difference is combining it with the _ltypeld routine.

Thanks again
J. Logan
ACAD 2018

I am one with the Force and the Force is with me.
AutoCAD Map 2018 Windows 10

ronjonp

  • Needs a day job
  • Posts: 7526
Re: _add layer defun fails...missing something
« Reply #4 on: May 13, 2021, 04:40:29 PM »
Could also simplify that conditional to an if statement.. a bit more readable:
Code - Auto/Visual Lisp: [Select]
  1. (if (tblobjname "ltype" ltype)
  2.   ltype
  3.   "CONTINUOUS"
  4. )
  5. ;; Something I use to load linetypes
  6. (defun _loadlt (/ ex v va)
  7.   (setq va (mapcar 'getvar (setq v '(cmdecho expert))))
  8.   (mapcar 'setvar v '(0 5))
  9.   (foreach lt (if (= 0 (getvar 'measurement))
  10.                 '("acad.lin" "ipco.lin")
  11.                 '("acadiso.lin" "ipco.lin")
  12.               )
  13.     (and (findfile lt) (command "-linetype" "L" "*" lt ""))
  14.   )
  15.   (mapcar 'setvar v va)
  16.   (princ)
  17. )
  18.  
« Last Edit: May 13, 2021, 04:48:07 PM by ronjonp »

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC