Database dbX = new Database(false, false);
Document docX = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.GetDocument(dbX);
DocumentLock docLoc1 = docX.LockDocument();
dbX.ReadDwgFile("c:\1.dwg", System.IO.FileShare.ReadWrite, true, "");
HostApplicationServices.WorkingDatabase = dbX;
using (Transaction trX = dbX.TransactionManager.StartTransaction())
{
LayoutManager.Current.CurrentLayout = layoutName;
DBDictionary lytDct = (DBDictionary)trX.GetObject(dbX.LayoutDictionaryId, OpenMode.ForWrite);
foreach (DBDictionaryEntry dictEntry in lytDct)
{
Layout lyt = (Layout)trX.GetObject(dictEntry.Value, OpenMode.ForWrite);
if (!lyt.ModelType && dictEntry.Key != layoutName)
{
lyt.Erase();
}
}
BlockTable bt = (BlockTable)trX.GetObject(dbX.BlockTableId, OpenMode.ForWrite);
ObjectId msId = bt[BlockTableRecord.ModelSpace];
BlockTableRecord btr = (BlockTableRecord)trX.GetObject(msId, OpenMode.ForWrite);
//Вставка внешней ссылки
string PathName = Application.DocumentManager.MdiActiveDocument.Name;
ObjectId acXrefId = dbX.AttachXref(PathName, System.IO.Path.GetFileNameWithoutExtension(PathName));
if (!acXrefId.IsNull)
{
// Attach the DWG reference to the current space
Autodesk.AutoCAD.Geometry.Point3d insPt = new Autodesk.AutoCAD.Geometry.Point3d(0, 0, 0);
using (BlockReference acBlkRef = new BlockReference(insPt, acXrefId))
{
BlockTableRecord acBlkTblRec;
acBlkTblRec = trX.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(dbX), OpenMode.ForWrite) as BlockTableRecord;
acBlkTblRec.AppendEntity(acBlkRef);
trX.AddNewlyCreatedDBObject(acBlkRef, true);
}
}
dbX.SaveAs("c:\1.dwg", DwgVersion.Current);
trX.Commit();
}