I just happen to start messing with something that was grabbing everything from a closed drawings model space.
And hard-coded it quickly for testing making sure I set all the parameters correct in functions and trying it in different scenarios with drawing open etc......
[CommandMethod("LoadStandardDetail")]
public void LoadStandardDetail()
{
string fileName = @"C:\Users\Jeff\Documents\HCSgroup\Detail Library\E401.dwg";
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
using (Database detailDb = new Database(false, true))
{
detailDb.ReadDwgFile(fileName, FileOpenMode.OpenForReadAndWriteNoShare, true, null);
using (Transaction detailTrx = detailDb.TransactionManager.StartTransaction())
{
BlockTable detailBt = (BlockTable)detailTrx.GetObject(detailDb.BlockTableId, OpenMode.ForRead);
BlockTableRecord detailModelSpace = (BlockTableRecord)detailTrx.GetObject(detailBt[BlockTableRecord.ModelSpace], OpenMode.ForRead);
ObjectIdCollection detailEntitiesIds = new ObjectIdCollection();
foreach (ObjectId objId in detailModelSpace)
{
detailEntitiesIds.Add(objId);
}
using (Transaction trx = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)trx.GetObject(db.BlockTableId, OpenMode.ForRead);
ObjectId modelSpaceId = bt[BlockTableRecord.ModelSpace];
IdMapping map = new IdMapping();
detailDb.WblockCloneObjects(detailEntitiesIds, modelSpaceId, map, DuplicateRecordCloning.MangleName, false);
trx.Commit();
}
detailTrx.Commit();
}
}
}
Which can be modified easily to print all the layers from a closed drawing to the command line and then modify to your specifications
[CommandMethod("PrintLayers")]
public void PrintLayers()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
string fileName = @"C:\Users\Jeff\Documents\HCSgroup\Detail Library\E401.dwg";
List<string> layerNames = new List<string>();
using (Database detailDb = new Database(false, true))
{
detailDb.ReadDwgFile(fileName, FileOpenMode.OpenForReadAndWriteNoShare, true, null);
using (Transaction detailTrx = detailDb.TransactionManager.StartTransaction())
{
LayerTable detailLayerTbl = (LayerTable)detailTrx.GetObject(detailDb.LayerTableId, OpenMode.ForRead);
foreach (ObjectId objId in detailLayerTbl)
{
LayerTableRecord layerTblRecord = (LayerTableRecord)detailTrx.GetObject(objId, OpenMode.ForRead);
layerNames.Add(layerTblRecord.Name);
}
detailTrx.Commit();
}
}
foreach (string layerName in layerNames)
{
ed.WriteMessage("\n{0}", layerName);
}
}
}
}