Thanks, I missed that one. @shricharana_bharadwaj Below is another class for DbExtentions. I think I got this from @_gile It might already be included in his library here. I know he also has Active.cs in it.
/// <summary>
/// Contains extension methods that facilitate working with objects in the context of a transaction.
/// </summary>
public static class DbExtensions
{
/// <summary>
/// Extension method that allows you to iterate through model space and perform an action
/// on a specific type of object.
/// </summary>
/// <typeparam name="T">The type of object to search for.</typeparam>
/// <param name="db">The database to use.</param>
/// <param name="action">A delegate that is called for each object found of the specified type.</param>
public static void ForEach<T>(this Database db, Action<T> action)
where T : Entity
{
db.UsingModelSpace((tr, modelSpace) => modelSpace.ForEach(tr, action));
}
/// <summary>
/// Extension method that allows you to iterate through model space and perform an action
/// on a specific type of object.
/// </summary>
/// <typeparam name="T">The type of object to search for.</typeparam>
/// <param name="db">The database to use.</param>
/// <param name="predicate"></param>
/// <param name="action">A delegate that is called for each object found of the specified type.</param>
public static void ForEach<T>(this Database db, Func<T, bool> predicate, Action<T> action)
where T : Entity
{
db.ForEach<T>(
obj =>
{
if (predicate(obj))
action(obj);
});
}
/// <summary>
/// Iterates through the specified symbol table, and performs an action on each symbol table record.
/// </summary>
/// <typeparam name="T">The type of symbol table record.</typeparam>
/// <param name="db">The database.</param>
/// <param name="tableId">The table id.</param>
/// <param name="action">A delegate that is called for each record.</param>
public static void ForEach<T>(this Database db, ObjectId tableId, Action<T> action) where T : SymbolTableRecord
{
db.UsingTransaction(tr => tableId.OpenAs<SymbolTable>(tr).Cast<ObjectId>().ForEach(tr, action));
}
/// <summary>
/// Extension method that allows you to iterate through model space and perform an action
/// on a specific type of object.
/// </summary>
/// <typeparam name="T">The type of object to search for.</typeparam>
/// <param name="document">The document to use.</param>
/// <param name="action">A delegate that is called for each object found of the specified type.</param>
/// <remarks>This method locks the specified document.</remarks>
public static void ForEach<T>(this Document document, Action<T> action)
where T : Entity
{
using (document.LockDocument())
{
document.Database.ForEach(action);
}
}
/// <summary>
/// Extension method that allows you to iterate through the objects in a block table
/// record and perform an action on a specific type of object.
/// </summary>
/// <typeparam name="T">The type of object to search for.</typeparam>
/// <param name="btr">The block table record to iterate.</param>
/// <param name="tr">The active transaction.</param>
/// <param name="action">A delegate that is called for each object found of the specified type.</param>
public static void ForEach<T>(this IEnumerable<ObjectId> btr, Transaction tr, Action<T> action)
where T : DBObject
{
RXClass theClass = RXObject.GetClass(typeof(T));
// Loop through the entities in model space
foreach (ObjectId objectId in btr)
{
// Look for entities of the correct type
if (objectId.ObjectClass.IsDerivedFrom(theClass))
{
action(objectId.OpenAs<T>(tr));
}
}
}
/// <summary>
/// Extension method that returns the LayerTableRecord's of the given database.
/// </summary>
/// <param name="database">The database.</param>
/// <param name="tr">A <see>Autodesk.AutoCAD.DatabaseServices.Transaction</see> within which to read the LayerTable.</param>
/// <returns>An enumerable list of <see>Autodesk.AutoCAD.DatabaseServices.LayerTableRecord</see> objects.</returns>
public static IEnumerable<LayerTableRecord> Layers(this Database database, Transaction tr)
{
return database.LayerTableId
.OpenAs<LayerTable>(tr)
.Cast<ObjectId>()
.OfType<LayerTableRecord>();
}
/// <summary>
/// Opens a database-resident object as the specified type within the context of the specified transaction,
/// using the specified open mode.
/// </summary>
/// <typeparam name="T">The type of object that the objectId represents.</typeparam>
/// <param name="objectId">The object id.</param>
/// <param name="tr">The transaction.</param>
/// <param name="openMode">The open mode.</param>
/// <returns>The database-resident object.</returns>
public static T OpenAs<T>(this ObjectId objectId, Transaction tr, OpenMode openMode)
where T : DBObject
{
return (T)tr.GetObject(objectId, openMode);
}
/// <summary>
/// Locks the document, opens the specified object, and passes it to the specified delegate.
/// </summary>
/// <typeparam name="T">The type of object the objectId represents.</typeparam>
/// <param name="document">The document.</param>
/// <param name="objectId">The object id.</param>
/// <param name="openMode">The open mode.</param>
/// <param name="action">A delegate that takes the opened object as an argument.</param>
public static void OpenAs<T>(this Document document, ObjectId objectId, OpenMode openMode, Action<T> action)
where T : DBObject
{
document.UsingTransaction(tr => action(objectId.OpenAs<T>(tr, openMode)));
}
/// <summary>
/// Opens a database-resident object as the specified type (for read) within the context of the specified transaction.
/// </summary>
/// <typeparam name="T">The type of object that the objectId represents.</typeparam>
/// <param name="objectId">The object id.</param>
/// <param name="tr">The transaction.</param>
/// <returns>The database-resident object.</returns>
public static T OpenAs<T>(this ObjectId objectId, Transaction tr)
where T : DBObject
{
return (T)tr.GetObject(objectId, OpenMode.ForRead);
}
/// <summary>
/// Opens a database-resident object as the specified type, using the specifed OpenMode within the context of the specified delegate.
/// </summary>
/// <typeparam name="T">The type of object that the objectId represents.</typeparam>
/// <param name="objectId">The object id.</param>
/// <param name="openMode">An <see>Autodesk.Autocad.DatabaseServices.OpenMode</see> constant.</param>
/// <param name="action">A delegate that takes the transaction and the OpenMode as arguments.</param>
public static void OpenAs<T>(this ObjectId objectId, OpenMode openMode, Action<T> action)
where T : DBObject
{
objectId.Database.UsingTransaction(tr => action(objectId.OpenAs<T>(tr, openMode)));
}
/// <summary>
/// Opens a database-resident object as the specified type (for write) within the context of the specified delegate.
/// </summary>
/// <typeparam name="T">The type of object that the objectId represents.</typeparam>
/// <param name="objectId">The object id.</param>
/// <param name="action">A delegate that takes a transaction as an argument.</param>
public static void OpenForWriteAs<T>(this ObjectId objectId, Action<T> action)
where T : DBObject
{
objectId.Database.UsingTransaction(tr => action(objectId.OpenAs<T>(tr, OpenMode.ForWrite)));
}
/// <summary>
/// Used to get a single value from a database-resident object.
/// </summary>
/// <typeparam name="TObject">The type of the object.</typeparam>
/// <typeparam name="TResult">The type of the result.</typeparam>
/// <param name="objectId">The object id.</param>
/// <param name="func">A delegate that takes the object as an argument and returns the value.</param>
/// <returns>A value of the specified type.</returns>
public static TResult GetValue<TObject, TResult>(this ObjectId objectId, Func<TObject, TResult> func)
where TObject : DBObject
{
TResult result = default;
objectId.Database.UsingTransaction(
tr =>
{
result = func(objectId.OpenAs<TObject>(tr));
});
return result;
}
/// <summary>
/// Executes a delegate function in the context of a transaction, and passes it the specified block table record.
/// </summary>
/// <param name="database">The database.</param>
/// <param name="blockName">Name of the block.</param>
/// <param name="action">A delegate that takes the transaction and the BlockTableRecord as arguments.</param>
public static void UsingBlockTable(this Database database, string blockName, Action<Transaction, BlockTableRecord> action)
{
database.UsingTransaction(
tr =>
{
// Get the block table
var blockTable = database.BlockTableId.OpenAs<BlockTable>(tr);
// Get the block table record
var tableRecord = blockTable[blockName].OpenAs<BlockTableRecord>(tr);
// Invoke the method
action(tr, tableRecord);
});
}
/// <summary>
/// Executes a delegate function in the context of a transaction, and passes it the collection
/// of Entity objects for the specified block table record.
/// </summary>
/// <param name="database">The database.</param>
/// <param name="blockName">Name of the block.</param>
/// <param name="action">A delegate that takes the transaction and the Entity collection as arguments.</param>
public static void UsingBlockTable(this Database database, string blockName, Action<IEnumerable<Entity>> action)
{
database.UsingBlockTable
(blockName,
(tr, blockTable) => action(from id in blockTable select id.OpenAs<Entity>(tr)));
}
/// <summary>
/// Executes a delegate function in the context of a transaction, and passes it the collection
/// of ObjectIds for the specified block table record.
/// </summary>
/// <param name="database">The database.</param>
/// <param name="blockName">Name of the block.</param>
/// <param name="action">A delegate that takes the transaction and the ObjectIds as arguments.</param>
public static void UsingBlockTable(this Database database, string blockName, Action<Transaction, IEnumerable<ObjectId>> action)
{
database.UsingTransaction(
tr =>
{
// Get the block table
var blockTable = database.BlockTableId.OpenAs<BlockTable>(tr);
// Get the block table record
var tableRecord = blockTable[blockName].OpenAs<BlockTableRecord>(tr);
// Invoke the method
action(tr, tableRecord.Cast<ObjectId>());
});
}
/// <summary>
/// Executes a delegate function in the context of a transaction,
/// and passes it the block table record of the current space.
/// </summary>
/// <param name="database">The database.</param>
/// <param name="blockName">Name of the block.</param>
/// <param name="action">A delegate that takes the transaction and the BlockTableRecord as arguments.</param>
public static void UsingCurrentSpace(this Database database, Action<Transaction, BlockTableRecord> action)
{
database.UsingTransaction(
tr =>
{
// Get the block table
var currentSpaceTableRec = database.CurrentSpaceId.OpenAs<BlockTableRecord>(tr, OpenMode.ForWrite);
// Invoke the method
action(tr, currentSpaceTableRec);
});
}
/// <summary>
/// Executes a delegate function in the context of a transaction, and passes it the collection
/// of ObjectIds for the current space block table record.
/// </summary>
/// <param name="database">The database.</param>
/// <param name="blockName">Name of the block.</param>
/// <param name="action">A delegate that takes the transaction and the ObjectId's as arguments.</param>
public static void UsingCurrentSpace(this Database database, Action<Transaction, IEnumerable<ObjectId>> action)
{
database.UsingTransaction(
tr =>
{
// Get the block table
var currentSpaceTableRec = database.CurrentSpaceId.OpenAs<BlockTableRecord>(tr, OpenMode.ForWrite);
// Invoke the method
action(tr, currentSpaceTableRec.Cast<ObjectId>());
});
}
/// <summary>
/// Executes a delegate function in the context of a transaction, and passes it the collection
/// of objects from the current space of the specified type.
/// </summary>
/// <param name="database">The database.</param>
/// <param name="action">A delegate that takes the transaction and the Entity collection as arguments.</param>
/// <typeparamref name="T">The type of object to retrieve.</typeparamref>
public static void UsingCurrentSpace<T>(this Database database, Action<IEnumerable<T>> action) where T : Entity
{
database.UsingCurrentSpace(
(tr, csOIDs) =>
{
RXClass rxClass = RXObject.GetClass(typeof(T));
action(from id in csOIDs
where id.ObjectClass.IsDerivedFrom(rxClass)
select id.OpenAs<T>(tr));
});
}
/// <summary>
/// Executes a delegate function with the collection of layers in the specified database.
/// </summary>
/// <param name="database">The database.</param>
/// <param name="action">A delegate that takes the collection of layers as an argument.</param>
public static void UsingLayerTable(this Database database, Action<IEnumerable<LayerTableRecord>> action)
{
database.UsingTransaction(
tr => action(from ObjectId id in database.LayerTableId.OpenAs<LayerTable>(tr)
select id.OpenAs<LayerTableRecord>(tr)));
}
/// <summary>
/// Executes a delegate function in the context of a transaction, and passes it the collection
/// of objects from model space of the specified type.
/// </summary>
/// <param name="database">The database.</param>
/// <param name="action">A delegate that takes the transaction and the Entity collection as arguments.</param>
/// <typeparamref name="T">The type of object to retrieve.</typeparamref>
public static void UsingModelSpace<T>(this Database database, Action<IEnumerable<T>> action) where T : Entity
{
database.UsingModelSpace(
(tr, ms) =>
{
RXClass rxClass = RXObject.GetClass(typeof(T));
action(from id in ms
where id.ObjectClass.IsDerivedFrom(rxClass)
select id.OpenAs<T>(tr));
});
}
/// <summary>
/// Executes a delegate function in the context of a transaction, and passes it the model space block table record.
/// </summary>
/// <param name="database">The database.</param>
/// <param name="action">A delegate that takes the transaction and the ObjectId's as arguments.</param>
public static void UsingModelSpace(this Database database, Action<Transaction, BlockTableRecord> action)
{
database.UsingBlockTable(BlockTableRecord.ModelSpace, action);
}
/// <summary>
/// Executes a delegate function in the context of a transaction, and passes it the collection
/// of ObjectIds for the model space block table record.
/// </summary>
/// <param name="database">The database.</param>
/// <param name="action">A delegate that takes the transaction and the ObjectIds as arguments.</param>
public static void UsingModelSpace(this Database database, Action<Transaction, IEnumerable<ObjectId>> action)
{
database.UsingBlockTable(BlockTableRecord.ModelSpace, action);
}
/// <summary>
/// Executes a delegate function in the context of a transaction, and passes it the collection
/// of objects from paper space of the specified type.
/// </summary>
/// <param name="database">The database.</param>
/// <param name="action">A delegate that takes the transaction and the Entity collection as arguments.</param>
/// <typeparamref name="T">The type of object to retrieve.</typeparamref>
public static void UsingPaperSpace<T>(this Database database, Action<IEnumerable<T>> action) where T : Entity
{
database.UsingPaperSpace(
(tr, ps) =>
{
RXClass rxClass = RXObject.GetClass(typeof(T));
action(from id in ps
where id.ObjectClass.IsDerivedFrom(rxClass)
select id.OpenAs<T>(tr));
});
}
/// <summary>
/// Executes a delegate function in the context of a transaction, and passes it the paper space block table record.
/// </summary>
/// <param name="database">The database.</param>
/// <param name="action">A delegate that takes the transaction and the ObjectIds as arguments.</param>
public static void UsingPaperSpace(this Database database, Action<Transaction, BlockTableRecord> action)
{
database.UsingBlockTable(BlockTableRecord.PaperSpace, action);
}
/// <summary>
/// Executes a delegate function in the context of a transaction, and passes it the collection
/// of ObjectIds for the paper space block table record.
/// </summary>
/// <param name="database">The database.</param>
/// <param name="action">A delegate that takes the transaction and the ObjectIds as arguments.</param>
public static void UsingPaperSpace(this Database database, Action<Transaction, IEnumerable<ObjectId>> action)
{
database.UsingBlockTable(BlockTableRecord.PaperSpace, action);
}
/// <summary>
/// Executes a delegate function within the context of a transaction on the specified database.
/// </summary>
/// <param name="database">The database.</param>
/// <param name="action">A delegate that takes the <b>Transaction</b> as an argument.</param>
public static void UsingTransaction(this Database database, Action<Transaction> action)
{
using (var tr = database.TransactionManager.StartTransaction())
{
try
{
action(tr);
tr.Commit();
}
catch (Exception)
{
tr.Abort();
throw;
}
}
}
/// <summary>
/// Executes a delegate function within the context of a transaction on the specified document.
/// The document is locked before the transaction starts.
/// </summary>
/// <param name="document">The document.</param>
/// <param name="action">A delegate that takes the <b>Transaction</b> as an argument.</param>
public static void UsingTransaction(this Document document, Action<Transaction> action)
{
using (document.LockDocument())
{
document.Database.UsingTransaction(action);
}
}
}
Ed
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to
post your code.