public ObjectId ImportLayout(String fileName, ref String layName, ref String paperName, ref ObjectId newViewPortId, int paperNo)
{
// Get the current document and database
Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
// Specify the layout name and drawing file to work with
string layoutName = layName;
string filename = fileName;
// Create a new database object and open the drawing into memory
Database acExDb = new Database(false, true);
acExDb.ReadDwgFile(filename, FileOpenMode.OpenForReadAndAllShare, true, "");
ObjectId layId = ObjectId.Null;
using (DocumentLock dLock = acDoc.LockDocument(DocumentLockMode.ProtectedAutoWrite, null, null, true))
// Create a transaction for the external drawing
{
using (Transaction acTransEx = acExDb.TransactionManager.StartTransaction())
{
// Get the layouts dictionary
DBDictionary layoutsEx =
acTransEx.GetObject(acExDb.LayoutDictionaryId,
OpenMode.ForRead) as DBDictionary;
// Check to see if the layout exists in the external drawing
if (layoutsEx.Contains(layoutName) == true)
{
// Get the layout and block objects from the external drawing
Layout layEx =
layoutsEx.GetAt(layoutName).GetObject(OpenMode.ForRead) as Layout;
BlockTableRecord blkBlkRecEx =
acTransEx.GetObject(layEx.BlockTableRecordId,
OpenMode.ForRead) as BlockTableRecord;
// Get the objects from the block associated with the layout
ObjectIdCollection idCol = new ObjectIdCollection();
foreach (ObjectId id in blkBlkRecEx)
{
idCol.Add(id);
}
// Create a transaction for the current drawing
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// Get the block table and create a new block
// then copy the objects between drawings
BlockTable blkTbl =
acTrans.GetObject(acCurDb.BlockTableId,
OpenMode.ForWrite) as BlockTable;
using (BlockTableRecord blkBlkRec = new BlockTableRecord())
{
int layoutCount = layoutsEx.Count - 1;
blkBlkRec.Name = paperName + paperNo + layoutsEx.Count.ToString();
blkTbl.Add(blkBlkRec);
acTrans.AddNewlyCreatedDBObject(blkBlkRec, true);
acExDb.WblockCloneObjects(idCol,
blkBlkRec.ObjectId,
new IdMapping(),
DuplicateRecordCloning.Ignore,
false);
// Create a new layout and then copy properties between drawings
DBDictionary layouts =
acTrans.GetObject(acCurDb.LayoutDictionaryId,
OpenMode.ForWrite) as DBDictionary;
// Добавка к коду
LayoutManager layMan = LayoutManager.Current;
using (Layout lay = new Layout())
{
lay.LayoutName = paperName + paperNo;
lay.AddToLayoutDictionary(acCurDb, blkBlkRec.ObjectId);
acTrans.AddNewlyCreatedDBObject(lay, true);
lay.CopyFrom(layEx);
layId = lay.Id;
newViewPortId = lay.Initialize();
// Тут ловим фатальную ошибку - Unhandled exception writing 0x0340
//layMan.CurrentLayout = lay.LayoutName;
DBDictionary plSets =
acTrans.GetObject(acCurDb.PlotSettingsDictionaryId,
OpenMode.ForRead) as DBDictionary;
// Check to see if a named page setup was assigned to the layout,
// if so then copy the page setup settings
if (lay.PlotSettingsName != "")
{
// Check to see if the page setup exists
if (plSets.Contains(lay.PlotSettingsName) == false)
{
plSets.UpgradeOpen();
using (PlotSettings plSet = new PlotSettings(lay.ModelType))
{
plSet.PlotSettingsName = lay.PlotSettingsName;
plSet.AddToPlotSettingsDictionary(acCurDb);
acTrans.AddNewlyCreatedDBObject(plSet, true);
DBDictionary plSetsEx =
acTransEx.GetObject(acExDb.PlotSettingsDictionaryId,
OpenMode.ForRead) as DBDictionary;
PlotSettings plSetEx =
plSetsEx.GetAt(lay.PlotSettingsName).GetObject(
OpenMode.ForRead) as PlotSettings;
plSet.CopyFrom(plSetEx);
}
}
}
}
}
// Regen the drawing to get the layout tab to display
//acDoc.Editor.Regen();
// Save the changes made
acTrans.Commit();
acTrans.Dispose();
}
}
else
{
// Display a message if the layout could not be found in the specified drawing
acDoc.Editor.WriteMessage("\nLayout '" + layoutName +
"' could not be imported from '" + filename + "'.");
}
// Discard the changes made to the external drawing file
acTransEx.Abort();
}
// Close the external drawing file
acExDb.Dispose();
}
return layId;
}