Author Topic: String to List @symbol  (Read 4345 times)

0 Members and 1 Guest are viewing this topic.

rhino

  • Guest
String to List @symbol
« on: November 12, 2011, 10:25:17 PM »
Hi,

How would you you turn this string into a list with dotted pairs?

"1@1000+2@3000+3@2000+1@1000" to (1 . 1000) (2 . 3000) (3 . 2000) (1 . 1000)

*both items in the dotted pair should be integers.

and also I'd like to check the string with the total length ie. "1@1000+2@3000+3@2000+1@1000" = (14000).

Thanks in advance.

Chris.
« Last Edit: November 13, 2011, 09:45:43 PM by rhino »

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: String to List @symbol
« Reply #1 on: November 13, 2011, 12:10:57 AM »
Code: [Select]
(defun footos (string)
  (read (vl-list->string
          (apply 'append
                 (mapcar '(lambda (a)
                            (cond ((eq a 64) (list 32 46 32))
                                  ((eq a 43) (list 41 40))
                                  ((list a))
                            )
                          )
                         (vl-string->list (strcat "((" string "))"))
                 )
          )
        )
  )
)

Code: [Select]
(footos "1@1000+2@3000+3@2000+1@1000")
Code: [Select]
(apply '+ (mapcar '(lambda (x) (* (car x) (cdr x))) (footos "1@1000+2@3000+3@2000+1@1000")))
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: String to List @symbol
« Reply #2 on: November 13, 2011, 06:15:53 AM »
Code: [Select]
(defun f (s / i)
  (if (setq i (vl-string-search "@" s))
    (cons (cons (atoi s) (atoi (substr s (+ i 2))))
          (if (setq i (vl-string-search "+" s))
            (f (substr s (+ i 2)))
          )
    )
  )
)
Code: [Select]
(f "1@1000+2@3000+3@2000+1@1000")
« Last Edit: November 13, 2011, 06:22:37 AM by ElpanovEvgeniy »

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: String to List @symbol
« Reply #3 on: November 13, 2011, 06:21:56 AM »
Code: [Select]
(defun f+ (s / i)
  (if (setq i (vl-string-search "@" s))
    (+ (* (atoi s) (atoi (substr s (+ i 2))))
       (if (setq i (vl-string-search "+" s))
         (f+ (substr s (+ i 2)))
         0
       )
    )
    0
  )
)
Code: [Select]
(f+ "1@1000+2@3000+3@2000+1@1000")

rhino

  • Guest
Re: String to List @symbol
« Reply #4 on: November 13, 2011, 12:44:53 PM »
Thank you both  :kewl: this helps me out a lot :)

@Elpanov, could you explain your code i'd like to understand the magic  :kewl:

Lee Mac

  • Seagull
  • Posts: 12915
  • London, England
Re: String to List @symbol
« Reply #5 on: November 13, 2011, 12:50:13 PM »
Another, similar method to Alan:

Code: [Select]
(defun f ( s )
    (if (vl-string-search "@" s)
        (f (vl-string-subst " . " "@" (vl-string-subst ")(" "+" s)))
        (read (strcat "((" s "))"))
    )
)

rhino

  • Guest
Re: String to List @symbol
« Reply #6 on: December 30, 2013, 03:24:36 PM »
Dear All,

Wish you complements of the season! and a Happy New Year 2014 in advance :)

Sorry to restart an old thread but I would like your help again in completing this code;

I like to apply this drawgrid function http://www.theswamp.org/index.php?topic=25575.0(from @ronjonp) to the converted dotted list to drawing vertical grid lines within a rectangle?

The list could start off with 1@dist or end with 1@dist which could result in an error if using foreach.
The other issue is that the first frame (grid) would start at a preset offset (say 500mm) from the total length (sum of the converted string).

@CAB The routine I am working on is very similar to what you worked on for array (albeit has varying spacing for the truss/frame members: http://www.theswamp.org/index.php?topic=2695.0


Thanks in advance,
Chris
« Last Edit: December 30, 2013, 03:54:12 PM by rhino »

rhino

  • Guest
Re: String to List @symbol
« Reply #7 on: December 31, 2013, 01:55:09 AM »
Ok here's what I got:

Code - Auto/Visual Lisp: [Select]
  1.  
  2. ;Variable settings
  3. (defun var_set ()
  4.   (setq  *var_lst* '("cmdecho" "osmode" "expert" "plinewid" "dynmode")
  5.          *var_usr*  (mapcar 'getvar *var_lst*))
  6.                     (mapcar 'setvar *var_lst* '(0 0 2 0 0))
  7. )
  8. (defun var_reset ()
  9.  (mapcar 'setvar *var_lst* *var_usr*)
  10. )
  11.  
  12. (defun *error* (msg)
  13.     (if (not
  14.           (member msg '("console break" "Function cancelled" "quit / exit abort" "" nil))
  15.         )
  16.       (princ (strcat "\nError: " msg))
  17.     )    
  18.     (and ;(vl-bt)
  19.         (var_reset)
  20.       )
  21.     (princ)
  22.   )
  23.  
  24. (defun string-list (data) ;by lee-mac @theswamp
  25.     (if (vl-string-search "@" data)
  26.         (string-list (vl-string-subst " . " "@" (vl-string-subst ")(" "+" data)))
  27.         (read (strcat "((" data "))"))
  28.     )
  29. )
  30.  
  31.  ;my-getstring function by Tony Tanzillo
  32. (defun my-getstring (msg)
  33.   (while
  34.      (eq ""
  35.        (setq response
  36.          (vl-string-trim " " (getstring T msg))
  37.         )
  38.       )
  39.      (princ "\nInvalid response...")
  40.    )
  41.  response
  42. )
  43.  
  44. ;Draw LWPoly function
  45. (defun LWPoly (pts cls lay col ext)
  46.   (entmakex (append (list (cons 0 "LWPOLYLINE")
  47.                           (cons 100 "AcDbEntity")
  48.                           (cons 100 "AcDbPolyline")
  49.                           (cons 90 (length pts))
  50.                           (cons 8 lay)
  51.                           (cons 62 col)
  52.                           (cons 70 cls))
  53.                     (mapcar (function (lambda (p) (cons 10 p))) pts) ext)
  54.         )
  55.   )
  56.  
  57. (defun C:Gridlines (/)
  58.  (var_set)
  59.  
  60.  (setq  pt1  '(0.0 0.0 0.0))
  61.   (initget 7)
  62.   (setq  *wid*  (getreal "\nEnter width: "))
  63.     (prompt "\nBay spacing to be entered in MILLIMETERS only
  64.            \nBay spacing must be entered in format as 1@7000+2@8000+1@6500 "
  65.       )
  66.   (setq bay_spc (my-getstring "\nEnter bay spacing: "))
  67.   (setq spc_lst (string-list bay_spc));convert string to dotted list
  68.   (setq *len* (apply '+ (mapcar '(lambda (x) (* (car x) (cdr x))) spc_lst))) ;add up the bay spacing to get total length
  69.  
  70.   (setq  pt2  (list (+ (car pt1) *len*) (cadr pt1) 0.0)
  71.          pt3  (list (car pt2) (+ (cadr pt1) *wid*) 0.0)
  72.          pt4  (list (car pt1) (+ (cadr pt1) *wid*) 0.0)
  73.          ctp  (list (+ (car pt1) (/ *len* 2.0)) (- (cadr pt3) (/ *wid* 2.0)))
  74.          dmeval (if (>= *len* *wid*) *len* *wid*)
  75.         )
  76.   (LWpoly (list pt1 pt2 pt3 pt4) 1 "0" 1 nil)
  77.   (command "_.zoom" (list (- (car pt1) 5000) (- (cadr pt1) 5000)) (list (+ (car pt3) 5000) (+ (cadr pt3) 5000))); required otherwise error in array cmd.
  78.  
  79.  
  80.   (LWpoly (list pt1 pt4) 1 "0" 1 nil);draw gridline
  81.  
  82.   ;(setq  frame (ssadd));create selection set for frame
  83.   (setq  grdline (entlast))
  84.     ;(command "chprop" grdline "" "ltscale" (* *wid* 0.005) "")
  85.     ; (ssadd grdline frame)
  86.         ;(command "_-layer" "s" "STEEL" ""
  87.         ;         "_-insert" "C:/LISP/Block Database/COL_RF.dwg" gl1 "" "" ""
  88.         ;  )
  89.         ;(setq  col (entlast))
  90.         ;(ssadd col frame)
  91.         ;(command "_-insert" "COL_RF" gl2 "" "" "-180")
  92.         ;(setq  col2 (entlast))
  93.         ;(ssadd col2 frame)
  94.          
  95.   (foreach n spc_lst
  96.     (command "-array" grdline "" "rectangular" "1" (1+ (car n)) (cdr n))
  97.     (setq grdline (entlast))
  98.     )
  99.  
  100. (*error* "")
  101.  
  102. )
  103.  

Quote
Code - Auto/Visual Lisp: [Select]
  1.  (foreach n spc_lst
  2.     (command "-array" grdline "" "rectangular" "1" (1+ (car n)) (cdr n))
  3.     (setq grdline (entlast))
  4.     )
  5.  

The above gets me the gridlines; but I have a block inserted at both ends of the grid line; how do I get the selection of those entities (selection set "frame") after each array?

or I would need to map a point list and draw each grid individually how do I go about that?


« Last Edit: December 31, 2013, 07:17:33 AM by rhino »

rhino

  • Guest
Re: String to List @symbol
« Reply #8 on: January 01, 2014, 03:20:58 AM »
anyone there?   :embarrassed: :| :cry:

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: String to List @symbol
« Reply #9 on: January 01, 2014, 09:55:50 AM »
It the new year! 

The young are hung over & the old like me are going to our day time Brunch.  ;D
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

rhino

  • Guest
Re: String to List @symbol
« Reply #10 on: January 02, 2014, 05:34:16 AM »
It the new year! 

The young are hung over & the old like me are going to our day time Brunch.  ;D

Haha Happy New Year CAB and all Swampers!!! :)