There are a few ways to modify a database object, through creating a Transacation explicitly, using an existing Transaction, creating an OpenCloseTransaction (not recommended though), and using the TopTransaction whatever it may be.
In this post, we are going to demonstrate how to do so.
The #1 means - creating a Transacation explicitly
public static void ModifyObject_Case1(ObjectId objId)
{
Transaction trans = objId.Database.TransactionManager.StartTransaction();
using(trans)
{
DBObject obj = (DBObject)trans.GetObject(objId, OpenMode.ForWrite);
if (obj is Circle)
{
Circle castObj = (Circle)obj;
castObj.Radius *= 2;
}
trans.Commit();
}
}
The #2 means - using an existing Transaction
public static void ModifyObject_Case2(Transaction trans, ObjectId objId)
{
DBObject obj = (DBObject)trans.GetObject(objId, OpenMode.ForWrite);
if (obj is Circle)
{
Circle castObj = (Circle)obj;
castObj.Radius *= 2;
}
//trans.Commit(); //Leave it to the caller.
}
The #3 means - creating an OpenCloseTransaction (not recommended in common situations)
public static void ModifyObject_Case3(ObjectId objId)
{
Transaction trans = objId.Database.TransactionManager.StartOpenCloseTransaction();
using (trans)
{
DBObject obj = (DBObject)trans.GetObject(objId, OpenMode.ForWrite);
if (obj is Circle)
{
Circle castObj = (Circle)obj;
castObj.Radius *= 2;
}
trans.Commit();
}
}
The #4 means - using the TopTransaction whatever it may be
public static void ModifyObject_Case4(ObjectId objId)
{
Transaction trans = objId.Database.TransactionManager.TopTransaction;
//using (trans)
//{
DBObject obj = (DBObject)trans.GetObject(objId, OpenMode.ForWrite);
if (obj is Circle)
{
Circle castObj = (Circle)obj;
castObj.Radius *= 2;
}
//trans.Commit(); //Leave it to the caller.
//}
}
A nice coder, Object Modifier, will be provided soon to address all these object modifying cases, common or not, recommended or not.
The leading edge AutoCAD .NET Addin Wizard (AcadNetAddinWizard) provides various project wizards, item wizards, coders and widgets to help program AutoCAD .NET addins.
Recent Comments