Yes, I am defining a rectangular area by giving 2 points in the code and then I am checking if any block reference is in the specified area if yes I add its block id to ObjectIdCollection and then I use this collection to copy the blocks to a new drawing. Here is the code for your reference.
ObjectIdCollection bIds = new ObjectIdCollection();
[CommandMethod("CopyEntitiesInArea")]
public void CopyEntitiesInArea()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
Point3d startPoint = new Point3d(720584.413, 2784591.731, 0);
Point3d endPoint = new Point3d(725570.341, 2783949.934, 0);
// Define the selection window
PromptSelectionResult selRes = ed.SelectWindow(startPoint, endPoint);
if (selRes.Status == PromptStatus.OK)
{
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
foreach (ObjectId objId in selRes.Value.GetObjectIds())
{
Entity ent = tr.GetObject(objId, OpenMode.ForRead) as Entity;
if (ent is BlockReference)
{
BlockReference blockRef = ent as BlockReference;
string blockName = blockRef.Name;
BlockTableRecord modelSpace = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
bIds.Add(blockRef.Id);
}
else
{
ed.WriteMessage("This entity is not a block");
}
}
tr.Commit();
}
}
else
{
ed.WriteMessage("\nNo objects selected.");
}
Document document = Application.DocumentManager.MdiActiveDocument;
Database database = document.Database;
Editor editor = document.Editor;
string localRoot = Application.GetSystemVariable("LOCALROOTPREFIX") as string;
string tempPath = "D:\\CopyBlock.DWG";
DocumentCollection documentCollection = Application.DocumentManager;
Document newDocument = documentCollection.Add(tempPath);
Database newDatabase = newDocument.Database;
using (DocumentLock docLock = newDocument.LockDocument())
{
Application.DocumentManager.MdiActiveDocument = newDocument;
using (Transaction transaction = newDatabase.TransactionManager.StartTransaction())
{
IdMapping mapping = new IdMapping();
database.WblockCloneObjects(bIds, newDatabase.BlockTableId, mapping, DuplicateRecordCloning.Ignore, false);
transaction.Commit();
}
}
documentCollection.MdiActiveDocument = newDocument;
newDocument.Database.SaveAs("Drawing2", DwgVersion.Current);
}