Here is a start
Add ideas that Thorsten(kaefer) mentioned
Unless I am wrong I can not think of a situation where you would call Transaction.Commit() in a for each loop.
[CommandMethod("SelectObjectsOnscreen")]
public static void SelectObjectsOnscreen()
{
// Get the current document and database
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
string txt = "";
Point3d mtxtInsertion = Point3d.Origin;
// Start a transaction
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// Request for objects to be selected in the drawing area
PromptSelectionResult acSSPrompt = acDoc.Editor.GetSelection();
// If the prompt status is OK, objects were selected
if (acSSPrompt.Status == PromptStatus.OK)
{
SelectionSet acSSet = acSSPrompt.Value;
// Step through the objects in the selection set
DBDictionary databaseDictionary = acCurDb.LayoutDictionaryId.GetObject(OpenMode.ForRead) as DBDictionary;
foreach (SelectedObject acSSObj in acSSet)
{
// Check to make sure a valid SelectedObject object was returned
{
// Open the selected object for write
Entity acEnt = acTrans.GetObject(acSSObj.ObjectId,
OpenMode.ForWrite) as Entity;
//[b] Somehow I need to fetch the entities from the rest of the layouts, based on the entities selected from the first layout[/b]
if (acEnt is MText)
{
MText mtxt = acEnt as MText;
txt = mtxt.Contents;
mtxtInsertion = mtxt.Location;
acEnt.Erase();
}
}
}
foreach (DBDictionaryEntry dictionaryEntry in databaseDictionary)
{
Layout lyt = dictionaryEntry.Value.GetObject(OpenMode.ForRead) as Layout;
BlockTableRecord btr = lyt.BlockTableRecordId.GetObject(OpenMode.ForRead) as BlockTableRecord;
foreach (ObjectId objId in btr)
{
Entity ent = objId.GetObject(OpenMode.ForRead) as Entity;
if (ent is MText)
{
MText mtxt = ent as MText;
if (mtxt.Contents == txt && mtxt.Location == mtxtInsertion)
{
mtxt.UpgradeOpen();
mtxt.Erase();
}
}
}
}
// Save the new object to the database
acTrans.Commit();
}
}
// Dispose of the transaction
}