.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Insert existing block into DWG C#

6 REPLIES 6
Reply
Message 1 of 7
supec
2535 Views, 6 Replies

Insert existing block into DWG C#

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
6 REPLIES 6
Message 2 of 7
supec
in reply to: supec

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;
}
}
Message 3 of 7
jbooth
in reply to: supec

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().
Message 4 of 7
Anonymous
in reply to: supec

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
Message 5 of 7
supec
in reply to: supec

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;
}
}
Message 6 of 7
supec
in reply to: supec

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

Thanks
Message 7 of 7
supec
in reply to: supec

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;

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost