'AutoLISP, How to export my selected polylines to a CSV with a name

I have this code below. It exports the selected polylines lenght to a CSV but it does not give it a name so i cant make a difference between two(or more) types of polyline.

My question is how to modify this code in order to be able to export the lenghts with the name of the linetype.

For example: I loaded ZIGZAG and TRACKS linetype, next I run my function and select all of the drawn polylines and I want to see in my CSV that which linetype is how long by name.

 (defun c:Polyline_számoló (/ s i e l fn)
      (if (and(setq s (ssget '((0 . "LWPOLYLINE"))))
          (setq fn (getfiled "Create Output File" "" "csv" 1)))
        (progn
          (setq s (_SortSSByXValue s))
          (setq i (sslength s))
          (while (setq e(ssname s (setq i (1- i))))
        (setq l (cons (vla-get-length (vlax-ename->vla-object e)) l))
        (ssdel e s)
          )
        )
      )
      (setq l (list (cd:CON_All2Str l nil)))
      (if (LM:WriteCSV l fn)
                    (startapp "explorer" fn)
                )
      (princ)
    )
    
    
    (defun cd:CON_All2Str (Lst Mode)
      (mapcar
        (function
          (lambda (%)
            (if Mode
              (vl-prin1-to-string %)
              (vl-princ-to-string %)
            )
          )
        )
        Lst
      )
    )
    
    
    
    (defun _SortSSByXValue (ss / lst i e add)
      (if (eq (type ss) 'PICKSET)
        (progn
          (repeat (setq i (sslength ss))
            (setq lst (cons (cons (setq e (ssname ss (setq i (1- i))))
                                  (cadr (assoc 10 (entget e)))
                            )
                            lst
                      )
            )
          )
          (setq add (ssadd))
          (foreach e (vl-sort lst (function (lambda (a b) (< (cdr a) (cdr b))))) (ssadd (car e) add))
          (if (> (sslength add) 0)
            add
          )
        )
      )
    )
    
    
    (defun LM:writecsv ( lst csv / des sep )
        (if (setq des (open csv "w"))
            (progn
                (setq sep (cond ((vl-registry-read "HKEY_CURRENT_USER\\Control Panel\\International" "sList")) (",")))
                (foreach row lst (write-line (LM:lst->csv row sep) des))
                (close des)
                t
            )
        )
    )
    
    
    
    (defun LM:lst->csv ( lst sep )
        (if (cdr lst)
            (strcat (LM:csv-addquotes (car lst) sep) sep (LM:lst->csv (cdr lst) sep))
            (LM:csv-addquotes (car lst) sep)
        )
    )
    
    
    
    (defun LM:csv-addquotes ( str sep / pos )
        (cond
            (   (wcmatch str (strcat "*[`" sep "\"]*"))
                (setq pos 0)    
                (while (setq pos (vl-string-position 34 str pos))
                    (setq str (vl-string-subst "\"\"" "\"" str pos)
                          pos (+ pos 2)
                    )
                )
                (strcat "\"" str "\"")
            )
            (   str   )
        )
    )


Solution 1:[1]

Here's a lisp function that will export a csv file. The csv file contains two sections:

1.) a length summary by linetype name
2.) an individual line summary with length and linetype

csv example:

--Length Summary By LineType--
LineType,Length
CENTER,739.97
HIDDEN,1858.61
--Length Breakdown By Individual Line--
LineType,Length
CENTER,246.656
HIDDEN,309.768
HIDDEN,309.768
CENTER,246.656
HIDDEN,309.768
HIDDEN,309.768
CENTER,246.656
HIDDEN,309.768
HIDDEN,309.768

Lisp code

;;www.cadwiki.net

(defun c:test (/ s i e l fn CSVSTRING CSVSTRINGLIST DATAITEM individualLineDataList LINELENGTH LINETYPE VLAOBJECT NEWASSOC NEWLENGTH PREVIOUSLENGTH lineTypeToLengthAssoc SUMMARYENTRY
              )
  (if (and (setq s (ssget '((0 . "LWPOLYLINE"))))
           (setq fn (getfiled "Create Output File" "" "csv" 1))
      )
    (progn
      (setq s (_SortSSByXValue s))
      (setq i (sslength s))
      (setq individualLineDataList (list))

      (while (setq e (ssname s (setq i (1- i))))
        (setq vlaObject (vlax-ename->vla-object e))
        (setq lineType (vla-get-linetype vlaObject))
        (setq lineLength (vla-get-length vlaObject))
        (setq dataItem (list lineType lineLength))
        (setq individualLineDataList (cons dataItem individualLineDataList))
        (setq summaryEntry (assoc lineType lineTypeToLengthAssoc))
        (if (/= summaryEntry nil)
          (progn
            (setq previousLength (cdr summaryEntry))
            (setq newLength (+ previousLength lineLength))
            (setq newAssoc (cons lineType newLength))
            (setq lineTypeToLengthAssoc (REMOVE-ASSOC-BY-KEY lineType lineTypeToLengthAssoc))
            (setq lineTypeToLengthAssoc (cons newAssoc lineTypeToLengthAssoc))
          )
          (progn
            (setq newAssoc (cons lineType lineLength))
            (setq lineTypeToLengthAssoc (cons newAssoc lineTypeToLengthAssoc))
          )
        )
        (ssdel e s)
      )
    )
  )
  (setq csvStringList (list (list "--Length Summary By LineType--")))
  (setq csvStringList (cons (list "LineType" "Length") csvStringList))
  (foreach assocItem lineTypeToLengthAssoc
    (setq csvString (summaryAssocToStringList assocItem))
    (setq csvStringList (cons csvString csvStringList))
  )
  (setq csvStringList (cons (list "--Length Breakdown By Individual Line--") csvStringList))
  (setq csvStringList (cons (list "LineType" "Length") csvStringList))
  (foreach item individualLineDataList
    (setq csvString (cd:CON_All2Str item nil))
    (setq csvStringList (cons csvString csvStringList))
  )
  (setq csvStringList (reverse csvStringList))
  (if (LM:WriteCSV csvStringList fn)
    (startapp "explorer" fn)
  )
  (princ)
)

(defun REMOVE-ASSOC-BY-KEY (assocKey assocList / newAssocList item)
  (setq newAssocList nil)
  (foreach item assocList
    (if (not (= (car item) assocKey))
      (setq newAssocList (append newAssocList (list item)))
    )
  )
  newAssocList
)


(defun summaryAssocToStringList (assocItem / LINELENGTH LINETYPE STRINGLIST)
  (setq lineType (car assocItem))
  (setq lineLength (cdr assocItem))
  (setq stringList (list lineType (rtos lineLength 2 2)))
)

(defun cd:CON_All2Str (Lst Mode)
  (mapcar
    (function
      (lambda (%)
        (if Mode
          (vl-prin1-to-string %)
          (vl-princ-to-string %)
        )
      )
    )
    Lst
  )
)



(defun _SortSSByXValue (ss / lst i e add)
  (if (eq (type ss) 'PICKSET)
    (progn
      (repeat (setq i (sslength ss))
        (setq lst (cons (cons (setq e (ssname ss (setq i (1- i))))
                              (cadr (assoc 10 (entget e)))
                        )
                        lst
                  )
        )
      )
      (setq add (ssadd))
      (foreach e (vl-sort lst (function (lambda (a b) (< (cdr a) (cdr b))))) (ssadd (car e) add))
      (if (> (sslength add) 0)
        add
      )
    )
  )
)


(defun LM:writecsv (lst csv / des sep)
  (if (setq des (open csv "w"))
    (progn
      (setq sep (cond ((vl-registry-read "HKEY_CURRENT_USER\\Control Panel\\International" "sList"))
                      (",")
                )
      )
      (foreach row lst (write-line (LM:lst->csv row sep) des))
      (close des)
      t
    )
  )
)



(defun LM:lst->csv (lst sep)
  (if (cdr lst)
    (strcat (LM:csv-addquotes (car lst) sep) sep (LM:lst->csv (cdr lst) sep))
    (LM:csv-addquotes (car lst) sep)
  )
)



(defun LM:csv-addquotes (str sep / pos)
  (cond
    ((wcmatch str (strcat "*[`" sep "\"]*"))
     (setq pos 0)
     (while (setq pos (vl-string-position 34 str pos))
       (setq str (vl-string-subst "\"\"" "\"" str pos)
             pos (+ pos 2)
       )
     )
     (strcat "\"" str "\"")
    )
    (str)
  )
)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1