2016-08-17 35 views
0

這是我的代碼,它返回所選塊中的屬性值, 但我希望這些值在表格中顯示在dwg文件中。而文本必須是單個多行文字對象。在autocad中以表格形式打印屬性

[CommandMethod("NLTAB")] 
    public void ListAttributes() 
    { 
    Document acDoc = Application.DocumentManager.MdiActiveDocument; 
    Editor ed = acDoc.Editor; 
    Database db = acDoc.Database; 

    using (Transaction tr = db.TransactionManager.StartTransaction()) 
    { 

     try 
     { 

      TypedValue[] filList = new TypedValue[2] { new TypedValue((int)DxfCode.Start, "INSERT"), new TypedValue((int)DxfCode.HasSubentities, 1) }; 
      SelectionFilter filter = new SelectionFilter(filList); 
      PromptSelectionOptions opts = new PromptSelectionOptions(); 
      opts.MessageForAdding = "Select block references: "; 
      PromptSelectionResult res = ed.GetSelection(opts, filter); 
      // Do nothing if selection is unsuccessful 
      if (res.Status != PromptStatus.OK) 
       return; 

      SelectionSet selSet = res.Value; 
      ObjectId[] idArray = selSet.GetObjectIds(); 

      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); 
      Vector3d disp = new Vector3d(0.0, -2.0 * db.Textsize, 0.0); 
      TextStyleTable ts = (TextStyleTable)tr.GetObject(db.TextStyleTableId, OpenMode.ForRead); 
      ObjectId mtStyleid = db.Textstyle; 
      if (ts.Has("NAL-TEXT")) 
      { 
       mtStyleid = ts["NAL-FORMAT"]; 
      } 

      var curSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite); 
      MText _outputHeading = new MText(); 

      _outputHeading.Location = startPoint; 
      _outputHeading.Width = 75.207; 
      _outputHeading.Height = 1.488; 

      _outputHeading.TextStyleId = mtStyleid; 
      string file = acDoc.Name; 
      string str1 = Path.GetFileNameWithoutExtension(file); 
      //string str1 = ("534-W10A-R1"); 
      //var curSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite); 

      Match match = Regex.Match(str1, @"^(\w+-[CSDWM]\d+[A-Z]-.)$"); 
      var pattern = @"^(\d+)-[A-Z](\d+)([A-Z])-"; 

      var groups = Regex.Match(str1, pattern).Groups; 
      var _projectCode = groups[1].Value; 
      var _phaseCode = _projectCode + "-" + groups[2].Value; 
      var _zoneCode = _phaseCode + groups[3].Value; 

      curSpace.AppendEntity(_outputHeading); 
      tr.AddNewlyCreatedDBObject(_outputHeading, true); 
      db.TransactionManager.QueueForGraphicsFlush(); 

      startPoint += disp; 
      HashSet<string> attValues = new HashSet<string>(); 

      foreach (ObjectId blkId in idArray) 
      { 
       BlockReference blkRef = (BlockReference)tr.GetObject(blkId, OpenMode.ForRead); 
       BlockTableRecord btr = (BlockTableRecord)tr.GetObject(blkRef.BlockTableRecord, OpenMode.ForWrite); 


       AttributeCollection attCol = blkRef.AttributeCollection; 
       foreach (ObjectId attId in attCol) 
       { 
        AttributeReference attRef = (AttributeReference)tr.GetObject(attId, OpenMode.ForRead); 
        string str = (attRef.TextString); 
        string tag = attRef.Tag; 

        if (attValues.Contains(str)) 
         continue; 
        if (btr.Name == "NAL-TAG crs ref") 
        { 
         var curSpace1 = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite); 
         MText mtext = new MText(); 

         mtext.Location = startPoint; 


         mtext.Contents = tag.ToString() + " : " + str + "\n"; 

         //ed.WriteMessage(text); 
         curSpace.AppendEntity(mtext); 
         tr.AddNewlyCreatedDBObject(mtext, true); 
         db.TransactionManager.QueueForGraphicsFlush(); 

         attValues.Add(str); 

         startPoint += disp; 
        } 
       } 
      } 
      tr.Commit(); 
     } 
     catch (Autodesk.AutoCAD.Runtime.Exception ex) 
     { 
      ed.WriteMessage(("Exception: " + ex.Message)); 
     } 
    } 
} 
+0

因此,您希望文本像表格一樣出現在.DWG上,但是您希望它們作爲MText(而不是表格)? –

+0

我的第一優先級是表格,如果它在文本中的確定 – jackson

回答

1

要板條箱表,在this blog post,你會發現在C#示例代碼,應該爲你工作。結果應該如下圖所示。

這裏是源代碼。創建表之後,如果確實需要文本,可以調用.Explode()並提取所有文本實體。

using Autodesk.AutoCAD.ApplicationServices; 
using Autodesk.AutoCAD.DatabaseServices; 
using Autodesk.AutoCAD.EditorInput; 
using Autodesk.AutoCAD.Geometry; 
using Autodesk.AutoCAD.Runtime; 

namespace TableCreation 
{ 
    public class Commands 
    { 
    [CommandMethod("CRT")] 
    static public void CreateTable() 
    { 
     Document doc = 
     Application.DocumentManager.MdiActiveDocument; 
     Database db = doc.Database; 
     Editor ed = doc.Editor; 

     PromptPointResult pr = 
     ed.GetPoint("\nEnter table insertion point: "); 
     if (pr.Status == PromptStatus.OK) 
     { 
     Table tb = new Table(); 
     tb.TableStyle = db.Tablestyle; 
     tb.NumRows = 5; 
     tb.NumColumns = 3; 
     tb.SetRowHeight(3); 
     tb.SetColumnWidth(15); 
     tb.Position = pr.Value; 

     // Create a 2-dimensional array 
     // of our table contents 
     string[,] str = new string[5, 3]; 
     str[0, 0] = "Part No."; 
     str[0, 1] = "Name "; 
     str[0, 2] = "Material "; 
     str[1, 0] = "1876-1"; 
     str[1, 1] = "Flange"; 
     str[1, 2] = "Perspex"; 
     str[2, 0] = "0985-4"; 
     str[2, 1] = "Bolt"; 
     str[2, 2] = "Steel"; 
     str[3, 0] = "3476-K"; 
     str[3, 1] = "Tile"; 
     str[3, 2] = "Ceramic"; 
     str[4, 0] = "8734-3"; 
     str[4, 1] = "Kean"; 
     str[4, 2] = "Mostly water"; 

     // Use a nested loop to add and format each cell 
     for (int i = 0; i < 5; i++) 
     { 
      for (int j = 0; j < 3; j++) 
      { 
      tb.SetTextHeight(i, j, 1); 
      tb.SetTextString(i, j, str[i, j]); 
      tb.SetAlignment(i, j, CellAlignment.MiddleCenter); 
      } 
     } 
     tb.GenerateLayout(); 

     Transaction tr = 
      doc.TransactionManager.StartTransaction(); 
     using (tr) 
     { 
      BlockTable bt = 
      (BlockTable)tr.GetObject(
       doc.Database.BlockTableId, 
       OpenMode.ForRead 
      ); 
      BlockTableRecord btr = 
      (BlockTableRecord)tr.GetObject(
       bt[BlockTableRecord.ModelSpace], 
       OpenMode.ForWrite 
      ); 
      btr.AppendEntity(tb); 
      tr.AddNewlyCreatedDBObject(tb, true); 
      tr.Commit(); 
     } 
     } 
    } 
    } 
} 
+0

但表中的數據取決於dwg文件,該文件根據圖不同而不同。 – jackson

+0

這段代碼是如何創建表格的一個例子,你需要在行和列上放置你想要的文本 –

+0

其實主要問題是在那裏我不知道如何取得行和列。 – jackson