private ObjectId CopyLayout(String fileName, ref String layName, ref String paperName, ref ObjectId newViewPortId, int paperNo)
{
Database templateDb = new Database(false, true);
templateDb.ReadDwgFile(fileName, FileOpenMode.OpenForReadAndAllShare, true, "");
int pageWidth = 0;
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Database db = doc.Database;
Layout newLayout;
LayoutManager layManager = LayoutManager.Current;
using (Transaction trx = db.TransactionManager.StartTransaction())
{
using (Transaction templateTrx = templateDb.TransactionManager.StartTransaction())
{
DBDictionary layoutDictionary = trx.GetObject(db.LayoutDictionaryId, OpenMode.ForRead) as DBDictionary;
while (layoutDictionary.Contains(paperName + paperNo.ToString()))
{
ed.WriteMessage("\nЭтот чертеж уже содержит лист с именем {0}. ", paperName + paperNo.ToString());
PromptStringOptions strOpt = new PromptStringOptions("Укажите имя листа: ");
PromptResult strRes = ed.GetString(strOpt);
if (strRes.Status != PromptStatus.OK)
{
MessageBox.Show("Ввод данных отменен");
return new ObjectId();
}
paperName = strRes.StringResult;
}
DBDictionary templateLayoutDictionary = templateTrx.GetObject(templateDb.LayoutDictionaryId, OpenMode.ForRead) as DBDictionary;
if (!(templateLayoutDictionary.Contains(layName)))
{
ed.WriteMessage("\nTemplate does not contain a Layout with that name.");
return new ObjectId();
}
ObjectId newLayoutId = layManager.CreateLayout(paperName+paperNo.ToString());
newLayout = trx.GetObject(newLayoutId, OpenMode.ForWrite) as Layout;
newLayout.LayoutName = paperName + paperNo.ToString();
Layout templateLayout = templateLayoutDictionary.GetAt(layName).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);
newViewPortId = newLayout.Initialize();
templateTrx.Commit();
trx.Commit();
}
}
return newLayout.Id;
//return newViewPortId;
}