2016-08-04 5 views
1

이것은 사용자 선택에 따라 블록을 선택하고 mtext를 사용하여 해당 블록에서 인쇄하는 코드입니다. mtext는 여기서 작동하지 않습니다. 또한 동일한 번호의 반복 된 속성을 원합니다. 미리 감사드립니다.블록의 속성이 mtext를 통해 인쇄되지 않음

[CommandMethod("NALATT")] 
    public void ListAttributes() 
    { 
     Document acDoc = Application.DocumentManager.MdiActiveDocument; 
     Editor ed = Application.DocumentManager.MdiActiveDocument.Editor; 
     Database db =HostApplicationServices.WorkingDatabase; 
     Transaction tr = db.TransactionManager.StartTransaction(); 

     // Start the transaction 
     try 
     { 
      // Build a filter list so that only 
      // block references are selected 
      TypedValue[] filList = new TypedValue[1] {new TypedValue((int)DxfCode.Start, "INSERT")}; 
      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(""); 

      //get the coordinates from user 
      ppo.Message = "\n Select the place for print output:"; 

      ppr = acDoc.Editor.GetPoint(ppo); 

      Point3d ptstart = ppr.Value; 

      ppo.UseBasePoint = true; 

      ppo.BasePoint = ptstart; 

      if (ppr.Status == PromptStatus.Cancel) return; 

      double x = ptstart.X; 

      double y = ptstart.Y; 

      double z = 1; 

      foreach (ObjectId blkId in idArray) 

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

       ed.WriteMessage("\nBlock: " + btr.Name); 

       btr.Dispose(); 

       AttributeCollection attCol = blkRef.AttributeCollection; 

       foreach (ObjectId attId in attCol) 
       { 

//이 부분은 제대로 작동하지 않습니다

    MText mtext = new MText(); 

        //mtext.SetDatabaseDefaults(); 

        mtext.Width = 2; 

        mtext.Location = new Point3d(x, y = y - 1, z); 

        AttributeReference attRef = 

(AttributeReference) tr.GetObject (attId, OpenMode.ForRead);

    string str =("\n" + attRef.TextString); 

        mtext.Contents = "\n" + str; 

        ed.WriteMessage(str); 

        btr.AppendEntity(mtext); 

        tr.AddNewlyCreatedDBObject(mtext, true); 
       } 
      } 
      tr.Commit(); 
     } 
     catch (Autodesk.AutoCAD.Runtime.Exception ex) 
     { 
      ed.WriteMessage(("Exception: " + ex.Message)); 
     } 
     finally 
     { 
      tr.Dispose(); 
     } 
    } 
} 

}

+0

내가 정확히 무엇을 내 DWG 파일 –

+0

에 텍스트를 인쇄 할을 게시 코드 "제대로 작동하지"에 가능한 한 폐쇄하기로? 'MText'가 충돌을 일으키거나 편집기에 표시되지 않습니까? 당신이 원하는 도움을 얻으려면 좀 더 정확하게해야합니다. –

+0

Mtext가 표시되지 않습니다 –

답변

0

나는 당신이 원하는 것을 정확히 이해하기 모르겠지만 다음 코드 "인쇄"현재 공간에서 선택한 블록의 속성 값. 내가 시도 는

[CommandMethod("NALATT")] 
    public void ListAttributes() 
    { 
     Document acDoc = Application.DocumentManager.MdiActiveDocument; 
     Editor ed = acDoc.Editor; 
     Database db = acDoc.Database; 
     using (Transaction tr = db.TransactionManager.StartTransaction()) 
     { 
      // Start the transaction 
      try 
      { 
       // Build a filter list so that only 
       // block references with attributes are selected 
       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); 

       //a HashSet to store printed strings to avoid duplicated values 
       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); 
        ed.WriteMessage("\nBlock: " + btr.Name); 

        // get the current space (where to print the attributes values) 
        var curSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite); 

        AttributeCollection attCol = blkRef.AttributeCollection; 
        foreach (ObjectId attId in attCol) 
        { 
         AttributeReference attRef = (AttributeReference)tr.GetObject(attId, OpenMode.ForRead); 
         string str = (attRef.TextString); 
         ed.WriteMessage("\n" + str); 
         if (attValues.Contains(str)) 
          continue; 

         // add the mtext to current space 
         MText mtext = new MText(); 
         mtext.Width = 2; 
         mtext.Location = startPoint; 
         mtext.Contents = str; 
         curSpace.AppendEntity(mtext); 
         tr.AddNewlyCreatedDBObject(mtext, true); 
         db.TransactionManager.QueueForGraphicsFlush(); 
         // add the string to the HashSet 
         attValues.Add(str); 
         // move the insertion point 
         startPoint += disp; 
        } 
       } 
       tr.Commit(); 
      } 
      catch (Autodesk.AutoCAD.Runtime.Exception ex) 
      { 
       ed.WriteMessage(("Exception: " + ex.Message)); 
      } 
     } 
    } 
+0

그 작품 덕분에 많은 gileCAD –

+0

나는 하나의 dwg 파일을 가지고 그 이름은 534-W1A-R1.dwg 나는 내 dwg 파일에 mtext를 사용하여 W1A-R1 만 인쇄하고 싶다. –