Here is my code
Code:
public static List<string> GetAllFormats () // Получает все примененные форматы для подсчета их количества
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Database db = doc.Database;
List<string> listOfFormatsInDrawing = new List<string>();
using ( Transaction tr = doc.TransactionManager.StartTransaction() )
{
BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
if ( bt.Has("MainTitle") )
{
foreach ( ObjectId btrId in bt )//bt набор btrRecord то есть по своей сути уже коллекция!
{
BlockTableRecord btr = tr.GetObject(btrId, OpenMode.ForRead) as BlockTableRecord;
if ( btr.Name.Contains("Title"))
{
ObjectIdCollection brIdCollection = btr.GetBlockReferenceIds(false, true);// собрать все Id от BlockReference из BlockTableRecord в коллекцию
foreach ( ObjectId brId in brIdCollection )
{
BlockReference br = tr.GetObject(brId, OpenMode.ForRead) as BlockReference;// получить все BlockReference
AttributeCollection attrIdCollection = br.AttributeCollection;// собрать все Id от атрибутов из BlockTableRecord в коллекцию
foreach ( ObjectId idAttRef in attrIdCollection )
{
AttributeReference att = tr.GetObject(idAttRef, OpenMode.ForRead) as AttributeReference;
if ( att.Tag=="Format" )
{
pageFormat=att.TextString;
listOfFormatsInDrawing.Add(pageFormat);
}
}
}
}
}
}
tr.Commit();
}
return listOfFormatsInDrawing;
}
[CommandMethod("CreateFormatList")]
public static void CreateFormatList ()//Создает список всех примененных в чертеже форматов
{
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
GetAllFormats();
List<string> formatInCurrentDrawing =GetAllFormats();
formatInCurrentDrawing.Sort();
string resultString= string.Join("| ", formatInCurrentDrawing);
// Start a transaction
using ( Transaction acTrans = acCurDb.TransactionManager.StartTransaction() )
{
// Open the Block table for read
BlockTable acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord acBlkTblRec = acTrans.GetObject(acBlkTbl[ BlockTableRecord.PaperSpace ], OpenMode.ForWrite) as BlockTableRecord;
// Create a multiline text object
using ( MText listOfFormats = new MText() )
{
listOfFormats.Location=new Point3d(-185, -15, 0);
listOfFormats.TextHeight=10.5;
listOfFormats.Attachment=AttachmentPoint.TopLeft;
listOfFormats.Width=182;
listOfFormats.Contents=$"This drawing contains {resultString}";
listOfFormats.Layer="-10-Viewports";
acBlkTblRec.AppendEntity(listOfFormats);
acTrans.AddNewlyCreatedDBObject(listOfFormats, true);
}
acTrans.Commit();
}
}