// Поиск всех текстов чертежа
public static void FindTextsInDrawing()
{
try
{
ListNotes.Clear();
// Get the current document and database, and start a transaction
Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.
MdiActiveDocument;
Database acCurDb = acDoc.Database;
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// Открываю Block table для чтения
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
OpenMode.ForRead) as BlockTable;
// Открываю Block table record Model space для чтения
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
OpenMode.ForRead) as BlockTableRecord;
FindItems(acTrans, acBlkTblRec);
}
}
catch (System.Exception e)
{
Editor acDocEd = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
acDocEd.WriteMessage(String.Format("Ошибка. {0}{1}" + Environment.NewLine, e.Message, e.StackTrace));
}
}
// поиск всех элементов Block table record
public static void FindItems(Transaction acTrans, BlockTableRecord blkTblRec)
{
foreach (ObjectId acObjId in blkTblRec)
{
Entity en = acTrans.GetObject(acObjId, OpenMode.ForRead) as Entity;
//ed.WriteMessage("\n" + acObjId.ObjectClass.DxfName);
if (en != null)
{
if (en.GetType() == typeof(DBText))
{
string str = ((DBText)en).TextString;
ListNotes.Add(new CNote(str, acObjId, DataType.DBText));
}
else if (en.GetType() == typeof(MText))
{
//if (ListObjectId.IndexOf(acObjId) == -1)
//{
string str = ((MText)en).Contents;
ListNotes.Add(new CNote(str, acObjId, DataType.MText));
//}
}
else if (en.GetType() == typeof(Table))
{
Autodesk.AutoCAD.DatabaseServices.Table tbl = (Table)en;
for (int row = 0; row < tbl.Rows.Count; row++)
for (int col = 0; col < tbl.Columns.Count; col++)
{
string str = tbl.Cells[row, col].TextString;
if (!String.IsNullOrEmpty(str))
ListNotes.Add(new CNote(str, acObjId, DataType.Table, row, col));
}
}
else if (en.GetType() == typeof(MLeader))
{
string str = ((MLeader)en).MText.Contents;
ListNotes.Add(new CNote(str, acObjId, DataType.MLeader));
}
else if (en.GetType() == typeof(BlockReference))
{
BlockReference bref = en as BlockReference;
AttributeCollection attributeCollection = bref.AttributeCollection;
if (attributeCollection.Count > 0) // считывание атрибутов блока
{
foreach (ObjectId att in bref.AttributeCollection)
{
DBObject dbObj = acTrans.GetObject(att, OpenMode.ForRead) as DBObject;
AttributeReference acAttRef = dbObj as AttributeReference;
//if (acAttRef.TextString.IndexOf("гост") != -1) {
//}
if (acAttRef.Visible)
ListNotes.Add(new CNote(acAttRef.TextString, acAttRef.ObjectId,
DataType.AttributeReference, 0, 0, acAttRef.IsMTextAttribute));
}
}
// Открываю Block table record BlockReference для чтения
BlockTableRecord acBlkTblRec = acTrans.GetObject(bref.BlockTableRecord,
OpenMode.ForRead) as BlockTableRecord;
FindItems(acTrans, acBlkTblRec);
}
}
}
}