Populate ExtensionDictionary and Xrecord with 310 DXF Code

Populate ExtensionDictionary and Xrecord with 310 DXF Code

MehtaRajesh
Advocate Advocate
1,278 Views
3 Replies
Message 1 of 4

Populate ExtensionDictionary and Xrecord with 310 DXF Code

MehtaRajesh
Advocate
Advocate

By using following code, I received output as mentioned below

(setq vlentity (vlax-ename->vla-object (car (entsel))))
(setq dict (vla-GetExtensionDictionary vlentity))
(setq eobjId (vla-get-objectid vlentity))
(setq xdTypes (vlax-make-safearray vlax-vbInteger '(0 . 0)))
(vlax-safearray-fill xdTypes (list 340))
(setq xdTypes (vlax-make-variant xdTypes))
(setq xdValues (vlax-make-safearray vlax-vbVariant '(0 . 0)))
(setq xdObject (vlax-make-variant vlentity))
(vlax-safearray-fill xdValues (list xdObject))
(setq xdValues (vlax-make-variant xdValues))
(setq xrecord (vla-AddXRecord dict "MyText"))
(vla-SetXRecordData xrecord xdTypes xdValues)

 

<Entity name: 2bb593a8320> 
((-1 . <Entity name: 2bb593a8320>) (0 . "DICTIONARY") (330 . <Entity name: 2bb593a8310>) (5 . "20C1A") (100 . "AcDbDictionary") (280 . 1) (281 . 1) (3 . "MyText") (360 . <Entity name: 2bb593a8330>)) 
<Entity name: 2bb593a8330> 
((-1 . <Entity name: 2bb593a8330>) (0 . "XRECORD") (5 . "20C1B") (102 . "{ACAD_REACTORS") (330 . <Entity name: 2bb593a8320>) (102 . "}") (330 . <Entity name: 2bb593a8320>) (100 . "AcDbXrecord") (280 . 1) (340 . <Entity name: 2bb593a8310>)) 

 I need to populate 310 DXF code as Xrecord as under, I tried my best to populate but not able to attach as required. Please guide me what changes I need to make in the code. Thx in advance.

<Entity name: 2bb593a0520> ((-1 . <Entity name: 2bb593a0520>) (0 . "DICTIONARY") (330 . <Entity name: 2bb593a0510>) (5 . "20AD2") (100 . "AcDbDictionary") (280 . 1) (281 . 1) (3 . " MyText ") (360 . <Entity name: 2bb593a0530>)) 
<Entity name: 2bb593a0530> ((-1 . <Entity name: 2bb593a0530>) (0 . "XRECORD") (5 . "20AD3") (102 . "{ACAD_REACTORS") (330 . <Entity name: 2bb593a0520>) (102 . "}") (330 . <Entity name: 2bb593a0520>) (100 . "AcDbXrecord") (280 . 1) (310 . "123456789ABCDEF101112131415161718191A1B1C1D1E1F202122232425262728292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F404142434445464748") (310 . "123456789ABCDEF101112131415161718191A1B1C1D1E1F202122232425262728292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F404142434445464748"))

Regards,

Raj

0 Likes
Accepted solutions (1)
1,279 Views
3 Replies
Replies (3)
Message 2 of 4

MehtaRajesh
Advocate
Advocate

Hi Experts and Autodesk developers,

Please revert back and guide me, whether developer able to add 310 DXF list (Hex values) as XRecords using Code?
I am also comfortable using C# code, though I have not yet tried in C# for this task.

Your expert guidance will be highly appreciated.

Regards,
Raj

0 Likes
Message 3 of 4

CodeDing
Advisor
Advisor

@MehtaRajesh ,

 

I do not work with the "vl-" functions enough to be sure how to accomplish this via that method.. but using general AutoLISP, I know that to add something to an existing Xrecord, you must delete the old one and replace it with a new one.. Here is a function that can add items to an existing Xrecord.. I hope it helps you.

(defun c:TEST ( / d x temp newItems)
  (setq d (dictadd (namedobjdict) "Test_Dictionary" (entmakex '((0 . "DICTIONARY") (100 . "AcDbDictionary")))))
  (setq x (dictadd d "Test_Xrecord" (entmakex '((0 . "XRECORD") (100 . "AcDbXrecord")))))
  (prompt "\nDict ------ :\n") (princ (entget d))
  (prompt "\nXrec ------ :\n") (princ (entget x))
  (setq newItems '((310 . "123ABC") (2 . "123ABC")))
  (if (setq temp (XrAddItems d x newItems))
    (progn
      (setq x temp)
      (prompt "\nXrec After Additions ------ :\n")
      (princ (entget x))
    );progn
  );if
  (princ)
);defun

(defun XrAddItems (enameXrOwner enameXr items / d tmp xrName xrList newXr)
;enameXrOwner - ename, of owner of Xrecord
;enameXr - ename, of Xrecord to add items to
;items - list, a list of dotted pair lists with items to add to Xrecord
; ------ Ex: ((1 . "abc") (2 . "def"))
  (if (and (setq d (entget enameXrOwner))
           (setq tmp (member (cons 350 enameXr) (reverse d)))
           (setq xrName (cdadr tmp))
           (setq xrList (member '(100 . "AcDbXrecord") (entget enameXr)))
           (setq newXr (entmakex (append '((0 . "XRECORD")) xrList items))))
    (progn
      (entdel enameXr)
      (dictadd enameXrOwner xrName newXr)
    );progn
  );if
);defun

Best,

~DD

Message 4 of 4

MehtaRajesh
Advocate
Advocate
Accepted solution

Thanks a lot DD for your reply,
your function works fine with common NOD,

But i need to update entity's ACAD_XDICTIONARY.
I took help of another .Net forum and able to do my task as follows.
Since method to populate 310 dxf code is different as need to convert to Byte as below

 var doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var ed = doc.Editor;

            var per = ed.GetEntity("\nSelect object: ");
            if (per.Status == PromptStatus.OK)
            {
                string str1 = "mystring";
                byte[] bytes1 = Encoding.ASCII.GetBytes(str1);
                using (var tr = db.TransactionManager.StartTransaction())
                {
                    var ent = (Autodesk.AutoCAD.DatabaseServices.Entity)tr.GetObject(per.ObjectId, OpenMode.ForRead);
                    var data = ent.GetXDictionaryXrecordData("Mytest");

                    if (data == null)
                    {
                        ent.SetXDictionaryXrecordData("Mytest", new TypedValue(310, bytes1));

                    }
                }
            }



public static void SetXrecordData(this DBDictionary dict, string key, ResultBuffer data)
            {
                if (dict == null)
                    throw new ArgumentNullException(nameof(dict));
                if (string.IsNullOrWhiteSpace(key))
                    throw new ArgumentNullException(nameof(key));
                Transaction tr = dict.Database.TransactionManager.TopTransaction;
                if (tr == null)
                    throw new Autodesk.AutoCAD.Runtime.Exception(Autodesk.AutoCAD.Runtime.ErrorStatus.NoActiveTransactions);
                Xrecord xrec;
                if (dict.Contains(key))
                {
                    xrec = ((ObjectId)dict[key]).GetObject<Xrecord>(OpenMode.ForWrite);

                }
                else
                {
                    dict.UpgradeOpen();
                    xrec = new Xrecord();
                    dict.SetAt(key, xrec);
                    tr.AddNewlyCreatedDBObject(xrec, true);
                }
                xrec.Data = data;
            }
0 Likes