in below mentioned code I am trying to add new Document and in Document trying to add few blocks. blocks are not getting added. i Have tried with lock document method as well.
if i am using acDocMgr.MdiActiveDocument = acDoc; this line code gets failed saying internal error !dbinsert.cpp@836:enoDatabase
and if not using the acDocMgr.MdiActiveDocument = acDoc; then blocks getting added , but on first document not on new added document.
please help...
private void btnCreate_Click(object sender, EventArgs e)
{
DocumentCollection acDocMgr = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager;
Document acDoc = acDocMgr.Add("acad.dwt");
acDocMgr.MdiActiveDocument = acDoc;
string filepath = "BlockCollection Path";
Point3d ip1 = new Point3d(6118.1031, 17854.7671, 0);
Point3d ip2 = new Point3d(6118.1031 + 41.6738, 17854.7671 - 70.5439, 0);
Point3d ip3 = new Point3d(6118.1031 + 41.6738, 17854.7671 - 70.5439 - 24.6172, 0);
Point3d[] mPoints= { ip1, ip2, ip3};
for (int i = 0; i < mPoints.Count; i++)
{
try
{
string blkFilepath = filepath + chkCollection[i];
InsertBlock(blkFilepath, mPoints[i], acDoc);//acDoc
blkFilepath = string.Empty;
}
catch (System.Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
public void InsertBlock(string dwgName, Point3d inspt, Document acDoc) //
{
Transaction tr = acDoc.TransactionManager.StartTransaction();
try
{
Database db = new Database(false, true);
db.ReadDwgFile(dwgName, System.IO.FileShare.Read, true, "");
ObjectId BlkId;
BlkId = acDoc.Database.Insert(System.IO.Path.GetFileNameWithoutExtension(dwgName), db, false);
BlockTable bt = (BlockTable)tr.GetObject(acDoc.Database.BlockTableId, OpenMode.ForRead, true);
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
BlockReference bref = new BlockReference(inspt, BlkId);
btr.AppendEntity(bref);
tr.AddNewlyCreatedDBObject(bref, true);
tr.Commit();
tr.Dispose();
}
catch (System.Exception ex)
{
MessageBox.Show(ex.ToString());
tr.Commit();
tr.Dispose();
}
}