PDA

View Full Version : How to update Xdata on existing entities in ACAD with objectARX?



ivan.markov
2007-06-06, 03:21 PM
Hi,

I am writing a C# application for ACAD using objectARX. I need to update the Xdata in a DBDictionary. I can get to the correct Xdata and iterate through it fine. My problem is when I try to modify it and I commit my transaction and save the database none of the changes get registered. I am posting some code bellow. Any help will be greatly appreciated.


TypedValue[] Xdata;
Database db = new Database();
Transaction trans=null;
ResultBuffer newXdata=null;
DBDictionary datadic=getDictionary(file.FullName);
//getDictionary just return a copy of the DBDictionary that I am looking for in a file
//that I open through the ReadDwgFile() command
//I used the switch command to create a modified copy of the existing Xdata
//because the TypedValue.Value and TypedValue.TypeCode are read only

if (datadic != null)
{
Xdata = datadic.XData.AsArray();
TypedValue[] tempXdata=new TypedValue[Xdata.Length];
for (int i = 0; i < Xdata.Length; i++)
{
switch(i)
{
case 6: tempXdata[i] = new TypedValue(Xdata[i].TypeCode, file.FullName);//'location
break;
case 7: tempXdata[i] = new TypedValue(Xdata[i].TypeCode, System.DateTime.Today.ToString("MMMM d, yyyy"));//'last mod date "MMM dd, yyyy"
break;
case 8: tempXdata[i] = new TypedValue(Xdata[i].TypeCode, HEISU_Forms.HEISUforms.HEISUfrm.UserName);//'last mod by
break;
case 9: tempXdata[i] = new TypedValue(Xdata[i].TypeCode, projInfo.projectDescription);//'Project Name
break;
case 10: tempXdata[i] = new TypedValue(Xdata[i].TypeCode, projInfo.projectNumber);//'Project Number
break;
case 12: tempXdata[i] = new TypedValue(Xdata[i].TypeCode, HEISU_Forms.HEISUforms.HEISUfrm.UserName);//'Creator
break;
case 16: tempXdata[i] = new TypedValue(Xdata[i].TypeCode, projInfo.fullStateName);//'full state name
break;
case 20: tempXdata[i] = new TypedValue(Xdata[i].TypeCode, projInfo.logoState);//'1 if logo is legal, 0 for eor
break;
default: tempXdata[i] = new TypedValue(Xdata[i].TypeCode, Xdata[i].Value);//transfer all other Xdata
break;
}
}
newXdata = new ResultBuffer(tempXdata);
}
try
{
DBObject obj;
db.ReadDwgFile(file.FullName, FileShare.ReadWrite, false, "");
trans = db.TransactionManager.StartTransaction();
obj=trans.GetObject(datadic.ObjectId, OpenMode.ForWrite);

obj.UpgradeOpen();
obj.XData = newXdata;
newXdata.Dispose();
obj.Close();
trans.Commit();
trans.Dispose();
db.SaveAs(@"U:test.dwg", null);
}
finally
{
try
{
db.CloseInput(true);

}
catch { }
}

ivan.markov
2007-06-06, 10:25 PM
If anyone is interested I found the answer to my previous question. I had to move the code from the getDictionary to the main function thus allowing me to open a connection to the ACAD db only once with ReadWrite access. That fixed my problem and the Xdata did get updated after that.