Thanks all for your valuable Suggestion.....
Finally I got the Solution using Block Attributes:
Before editing anything Locking the Document can Solve All Issues:
The Code is As:
public class TITLEBLOCK
{
[Autodesk.AutoCAD.Runtime.CommandMethod("TITLEBLOCK1")]
public void TitleBlock1Command(string projectname)
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
string blockName = "COMPBLK";
string attbName = "PROJECT-NAME";
string attbValue = projectname;
UpdateAttributesInDatabase(
db,
blockName,
attbName,
attbValue
);
}
private void UpdateAttributesInDatabase(
Database db,
string blockName,
string attbName,
string attbValue
)
{
Document doc =
Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
// Get the IDs of the spaces we want to process
// and simply call a function to process each
ObjectId msId, psId;
Transaction tr = db.TransactionManager.StartTransaction();
using (tr)
{
BlockTable bt =
(BlockTable)tr.GetObject(
db.BlockTableId,
OpenMode.ForRead
);
msId =
bt[BlockTableRecord.ModelSpace];
psId =
bt[BlockTableRecord.PaperSpace];
// Not needed, but quicker than aborting
tr.Commit();
}
int msCount =
UpdateAttributesInBlock(
msId,
blockName,
attbName,
attbValue
);
int psCount =
UpdateAttributesInBlock(
psId,
blockName,
attbName,
attbValue
);
ed.Regen();
// Display the results
ed.WriteMessage(
"\nProcessing file: " + db.Filename
);
ed.WriteMessage(
"\nUpdated {0} attribute{1} of CHTITLEBLOCK in the modelspace.",
msCount,
msCount == 1 ? "" : "s",
attbName
);
ed.WriteMessage(
"\nUpdated {0} attribute{1} of CHTITLEBLOCK in the default paperspace.",
psCount,
psCount == 1 ? "" : "s",
attbName
);
}
private int UpdateAttributesInBlock(
ObjectId btrId,
string blockName,
string attbName,
string attbValue
)
{
// Will return the number of attributes modified
int changedCount = 0;
Document doc =
Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
DocumentLock doclock = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument();
Database db = doc.Database;
Editor ed = doc.Editor;
Transaction tr =
doc.TransactionManager.StartTransaction();
using (tr)
{
BlockTableRecord btr =
(BlockTableRecord)tr.GetObject(btrId, OpenMode.ForRead);
// Test each entity in the container...
using (DocumentLock acLckDoc = doc.LockDocument())
foreach (ObjectId entId in btr)
{
Entity ent =
tr.GetObject(entId, OpenMode.ForRead)
as Entity;
if (ent != null)
{
BlockReference br = ent as BlockReference;
if (br != null)
{
BlockTableRecord bd =
(BlockTableRecord)tr.GetObject(
br.BlockTableRecord,
OpenMode.ForRead
);
// ... to see whether it's a block with
// the name we're after
if (bd.Name.ToUpper() == blockName)
{
// Check each of the attributes...
foreach (
ObjectId arId in br.AttributeCollection
)
{
DBObject obj =
tr.GetObject(
arId,
OpenMode.ForRead
);
AttributeReference ar =
obj as AttributeReference;
if (ar != null)
{
// ... to see whether it has
// the tag we're after
if (ar.Tag.ToUpper() == attbName)
{
// If so, update the value
// and increment the counter
ar.UpgradeOpen();
ar.TextString = attbValue;
ar.DowngradeOpen();
changedCount++;
}
}
}
}
// Recurse for nested blocks
changedCount +=
UpdateAttributesInBlock(
br.BlockTableRecord,
blockName,
attbName,
attbValue
);
}
}
}
tr.Commit();
tr.Dispose();
}
doclock.Dispose();
return changedCount;
}
}