Community
Civil 3D Customization
Welcome to Autodesk’s AutoCAD Civil 3D Forums. Share your knowledge, ask questions, and explore popular AutoCAD Civil 3D Customization topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

lisp read csv, with values ​​and load into cv3d

11 REPLIES 11
Reply
Message 1 of 12
Dambrowski
649 Views, 11 Replies

lisp read csv, with values ​​and load into cv3d

I'm trying to put together a lisp to read a csv file, or it could be txt that contains the name of the block, x and y coordinates, size and angle of the block and a label.

But there is something wrong with the code, can anyone help me fix it???

 

value in csv file:

arvore100100880AA

 

lisp until now, in need of correction

 

(defun c:arvore ()
(setq file_path "C:/Caminho/Para/Seu/Arquivo.csv") ; Defina o caminho do seu arquivo CSV

(setq file (open file_path "r")) ; Abre o arquivo para leitura
(while (setq line (read-line file)) ; Lê cada linha do arquivo
(setq fields (vl-string-split line ",")) ; Divide a linha em campos usando a vírgula como delimitador
(setq block_name (nth 0 fields)) ; Obtém o nome do bloco da primeira coluna
(setq x (nth 1 fields)) ; Obtém a coordenada X da segunda coluna
(setq y (nth 2 fields)) ; Obtém a coordenada Y da terceira coluna
(setq scale_x (nth 3 fields)) ; Obtém a escala X da quarta coluna
(setq scale_y (nth 4 fields)) ; Obtém a escala Y da quinta coluna
(setq rotation (nth 5 fields)) ; Obtém o ângulo de rotação da sexta coluna
(setq label (nth 6 fields)) ; Obtém o rotulo da setima coluna

; Insere o bloco e o rotulo com base nos valores lidos do arquivo
(command "INSERT" block_name (strcat x "," y) scale_x scale_y rotation)
(command "TEXT" (strcat x "," y) scale_x scale_y (nth 6 fields)) ;
)
(close file) ; Fecha o arquivo após a leitura
(princ) ; Encerra a rotina
)

Tags (1)
Labels (2)
11 REPLIES 11
Message 2 of 12
tcorey
in reply to: Dambrowski

Replace

(strcat x "," y)

with

(list x y)

 



Tim Corey
MicroCAD Training and Consulting, Inc.
Redding, CA
Autodesk Gold Reseller

New knowledge is the most valuable commodity on earth. -- Kurt Vonnegut
Message 3 of 12
Dambrowski
in reply to: Dambrowski

I get this error;

 

Command: ARVORE
; error: bad argument type: FILE nil

 

I'm going to put my files here, maybe my csv is badly formatted

Message 4 of 12
hippe013
in reply to: Dambrowski

Your CSV file is separated by semicolons not commas. 

 

***EDIT***

And your file path does not appear to be formatted correctly.

 

See help file for the "open" function.

 

AutoCAD 2024 Developer and ObjectARX Help | open (AutoLISP) | Autodesk

 

Message 5 of 12
Jeff_M
in reply to: Dambrowski

And the file_path returns this: "C:UUsersCAD2Downloadsatt.csv" 

Change the string to either use 2 \\ or one /

(setq file_path "C:/Users/CAD2/Downloads/att.csv")

 

Jeff_M, also a frequent Swamper
EESignature
Message 6 of 12
Dambrowski
in reply to: Dambrowski

I corrected the path, now it returns another error:

Command: ARVORE
; error: no function definition: VL-STRING-SPLIT

 

 

(defun c:arvore ()
(setq file_path "C:/Users/CAD2/Downloads/att.csv") ; Defina o caminho do seu arquivo CSV

(setq file (open file_path "r")) ; Abre o arquivo para leitura
(while (setq line (read-line file)) ; Lê cada linha do arquivo
(setq fields (vl-string-split line ";")) ; Divide a linha em campos usando a vírgula como delimitador
(setq block_name (nth 0 fields)) ; Obtém o nome do bloco da primeira coluna
(setq x (nth 1 fields)) ; Obtém a coordenada X da segunda coluna
(setq y (nth 2 fields)) ; Obtém a coordenada Y da terceira coluna
(setq scale_x (nth 3 fields)) ; Obtém a escala X da quarta coluna
(setq scale_y (nth 4 fields)) ; Obtém a escala Y da quinta coluna
(setq rotation (nth 5 fields)) ; Obtém o ângulo de rotação da sexta coluna
(setq label (nth 6 fields)) ; Obtém o rotulo da setima coluna

; Insere o bloco e o rotulo com base nos valores lidos do arquivo
(command "INSERT" block_name (strcat x "," y) scale_x scale_y rotation)
(command "TEXT" (strcat x "," y) scale_x scale_y (nth 6 fields)) ;
)
(close file) ; Fecha o arquivo após a leitura
(princ) ; Encerra a rotina
)

Message 7 of 12
Jeff_M
in reply to: Dambrowski

Where did you get the lisp file from? It likely had that function, either included or as a stand alone lisp. It is not a standard vl-* function

Jeff_M, also a frequent Swamper
EESignature
Message 8 of 12
Dambrowski
in reply to: Jeff_M

from the internet, maybe I don't have access to civild3, would there be another way to load the file in additional variables and in the ..insert and text commands?
Message 9 of 12
Dambrowski
in reply to: Dambrowski

This lisp is exactly what I need, but I wanted to change the values... that were read from a .txt file

(defun c:arvore () (command
"insert" "arvore" "100,100" "1" "1" "0"
"text" "100,100" "5" "0" "A1"
"insert" "arvore" "200,200" "2" "2" "0"
"text" "200,200" "5" "0" "A2"
"insert" "arvore" "300,300" "5" "5" "0"
"text" "300,300" "5" "0" "A3"
))

use the civil 3d 2018
Message 10 of 12
Dambrowski
in reply to: Dambrowski

what I got, with this error:

 

Command: ARVORE
; error: too many arguments

 

(defun c:arvore ()
(setq file_path "C:/Users/CAD2/Downloads/att.txt") ; Substitua pelo caminho do seu arquivo .txt
(setq file (open file_path "r"))

(if file
(progn
(while (setq line (read-line file))
(setq data (vl-string->list line ";")) ; Alterado para usar ";" como separador
(if (= (car data) "arvore") ; Verifica se o comando é "arvore"
(progn
(setq params (vl-string->list (nth 1 data) ",")) ; Separa os parâmetros por ","
(setq x_coord (nth 1 params))
(setq y_coord (nth 2 params))
(setq scale_x (nth 3 params))
(setq scale_y (nth 4 params))
(setq angle (nth 5 params))
(setq raw (nth 6 params))
(command "insert" raw x_coord y_coord scale_x scale_y angle) ; Corrigido para usar o valor correto do bloco
)
)
)
(close file)
)
(alert "Falha ao abrir o arquivo.")
)
)

 

 

Message 11 of 12
hosneyalaa
in reply to: Dambrowski

hi @Dambrowski 

try

(defun c:test (/	BLOCK_NAME	  DATA	   F	    FIELDS
	       FILEPATH	I	 LABEL	  LN	   ROTATION SCALE_X
	       SCALE_Y	X	 Y
	      )
  (vl-load-com)
  (if (setq FilePath (getfiled "Select CSV file to read :"
			       (getvar "dwgprefix")
			       "CSV"
			       4
		     )
      )
    (progn

      (if filepath
	(progn
	  (setq f (open FilePath "r"))
	  (while (setq ln (read-line f))
	    (setq data (cons (str2list ln ";") data))
	  )
	  (close f)
	  (setq data (reverse data))
	  (setq i -1)


	  (while (setq fields (nth (setq i (1+ i)) data))
					; Lê cada linha do arquivo

	    (setq block_name (nth 0 fields))
					; Obtém o nome do bloco da primeira coluna
	    (setq x (nth 1 fields))	; Obtém a coordenada X da segunda coluna
	    (setq y (nth 2 fields))	; Obtém a coordenada Y da terceira coluna
	    (setq scale_x (nth 3 fields))
					; Obtém a escala X da quarta coluna
	    (setq scale_y (nth 4 fields))
					; Obtém a escala Y da quinta coluna
	    (setq rotation (nth 5 fields))
					; Obtém o ângulo de rotação da sexta coluna
	    (setq label (nth 6 fields))	; Obtém o rotulo da setima coluna

					; Insere o bloco e o rotulo com base nos valores lidos do arquivo
	    (command "INSERT"
		     block_name
		     (list x y)
		     scale_x
		     scale_y
		     rotation
	    )
	    (command "TEXT" (list x y) scale_x scale_y (nth 6 fields))
					;
	  )





	)
      )
    )
  )
)
					; ;     https://forums.autodesk.com/t5/civil-3d-customization/creating-profile-from-file/td-p/8187626
;;;str2list by John Uhden, as posted to the adesk customization newsgroup a long time ago
(defun Str2List	(str pat / i j n lst)
  (cond
    ((/= (type str) (type pat) 'STR))
    ((= str pat) '(""))
    (T
     (setq i 0
	   n (strlen pat)
     )
     (while (setq j (vl-string-search pat str i))
       (setq lst (cons (substr str (1+ i) (- j i)) lst)
	     i	 (+ j n)
       )
     )
     (reverse (cons (substr str (1+ i)) lst))
    )
  )
)

 

Message 12 of 12
Dambrowski
in reply to: Dambrowski

I created the "arvore" block and started lisp and got these messages:

 

command: TEST
INSERT Enter block name or [?] <ARVORE>: arvore,100,100,18,18,0,AAA Warning: If you are trying to insert the file: arvore,100,100,18,18,0,AAA
it must be inserted using the <block>=<filename> syntax.
Command:
Command:
Command:
Command:
Command: TEXT
Current text style: "Standard" Text height: 0.200 Annotative: No Justify: Left
Specify start point of text or [Justify/Style]:
Command:
Command:
Command:
Command: nil

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Rail Community


 

Autodesk Design & Make Report