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:
arvore | 100 | 100 | 8 | 8 | 0 | AA |
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
)
Replace
(strcat x "," y)
with
(list x y)
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
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
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
)
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.")
)
)
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))
)
)
)
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.