[CommandMethod("ListBlockReferences")]
public static void SetCurrentPageNumber( )
{
//--------------------------------------------------------------------Mandatory variables
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
Editor ed = acDoc.Editor;
Transaction acTrans = acCurDb.TransactionManager.StartTransaction( );
//--------------------------------------------------------------------Mandatory variables
int curPageNumber = 1;
List<long> listOfHandles = new List<long>( );
ObjectIdCollection attIdWithPage = new ObjectIdCollection( );
using (acTrans)
{
BlockTable bt = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead, false, true) as BlockTable;
foreach (ObjectId btrId in bt)
{
BlockTableRecord btr = acTrans.GetObject(btrId, OpenMode.ForRead, false, true) as BlockTableRecord;
if (btr.Name.StartsWith("Regular"))
{
ObjectIdCollection brIdCollection = btr.GetBlockReferenceIds(false, true);
curPageNumber = curPageNumber + brIdCollection.Count;
foreach (ObjectId brId in brIdCollection)
{
BlockReference br = acTrans.GetObject(brId, OpenMode.ForRead) as BlockReference;
{
AttributeCollection brAttCollection = br.AttributeCollection;
foreach (ObjectId attId in brAttCollection)
{
AttributeReference attribute = acTrans.GetObject(attId, OpenMode.ForRead) as AttributeReference;
if (attribute.Tag == "Page")
{
attIdWithPage.Add(attId);
listOfHandles.Add(br.Handle.Value);
}
}
}
}
}
}
long brHandle = listOfHandles.Max( );
Handle yyy = new Handle(brHandle);
ObjectId idTitle = acCurDb.GetObjectId(false,yyy, 0);
if (idTitle!=null)
{
BlockReference br = acTrans.GetObject(idTitle, OpenMode.ForRead) as BlockReference;
{
AttributeCollection brAttCollection = br.AttributeCollection;
foreach (ObjectId attId in brAttCollection)
{
AttributeReference attribute = acTrans.GetObject(attId, OpenMode.ForWrite) as AttributeReference;
if (attribute.Tag == "Page")
{
attribute.TextString = curPageNumber.ToString( );
}
}
}
}
acTrans.Commit( );
}
Application.DocumentManager.MdiActiveDocument.Editor.Regen( );
}