namespace DrawingSynchronizer
{
class Drawing
{
public List<Database> GetDrawingDatabases(string path)
{
List<Database> originDrawings = new List<Database>();
try
{
DirectoryInfo d = new DirectoryInfo(path);
FileInfo[] Files = d.GetFiles("*.dwg");
foreach (FileInfo file in Files)
{
var fileName = Path.GetFileName(file.FullName);
string dwgFlpath = path + fileName;
using (Database db = new Database(false, true))
{
db.ReadDwgFile(dwgFlpath, FileOpenMode.OpenForReadAndAllShare, false, null);
db.CloseInput(true);
originDrawings.Add(db);
}
}
}
catch (Exception ex)
{
Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(ex.ToString());
}
return originDrawings;
}
public ObjectIdCollection GetObjects(Database db)
{
ObjectIdCollection objectIdCollection = new ObjectIdCollection();
Transaction tr = db.TransactionManager.StartTransaction();
using ( tr )
{
BlockTable bt = (BlockTable) tr.GetObject(db.BlockTableId, OpenMode.ForRead, false, false);
BlockTableRecord btr = (BlockTableRecord) tr.GetObject(bt[ BlockTableRecord.ModelSpace ], OpenMode.ForRead);
foreach ( var item in btr )
{
Entity ent = (Entity) tr.GetObject(item, OpenMode.ForRead);
if ( ent.Layer.Contains("0") )
{
objectIdCollection.Add(ent.ObjectId);
}
}
tr.Commit();
}
return objectIdCollection;
}
}
}