Author Topic: Edit a value in the NamedObjectDict  (Read 1763 times)

0 Members and 1 Guest are viewing this topic.

latour_g

  • Newt
  • Posts: 184
Edit a value in the NamedObjectDict
« on: June 13, 2016, 02:20:01 PM »
I would like to edit a value of a Data in the NamedObjectDict.  It's seem to be read only so I wonder if there is another way to achieve that.  Here is what I have so far.

Code - C#: [Select]
  1.         private void btnDict_Click(object sender, EventArgs e)
  2.         {
  3.             Document doc = AcadApp.DocumentManager.MdiActiveDocument;
  4.             Database db = doc.Database;
  5.             Editor ed = doc.Editor;
  6.  
  7.             using (doc.LockDocument())
  8.             {
  9.                 using (Transaction tr = HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction())
  10.                 {
  11.                     DBDictionary nod = tr.GetObject(HostApplicationServices.WorkingDatabase.NamedObjectsDictionaryId, OpenMode.ForRead) as DBDictionary;
  12.  
  13.                     if (nod.Contains("AcadDim_CRX"))
  14.                     {
  15.                         foreach (DBDictionaryEntry de in nod)
  16.                         {
  17.                             if (de.Key == "AcadDim_CRX")
  18.                             {
  19.                                 Xrecord layoutDB = (Xrecord)tr.GetObject(de.Value, OpenMode.ForRead);
  20.                                 string c = "";
  21.                                 foreach (TypedValue value in layoutDB.Data)
  22.                                 {
  23.                                     if (value.TypeCode == 66) value.Value = "1";    //----------------------> Error, this is read only
  24.                                 }                              
  25.                             }
  26.                         }
  27.                     }
  28.  
  29.                     tr.Commit();
  30.                 }
  31.             }
  32.         }
  33.  
  34.  
« Last Edit: June 13, 2016, 03:48:52 PM by latour_g »

n.yuan

  • Bull Frog
  • Posts: 348
Re: Edit a value in the NamedObjectDict
« Reply #1 on: June 13, 2016, 04:33:09 PM »
Shouldn't a database NamedDictionary's direct children be DBDictionaries? That is, each DBDictionaryEntry of database NamedDictionary is key-indexed ObjectId of an DBDictionary. So, in your case, the DBDictionaryEntry "AcadDim_CRX" should be a DBDictionary, not a XRecord (well, it could be XRecord behind the scene, but at least in .NET API, DBDictionary is not derived from XRecord publicly). While one may be directly add custom entry into Database's NamedDictionary as XRecord, it is bad practice. the direct child of Database.NamedDictionary should always be another DBDictionary (with key-indexed, thus NamedDictionary).

Thus, you case should be like:

...
if (node.Contains("AcadDim_CRX")
{
    var theDic=(DBDictionary)tr.GetObject(not.GetAt("AcadDim_CRX",OpenMode.ForRead);

    //Then you dig into each DBDictionaryEntry in this named dictionary, it could be XRecord, or another DbDictionary ...
   
}

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Edit a value in the NamedObjectDict
« Reply #2 on: June 13, 2016, 04:36:27 PM »
Hi,

++ with Norman about DBDictionary vs Xrecord as first entries in the NOD.

You do not need to iterate dictionary entries, you can access dictionary entries directly using the key (this is the main purpose of dictionaries).

You cannot directly edit a ResultBuffer neother a TypedValue value. You should convert the ResultBuffer into a TypedValue array, replace the TypedValue with a new one and re-build the ResultBuffer.

Non tested code:
Code - C#: [Select]
  1.         private void btnDict_Click(object sender, EventArgs e)
  2.         {
  3.             Document doc = AcadApp.DocumentManager.MdiActiveDocument;
  4.             Database db = doc.Database;
  5.             Editor ed = doc.Editor;
  6.  
  7.             using (doc.LockDocument())
  8.             {
  9.                 using (Transaction tr = HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction())
  10.                 {
  11.                     DBDictionary nod = tr.GetObject(HostApplicationServices.WorkingDatabase.NamedObjectsDictionaryId, OpenMode.ForRead) as DBDictionary;
  12.  
  13.                     if (nod.Contains("AcadDim_CRX"))
  14.                     {
  15.                         Xrecord layoutDB = (Xrecord)tr.GetObject(nod.GetAt("AcadDim_CRX"), OpenMode.ForWrite);
  16.                         TypedValue[] data = layoutDB.Data.AsArray();
  17.                         for (int i = 0; i < data.Length; i++)
  18.                         {
  19.                             TypedValue value = data[i];
  20.                             if (value.TypeCode == 66)
  21.                             {
  22.                                 data[i] = new TypedValue(66, "1");
  23.                                 break;
  24.                             }
  25.                         }
  26.                         layoutDB.Data = new ResultBuffer(data);
  27.                     }
  28.                     tr.Commit();
  29.                 }
  30.             }
  31.         }
Speaking English as a French Frog

latour_g

  • Newt
  • Posts: 184
Re: Edit a value in the NamedObjectDict
« Reply #3 on: June 13, 2016, 04:56:08 PM »
Thank you for all the information. 

Gile : I have test your code and it's doing what I need.