how to convert string point format to point format

how to convert string point format to point format

ayadmustafa
Participant Participant
1,724 Views
4 Replies
Message 1 of 5

how to convert string point format to point format

ayadmustafa
Participant
Participant

hello everyone

i am a beginner ...i need away to convert string like this "2.5,7.5" to point format that can any command accept it as input point .... sorry if it is a silly question 

0 Likes
1,725 Views
4 Replies
Replies (4)
Message 2 of 5

dbhunia
Advisor
Advisor

I did not get your point exactly .......

 

But to convertstring like this "2.5,7.5" as a point simply use like...

 

(setq P1 "2.5,7.5")

(setq P2 "5.5,10.5")

(command "_line" P1 P2 "")

 

Here P1 & P2 are two points...... And using these two point you can do any thing (Here I am drawing a line)


Debashis Bhunia
Co-Founder of Geometrifying Trigonometry(C)
________________________________________________
Walking is the First step of Running, Technique comes Next....
0 Likes
Message 3 of 5

ВeekeeCZ
Consultant
Consultant

This "conversion" is usually called "parsing". It's a very common task, probably most often needed when you read data from a file.  You can find many examples here on the forum when you search... or some guys might share their own versions... 

HERE is a one, quite a complex one, written by Lee Mac. As a beginner, just take it as a black box... 

BTW it worth to search thru all the page, a lot of useful stuff. Either whole programs, or just subroutines as this one.

0 Likes
Message 4 of 5

john.uhden
Mentor
Mentor

It's not a silly question at all.

   My motto #1:  "You've got to start somewhere."

   My motto #2:  "If you don't know, ask."

Most all of us here love to help beginners because they are trying to learn.

@dbhunia gave you good advice.

From what he responded you can clearly see that AutoCAD can accept AutoLisp coordinate input just as you might enter it from the keyboard...  x,y and even x,y,z as a string.

But if you need to mathematically process the point, then you probably want to convert it to the same format that one would be returned from the getpoint function... (x y z) as a list of reals.

Here's what I use:

  ;; Function to convert a string with delimiters into a list
  ;; pat is the delimiter and can contain multiple characters
   (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))
       )
     )
   )

For example:

Command: (setq a "5.7,7.8")
"5.7,7.8"

Command: (@str2list a ",")
("5.7" "7.8")

Command: (mapcar 'read (@str2list a ","))
(5.7 7.8)

I hope that helps you.

John F. Uhden

0 Likes
Message 5 of 5

_gile
Consultant
Consultant

My 2 cents

;; gc:str2lst
;; Transforme un chaine avec séparateur en liste de chaines
;; Converts a string with separator into a list of strings
;;
;; Arguments
;; str : la chaîne / the string
;; sep : le séparateur / the separator
(defun gc:str2lst (str sep / len lst)
  (setq len (strlen sep))
  (while (setq pos (vl-string-search sep str))
    (setq lst (cons (substr str 1 pos) lst)
	  str (substr str (+ len pos 1))
    )
  )
  (reverse (cons (substr str 1 pos) lst))
)

;; gc:str2pt
;; Transforme une chaine en point 3d (liste de nombres réels)
;; Converts a string into a point (list of real numbers)
;;
;; Argument
;; str : la chaîne / the string
(defun gc:str2pt (str)
  (setq str (mapcar 'read (gc:str2lst str ",")))
  (if (and (vl-every 'numberp str)
	   (< 1 (length str) 4)
      )
    (trans str 0 0)
  )
)


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes