Author Topic: -+{ Challenge }+- Serialize Xdata  (Read 8700 times)

0 Members and 1 Guest are viewing this topic.

It's Alive!

  • BricsCAD
  • Needs a day job
  • Posts: 6941
  • AKA Daniel
-+{ Challenge }+- Serialize Xdata
« on: January 22, 2010, 04:59:02 AM »
Serialize the Xdata from all the entities (modelspace)  in the attached drawing, then read the data back into memory.
Format or language does not matter, only that you can read/write your data

There are five xdata elements attached to each point, including the app name (myxdata)
How fast can you doit?

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: -+{ Challenge }+- Serialize Xdata
« Reply #1 on: January 22, 2010, 05:04:36 AM »

How fast can you doit?


about 3 months I think :)


[added:] hmmmm
Non Autodesk DWG.  This DWG file was saved by a software application that was
not developed or licensed by Autodesk.
:)
« Last Edit: January 22, 2010, 05:12:29 AM by Kerry Brown »
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

--> Donate to theSwamp<--

frtfff

  • Bull Frog
  • Posts: 215
Re: -+{ Challenge }+- Serialize Xdata
« Reply #2 on: January 22, 2010, 05:19:59 AM »
 :kewl:

It's Alive!

  • BricsCAD
  • Needs a day job
  • Posts: 6941
  • AKA Daniel
Re: -+{ Challenge }+- Serialize Xdata
« Reply #3 on: January 22, 2010, 05:47:37 AM »
... hmmmm
Non Autodesk DWG.  This DWG file was saved by a software application that was
not developed or licensed by Autodesk.
:)

Ha, yeah Bricscad has a cooler xdata editor   :-)

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: -+{ Challenge }+- Serialize Xdata
« Reply #4 on: January 22, 2010, 06:33:07 AM »


ok,

I'll try a Lisp hack.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

--> Donate to theSwamp<--

grrrr

  • Mosquito
  • Posts: 8
Re: -+{ Challenge }+- Serialize Xdata
« Reply #5 on: January 22, 2010, 06:54:28 AM »
hello dan,
where is the problem.
try in lisp and spend 2 hours.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: -+{ Challenge }+- Serialize Xdata
« Reply #6 on: January 22, 2010, 07:02:38 AM »
Well, not quite 2 hours

'Wanda' grabbed it in 14 seconds ... a bit slow, but thats to be expected.

Code: [Select]
(defun c:kdub_doit (/ start ss datalist datafilename datalist
                    itemdata readdatalist linecount elapsedtime)
  ;; codeHimBelonga kdub@theSwamp 20100122
  (setq start (getvar "millisecs"))
  (if (setq ss (ssget "X" '((-3 ("myxdata")))))
    (progn (setq datalist     '()
                 datafilename "c:/Test_DoIt_MyXData.txt"
           )
           (close (open datafilename "W"))
           ;;------------------------------
           ;; Collect the Data
           (foreach item (kdub:ss->entlist ss)
             (if (setq itemdata
                        (cdr
                          (cadr
                            (assoc -3
                                   (setq ent (entget item '("myxdata")))
                            )
                          )
                        )
                 )
               (setq datalist (cons itemdata datalist))
             )
           )
           ;;------------------------------
           ;; Write it Out
           (kdub:list-to-textfile
             datafilename
             (mapcar 'vl-prin1-to-string (reverse datalist))
           )
           ;;------------------------------
           ;; Read it Back
           (setq readdatalist (mapcar 'read
                                      (kdub:textfile-to-list datafilename)
                              )
                 linecount    (length readdatalist)
                 elapsedtime  (itoa (- (getvar "millisecs") start))
           )
    )
  )
  (princ (strcat "\n "
                 (itoa linecount)
                 " Objects Collected in : "
                 elapsedtime
                 " Milliseconds."
         )
  )
  (princ)
)
(princ)

LIBRARY STUFF
Code: [Select]
;;;------------------------------------------------------------------
;;;------------------------------------------------------------------
;;;
(defun kdub:ss->entlist (ss / i returnval)
  (if (and ss (< 0 (sslength ss)))
    (progn (setq i 0)
           (repeat (sslength ss)
             (setq returnval (cons (ssname ss i) returnval)
                   i         (1+ i)
             )
           )
    )
  )
  (if returnval
    (reverse returnval)
    nil
  )
)

;;;------------------------------------------------------------------
;;;------------------------------------------------------------------
;;;
(defun kdub:textfile-to-list (filename      /
                              fileobject    filefound
                              filesystem    openfileas
                              returnvalue
                             )
  (if (setq filefound (findfile filename))
    (progn (setq filesystem (vlax-create-object "Scripting.FileSystemObject")
                 fileobject (vlax-invoke filesystem "GetFile" filefound)
                 openfileas (vlax-invoke fileobject "OpenAsTextStream" 1 0)
           )
           (while (= (vlax-get openfileas "AtEndOfStream") 0)
             (setq returnvalue (cons (vlax-invoke openfileas "ReadLine")
                                     returnvalue
                               )
             )
           )
           (vlax-invoke openfileas "Close")
           (vlax-release-object openfileas)
           (vlax-release-object fileobject)
           (vlax-release-object filesystem)
           (reverse returnvalue)
    )
    nil
  )
)

;;;------------------------------------------------------------------
;;;------------------------------------------------------------------
;;;
(defun kdub:list-to-textfile (filename      datalist
                              /             fileobject
                              filefound     filesystem
                              openfileas    returnvalue
                             )
  (if (setq filefound (findfile filename))
    (progn (setq filesystem (vlax-create-object "Scripting.FileSystemObject")
                 fileobject (vlax-invoke filesystem "GetFile" filefound)
                 openfileas (vlax-invoke fileobject "OpenAsTextStream" 2 0)
           )
           (foreach line datalist
             (vlax-invoke openfileas "WriteLine" line)
           )
           (vlax-invoke openfileas "Close")
           (vlax-release-object openfileas)
           (vlax-release-object fileobject)
           (vlax-release-object filesystem)
           t
    )
    nil
  )
)
;;;------------------------------------------------------------------
;;;------------------------------------------------------------------
;;;

and the piccy to proove it :)
« Last Edit: January 22, 2010, 07:31:26 AM by Kerry Brown »
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

--> Donate to theSwamp<--

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: -+{ Challenge }+- Serialize Xdata
« Reply #7 on: January 22, 2010, 07:10:51 AM »


Dan, I don't have BricsCad installed yet .. be interesting to compare times when I do.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

--> Donate to theSwamp<--

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: -+{ Challenge }+- Serialize Xdata
« Reply #8 on: January 22, 2010, 07:18:22 AM »
< ..> There are five xdata elements attached to each point, including the app name (myxdata)

Dan
I only get 4 elements attached each point ????

Piccy attached:  8204 kB file :)
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

--> Donate to theSwamp<--

It's Alive!

  • BricsCAD
  • Needs a day job
  • Posts: 6941
  • AKA Daniel
Re: -+{ Challenge }+- Serialize Xdata
« Reply #9 on: January 22, 2010, 07:39:06 AM »
4 + the appname  :-)

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: -+{ Challenge }+- Serialize Xdata
« Reply #10 on: January 22, 2010, 07:41:58 AM »
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

--> Donate to theSwamp<--

It's Alive!

  • BricsCAD
  • Needs a day job
  • Posts: 6941
  • AKA Daniel
Re: -+{ Challenge }+- Serialize Xdata
« Reply #11 on: January 22, 2010, 07:53:27 AM »
Nice !
Quote

 ; Autocad 100000 Objects Collected in : 7878 Milliseconds.
 ; Autocad 100000 Objects Collected in : 7956 Milliseconds.
 ; Autocad 100000 Objects Collected in : 7878 Milliseconds.
 
 ; Bricscad 100000 Objects Collected in : 10185 Milliseconds. <---?
 ; Bricscad 100000 Objects Collected in : 5959 Milliseconds.
 ; Bricscad 100000 Objects Collected in : 5990 Milliseconds.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: -+{ Challenge }+- Serialize Xdata
« Reply #12 on: January 22, 2010, 07:56:31 AM »


Whoa, what sort of beast are you running there ??  :?
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

--> Donate to theSwamp<--

gile

  • Water Moccasin
  • Posts: 2231
  • Marseille, France
Re: -+{ Challenge }+- Serialize Xdata
« Reply #13 on: January 22, 2010, 08:13:15 AM »
Hi,

If I don't misundertand the challenge, here're my C# 2 cents

Code: [Select]
using System;
using System.IO;
using System.Text;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using acadApp = Autodesk.AutoCAD.ApplicationServices.Application;

namespace SerializeChallenge
{
    public class Class1
    {

        [CommandMethod("Test")]
        public void Test()
        {
            DateTime t0 = DateTime.Now;
            Document doc = acadApp.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            string filename = "C:\\SerializeChallenge.txt";
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead);
                using (TextWriter writer = new StreamWriter(filename))
                {
                    foreach (ObjectId id in btr)
                    {
                        Entity ent = (Entity)tr.GetObject(id, OpenMode.ForRead, false);
                        ResultBuffer resbuf = ent.XData;
                        if (resbuf == null)
                            continue;
                        TypedValue[] datas = resbuf.AsArray();
                        StringBuilder sb = new StringBuilder(datas.Length);
                        foreach (TypedValue tv in datas)
                        {
                            sb.Append(tv.Value);
                            sb.Append(" ");
                        }
                        writer.WriteLine(sb.ToString());
                    }
                    writer.Close();
                }
                tr.Commit();
            }
            using (TextReader reader = new StreamReader(filename))
            {
                string line = reader.ReadToEnd();
            }
            DateTime t1 = DateTime.Now;
            TimeSpan ts = t1 - t0;
            ed.WriteMessage("\nEllapsed milliseconds: " + ts.TotalMilliseconds.ToString());
        }
    }
}

Quote
Commande: test

Ellapsed milliseconds: 2062.5
Commande:
TEST
Ellapsed milliseconds: 2031.25
Commande:
TEST
Ellapsed milliseconds: 2031.25
Commande:
TEST
Ellapsed milliseconds: 2015.625
Speaking English as a French Frog

It's Alive!

  • BricsCAD
  • Needs a day job
  • Posts: 6941
  • AKA Daniel
Re: -+{ Challenge }+- Serialize Xdata
« Reply #14 on: January 22, 2010, 08:15:46 AM »