Author Topic: Ignore stringp nil error and continue program - output line lengths to excel  (Read 234 times)

0 Members and 1 Guest are viewing this topic.

Methodman

  • Mosquito
  • Posts: 2
I have a routine for generating an excel list of pipe lengths for each pipe diameter. Pipe lengths are plines, arcs etc. and I differentiate pipe diameters by putting them in different layers.

I started with a code found here: https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/export-to-excel-table-all-objects-from-specific-layer/td-p/8337483

Originally, the code searched for a predefined linestyle and gave a length output.

I have adjusted to search for predefined layers and give a length output. This works ok.

Issue is as follows:

- The layer list in the code results in the following error if it can't find *all* of the layers in the drawing: Command: TEST ; error: bad argument type: stringp nil

I understand the code is searching for all of the layers, and when even 1 of the layers doesn't exist in the drawing (ie. nil), it crashes the program and refuses to continue.

How can I tell it to ignore this error and continue the program despite this? Most of my drawings will only have 1 or 2 pipe diameters, rather than the list of 20 or so I will list in the program.

Thanks,

MM

Code: [Select]
  (defun GetCurveLength (ent /)
    (setq ent (vlax-ename->vla-object ent))
    (vlax-curve-getDistAtParam
      ent
      (vlax-curve-getEndParam ent)
    )
  )

  (setq lineList '(
   "PIPE LENGTHS - PVC-100 DW"
   "PIPE LENGTHS - PVC-150 DW"
   "PIPE LENGTHS - PVC-225 DW"
  )
  )

  (setq lineList
(mapcar
   '(lambda (layer)
      (if (setq
    ss (ssget "_X"
      ;(list '(0 . "*LINE") (cons 6 lineStyle))
  (list '(0 . "LWPOLYLINE") (cons 8 layer))
       )
  )
(progn
  (setq index 0
totalObj 0
  )
  (repeat (sslength ss)
    (setq totalObj
   (+ totalObj (GetCurveLength (ssname ss index)))
    )
    (setq index (1+ index))
  )
  (list layer (rtos totalObj 2))
  ;(list linestyle (rtos totalObj 2))
)
      )
    )
   lineList
)
  )

  (setq lineList (LM:insertnth
   (list "Line List" "------------------")
   0
   lineList
)
  )

  (setq bom lineList)
« Last Edit: February 13, 2020, 06:02:31 AM by Methodman »

ronjonp

  • Needs a day job
  • Posts: 7104
If you stick with using mapcar try this (vl-remove 'nil )
Code - Auto/Visual Lisp: [Select]
  1.   linelist (vl-remove
  2.              'nil
  3.              (mapcar '(lambda (layer)
  4.                         (if (setq ss (ssget "_X" ;(list '(0 . "*LINE") (cons 6 lineStyle))
  5.                                             (list '(0 . "LWPOLYLINE") (cons 8 layer))
  6.                                      )
  7.                             )
  8.                           (progn (setq index 0
  9.                                        totalobj 0
  10.                                  )
  11.                                  (repeat (sslength ss)
  12.                                    (setq totalobj (+ totalobj (getcurvelength (ssname ss index))))
  13.                                    (setq index (1+ index))
  14.                                  )
  15.                                  (list layer (rtos totalobj 2)) ;(list linestyle (rtos totalObj 2))
  16.                           )
  17.                         )
  18.                       )
  19.                      linelist
  20.              )
  21.            )
  22. )

Windows 10 x64 - AutoCAD /C3D 2020

Custom Build PC

kpblc

  • Bull Frog
  • Posts: 346
Another one code:
Code - Auto/Visual Lisp: [Select]
  1. (defun t1 (/ linelist selset layer)
  2.   (if (and (setq linelist '("PIPE LENGTHS - PVC-100 DW" "PIPE LENGTHS - PVC-150 DW" "PIPE LENGTHS - PVC-225 DW"))
  3.            (setq selset (ssget "_X"
  4.                                (list (cons 0 "LWPOLYLINE")
  5.                                      (cons 8
  6.                                            (strcat (car linelist)
  7.                                                    (apply (function strcat) (mapcar (function (lambda (x) (strcat "," x))) (cdr linelist)))
  8.                                                    ) ;_ end of strcat
  9.                                            ) ;_ end of cons
  10.                                      ) ;_ end of list
  11.                                ) ;_ end of ssget
  12.                  ) ;_ end of setq
  13.            (setq linelist (mapcar (function list) linelist)
  14.                  selset   ((lambda (/ tab item)
  15.                              (repeat (setq tab  nil
  16.                                            item (sslength selset)
  17.                                            ) ;_ end setq
  18.                                (setq tab (cons (ssname selset (setq item (1- item))) tab))
  19.                                ) ;_ end of repeat
  20.                              ) ;_ end of lambda
  21.                            )
  22.                  ) ;_ end of setq
  23.            ) ;_ end of and
  24.     (progn (foreach ent selset
  25.              (setq layer (cdr (assoc 8 (entget ent))))
  26.              (setq linelist (subst (cons layer
  27.                                          (+ (vlax-curve-getdistatparam ent (vlax-curve-getendparam ent))
  28.                                             (cond ((cdr (assoc layer linelist)))
  29.                                                   (t 0.)
  30.                                                   ) ;_ end of cond
  31.                                             ) ;_ end of +
  32.                                          ) ;_ end of cons
  33.                                    (assoc layer linelist)
  34.                                    linelist
  35.                                    ) ;_ end of subst
  36.                    ) ;_ end of setq
  37.              ) ;_ end of foreach
  38.            (princ
  39.              (apply (function strcat)
  40.                     (mapcar (function (lambda (x) (strcat "\n" (strcat (car x) " - " (rtos (cdr x) 2 4))))) linelist)
  41.                     ) ;_ end of apply
  42.              ) ;_ end of princ
  43.            ) ;_ end of progn
  44.     ) ;_ end of if
  45.   ) ;_ end of defun
Sorry for my English.

Methodman

  • Mosquito
  • Posts: 2
I tried the first suggestion by ronjonp and it worked great!

Thank you very much for the help and the fast response. Perfect.

ronjonp

  • Needs a day job
  • Posts: 7104
I tried the first suggestion by ronjonp and it worked great!

Thank you very much for the help and the fast response. Perfect.
Glad to help.  :-)

Windows 10 x64 - AutoCAD /C3D 2020

Custom Build PC