2013-05-14 2 views
0

로드하려는 도면에 레이어 선 종류를 설정할 수 있도록 활성 문서에 선 종류를로드하려고합니다. 이제 문제는 내가 그것을로드하려고 할 때, autocad는 나를 예외로 준다 : eWasOpenForRead. 그래서 나는 커맨드 라인을 통해 그것을 보내려고 노력했지만 그것은 효과가 있었지만, 지금 쓰고있는 애플리케이션은 ... 왜 이런 일이 일어 났는지 아무도 모른다.C# AutoCAD 도면에 선 종류로드

코드 :

 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(); 
     } 
    } 

답변

0

는 문제가 StartTransaction에()를 사용하십시오

.StartOpenCloseTransaction() 

의 사용이었다

코드

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(); 
     } 
    } 
0

귀하의 모든 OpenModeForRead이다.

도면에서 무언가를 변경하려는 경우 도면을 변경하려는 부분에 ForWrite을 사용해야합니다. (정확한 단어가 맞는지는 모르겠지만 아이디어는 그 것이다.) 지금은 API와 familliar 해요 것을

+0

당신이 '를 보면 curLayer.UpgradeOpen();'. 이렇게하면 열어 놓은 상태로 DBObject를 열어서 이미 읽었을 때 쓸 수 있습니다. 'curLayer.DownGradeOpen();'그것을 다시 읽기 위해 되돌립니다. –