我正在尝试将线型加载到活动文档中,以便我可以将图层线型设置为我尝试加载的线型。现在的问题是当我尝试加载它时,autocad 给了我一个例外:eWasOpenForRead。所以我尝试通过命令行发送它并且它有效,但是我写的应用程序刚刚结束......有人知道为什么会发生这种情况吗?
代码:
private void defaultLayerFix() {
// Open a transaction for fixing layers that have the correct name
// but not the correct color or linetype.
using(curTrans = db.TransactionManager.StartOpenCloseTransaction()) {
// Open the layerTable and lineType table for read.
dwgLyrTbl = curTrans.GetObject(
db.LayerTableId,OpenMode.ForRead) as LayerTable;
acLinTbl = curTrans.GetObject(
db.LinetypeTableId,OpenMode.ForRead) as LinetypeTable;
// Check each layer against the standard layers DataSet.
foreach (ObjectId layID in dwgLyrTbl) {
LayerTableRecord curLayer = curTrans.GetObject(
layID,OpenMode.ForRead) as LayerTableRecord;
var layerFound = _LayerDataTable.Rows.Find(curLayer.Name.ToUpper());
if(layerFound != null){
// Upgrade the layerTable and LayerRecord for write.
dwgLyrTbl.UpgradeOpen();
curLayer.UpgradeOpen();
// modify the color of the layer
curLayer.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(
Autodesk.AutoCAD.Colors.ColorMethod.ByAci, (short)layerFound[2]);
// I tried to put it in its own transaction in hopes that it would help.. but it didnt
using(Transaction tempTrans = db.TransactionManager.StartOpenCloseTransaction()){
// if the layer is not loaded in the current dwg
if(!acLinTbl.Has((string)layerFound[3]))
try {
db.LoadLineTypeFile((string)layerFound[3], "acad.lin");
} catch (Autodesk.AutoCAD.Runtime.Exception e) {
Editor ed = acDoc.Editor;
ed.WriteMessage(e.Message);
}
tempTrans.Commit();
}
// Change current layer linetype to...
curLayer.LinetypeObjectId = acLinTbl[(string)layerFound[3]];
// Downgrade the layerTable, lineTable and the LayerRecord for read.
curLayer.DowngradeOpen();
dwgLyrTbl.DowngradeOpen();
// Remove the layer from the standards list
// so we do not attempt to have redundant layers.
for(int i = _LayerDataTable.Rows.Count-1; i >= 0; i--) {
System.Data.DataRow dr = _LayerDataTable.Rows[i];
if (dr["NAME"] == layerFound[0]){
dr.Delete();
break;
}
}
} else {
// if the layer is not in within standard add it to a data set
// to fix manually.
if(curLayer.Name == "0")
continue;
var newRow = LayersToFix.NewRow();
newRow["NAME"] = curLayer.Name;
// TODO get color name..
newRow["COLOR"] = curLayer.Color.ColorName;
newRow["COLOR_ID"] = 0;
LinetypeTableRecord Ltype = (LinetypeTableRecord)curTrans.GetObject(
curLayer.LinetypeObjectId,OpenMode.ForRead
);
newRow["LINETYPE"] = Ltype.Name;
LayersToFix.Rows.Add(newRow);
}
}
// accept the changes made in this transaction
curTrans.Commit();
}
}