Review my code pls

Review my code pls

t_czajkowskiT3PND
Explorer Explorer
855 Views
6 Replies
Message 1 of 7

Review my code pls

t_czajkowskiT3PND
Explorer
Explorer

Hello, 

So, I'm new to code as a whole and decided to start with LISP, learning alone by using youtube and chatGPT helped me on simpler stuff but I found a problem that I still don't know how to fix and since I'm not familiar with the terminology I can't even ask questions the proper way.

The issue here is; the code is interpreting the txt variable as something that idk what it is and, for that reason, just breaks, the placing of the block works fine, if I switch the txt variable for actual text it also work, could someone enlighten me on this? 

 

PS.: Its crude work, I know, still don't understand half the things I've written, just trying my best to not have to do it by hand.

0 Likes
Accepted solutions (2)
856 Views
6 Replies
Replies (6)
Message 2 of 7

Kent1Cooper
Consultant
Consultant

A guess [untested]:

Help for (read) says it returns only the first thing, and a string should not contain spaces.  [But it gives you the numbers as numbers.]  You will need to construct the list differently.  And if you do that other than with (read), make sure the coordinates parts, which start as text strings, are converted to numbers with (atof).

Kent Cooper, AIA
0 Likes
Message 3 of 7

t_czajkowskiT3PND
Explorer
Explorer

I believe its the exact opposite, the XY works fine, the problem is setting the text on the blocks, how can I convert it?

0 Likes
Message 4 of 7

Kent1Cooper
Consultant
Consultant
Accepted solution

See the edited version of my first Reply.  Also, is the Block defined for uniform scaling?  It looks like you have only one scale factor entry in the Insert command.

Further EDIT:  The return from (read) of a text string that is not numerical is not a text string:

(type (read "TESTING"))
SYM

So you could get a variable name out of it, or something.  But for use as an Attribute value, it needs to be a text string, so (read) should not be applied to the original of that part.

Kent Cooper, AIA
0 Likes
Message 5 of 7

ВeekeeCZ
Consultant
Consultant
Accepted solution

Edit: Issue misinterpreted.

 

(defun c:SondaImport (/ LM:str->lst fileName csvFile line coords x y xy txt)
  
  ;; Lee Mac ;; https://www.lee-mac.com/stringtolist.html
  ;; String to List  -  Lee Mac
  ;; Separates a string using a given delimiter
  ;; str - [str] String to process
  ;; del - [str] Delimiter by which to separate the string
  ;; Returns: [lst] List of strings

  ;; this function is challenging to understand, just use it a black box.
  (defun LM:str->lst ( str del / pos )
    (if (setq pos (vl-string-search del str))
      (cons (substr str 1 pos) (LM:str->lst (substr str (+ pos 1 (strlen del))) del))
      (list str)
      )
    )
  
  (setq fileName (getfiled "Select CSV File" "" "csv" 4))
  
  (setvar "ATTDIA" 0)
  (setvar "ATTREQ" 1)
  (if (and fileName (setq csvFile (open fileName "r")))
    (while (setq line (read-line csvFile))
      (setq coords (LM:str->lst line ","))   ;; it returns a list of strings '("1.1" "2.2" "Text")
      (setq x (read (car coords)))           ;; converts a string to real-number  "1.1" to 1.1
      (setq y (read (cadr coords)))	     ;; converts a string to real-number  "1.1" to 1.1	
      (setq txt (caddr coords))
      (setq xy (list x y))
      (command "-INSERT" "SONDAGEM" "_s" 1 "_r" 0 "_non" xy txt)
      ))
  
  (close csvFile)
  (setvar "ATTDIA" 1)
  (princ)
  )

 

0 Likes
Message 6 of 7

t_czajkowskiT3PND
Explorer
Explorer

 Hello again, so, I managed to fix my code before seeing your reply, just had to add the 12º and 13º line so, hell yeah (??) Don't know the limitations of my solution yet.

 

Anyhow,

I found my solution when thinking on how the program would deal with alphanumeric data which is the case and just brute force it, I'm really interested in those subfuntions tho, is there a library of those that I can read what/how it does/work and copy paste to my codes? 

Imma go read the entirety of the Mac Lee site you linked, learning this on my free time should be fun.

 

(defun c:SondaImport (/ fileName csvFile line coords x y xy txt txtAUX)
  (setq fileName (getfiled "Select CSV File" "" "csv" 4))
  (setvar "ATTDIA" 0)
  (if (and fileName (setq csvFile (open fileName "r")))
    (progn
      (while (setq line (read-line csvFile))
        (setq line (vl-string-translate "," " " line))
        (setq coords (read (strcat "(" line ")")))
            (setq x (car coords))
            (setq y (cadr coords))
            (setq txtAUX (caddr coords))
            (setq txt(vl-princ-to-string txtAUX))
            (setq xy (list x y))
            (command "-INSERT" "SONDAGEM" xy 1 0 txt)
          )
        )
      )
      (close csvFile)
    (setvar "ATTDIA" 1)
 (princ)
)

 

 

0 Likes
Message 7 of 7

ВeekeeCZ
Consultant
Consultant

Sorry, it seems like I misinterpreted the issue... 

 

Anyhow, Lee's site is a really great library... but sometimes it could be challenging to understand. Maybe afralisp is a better place to start... https://www.afralisp.net/archive/index.htm