2016-08-19 84 views
0

這是我的代碼,它根據輸入的名稱在我的dwg文件中選擇一個特定的塊。 但我想選擇整個頁面,只想要一個特定塊的表格。 而且標題部分也給了我錯誤的信息,我有一個四個屬性,但它只在標題部分打印一個。請提出解決方案。使用鼠標點擊代替鍵入名稱來選擇塊

namespace TableCreation 
    { 
    public class Commands 
    { 
    // Set up some formatting constants 
    // for the table 

    private const double colWidth = 15.0; 
    private const double rowHeight = 3.0; 
    private const double textHeight = 1.0; 

    private const CellAlignment cellAlign = 
     CellAlignment.MiddleCenter; 

    // Helper function to set text height 
    // and alignment of specific cells, 
    // as well as inserting the text 

    static public void SetCellText(Table tb, int row, int col, string value) 
    { 
     tb.SetAlignment(row, col, cellAlign); 
     tb.SetTextHeight(row, col, textHeight); 
     tb.SetTextString(row, col, value); 
    } 

    [CommandMethod("BAT")] 
    static public void BlockAttributeTable() 
    { 
     Document doc = Application.DocumentManager.MdiActiveDocument; 
     Database db = doc.Database; 
     Editor ed = doc.Editor; 


     PromptStringOptions opt = new PromptStringOptions("\nEnter name of block to list: "); 
     PromptResult pr = ed.GetString(opts); 

     if (pr.Status == PromptStatus.OK) 
     { 
      string blockToFind = pr.StringResult.ToUpper(); 


      Transaction tr = doc.TransactionManager.StartTransaction(); 
      using (tr) 
      { 
       // Let's check the block exists 

       BlockTable bt = (BlockTable)tr.GetObject(doc.Database.BlockTableId, OpenMode.ForRead); 

       if (!bt.Has(blockToFind)) 
       { 
        ed.WriteMessage("\nBlock " + blockToFind + " does not exist."); 
       } 
       else 
       { 
        // And go through looking for 
        // attribute definitions 

        StringCollection colNames = new StringCollection(); 

        BlockTableRecord bd = (BlockTableRecord)tr.GetObject(bt[blockToFind], OpenMode.ForRead); 
        foreach (ObjectId adId in bd) 
        { 
         DBObject adObj = tr.GetObject(adId, OpenMode.ForRead); 

         // For each attribute definition we find... 

         AttributeDefinition ad = adObj as AttributeDefinition; 
         if (ad != null) 
         { 
          // ... we add its name to the list 

          colNames.Add(ad.Tag); 
         } 
        } 
        if (colNames.Count == 0) 
        { 
         ed.WriteMessage("\nThe block " + blockToFind + " contains no attribute definitions."); 
        } 
        else 
        { 
         // Ask the user for the insertion point 
         // and then create the table 

         PromptPointResult ppr; 
         PromptPointOptions ppo = new PromptPointOptions(""); 
         ppo.Message = "\n Select the place for print output:"; 
         //get the coordinates from user 
         ppr = ed.GetPoint(ppo); 
         if (ppr.Status != PromptStatus.OK) 
          return; 
         Point3d startPoint = ppr.Value.TransformBy(ed.CurrentUserCoordinateSystem); 
         //Point3d startPoint1 = startPoint.Subtract(); 
         Vector3d disp = new Vector3d(0.0, -2.0 * db.Textsize, 0.0); 
         Vector3d disp2 = new Vector3d(0.0, -2.0 * db.Textsize, 0.0); 

         if (ppr.Status == PromptStatus.OK) 
         { 
          Table tb = new Table(); 
          tb.TableStyle = db.Tablestyle; 
          tb.NumRows = 1; 
          tb.NumColumns = colNames.Count; 
          tb.SetRowHeight(rowHeight); 
          tb.SetColumnWidth(colWidth); 
          tb.Position = startPoint; 

          // Let's add our column headings 

          for (int i = 0; i < colNames.Count; i++) 
          { 
           SetCellText(tb, 0, i, colNames[i]); 
          } 

          // Now let's search for instances of 
          // our block in the modelspace 

          BlockTableRecord ms = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.PaperSpace], OpenMode.ForRead); 

          int rowNum = 1; 
          foreach (ObjectId objId in ms) 
          { 
           DBObject obj = tr.GetObject(objId, OpenMode.ForRead); 
           BlockReference br = obj as BlockReference; 
           if (br != null) 
           { 
            BlockTableRecord btr = (BlockTableRecord)tr.GetObject(br.BlockTableRecord, OpenMode.ForRead); 
            using (btr) 
            { 
             if (btr.Name.ToUpper() == blockToFind) 
             { 
              // We have found one of our blocks, 
              // so add a row for it in the table 

              tb.InsertRows(rowNum, rowHeight, 1); 

              // Assume that the attribute refs 
              // follow the same order as the 
              // attribute defs in the block 

              int attNum = 0; 
              foreach (ObjectId arId in br.AttributeCollection) 
              { 
               DBObject arObj = tr.GetObject(arId, OpenMode.ForRead); 
               AttributeReference ar = arObj as AttributeReference; 
               if (ar != null) 
               { 
                // Embed or link the values 

                string strCell; 
                //if (embed) 
                //{ 
                strCell = ar.TextString; 
                //} 
                //else 
                //{ 
                string strArId = arId.ToString(); 

                strArId = strArId.Trim(new char[] { '(', ')' }); 

                SetCellText(tb, rowNum, attNum, strCell); 
                ed.WriteMessage(strCell); 
               } 
               attNum++; 
              } 
              rowNum++; 
             } 
            } 
           } 
          } 
          tb.GenerateLayout(); 

          ms.UpgradeOpen(); 
          ms.AppendEntity(tb); 
          tr.AddNewlyCreatedDBObject(tb, true); 
          tr.Commit(); 
         } 
        } 
       } 
      } 
     } 
    } 
} 

}

回答

0

詢問用戶選擇在屏幕上,你可以使用PromptEntityOptions而是選擇如BlockReference塊。我修改你的代碼的一部分,包括此:

[CommandMethod("BAT")] 
static public void BlockAttributeTable() 
{ 
    Document doc = Application.DocumentManager.MdiActiveDocument; 
    Database db = doc.Database; 
    Editor ed = doc.Editor; 

    PromptEntityOptions opts = new PromptEntityOptions("\nSelect a block: "); 
    opts.SetRejectMessage("Only block references"); 
    opts.AddAllowedClass(typeof(BlockReference), true); 
    PromptEntityResult pr = ed.GetEntity(opts); 

    if (pr.Status == PromptStatus.OK) 
    { 
    Transaction tr = doc.TransactionManager.StartTransaction(); 
    using (tr) 
    { 
     BlockReference blockRef = tr.GetObject(pr.ObjectId, OpenMode.ForRead) as BlockReference; 

     BlockTableRecord bd = (BlockTableRecord)tr.GetObject(blockRef.BlockTableRecord, OpenMode.ForRead); 
     foreach (ObjectId adId in bd) 
     { 
     // ToDo: here you can handle entities inside the block definition 
     } 

    } 
    } 
} 
+0

這讓我異常「設置拒絕消息異常」 – jackson

+0

使用SetRejectMessage第一 – jackson

+0

仍然給予致命的錯誤你能OLZ在我原來的解決方案進行修改並送還給我。 – jackson

相關問題