0

我正在尝试将线型加载到活动文档中,以便我可以将图层线型设置为我尝试加载的线型。现在的问题是当我尝试加载它时,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();
        }
    }
4

2 回答 2

0

你所有OpenMode的都是ForRead

如果您要更改绘图中的某些内容,则应ForWrite在要更改绘图的部分中使用。(我不确定确切的词是不是那个,但想法是那个)。

于 2013-05-14T20:20:53.423 回答
0

现在我已经熟悉了 API,问题是使用

.StartOpenCloseTransaction()

一定要使用 StartTransaction()

代码

    private void defaultLayerFix (){
        using(var DLFTrans = db.TransactionManager.StartTransaction()){
            // Open the layerTable table for read.
            dwgLyrTbl = DLFTrans.GetObject(
                db.LayerTableId,OpenMode.ForRead) as LayerTable;

            // Foreach layer in current DWG
            foreach (ObjectId layID in dwgLyrTbl) {
                var curLayer = DLFTrans.GetObject(
                    layID,OpenMode.ForRead) as LayerTableRecord;

                FixLayer(curLayer);
            }
            DLFTrans.Commit();
        }
    }

    private void FixLayer(LayerTableRecord curLayer){

        // Open a transaction for fixing layers that have the correct name
        // but not the correct color or linetype.
        using(curTrans = db.TransactionManager.StartTransaction()) {

            // Check Standard layer data table for current layer
            var layerFound = _LayerDataTable.Rows.Find(curLayer.Name.ToUpper());

            // If found
            if(layerFound != null) {

                // Open the Line Type table for read
                acLinTbl = curTrans.GetObject(
                    db.LinetypeTableId,OpenMode.ForRead) as LinetypeTable;

                // Send to load.
                LoadLinetype((string)layerFound[3]);

                // If linetype table has the needed linetype.
                if (acLinTbl.Has((string)layerFound[3])){

                    // Upgrade the current layer for Write.
                    curLayer.UpgradeOpen();

                    // Change the layers Linetype.
                    curLayer.LinetypeObjectId = acLinTbl[(string)layerFound[3]];

                    // Change the layers color.
                    curLayer.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(
                        Autodesk.AutoCAD.Colors.ColorMethod.ByAci, (short)layerFound[2]);

                    // Downgrade the layer from write to read.
                    curLayer.DowngradeOpen();

                    // Remove the layer from the Standard Layers Dataset within current dwg.
                    for(int i = _LayerDataTable.Rows.Count-1; i >= 0; i--) {
                        var dr = _LayerDataTable.Rows[i];
                        if (dr["NAME"] == layerFound[0]){
                            dr.Delete();
                            break;
                        }
                    }
                } else {
                    MessageBox.Show("awe shit, still not working");
                }
            } else {

                // If the layer is not in within standard add it to a data set
                // to fix manually.
                if(curLayer.Name != "0") {

                    // Create a new row in dataset
                    var newRow = LayersToFix.NewRow();
                    newRow["NAME"] = curLayer.Name;
                    // TODO get color name..
                    newRow["COLOR"] = curLayer.Color.ColorName;
                    newRow["COLOR_ID"] = 0;
                    var Ltype = (LinetypeTableRecord)curTrans.GetObject(
                        curLayer.LinetypeObjectId,OpenMode.ForRead
                    );
                    newRow["LINETYPE"] = Ltype.Name;

                    // Add the new row to the dataset.
                    LayersToFix.Rows.Add(newRow);
                }
            }

            // Accept changes and dispose of the transacton
            curTrans.Commit();
        }
    }


    private void LoadLinetype(string ltName)
    {
        // Start a transaction
        using (var acTrans = db.TransactionManager.StartTransaction())
        {
            // Open the Linetype table for read
            var acLineTypTbl = acTrans.GetObject(db.LinetypeTableId, OpenMode.ForRead) as LinetypeTable;

            // If the linetype does not exist
            if (!acLineTypTbl.Has(ltName))
            {
                // Load the Center Linetype
                db.LoadLineTypeFile(ltName, "acad.lin");
            }

            // Save the changes and dispose of the transaction
            acTrans.Commit();
        }
    }
于 2013-05-15T13:07:51.403 回答