Lisp to get Coordinates and Color of multiple line to csv

Lisp to get Coordinates and Color of multiple line to csv

hythamthelove
Advocate Advocate
733 Views
6 Replies
Message 1 of 7

Lisp to get Coordinates and Color of multiple line to csv

hythamthelove
Advocate
Advocate

Hello everyone, 

I want a lisp that enables me to select multiple lines and then export coordinates of these lines and the lines color to csv file, so the cells in csv file can be like that:

hythamthelove_0-1654185759573.png

Can anyone help with that ?

Thanks in Advance

0 Likes
734 Views
6 Replies
Replies (6)
Message 2 of 7

Kent1Cooper
Consultant
Consultant

Most of that should be doable easily enough, but the color numbers with commas in them may be a problem, since a comma is the next-cell trigger.  I tried something with the ` and \ escape characters that work to read control characters literally in some other situations, and I tried putting (chr 44) in place of commas, but none of those got around the issue -- the things I wanted strung together with commas between in one cell ended up in separate cells when I opened the file in Excel.  I don't deal with writing to .csv files enough to know, but maybe someone out there knows of a way to force mid-string commas that don't trigger moving over to a new cell.

EDIT:  Found it.  I forced that kind of content in the .csv file, then read out in AutoLisp with (read-line) to see how it's formatted.  It's controlling the quotation marks with a "nested" pair around what you want to include commas within the single cell -- the format of the line written to the file by (write-line) would be:

 

"353560.1,26325,353560.1,51149.99,\"221,3,3\""

Kent Cooper, AIA
0 Likes
Message 3 of 7

CADaSchtroumpf
Advisor
Advisor

This ?

(defun LM:True->RGB ( c )
  (mapcar '(lambda ( x ) (lsh (lsh (fix c) x) -24)) '(8 16 24))
)
(defun c:ptdefLINE2CSV ( / js dxf_cod mod_sel n lremov str_sep oldim ename X1 Y1 X2 Y2 col tmp f_open)
  (princ "\nSelect a model object to make filter: ")
  (while
    (null
      (setq js
        (ssget "_+.:E:S"
          (list
            '(0 . "LINE")
            (cons 67 (if (eq (getvar "CVPORT") 1) 1 0))
            (cons 410 (if (eq (getvar "CVPORT") 1) (getvar "CTAB") "Model"))
          )
        )
      )
    )
    (princ "\nIsn't a Line!")
  )
  (vl-load-com)
  (setq dxf_cod (entget (ssname js 0)))
  (foreach m (foreach n dxf_cod (if (not (member (car n) '(0 67 410 8 6 48))) (setq lremov (cons (car n) lremov))))
    (setq dxf_cod (vl-remove (assoc m dxf_cod) dxf_cod))
  )
  (initget "Single All Manual")
  (if (eq (setq mod_sel (getkword "\nApply filter to ? [Single/All/Manual]<Manual>: ")) "Single")
    (setq n -1)
    (if (eq mod_sel "All")
        (setq js (ssget "_X" dxf_cod) n -1)
        (setq js (ssget dxf_cod) n -1)
    )
  )
  (setq
    tmp (vl-filename-mktemp "tmp.csv")
    f_open (open tmp "w")
    str_sep ";"
    oldim (getvar "dimzin")
  )
  (setvar "dimzin" 0)
  (write-line (strcat "X1" str_sep "Y1" str_sep "X2" str_sep "Y2" str_sep "Color") f_open)
  (repeat (sslength js)
    (setq
      ename (ssname js (setq n (1+ n)))
      dxf_cod (entget ename)
      X1 (cadr (assoc 10 dxf_cod))
      Y1 (caddr (assoc 10 dxf_cod))
      X2 (cadr (assoc 11 dxf_cod))
      Y2 (caddr (assoc 11 dxf_cod))
    )
    (write-line
      (strcat
        (rtos X1 2 3)
        str_sep
        (rtos Y1 2 3)
        str_sep
        (rtos X2 2 3)
        str_sep
        (rtos Y2 2 3)
        str_sep
        (if (assoc 420 dxf_cod)
          (progn
            (setq col (LM:True->RGB (cdr (assoc 420 dxf_cod))))
            (strcat (itoa (car col)) "," (itoa (cadr col)) "," (itoa (caddr col)))
          )
          (itoa (if (assoc 62 dxf_cod) (cdr (assoc 62 dxf_cod)) 256))
        )
      )
      f_open
    )
  )
  (close f_open)
  (startapp "notepad" tmp)
  (setvar "dimzin" oldim)
  (prin1)
)
0 Likes
Message 4 of 7

hythamthelove
Advocate
Advocate

Thank you for your effort, would you please share the lisp file ?

0 Likes
Message 5 of 7

hythamthelove
Advocate
Advocate

That works good actually, but it makes me select 1 line only and i don't know the result gives several lines with several color numbers, can there be something enables me to select all the lines at once ?

0 Likes
Message 6 of 7

CADaSchtroumpf
Advisor
Advisor

@hythamthelove 

You can apply the filter of the selected entity to the single selected object, to all the entities corresponding to the filter or to the manual selection (window, crossing, fence, etc...) also corresponding to the filtered.

 

If the filter seems too restrictive to you because it does not return all the expected objects, you can modify on the line 22 in the code the list of DXF codes to keep:
The least restrictive would be to change the list '(0 67 410 8 6 48) to '(0 67 410)
8 being for the layer name.
6 being for linetype.
48 being for the linetype scale forced to the entitie.

 

If you want to import the CSV into Excel, you will have to choose ';' (semi-colon) as a column separator.
Also pay attention to the configuration of the decimal separator in windows depending on the country, for me it is the '.' but it can be the ','

Message 7 of 7

Kent1Cooper
Consultant
Consultant

@hythamthelove wrote:

Thank you for your effort, would you please share the lisp file ?


[I don't have one.  I was only trying to get as far as figuring out whether it's possible for a single cell in a comma-separated file to contain a text string with commas within it.  I could try incorporating that into something, if what's been suggested doesn't do it for you.]

Kent Cooper, AIA
0 Likes