Try this sample code,
add to namespace declarations:
using Autodesk.AutoCAD.Internal;
public void testInsertBlock()
{
string blockName = "BlockName";//<-- block name to be inserted
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Database db = doc.Database;
DocumentLock doclock = doc.LockDocument();
using (doclock)
{
Transaction tr = db.TransactionManager.StartTransaction();
using (tr)
{
try
{
BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
if (!bt.Has(blockName))
{
ed.WriteMessage("\nBlock \"{0}\" does not exist", blockName);
return;
}
ObjectId blkID = bt[blockName];
ed.WriteMessage("\nBlock \"{0}\" does exist, ObjectId is: {1}", blockName, blkID);
PromptPointOptions ptOpt = new PromptPointOptions("\nSpecify a block position : ");
PromptPointResult res = ed.GetPoint(ptOpt);
if (res.Status != PromptStatus.OK)
{
if (res.Status == PromptStatus.Cancel)
{
ed.WriteMessage("\nInterrupted by user. Exit.."); return;
}
else
{
ed.WriteMessage("\nError on specifying a point.Exit..."); return;
}
}
Point3d pt = res.Value;
BlockTableRecord btr = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
BlockTableRecord btrec = tr.GetObject(blkID, OpenMode.ForRead) as BlockTableRecord;
BlockReference bref = new BlockReference(pt, blkID);
bref.SetDatabaseDefaults();
if (btrec.Annotative == AnnotativeStates.True)
{
ObjectContextManager ocm = db.ObjectContextManager;
ObjectContextCollection occ = ocm.GetContextCollection("ACDB_ANNOTATIONSCALES");
ObjectContexts.AddContext(bref, occ.CurrentContext);
}
Matrix3d mat = Matrix3d.Identity;
bref.TransformBy(mat);
btr.AppendEntity(bref);
tr.AddNewlyCreatedDBObject(bref, true);
if (btrec.HasAttributeDefinitions)
{
Autodesk.AutoCAD.DatabaseServices.AttributeCollection atcoll = bref.AttributeCollection;
foreach (ObjectId subid in btrec)
{
Entity ent = (Entity)subid.GetObject(OpenMode.ForRead);
AttributeDefinition attDef = ent as AttributeDefinition;
if (attDef != null)
{
//ed.WriteMessage("\nValue: " + attDef.TextString);// for debug only
AttributeReference attRef = new AttributeReference();
attRef.SetDatabaseDefaults();
attRef.SetAttributeFromBlock(attDef, bref.BlockTransform);
attRef.Position = attDef.Position.TransformBy(bref.BlockTransform);
attRef.Tag = attDef.Tag;
attRef.AdjustAlignment(db);
atcoll.AppendAttribute(attRef);
tr.AddNewlyCreatedDBObject(attRef, true);
}
}
}
tr.Commit();
}
catch (Autodesk.AutoCAD.Runtime.Exception ex)
{
ed.WriteMessage(("AutoCAD Exception: " + ex.Message + "\n" + ex.StackTrace));
}
catch (System.Exception ex)
{
ed.WriteMessage(("Sytem Exception: " + ex.Message + "\n" + ex.StackTrace));
}
finally
{
// optional message
}
}
}
}
_____________________________________
C6309D9E0751D165D0934D0621DFF27919