[CommandMethod ("testsavelay")]
public static void testsavelay ()
{
try
{
string mainfullpath = @"c:\Temp\testdwg\layone.dwg";
Database tempDb; tempDb = new Database (false, true); tempDb.ReadDwgFile (mainfullpath, FileShare.ReadWrite, false, "");
tempDb.CloseInput (true);
using (var Tx = tempDb.TransactionManager.StartTransaction ())
{
string newName = "NewName";
LayerTable lt = Tx.GetObject (tempDb.LayerTableId, OpenMode.ForRead) as LayerTable;
XrefGraph xg = tempDb.GetHostDwgXrefGraph (false); GraphNode root = xg.RootNode;
for (int o = 0; o <root.NumOut; o ++)
{
XrefGraphNode node = root.Out (o) as XrefGraphNode;
var blockId = node.BlockTableRecordId;
var btr = (BlockTableRecord) Tx.GetObject (blockId, OpenMode.ForRead);
if (! node.IsNested)
{
foreach (ObjectId ltrId in lt)
{
LayerTableRecord ltr = Tx.GetObject (ltrId, OpenMode.ForRead) as LayerTableRecord;
if (ltr.Name.StartsWith (btr.Name))
{
ltr.UpgradeOpen ();
ltr.Name = ltr.Name.Replace (btr.Name, newName);
}
}
btr.UpgradeOpen ();
btr.Name = newName;
}
}
Tx.Commit ();
}
tempDb.SaveAs (mainfullpath, DwgVersion.Current);
tempDb.CloseInput(true);
}
catch (SystemException ex)
{
Editor ed = acadapp.DocumentManager.MdiActiveDocument.Editor;
ed.WriteMessage (ex.Message);
}
}