Insert existing block into DWG C#

Insert existing block into DWG C#

Anonymous
Not applicable
2,770 Views
6 Replies
Message 1 of 7

Insert existing block into DWG C#

Anonymous
Not applicable
how to insert existing block named "V1" into drawing?
I'm bit under pressure and I'm not able to make from samples mentioned on this forum.
thanks in advance
0 Likes
2,771 Views
6 Replies
Replies (6)
Message 2 of 7

Anonymous
Not applicable
So far I managed to make code below. But now I have no time to try it, could anybody fly though it and highlight probable mistakes?
Thanks

private acadDb.ObjectId CreateBlock(Autodesk.AutoCAD.Geometry.Point3d pos)
{
// get the current working database
acadDb.Database db = acadDb.HostApplicationServices.WorkingDatabase;
using (acadDb.Transaction trans = db.TransactionManager.StartTransaction())
{
acadDb.BlockTable bt = (acadDb.BlockTable)(trans.GetObject(db.BlockTableId, acadDb.OpenMode.ForWrite));
acadDb.BlockTableRecord btr = (acadDb.BlockTableRecord)trans.GetObject(bt["V1"], acadDb.OpenMode.ForWrite);
// Create the block reference...use the return from CreateEmployeeDefinition directly!
acadDb.BlockReference br = new acadDb.BlockReference(pos, btr.ObjectId);
// Add the reference to ModelSpace
btr.AppendEntity(br);
// Add the attribute reference to the block reference
trans.AddNewlyCreatedDBObject(br, true);
acadDb.ObjectId retId = br.ObjectId;
trans.Commit();
return retId;
}
}
0 Likes
Message 3 of 7

jbooth
Advocate
Advocate
What you have looks close enough, you *might need to alter it a bit, but the basis is correct.

I would suggest:
--Try to open bt with ForRead access. It may not require write access.
--Use: if(bt.Has("V1")) as a "block exists" check.
--Create the reference directly via: acadDb.BlockReference br = New acadDb.BlockReference(pos, bt["V1"])

Of course, if the block doesn't exist you can load it from another drawing. Try working with the AcDb.Database methods: ReadDwgFile(), Insert() and WblockCloneObjects().
0 Likes
Message 4 of 7

Anonymous
Not applicable
There are posts here that show how to do
that, which should give you the answers
you need.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2008
Supporting AutoCAD 2000 through 2008
http://www.acadxtabs.com

wrote in message news:5640783@discussion.autodesk.com...
how to insert existing block named "V1" into drawing?
I'm bit under pressure and I'm not able to make from samples mentioned on this forum.
thanks in advance
0 Likes
Message 5 of 7

Anonymous
Not applicable
Doesnt work:( no error message, no inserted block, I call this method with:
CreateBlock(dataSet.Tables[0].Rows[intRadek]["sloupec"].ToString(), new Autodesk.AutoCAD.Geometry.Point3d(x, 0, 0));


private acadDb.ObjectId CreateBlock(string nameOfBlock, Autodesk.AutoCAD.Geometry.Point3d pos)
{
// get the current working database
acadDb.Database db = acadDb.HostApplicationServices.WorkingDatabase;
using (acadDb.Transaction trans = db.TransactionManager.StartTransaction())
{
acadDb.BlockTable bt = (acadDb.BlockTable)(trans.GetObject(db.BlockTableId, acadDb.OpenMode.ForWrite));
acadDb.BlockTableRecord btr = (acadDb.BlockTableRecord)trans.GetObject(bt[nameOfBlock], acadDb.OpenMode.ForWrite);
// Create the block reference...use the return from CreateEmployeeDefinition directly!
//acadDb.BlockReference br = new acadDb.BlockReference(pos, bt[nameOfBlock]);
acadDb.BlockReference br = new acadDb.BlockReference(pos, btr.ObjectId);
// Add the reference to ModelSpace
btr.AppendEntity(br);
// Add the attribute reference to the block reference
trans.AddNewlyCreatedDBObject(br, true);
acadDb.ObjectId retId = br.ObjectId;
trans.Commit();
return retId;
}
}
0 Likes
Message 6 of 7

Anonymous
Not applicable
It seems it works with:
acadDb.BlockTableRecord btr = (acadDb.BlockTableRecord)trans.GetObject(bt[acadDb.BlockTableRecord.ModelSpace], acadDb.OpenMode.ForWrite);

Thanks
0 Likes
Message 7 of 7

Anonymous
Not applicable
Whole code, for future lames as I am:):

private acadDb.ObjectId CreateBlock(string nameOfBlock, Autodesk.AutoCAD.Geometry.Point3d pos)
{
// get the current working database
acadDb.Database db = acadDb.HostApplicationServices.WorkingDatabase;
acadDb.Transaction trans = db.TransactionManager.StartTransaction();
acadDb.BlockTable bt = (acadDb.BlockTable)(trans.GetObject(db.BlockTableId, acadDb.OpenMode.ForRead));
acadDb.BlockTableRecord btr = (acadDb.BlockTableRecord)trans.GetObject(bt[acadDb.BlockTableRecord.ModelSpace], acadDb.OpenMode.ForWrite);
// Create the block reference...use the return from CreateEmployeeDefinition directly!
acadDb.BlockReference br = new acadDb.BlockReference(pos, bt[nameOfBlock]);
//acadDb.BlockReference br = new acadDb.BlockReference(pos, btr.ObjectId);
// Add the reference to ModelSpace
btr.AppendEntity(br);
// Add the attribute reference to the block reference
trans.AddNewlyCreatedDBObject(br, true);
acadDb.ObjectId retId = br.ObjectId;
// trans.Commit();


trans.Commit();
trans.Dispose();
return retId;
0 Likes