Author Topic: Convert string to list?  (Read 17278 times)

0 Members and 1 Guest are viewing this topic.

Brick_top

  • Guest
Convert string to list?
« on: September 28, 2012, 09:13:08 AM »
Anyone knows how to remove the "" from this:

Code: [Select]
"(((-1 . <Entity name: 7ef08940>) (0 . LWPOLYLINE) (330 . <Entity name:
7ef05cf8>) (5 . 1368) (100 . AcDbEntity) (67 . 0) (410 . Model) (8 . 0) (100 .
AcDbPolyline) (90 . 7) (70 . 0) (43 . 0.0) (38 . 0.0) (39 . 0.0) (10 -49522.9
14762.6) (40 . 0.0) (41 . 0.0) (42 . 0.0) (91 . 0) (10 8723.02 44922.5) (40 .
0.0) (41 . 0.0) (42 . 0.0) (91 . 0) (10 39807.6 1190.63) (40 . 0.0) (41 . 0.0)
(42 . 0.0) (91 . 0) (10 95035.6 21096.2) (40 . 0.0) (41 . 0.0) (42 . 0.0) (91 .
0) (10 110729.0 49748.1) (40 . 0.0) (41 . 0.0) (42 . 0.0) (91 . 0) (10 134872.0
18080.2) (40 . 0.0) (41 . 0.0) (42 . 0.0) (91 . 0) (10 141512.0 -22937.3) (40 .
0.0) (41 . 0.0) (42 . 0.0) (91 . 0) (210 0.0 0.0 1.0)))"

?

which is the content of a variable
thanks a lot

Lee Mac

  • Seagull
  • Posts: 12905
  • London, England
Re: Convert string to list?
« Reply #1 on: September 28, 2012, 09:48:56 AM »
You would use the read function, however, since the DXF data has been converted to a string, the entity name pointers (e.g. <Entity name: 7ef08940>) will no longer be accessible and will cause the read function to error since they will be interpreted as two separate symbols which form an invalid dotted pair structure.

For example, the dotted pairs containing entity names would be equivalent to attempting to convert:

$ (read "((-1 . a b))")
; error: extra cdrs in dotted pair on input


Since the two parts of the entity name pointer would be interpreted as two individual symbols:

(-1 . <Entity name: 7ef08940>)

And a dotted pair may only consist of two atoms, no more, no less.
« Last Edit: September 28, 2012, 09:55:49 AM by Lee Mac »

chlh_jd

  • Guest
Re: Convert string to list?
« Reply #2 on: September 28, 2012, 10:07:37 AM »
For re-entmake , I use this
Code: [Select]
(defun obj->str (ent / relst mid)
  (setq relst (list -1 330 330 5 100 100 102 102 410))
  (setq mid (print ent))
  (foreach num relst
    (progn
      (setq mid (vl-remove (assoc num mid) mid))
    )
  )
  (vl-prin1-to-string mid)
)
(defun obj->str->fn (ent fn / f_test str)
  (setq f_test (open fn "a"))
  (setq str (obj->str ent))
  (prin1 str f_test)
  (close f_test)
)
(defun str->obj (str)
  (entmake (string->entlst str))
)
(defun string->entlst  (str / b l res)
  (setq l (string->strlst str ")"))
  (foreach a  l
    (cond ((wcmatch a "*\"*")
   (setq b (string->strlst
     (vl-string-translate
       "\""
       " "
       (vl-string-trim "(" (vl-string-trim " " a)))
     "."))
   (setq res (cons (cons (eval (read (car b)))
(vl-string-trim " " (cadr b)))
   res)))
  ((setq res (cons (read (strcat a ")")) res)))))
  (reverse res))
(defun string->strlst (str del / lst a s1 cha pos i)
  (setq s1 ""
i  0
a  (ascii del)
  )
  (while (setq pos (vl-string-position a str i))
    (setq s1 (substr str (1+ i) (- pos i)))
    (if (/= s1 "")
      (setq lst (cons s1 lst))
    )
    (setq i (1+ pos))
  )
  (setq s1 (substr str (1+ i)))
  (if (/= s1 "")
    (setq lst (cons s1 lst))
  )
  (reverse lst)
)

dgorsman

  • Water Moccasin
  • Posts: 2437
Re: Convert string to list?
« Reply #3 on: September 28, 2012, 10:46:44 AM »
Better back up a step.  If this is from a (princ...) to the command line, those wrapping double-quotes DON'T ACTUALLY EXIST.  Moreover, the (entget...) function which generates this type of return doesn't return a string value, it returns a list of dotted pairs.

Context is everything - a little more information would lead to a nicely curved garden path, rather than a cloverleaf-intersection.   :wink:
If you are going to fly by the seat of your pants, expect friction burns.

try {GreatPower;}
   catch (notResponsible)
      {NextTime(PlanAhead);}
   finally
      {MasterBasics;}

pBe

  • Bull Frog
  • Posts: 402
Re: Convert string to list?
« Reply #4 on: September 29, 2012, 10:41:03 PM »
it looks as if the OP used vl-princ-to-string, notice how the second element of DXF 0 or 8 or 410 looses its double quotes on the string on the first post? --->(0 . LWPOLYLINE)

with princ ---> (0 . "LWPOLYLINE")
with vl-prin1-to-string -- > (0 . \"LWPOLYLINE\")

Now just for the fun of it. go ahead and convert the string [just as it is now]  to a usable data :)

EDIT: Yes, its a challenge :D
« Last Edit: September 29, 2012, 10:49:20 PM by pBe »

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Convert string to list?
« Reply #5 on: September 29, 2012, 11:28:00 PM »
Better back up a step.  If this is from a (princ...) to the command line, those wrapping double-quotes DON'T ACTUALLY EXIST.  Moreover, the (entget...) function which generates this type of return doesn't return a string value, it returns a list of dotted pairs.

Context is everything - a little more information would lead to a nicely curved garden path, rather than a cloverleaf-intersection.   :wink:

Yes
and
Yes

:)
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

pBe

  • Bull Frog
  • Posts: 402
Re: Convert string to list?
« Reply #6 on: September 30, 2012, 12:21:01 AM »
Code - Auto/Visual Lisp: [Select]
  1. (defun  RFS (strData / p q d r edata)          ;<- remake From String
  2.   (while (and
  3.            (setq p (vl-string-position 40 strData 0 T))
  4.            (not (equal strData "("))
  5.            )
  6.     (setq q (if
  7.           (setq f (vl-string-search "\n" (setq q (substr strData p))))
  8.            (strcat (substr q 1 f) (substr q (+ f 2)))
  9.            q
  10.            )
  11.       )
  12.     (if
  13.       (setq r (vl-some
  14.             '(lambda (x)
  15.                (if (setq p (vl-string-search x q))
  16.                  x ))
  17.             '("(0 ." "(8 ." "(100 ." "(410 ." "(-1 ." "(5 ." "(330 .")
  18.             )
  19.         )
  20.        (if (member (setq d (read (substr r 2))) '(0 8 100 410))
  21.          (setq edata
  22.             (cons
  23.               (cons d (vl-string-trim ". )" (substr q (strlen r))))
  24.               eData
  25.               )) q
  26.          )
  27.        (setq eData (cons (read q) eData))
  28.        )
  29.     (setq strData (substr strData 1 (- (strlen strData) (strlen q))))
  30.     )
  31.   eData
  32.   )

(entmakex (rts postedstring))

Usable: --->>> Excludes DXF 0 5 330 <<<---but you guys already know that.

Brick_top

  • Guest
Re: Convert string to list?
« Reply #7 on: October 01, 2012, 03:46:17 AM »
Better back up a step.  If this is from a (princ...) to the command line, those wrapping double-quotes DON'T ACTUALLY EXIST.  Moreover, the (entget...) function which generates this type of return doesn't return a string value, it returns a list of dotted pairs.

Context is everything - a little more information would lead to a nicely curved garden path, rather than a cloverleaf-intersection.   :wink:

Hi there.

Sorry for the lack of context. I'm using princ to a txt file and then I'm using the read-line function to put the content to a variable, and what I posted is the result of the read-line to a variable.

What I'm trying to do is to save some objects info in a txt file so I can later "rebuild" them when I want.

Brick_top

  • Guest
Re: Convert string to list?
« Reply #8 on: October 01, 2012, 03:51:55 AM »
You would use the read function, however, since the DXF data has been converted to a string, the entity name pointers (e.g. <Entity name: 7ef08940>) will no longer be accessible and will cause the read function to error since they will be interpreted as two separate symbols which form an invalid dotted pair structure.

For example, the dotted pairs containing entity names would be equivalent to attempting to convert:

$ (read "((-1 . a b))")
; error: extra cdrs in dotted pair on input


Since the two parts of the entity name pointer would be interpreted as two individual symbols:

(-1 . <Entity name: 7ef08940>)

And a dotted pair may only consist of two atoms, no more, no less.

Thanks a lot for your explanation, fortunately it makes sense to me  :-)

Brick_top

  • Guest
Re: Convert string to list?
« Reply #9 on: October 01, 2012, 03:55:23 AM »
it looks as if the OP used vl-princ-to-string, notice how the second element of DXF 0 or 8 or 410 looses its double quotes on the string on the first post? --->(0 . LWPOLYLINE)

with princ ---> (0 . "LWPOLYLINE")
with vl-prin1-to-string -- > (0 . \"LWPOLYLINE\")

Now just for the fun of it. go ahead and convert the string [just as it is now]  to a usable data :)

EDIT: Yes, its a challenge :D

I have to look into those functions, I have a lot to learn it seems

Brick_top

  • Guest
Re: Convert string to list?
« Reply #10 on: October 01, 2012, 03:56:08 AM »
Code - Auto/Visual Lisp: [Select]
  1. (defun  RFS (strData / p q d r edata)          ;<- remake From String
  2.   (while (and
  3.            (setq p (vl-string-position 40 strData 0 T))
  4.            (not (equal strData "("))
  5.            )
  6.     (setq q (if
  7.           (setq f (vl-string-search "\n" (setq q (substr strData p))))
  8.            (strcat (substr q 1 f) (substr q (+ f 2)))
  9.            q
  10.            )
  11.       )
  12.     (if
  13.       (setq r (vl-some
  14.             '(lambda (x)
  15.                (if (setq p (vl-string-search x q))
  16.                  x ))
  17.             '("(0 ." "(8 ." "(100 ." "(410 ." "(-1 ." "(5 ." "(330 .")
  18.             )
  19.         )
  20.        (if (member (setq d (read (substr r 2))) '(0 8 100 410))
  21.          (setq edata
  22.             (cons
  23.               (cons d (vl-string-trim ". )" (substr q (strlen r))))
  24.               eData
  25.               )) q
  26.          )
  27.        (setq eData (cons (read q) eData))
  28.        )
  29.     (setq strData (substr strData 1 (- (strlen strData) (strlen q))))
  30.     )
  31.   eData
  32.   )

(entmakex (rts postedstring))

Usable: --->>> Excludes DXF 0 5 330 <<<---but you guys already know that.

Now you have made it very difficult for me to take your challenge... that function did what I wanted!

Brick_top

  • Guest
Re: Convert string to list?
« Reply #11 on: October 01, 2012, 04:03:47 AM »
Now that I'm at it...

does anyone know if it would be too slow to "or possible" to store and remake at least 40000 entities stored in a txt file?

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Convert string to list?
« Reply #12 on: October 01, 2012, 04:07:44 AM »

Do you have control over how they are saved to a file ??
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Convert string to list?
« Reply #13 on: October 01, 2012, 04:19:46 AM »

You cant re-use handles
You can't re-use ObjectID's

You need a way of asserting that Table objects exist in the target drawing ; ie layers, styles, linetypes etc, etc.


Hint:
Do you know what the acronym DXF stands for ??
Do you know the commands DXFOUT, DXFIN ??
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Brick_top

  • Guest
Re: Convert string to list?
« Reply #14 on: October 01, 2012, 04:37:03 AM »

Do you have control over how they are saved to a file ??

Well, I haven't done anything like this but in my ignorance I think I can number the entities.