Author Topic: string list to real list  (Read 2486 times)

0 Members and 1 Guest are viewing this topic.

Robert98

  • Guest
string list to real list
« on: January 18, 2014, 02:09:57 PM »
Hi
I try to remember some before advices about list manipulating , so I got a list from notepad and it comes to command line in this format :
("500.00 500.00 " "532.75 481.74 "
"532.75 481.74 "  "565.50 463.47")
 I want to become it free of   ' " '  , so when I use this code :
Code: [Select]
   (mapcar 'Read (reverse lst))

But I'll see a list like this one :
 (500.00 532.75 )
 Now I want joint them to each other ("500.00 500.00 " be converted to "500.00500.00") and then separarate
 them by substr function , ("500.00500.00" become "500.00" "500")
 Is it possible or I have to find better solution ?
 

Lee Mac

  • Seagull
  • Posts: 12915
  • London, England
Re: string list to real list
« Reply #1 on: January 18, 2014, 02:16:31 PM »
Code - Auto/Visual Lisp: [Select]
  1. (defun f ( l )
  2.     (mapcar '(lambda ( x ) (read (strcat "(" x ")"))) l)
  3. )
Code - Auto/Visual Lisp: [Select]
  1. _$ (f '("500.00 500.00 " "532.75 481.74 " "532.75 481.74 "  "565.50 463.47"))
  2. ((500.0 500.0) (532.75 481.74) (532.75 481.74) (565.5 463.47))

Or, as a single list:
Code - Auto/Visual Lisp: [Select]
  1. (defun f ( l )
  2.     (apply 'append (mapcar '(lambda ( x ) (read (strcat "(" x ")"))) l))
  3. )
Code - Auto/Visual Lisp: [Select]
  1. _$ (f '("500.00 500.00 " "532.75 481.74 " "532.75 481.74 "  "565.50 463.47"))
  2. (500.0 500.0 532.75 481.74 532.75 481.74 565.5 463.47)

Robert98

  • Guest
Re: string list to real list
« Reply #2 on: January 19, 2014, 06:18:12 AM »
Dear Mac,
I have to thank you for your reply. Your solution is good enough that I won't even think about another one.
Special thanks to you again and I hope you'd have a great weekend.

Lee Mac

  • Seagull
  • Posts: 12915
  • London, England
Re: string list to real list
« Reply #3 on: January 19, 2014, 07:22:41 AM »
Thank you for your kind words Robert, enjoy your weekend also :-)

Robert98

  • Guest
Re: string list to real list
« Reply #4 on: January 19, 2014, 09:33:36 AM »
Dear Members,
    After that Mr Lee's solution , I practiced and Now ,I have a spagetti list that contains some parallel LWpolylines coordinates , and I want
    separate them and put upper Plines points to U_list and lower Plines points to L_list .
       
Quote

            my parallel lines are like this but not essentially horizontal ones (each piece is lwpolyline)

                                        ____   ____  ____  _____
                                        ____   ____  ____  _____
 

       

            so I wrote this code :
     
Code: [Select]
[code=cadlisp-7]
 (setq Len_list
    (mapcar '(lambda (Ai)(distance Ai Ai)) f))
    (if     (and
                      (>= 12.40 ) ; 12.40 is distance of parallels with a -bit tolerance
      (<= 12.60 )  ; 12.60 is distance of parallels with a +bit tolerance
            )
    (progn
        (setq U_list (list Ai)
          L_list (list Ai)
        )
      )
)
      [/code]

   Now I have two questions :
       1- Can I work on above codes and then create new continuous plines for upper and lower plines separately ?
       2- Do I have to change my method and think about new function like vlax-curve-getclosestpointto ?
                please hint me .
                Thanks ...
« Last Edit: January 19, 2014, 09:39:15 AM by Robert98 »

Lee Mac

  • Seagull
  • Posts: 12915
  • London, England
Re: string list to real list
« Reply #5 on: January 19, 2014, 10:47:16 AM »
Could you post a sample drawing Robert?

Lee Mac

  • Seagull
  • Posts: 12915
  • London, England
Re: string list to real list
« Reply #6 on: January 19, 2014, 11:18:33 AM »
Quote

            my parallel lines are like this but not essentially horizontal ones (each piece is lwpolyline)

                                        ____   ____  ____  _____
                                        ____   ____  ____  _____
 

If I have understood your intentions, try the following:
Code: [Select]
(defun c:mergepoly ( / e i l m n p q s v x z )
    (if (setq s (ssget "_:L" '((0 . "LWPOLYLINE") (90 . 2))))
        (progn
            (repeat (setq i (sslength s))
                (setq e (ssname s (setq i (1- i)))
                      v (mapcar '(lambda ( x ) (trans (cdr x) e 0)) (vl-remove-if-not '(lambda ( x ) (= 10 (car x))) (entget e)))
                )
                (entdel e)
                (if (setq x (vl-some '(lambda ( x ) (if (LM:ListCollinear-p (vl-list* (car x) (cadr x) v)) x)) l))
                    (setq l (subst (append v x) x l))
                    (setq l (cons v l))
                )
            )
            (foreach x l
                (setq v (mapcar '- (car x) (cadr x))
                      m (caddr (trans (car x) 0 v))
                      p (car x)
                      n m
                      q p
                )
                (foreach y (cdr x)
                    (setq z (caddr (trans y 0 v)))
                    (if (< m z) (setq m z p y))
                    (if (< z n) (setq n z q y))
                )
                (entmake (list '(0 . "LINE") (cons 10 p) (cons 11 q)))
            )
        )
    )
    (princ)
)
                   
;; List Collinear-p  -  Lee Mac
;; Returns T if all points in a list are collinear

(defun LM:ListCollinear-p ( lst )
    (or (null (cddr lst))
        (and (LM:Collinear-p (car lst) (cadr lst) (caddr lst))
             (LM:ListCollinear-p (cdr lst))
        )
    )
)

;; Collinear-p  -  Lee Mac
;; Returns T if p1,p2,p3 are collinear

(defun LM:Collinear-p ( p1 p2 p3 )
    (
        (lambda ( a b c )
            (or
                (equal (+ a b) c 1e-8)
                (equal (+ b c) a 1e-8)
                (equal (+ c a) b 1e-8)
            )
        )
        (distance p1 p2) (distance p2 p3) (distance p1 p3)
    )
)
(princ)

Robert98

  • Guest
Re: string list to real list
« Reply #7 on: January 19, 2014, 11:39:10 AM »
Could you post a sample drawing Robert?

Thanks for your attention Mr Lee. Actually I want to change these segmented plines to two merged upper and lower ( parallel ) plines.
Suppose these lines are two sides of a road.
I have attached a sample drawing.

Lee Mac

  • Seagull
  • Posts: 12915
  • London, England
Re: string list to real list
« Reply #8 on: January 19, 2014, 12:06:13 PM »
The task becomes slightly more complex when the segments are non-linear...
Maybe PEDIT > MULTIPLE > JOIN > JOINTYPE > EXTEND will get you part of the way there.

Robert98

  • Guest
Re: string list to real list
« Reply #9 on: January 20, 2014, 11:34:00 AM »
Dear Lee,
  I thought about your idea (the complexities of the problem) and since I'm not expert at lisp programming in contrast to
  you and other dear members,I shifted to VB that is simpler for me , so I solved this problem in these steps :
  1- export coordinates of poly lines to a vector array in a text file (x1 y1 x2 y2 ...) .
  2- After checking output coordinates manually , I load them again ( we can skip this step )
  3- by below codes I find midpoints of upper and lower coordinates
  4- fit a poly line to these points
  5- offset them up and down (optional)

  Actually I solved it Mathematically
 my codes :
 
Code: [Select]
Export_All_Polylines_Vertexes_ToFile "D:\Users\my Temp\TEST2.txt" ' It's a sub routine

    Import_All_Polylines_Vertexes_To_Array "D:\Users\my Temp\TEST2.txt", imp_Array
    Id1 = (UBound(imp_Array) + 1)
     ReDim X(0 To Id1 / 2)
     ReDim Y(0 To Id1 / 2)
             For i = 0 To (Id1 / 2) - 1 Step 2
                 X(i) = imp_Array(2 * i)
             Next i
         For i = 0 To (Id1 / 2) - 1 Step 2
            Y(i) = imp_Array((2 * i) + 1)
         Next i
  '  Debug.Print UBound(X), UBound(Y)
 
    n = 0
 
   For i = 0 To UBound(X) - 1
      For j = 1 To UBound(X)
       
         ds = getdist(X(i), Y(i), X(j), Y(j))
             If (ds >= 12.4 And ds <= 12.6) Then
                ReDim DMX(0 To n)
                 ReDim DMY(0 To n)
                DMX(n) = Round(((X(i) + X(j)) / 2), 2) ' X of Midpoints coordinates
                DMY(n) = Round(((Y(i) + Y(j)) / 2), 2) ' Y of Midpoints coordinates
                Debug.Print n, DMX(n), DMY(n)
                n = n + 1
                 Else

            End If
        Next j
     Next i
Me.Show

Now, if you and other experts accept this solution I'll translate it to lisp codes .   
Thanks for your advices though.