public class EraseClouds
{
[CommandMethod("EraseClouds", CommandFlags.Session)]
public void EraseCloud()
{
Autodesk
.AutoCAD.Windows.OpenFileDialog dia
= new Autodesk
.AutoCAD.Windows.OpenFileDialog( "Select drawings to erase could entities from.", "", "dwg", "tmwCloudErase",
Autodesk.AutoCAD.Windows.OpenFileDialog.OpenFileDialogFlags.AllowMultiple
);
if (dia.ShowDialog() != DialogResult.OK) return;
Document cdoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database cdb = cdoc.Database;
string[] files = dia.GetFilenames();
Document doc;
DocumentLock dlock;
Database db;
BlockTable bt;
BlockTableRecord btr;
Entity ent;
LayerTable lt;
LayerTableRecord ltr;
bool didErase;
foreach (string name in files) {
didErase = false;
db = MyUtility.GetDatabaseAtPath(name, cdb, FileShare.Read, FindFileHint.XRefDrawing, out doc);
if (db == null) continue;
if (doc != null) dlock = doc.LockDocument();
using (Transaction trans = db.TransactionManager.StartTransaction()) {
bt = trans.GetObject(db.BlockTableId, OpenMode.ForRead, false, true) as BlockTable;
foreach (ObjectId id in bt){
btr = trans.GetObject(id, OpenMode.ForRead, false, true) as BlockTableRecord;
foreach (ObjectId oid in btr){
ent = trans.GetObject(oid, OpenMode.ForRead, false, true) as Entity;
if (string.Compare(ent.Layer, 0, "Cloud-", 0, 6, true) == 0 ) {
ent.UpgradeOpen();
ent.Erase();
ent.DowngradeOpen();
didErase = true;
}
}
}
lt = trans.GetObject(db.LayerTableId, OpenMode.ForRead, false, true) as LayerTable;
foreach (ObjectId id in lt){
ltr = trans.GetObject(id, OpenMode.ForRead, false, true) as LayerTableRecord;
if (string.Compare(ltr.Name, 0, "Cloud-", 0, 6, true) != 0) continue;
ltr.UpgradeOpen();
ltr.Erase();
ltr.DowngradeOpen();
didErase = true;
}
trans.Commit();
}
if (doc == null) {
if (didErase){
db.SaveAs(name, true, DwgVersion.Current, db.SecurityParameters);
cdoc.Editor.WriteMessage("\n Erased cloud entities from: " + name);
}
db.Dispose(); // this is because of the helper function
}
else {
if (didErase)
cdoc.Editor.WriteMessage("\n Erase cloud entities from opened drawing: " + name);
}
}
}
}