Author Topic: Write and read Xrecord  (Read 2700 times)

0 Members and 1 Guest are viewing this topic.

krkec

  • Guest
Write and read Xrecord
« on: October 30, 2013, 06:38:37 AM »
Could someone tell me why it wont work??
I am trying to write and read Xrecord.

Code: [Select]

namespace kec_tools
{
    public partial class Form1 : Form
    {
        #region funkcije
        private bool checkXrecord(String objekt)
        {
            Document mydoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database madata = mydoc.Database;
            Editor myed = mydoc.Editor;
            using (Transaction trans = madata.TransactionManager.StartTransaction())
            {
                DBDictionary nod = (DBDictionary)trans.GetObject(madata.NamedObjectsDictionaryId, OpenMode.ForRead);
                if (nod.Contains(objekt))
                {
                    return true;
                }
                else
                {
                    return false;
                }

            }
        }

        private void writeXrecord(String txt, int num)
        {

            /*if (checkXrecord("ViskoOPT"))
            {
               
            }*/
            Document mydoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database madata = mydoc.Database;
            Editor myed = mydoc.Editor;
            using (Transaction trans = madata.TransactionManager.StartTransaction())
            {

                DBDictionary nod = (DBDictionary)trans.GetObject(madata.NamedObjectsDictionaryId, OpenMode.ForWrite);
                Xrecord myXrecord = new Xrecord();
                myXrecord.Data = new ResultBuffer(
                    new TypedValue((int)DxfCode.Text, txt),
                    new TypedValue((int)DxfCode.Int16, num));
                nod.SetAt("ViskoOPT", myXrecord);
                trans.AddNewlyCreatedDBObject(myXrecord, true);
            }
        }

        private void readXrecord()
        {
            Document mydoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database madata = mydoc.Database;
            Editor myed = mydoc.Editor;
            using (Transaction trans = madata.TransactionManager.StartTransaction())
            {

                DBDictionary nod = (DBDictionary)trans.GetObject(madata.NamedObjectsDictionaryId, OpenMode.ForRead);
                ObjectId myDataId = nod.GetAt("ViskoOPT");

                Xrecord myXrecord = (Xrecord)trans.GetObject(myDataId, OpenMode.ForRead);

                List<TypedValue> polje = new List<TypedValue>(2);
                foreach (TypedValue value in myXrecord.Data)
                {
                    polje.Add(value);
                }
                int a = (int)polje[1].Value;
                String output = (String)polje[0].Value+ a.ToString();
                MessageBox.Show(output);

            }
        }
        #endregion

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            writeXrecord("This works!!!!", 1);
            readXrecord();
        }
    }
}


Bert

  • Guest
Re: Write and read Xrecord
« Reply #1 on: October 30, 2013, 06:52:46 AM »
Was it because you got a 'eLock' voilation in your writeXrecord-method ?

you can solve this by using mydoc.LockDocument() in the right place :
http://www.theswamp.org/index.php?topic=14378.0
Code - Text: [Select]
  1. using (DocumentLock @lock = mydoc.LockDocument()) {
  2.         using (Transaction trans = madata.TransactionManager.StartTransaction()) {
  3.                 // Your code here...
  4.         }
  5. }

Also your readXrecord cannot find "ViskoOPt" because you did not commit the changes you made in your transaction in your writeXrecord-method.
(Add trans.Commit();)

Code - Text: [Select]
  1. private void writeXrecord(String txt, int num)
  2. {
  3.         Document mydoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
  4.         Database madata = mydoc.Database;
  5.         Editor myed = mydoc.Editor;
  6.         //lock your document for during this transaction
  7.         using (DocumentLock @lock = mydoc.LockDocument()) {
  8.                 using (Transaction trans = madata.TransactionManager.StartTransaction()) {
  9.                         DBDictionary nod = (DBDictionary)trans.GetObject(madata.NamedObjectsDictionaryId, OpenMode.ForWrite);
  10.                         Xrecord myXrecord = new Xrecord();
  11.                         myXrecord.Data = new ResultBuffer(new TypedValue(Convert.ToInt32(DxfCode.Text), txt), new TypedValue(Convert.ToInt32(DxfCode.Int16), num));
  12.                         nod.SetAt("ViskoOPT", myXrecord);
  13.                         trans.AddNewlyCreatedDBObject(myXrecord, true);
  14.                         //commit your transaction
  15.                         trans.Commit();
  16.                 }
  17.         }
  18. }
« Last Edit: October 30, 2013, 06:58:18 AM by Bert »

n.yuan

  • Bull Frog
  • Posts: 348
Re: Write and read Xrecord
« Reply #2 on: October 30, 2013, 09:49:34 AM »
It is not a good practice to directly write data (XRecord) into NamedDictionary at database level. If you need to store data into NamedDictionary, you'd better create a new DBDictionary underneath the database-level NamedDictionary with a unique key following a good naming convention.

If you use some database browsing tool, such as MgdDB tool, you can see there are many DBDictionary entries in database-level NameDictionary, some are built-in for vanilla AutoCAD, some are for AutoCAD vertical cousins, some are from third party tools. Create your own DBDictionary underneath the top-level NamedDictionary and only mess it up inside your own dictionary.

dgorsman

  • Water Moccasin
  • Posts: 2437
Re: Write and read Xrecord
« Reply #3 on: October 30, 2013, 10:21:09 AM »
^ +1

Makes life much easier when you only have to worry about your own container.  If necessary (and I find it usually is) there can be dictionaries inside dictionaries (inside dicitonaries) to keep everything well organized.
If you are going to fly by the seat of your pants, expect friction burns.

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

krkec

  • Guest
Re: Write and read Xrecord
« Reply #4 on: October 31, 2013, 02:15:35 AM »
Thanks guys I tryed Berts solution and it works, but i will try other solutions

krkec

  • Guest
Re: Write and read Xrecord
« Reply #5 on: October 31, 2013, 02:19:26 AM »
where can i find MgdDB tool. I google it but i cant find it

MexicanCustard

  • Swamp Rat
  • Posts: 705
Re: Write and read Xrecord
« Reply #6 on: October 31, 2013, 07:43:57 AM »
Revit 2019, AMEP 2019 64bit Win 10