参考:https://spiderinnet1.typepad.com/blog/2013/03/autocad-net-safely-create-new-database-and-saveas.html
[CommandMethod("testdata")]
public static void testdata()
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
string path = @"c:\temp\test.dwg";
try
{
using (Database db = new Database(false, true))
{
db.ReadDwgFile(path, FileOpenMode.OpenForReadAndAllShare, false, null);
db.CloseInput(true);
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForRead);
foreach (ObjectId id in btr)
{
Entity ent = (Entity)tr.GetObject(id, OpenMode.ForWrite);
ent.ColorIndex = 1;
}
tr.Commit();
}
db.SaveAs(@"c:\temp\test_2red.dwg", DwgVersion.Current);
MessageBox.Show("保存完毕!");
}
}
catch (System.Exception ex)
{
ed.WriteMessage(ex.ToString());
}
}