Author Topic: Block with attributes from CSV file  (Read 822 times)

0 Members and 1 Guest are viewing this topic.

nekonihonjin

  • Newt
  • Posts: 103
Block with attributes from CSV file
« on: August 09, 2022, 10:29:43 PM »
Hi guys I have this code that uploads information from a CSV file with coordinates and values. I wonder if it would be too complicated to make it, instead of creating points and text, create a block with attributes for each of the columns with values after the coordinates.

Code: [Select]
; thanks to Lee-mac for this defun
; www.lee-mac.com
; 44 is comma 32 is space
(defun _csv->lst ( str / pos )
(if (setq pos (vl-string-position 44 str))
(cons (substr str 1 pos) (_csv->lst (substr str (+ pos 2))))
(list str)
    )
)

(defun C:samples(/ data file enx obj old new)
(setq fo (open (getfiled "Select CSV File" "" "csv" 16) "R"))
(setq lst '())

(while (setq nline (read-line fo))
    (setq lst (cons (_csv->lst nline) lst))
          (setq lst (nth 0 lst))

         (setq ID (nth 0 lst) X (nth 1 lst) Y (nth 2 lst) Z (nth 3 lst) AU (nth 4 lst) AG (nth 5 lst) PB (nth 6 lst) ZN (nth 7 lst) CU (nth 8 lst))
   (setq romp (strcat X "," Y "," Z))
         (setq YAU (- (atof Y) 0.5))
         (setq Y2 (rtos YAU 2 4))
         (setq rompAU (strcat X "," Y2 "," Z))
         (setq YAG (- (atof Y) 1.0))
         (setq Y3 (rtos YAG 2 4))
         (setq rompAG (strcat X "," Y3 "," Z))
         (setq YPB (- (atof Y) 1.5))
         (setq Y4 (rtos YPB 2 4))
         (setq rompPB (strcat X "," Y4 "," Z))
         (setq YZN (- (atof Y) 2.0))
         (setq Y5 (rtos YZN 2 4))
         (setq rompZN (strcat X "," Y5 "," Z))
         (setq YCU (- (atof Y) 2.5))
         (setq Y6 (rtos YCU 2 4))
         (setq rompCU (strcat X "," Y6 "," Z))

         (setq AUtx (strcat "Au " AU))
         (setq AGtx (strcat "Ag " AG))
         (setq PBtx (strcat "Pb " PB))
         (setq ZNtx (strcat "Zn " ZN))
         (setq CUtx (strcat "Cu " CU))

         (command "_point" romp)
         (command "_Text" romp 0.5 0 ID)
         (command "_Text" rompAU 0.3 0 AUtx)
         (command "_Text" rompAG 0.3 0 AGtx)
         (command "_Text" rompPB 0.3 0 PBtx)
         (command "_Text" rompZN 0.3 0 ZNtx)
         (command "_Text" rompCU 0.3 0 CUtx)

)

(princ)
)

I leave the CSV file I am using.

BIGAL

  • Swamp Rat
  • Posts: 1409
  • 40 + years of using Autocad
Re: Block with attributes from CSV file
« Reply #1 on: August 10, 2022, 10:10:49 PM »
If you set attreq 1 for a start this means except attributes, then a simple -insert.

(setvar 'attreq 1)
(command "-insert" bname s 1 pt 0 att1 att2 att3 att4 ....)


Look at your code its almost there.
A man who never made a mistake never made anything

nekonihonjin

  • Newt
  • Posts: 103
Re: Block with attributes from CSV file
« Reply #2 on: August 10, 2022, 10:58:25 PM »

Now it's working, thanks BIGAL!
« Last Edit: August 11, 2022, 03:17:46 PM by nekonihonjin »