This is my c# code to identify the block containing attribute in autocad. In my dwg file there is repeated no.of attribute i want to display each selected attribute only once and avoid duplicate values.
[CommandMethod("NLTAG")]
public void ListAttributes()
{
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Editor ed=Application.DocumentManager.MdiActiveDocument.Editor;
Database db = HostApplicationServices.WorkingDatabase;
Transaction tr = db.TransactionManager.StartTransaction();
try
{
TypedValue[] filList = new TypedValue[1] { new TypedValue((int)DxfCode.Start, "INSERT") };
SelectionFilter filter = new SelectionFilter(filList);
// 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.ForRead);
////ed.WriteMessage(
//// "\nBlock: " + btr.Name
////);
btr.Dispose();
AttributeCollection attCol = blkRef.AttributeCollection;
AttributeCollection parts = blkRef.AttributeCollection;
foreach (ObjectId attId in attCol)
{
AttributeReference attRef(AttributeReference)tr.GetObject(attId, OpenMode.ForWrite);
string str = ("\n " + attRef.TextString);//here i get duplicate value
ed.WriteMessage(str);
}
}
tr.Commit();
}
catch (Autodesk.AutoCAD.Runtime.Exception ex)
{
ed.WriteMessage(("Exception: " + ex.Message));
}
finally
{
tr.Dispose();
}
}