簡體   English   中英

根據用戶選擇顯示塊信息的代碼

[英]Code which display the information of block as per user selection

這是我根據用戶選擇在dwg中選擇塊的代碼,並在mtext的幫助下在dwg文件上打印塊的詳細信息但是mtext不工作它不會打印任何東西。 它給了我例外。

    [CommandMethod("LAT")]
    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();
            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.Height = 2;

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

                    string str =
                        ////("\n  Attribute Tag: "
                        ////  + attRef.Tag
                        ////  + "\n    Attribute String: "
                        ("\n" + attRef.TextString);
                    mtext.Contents = ("hELLO");
                    acDoc.Editor.WriteMessage("\n" + str);
                    btr.AppendEntity(mtext);
                    tr.AddNewlyCreatedDBObject(mtext, true);
                }
            }
            tr.Commit();
        }
        catch (Autodesk.AutoCAD.Runtime.Exception ex)
        {
            ed.WriteMessage(("Exception: " + ex.Message));
        }
        finally
        {
            tr.Dispose();
        }
    }
}

}

您必須遍歷模型空間並找到所有Hatches。 以下是執行此操作的方法之一:

[CommandMethod("FindAllHatches")]
public static void FindAllHatches()
{
    Document acDoc = Application.DocumentManager.MdiActiveDocument;

    acDoc.Editor.WriteMessage("\nSearching for Hatches");

    var db = acDoc.Database;
    using (Transaction transaction = db.TransactionManager.StartTransaction())
    {
        ObjectId idModelSpace = SymbolUtilityServices.GetBlockModelSpaceId(db);

        BlockTableRecord modelSpace =
            transaction.GetObject(idModelSpace, OpenMode.ForRead) as
            BlockTableRecord;

        var sbReportText = new StringBuilder(); //usging System.Text
        double fTotalArea = 0.0;
        int nTotalHatches = 0;

        foreach (var objId in modelSpace)
        {
            var entity = transaction.GetObject(objId, OpenMode.ForRead);
            Hatch hatch = entity as Hatch;
            if (hatch == null)
                continue; //not hatch

            nTotalHatches++;
            fTotalArea += hatch.Area;

            acDoc.Editor.WriteMessage("\nFound Hatch Area={0}", hatch.Area);
            sbReportText.AppendFormat("Hatch Area={0}\n", hatch.Area);
        }

        if (nTotalHatches == 0)
            return; //no hatches found

        modelSpace.UpgradeOpen();

        MText acMText = new MText();
        acMText.SetDatabaseDefaults();

        sbReportText.AppendFormat("Count = {0}, Total Area = {1}",
            nTotalHatches, fTotalArea);
        acMText.Contents = sbReportText.ToString();

        modelSpace.AppendEntity(acMText);
        transaction.AddNewlyCreatedDBObject(acMText, true);

        transaction.Commit();
    }
}

你的acBlkTblRec是我的例子中的modelSpace

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM