Jump to content

Text from CSV in various layers


Gerard

Recommended Posts

Here's a simple example using my Read CSV function:

(defun c:txtfromcsv ( / csv des hgt ins itm str sty )
    (setq hgt (getvar 'textsize)
          sty (getvar 'textstyle)
    )
    (cond
        (   (not (setq csv (getfiled "Select CSV File" "" "csv" 16)))
            (princ "\n*Cancel*")
        )
        (   (not (setq des (open csv "r")))
            (princ "\nUnable to open CSV file for reading.")
        )
        (   t
            (while (setq str (read-line des))
                (setq itm (LM:csv->lst str "," 0))
                (if (and (< 3 (length itm))
                         (snvalid (car itm))
                         (apply 'and (setq ins (mapcar 'distof (mapcar '(lambda ( a b ) a) (cdr itm) '(0 1 2)))))
                    )
                    (entmake
                        (list
                           '(000 . "TEXT")
                            (cons 010 ins)
                            (cons 007 sty)
                            (cons 040 hgt)
                            (cons 001 (last itm))
                            (cons 008 (car  itm))
                        )
                    )
                )
            )
            (close des)
        )
    )
    (princ)
)

;; CSV -> List  -  Lee Mac
;; Parses a line from a CSV file into a list of cell values.
;; str - [str] string read from CSV file
;; sep - [str] CSV separator token
;; pos - [int] initial position index (always zero)
 
(defun LM:csv->lst ( str sep pos / s )
    (cond
        (   (not (setq pos (vl-string-search sep str pos)))
            (if (wcmatch str "\"*\"")
                (list (LM:csv-replacequotes (substr str 2 (- (strlen str) 2))))
                (list str)
            )
        )
        (   (or (wcmatch (setq s (substr str 1 pos)) "\"*[~\"]")
                (and (wcmatch s "~*[~\"]*") (= 1 (logand 1 pos)))
            )
            (LM:csv->lst str sep (+ pos 2))
        )
        (   (wcmatch s "\"*\"")
            (cons
                (LM:csv-replacequotes (substr str 2 (- pos 2)))
                (LM:csv->lst (substr str (+ pos 2)) sep 0)
            )
        )
        (   (cons s (LM:csv->lst (substr str (+ pos 2)) sep 0)))
    )
)

(defun LM:csv-replacequotes ( str / pos )
    (setq pos 0)
    (while (setq pos (vl-string-search  "\"\"" str pos))
        (setq str (vl-string-subst "\"" "\"\"" str pos)
              pos (1+ pos)
        )
    )
    str
)

(princ)

 

  • Like 1
Link to comment
Share on other sites

Tried to add rotate column before text column and I modified the code at entmake

output ; error: bad DXF group: (50 . "0.000000000000000")

Please help

 

(entmake
                        (list
                           '(000 . "TEXT")
                            (cons 010 ins)
                            (cons 007 sty)
                            (cons 040 hgt)
                            (cons 001 (last itm))
                            (cons 008 (car  itm))
                            (cons 050 (car (cdr (reverse itm))))
                        )
                    )

Link to comment
Share on other sites

The rotation angle will need to be a numerical value expressed in radians, and so assuming that your text file contains a value in degrees, try:

(cons 050 (* pi (/ (atof (cadr (reverse itm))) 180.0)))

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...