[CommandMethod("CopyLayout")]
public void CopyLayout()
{
string layoutName = "Test";
string fileName = @"C:\Testing\LayoutTest.dwt";
using (Database templateDb = new Database(false, true) as Database)
{
templateDb.ReadDwgFile(fileName, FileOpenMode.OpenForReadAndAllShare, true, "");
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Database db = doc.Database;
LayoutManager layoutManager = LayoutManager.Current;
using (Transaction trx = db.TransactionManager.StartTransaction())
using (Transaction templateTrx = templateDb.TransactionManager.StartTransaction())
{
DBDictionary layoutDictionary = trx.GetObject(db.LayoutDictionaryId, OpenMode.ForRead) as DBDictionary;
if (layoutDictionary.Contains(layoutName))
{
ed.WriteMessage("\nThis Layout is already in drawing.");
return;
}
DBDictionary templateLayoutDictionary = templateTrx.GetObject(templateDb.LayoutDictionaryId, OpenMode.ForRead) as DBDictionary;
if (!(templateLayoutDictionary.Contains(layoutName)))
{
ed.WriteMessage("\nTemplate does not contain a Layout with that name.");
return;
}
ObjectId newLayoutId = layoutManager.CreateLayout(layoutName);
Layout newLayout = trx.GetObject(newLayoutId, OpenMode.ForWrite) as Layout;
Layout templateLayout = templateLayoutDictionary.GetAt(layoutName).GetObject(OpenMode.ForWrite) as Layout;
newLayout.CopyFrom(templateLayout);
BlockTableRecord templateLayoutBtr = templateTrx.GetObject(templateLayout.BlockTableRecordId, OpenMode.ForRead) as BlockTableRecord;
ObjectIdCollection objIdColl = new ObjectIdCollection();
foreach (ObjectId id in templateLayoutBtr)
{
objIdColl.Add(id);
}
BlockTable extBt = templateDb.BlockTableId.GetObject(OpenMode.ForRead) as BlockTable;
IdMapping map = new IdMapping();
db.WblockCloneObjects(objIdColl, newLayout.BlockTableRecordId, map, DuplicateRecordCloning.Replace, false);
newLayout.Initialize();
PlotSettingsValidator psv = PlotSettingsValidator.Current;
psv.SetZoomToPaperOnUpdate(newLayout, true);
templateTrx.Commit();
trx.Commit();
}
layoutManager.CurrentLayout = layoutName;
}
}