hi folks:
i meet a new problem,this is code:
using System ;
using Autodesk.AutoCAD.Runtime ;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Colors;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.LayerManager;
using Autodesk.AutoCAD.Windows;
[assembly: CommandClass(typeof(ClassLibrary.Class))]
namespace ClassLibrary {
///
/// Summary description for Class.
///
public class Class {
public Class() {
this.test();
}
// Define Command "tomato"
[CommandMethod("fileaction")]
// This method i want to open a dwg file and unload any layer;
public void test() {
Autodesk.AutoCAD.ApplicationServices.Document file=Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.Open("c:\\1.dwg",false);//if 1.dwg hasn't any problem,it is ok and exist
Database db=file.Database;
Transaction trans=db.TransactionManager.StartTransaction();
Editor ed=Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
try{
LayerTable lt=(LayerTable)trans.GetObject(db.LayerTableId,OpenMode.ForRead);
foreach (ObjectId ob in lt) {
LayerTableRecord ltr=(LayerTableRecord)trans.GetObject(ob,OpenMode.ForWrite);//when writing action ,occur exception error ,why?
if (ltr.IsLocked) {ltr.IsLocked=false;
}
if (ltr.IsFrozen) {ltr.IsFrozen=false;
}
}
trans.Commit();
}catch{
ed.WriteMessage("error!");
}finally{
trans.Dispose();
}
}
}
}