I found some code and modified it according to my requirement. But this code sends 'Insertion Point' twice. Could anybody point where I have gone wrong?
[CommandMethod("test")]
public void test()
{
string fName=@"C:\a.dwg";
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
//Get the editor object
Editor ed = doc.Editor;
string blkName="a";
//Get the working database
Database dwg = doc.Database;
//Start a transaction
Transaction tr = doc.TransactionManager.StartTransaction();
try
{
//Create temporary database to read in block
Database dbDwg = new Database();
//Read the file from disk
dbDwg.ReadDwgFile(fName, System.IO.FileShare.Read, true, "");
//Insert it into the current database.
dwg.Insert(blkName, dbDwg, false);
dbDwg.Dispose();
//Open the block for read.
BlockTable bt = (BlockTable)tr.GetObject(dwg.BlockTableId, OpenMode.ForRead);
//See if the block table record exists
if (bt.Has(blkName))
{
//Prompt user for insertion point
PromptPointOptions po = new PromptPointOptions("Select insertion point: ");
//Get user to select the insertion point
PromptPointResult pr = ed.GetPoint(po);
//user selected a valid point
if (pr.Status == PromptStatus.OK)
{
//fix up the point to be a Point3D object
Point3d pt = new Point3d(pr.Value.X, pr.Value.Y, pr.Value.Z);
ObjectId bId = bt[blkName];
//insert the block at the insertion point
BlockReference br = new BlockReference(pt, bId);
BlockJig blkJig = new BlockJig(br);
//Perform the jig operation
pr = ed.Drag(blkJig);
if (pr.Status == PromptStatus.OK)
{
//Add block reference to the current space
BlockTableRecord curSpace = (BlockTableRecord)tr.GetObject(dwg.CurrentSpaceId, OpenMode.ForWrite);
//Add the block reference to the current space
curSpace.AppendEntity(blkJig.GetEntity());
//Tell the transaction
tr.AddNewlyCreatedDBObject(blkJig.GetEntity(), true);
//Commit transaction to database
tr.Commit();
}
}
else
{
MessageBox.Show("Block " + blkName + " does not exists");
}
}
}
catch (System.Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
//Always dispose of transaction
tr.Dispose();
}
}
Thanks