Save a string value in dwg file

Save a string value in dwg file

abdul.haseeb
Contributor Contributor
1,033 Views
4 Replies
Message 1 of 5

Save a string value in dwg file

abdul.haseeb
Contributor
Contributor

Hi all,

 

I need to save a string value in dwg file say "something to be saved".

then when I needed I should get this value.

 

Please kindly help to get this.

0 Likes
Accepted solutions (1)
1,034 Views
4 Replies
Replies (4)
Message 2 of 5

_gile
Consultant
Consultant

Hi,

 

You can use an Xrecord in a DBDictionary to store the string.

You'll find many example in this forum, here's one.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 5

abdul.haseeb
Contributor
Contributor

Thanks for this reply.

 

But in need something different....

Like variable while are already available in DWG 

 

USERS1-5

 

Actually I want to save it in VB.net and want to get the same in ObjectARX c++ and vice versa

 

 

 

0 Likes
Message 4 of 5

_gile
Consultant
Consultant
Accepted solution

USERS1-5 is not saved in the drawing, this is why you have to use an Xrecord (or some Extended data attached to an unerasable object as the layer "0").

Xrecords (and xdata) can be accessed by any AutoCAD programation environment (AutoLISP, COM, .NET, ObjectARX).

"ABDUL_HASEED" and an Xrecord named "StringValue" which only contains one string.

 

        public void SaveStringInCurrentDrawing(string str)
        {
            var doc = Application.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            using (doc.LockDocument())
            using (var tr = new OpenCloseTransaction())
            {
                var nod = (DBDictionary)tr.GetObject(db.NamedObjectsDictionaryId, OpenMode.ForRead);
                DBDictionary dict;
                if (nod.Contains("ABDUL_HASEEB"))
                {
                    dict = (DBDictionary)tr.GetObject(nod.GetAt("ABDUL_HASEEB"), OpenMode.ForRead);
                }
                else
                {
                    dict = new DBDictionary();
                    nod.UpgradeOpen();
                    nod.SetAt("ABDUL_HASEEB", dict);
                    tr.AddNewlyCreatedDBObject(dict, true);
                }
                Xrecord xrec;
                if (dict.Contains("StringValue"))
                {
                    xrec = (Xrecord)tr.GetObject(dict.GetAt("StringValue"), OpenMode.ForWrite);
                }
                else
                {
                    xrec = new Xrecord();
                    if (!dict.IsWriteEnabled) tr.GetObject(dict.ObjectId, OpenMode.ForWrite);
                    dict.SetAt("StringValue", xrec);
                    tr.AddNewlyCreatedDBObject(xrec, true);
                }
                xrec.Data = new ResultBuffer(new TypedValue(1, str));
                tr.Commit();
            }
        }

        public string GetSavedStringInCurrentDrawing()
        {
            var doc = Application.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            using (doc.LockDocument())
            using (var tr = new OpenCloseTransaction())
            {
                var nod = (DBDictionary)tr.GetObject(db.NamedObjectsDictionaryId, OpenMode.ForRead);
                if (!nod.Contains("ABDUL_HASEEB"))
                    return null;
                var dict = (DBDictionary)tr.GetObject(nod.GetAt("ABDUL_HASEEB"), OpenMode.ForRead);
                if (!dict.Contains("ABDUL_HASEEB"))
                    return null;
                var xrec = (Xrecord)tr.GetObject(dict.GetAt("StringValue"), OpenMode.ForWrite);
                if (xrec.Data == null)
                    return null;
                return xrec.Data.AsArray()[0].Value.ToString();
            }
        }


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 5 of 5

stefan.hofer
Advocate
Advocate

you can also use custom properties:

 

thisDrawing.SummaryInfo.AddCustomInfo("MyKey", "MyValue");

and

string value = "";
thisDrawing.SummaryInfo.GetCustomByKey("MyKey", out value);

thisdrawing:

using acApp = Autodesk.AutoCAD.ApplicationServices.Application;
//...
thisDrawing = (AcadDocument)acApp.DocumentManager.MdiActiveDocument.GetAcadDocument();
0 Likes