Jump to content

Getting info from strings


saim

Recommended Posts

Hello again!
I mannaged to read some strings from a text file and place them on a list. When I tell cad to print them on screen, I get the following result:

Quote

("PV1;2;2" "PV2;6;6" "PV3;8;10")

That's what I expected, so I'm fine, so far.

 

Now I want to get those strings and convert them in information to draw something. Like a polyline from point (2,2) to (6,6) and to (8,10).

In other words: how do I get CAD to undertand that list as "the point 'PV1' has the x-coordinate equal 2 and the y coordinate equal 2", and so on?

The cue I used to change from one info to another is the ";" (semocolon) - is that ok or is there some kind of default?

 

...

I may need to make this issue a bit more complex, but I need that milestone to go further...

 

BTW, in case it matters how I got that line, I used the following code and the following text file contents:
 


(defun c:desenha( / dflt fname fl fil plt1)
	(setq dflt (strcat (getvar 'dwgprefix) (substr (getvar 'dwgname) 1 (- (strlen (getvar 'dwgname)) 4)) )) ; dá um nome default pro arquivo
	(setq fname (getfiled "esse é o título da cx de diálogo" dflt "txt" 8)) ; usa a função getfiled pra procurar o arquivo
	(setq fl (open fname "r"))
	(while (setq fil (read-line fl))
		(if (not (null fil))
			(setq plt1 (append plt1 (list fil)))
		) ;end if
		) ;end while
	(close fl)
	
	(print plt1)
	(princ)
)
Quote

PV1;2;2
PV2;6;6
PV3;8;10
 

 

Link to comment
Share on other sites

Hi,

An example of how to convert a list of strings to a list of coordinates based on your list if your list is always with the same form.

(foreach str '("PV1;2;2" "PV2;6;6" "PV3;8;10")
  (setq lst (cons (read (strcat "(" (vl-string-translate ";" "," (substr str 5)) ")")) lst))
  )

You need to reverse the list afterwards.

 

  • Like 1
Link to comment
Share on other sites

You could use a function such as my String to List function to convert the string to a list using a supplied token, for example:

(defun c:desenha2 ( / des lst str txt )
    (if (setq txt (getfiled "Esse é o título da cx de diálogo" (strcat (getvar 'dwgprefix) (vl-filename-base (getvar 'dwgname))) "txt" 8))
        (if (setq des (open txt "r"))
            (progn
                (while (setq str (read-line des))
                    (setq lst (cons (LM:str->lst str ";") lst))
                )
                (close des)

                (if
                    (setq lst
                        (vl-remove nil
                            (mapcar
                                (function
                                    (lambda ( i / n x y )
                                        (if
                                            (and
                                                (wcmatch (car i) "PV*")
                                                (setq n (atoi (substr (car i) 3)))
                                                (setq x (distof (cadr  i) 2))
                                                (setq y (distof (caddr i) 2))
                                            )
                                            (list n 10 x y)
                                        )
                                    )
                                )
                                lst
                            )
                        )
                    )
                    (entmake
                        (append
                            (list
                               '(000 . "LWPOLYLINE")
                               '(100 . "AcDbEntity")
                               '(100 . "AcDbPolyline")
                                (cons 90 (length lst))
                               '(070 . 0)
                            )
                            (mapcar 'cdr (vl-sort lst '(lambda ( a b ) (< (car a) (car b)))))
                        )
                    )
                )
            )
            (princ "\nUnable to open file for reading.")
        )
        (princ "\n*Cancel*")
    )
    (princ)
)

;; 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
 
(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)
    )
)

(princ)

 

Edited by Lee Mac
  • Like 1
Link to comment
Share on other sites

oh darn , too late again... awell...


(defun SplitStr ( s d / p ) (if (setq p (vl-string-search d s))(cons (substr s 1 p)
  (SplitStr (substr s (+ p 1 (strlen d))) d)) (list s)))

(defun istext (s)(if (= (type s) 'str) t nil))

(defun plc (p l c)(entmakex (append (list '(0 . "LWPOLYLINE") '(100 . "AcDbEntity")
  '(100 . "AcDbPolyline")(cons 8 l)(cons 90 (length p))(cons 70 c))(mapcar '(lambda (pt) (cons 10 pt)) p))))

(defun t1 (sl / pl )(and (vl-consp sl)(vl-every 'istext sl)(vl-every '(lambda (x)(wcmatch x "*;*;*")) sl)
   (vl-consp (setq pl (mapcar '(lambda (x)(mapcar 'atoi (cdr (SplitStr x ";")))) sl))) (plc pl "0" 0)))

; test : (t1 '("PV1;2;2" "PV2;6;6" "PV3;8;10"))

  • Like 1
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...