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

Copy an block to another block

5 REPLIES 5
Reply
Message 1 of 6
jobro888us
252 Views, 5 Replies

Copy an block to another block

Hello,

I want to copy a block in a drawing to anotherblock in the same drawing. First I inserted a WBock thats OK.
But How can I copy a block(blokreference) in .NET?
I tryed deepclone and getTransformedCopy
I have the following code:

using (Transaction trans = databas.TransactionManager.StartTransaction())

{
Database db = new Database(false, false);

db.ReadDwgFile(comp, System.IO.FileShare.Read, true, "");

ObjectId NewBlkId = new ObjectId();

NewBlkId = doc.Database.Insert(comp, db, false);

BlockTable bt = (BlockTable)trans.GetObject(databas.BlockTableId,
OpenMode.ForRead, true);

BlockTableRecord btr
= (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);

BlockReference bref = new BlockReference(new
Autodesk.AutoCAD.Geometry.Point3d(10, 10, 0), NewBlkId);

btr.AppendEntity(bref);

trans.AddNewlyCreatedDBObject(bref, true);

Matrix3d matr = new Matrix3d();
;
BlockReference bref2 = (BlockReference)bref.GetTransformedCopy(matr);
//I tryed this but I get an exception that the block already exist:
//BlockReference bref4 = (BlockReference)bref.DeepClone(bref2, new IdMapping(), true);



btr.AppendEntity(bref2);
trans.AddNewlyCreatedDBObject(bref2, true);

trans.Commit();
}

What I am doing wrong?
thanks

jobro
5 REPLIES 5
Message 2 of 6
cadMeUp
in reply to: jobro888us

A couple of things, you should close the database that you are opening after you insert the block in the database:

>> Database db = new Database(false, false);
>> db.ReadDwgFile(comp, System.IO.FileShare.Read, true, "");

db.CloseInput(true);
db.Dispose();

Set your transform matrix to displacement:

Point3d p1 = new Point3d(0,0,0);
Point3d p2 = new Point3d(10,10,0);
Matrix3d matr = Matrix3d.Displacement(p2-p1);

Also, you really should add some error checking, try/catch blocks, to test for error conditions.

Question:
Are you trying to add a block reference to another actual block reference where one block would become a part of another block?
Message 3 of 6
jobro888us
in reply to: jobro888us

Hi Larry, thanks for your reaction.
You are right about the try and catch but I do that in the method which envokes this one.

What I try to do is to insert a block from the harddisk: a normal dwg file, in my drawing. That works fine but if the drawing is inserted, the inserted block has no name and it is not a part of my blocktable. I cant see the block if I use the Autocad command: block:

I think I have the to give a block a name but the next lines creates an "eInvalidName exception".


NewBlkId = doc.Database.Insert(comp, db, false);

BlockTable bt = (BlockTable)trans.GetObject(databas.BlockTableId,
OpenMode.ForRead, true);

BlockTableRecord btr
= (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);

btr.name = "AK_E_MAT_ONDERHOEK";

in the last line the exception appears, so it is not allowed to give a btr an name.

Do you know how I can solve the problem and add the block to the blocktable.

thanks

Jobro
Message 4 of 6
jobro888us
in reply to: jobro888us

Ok, problem is solved.
Message 5 of 6
cadMeUp
in reply to: jobro888us

Ok, Good!
I just want to make sure that with:
db.Insert("BLOCK_NAME_HERE", db, true)

The first parameter is expecting a new block name (with valid characters) and not a path.
Message 6 of 6
jobro888us
in reply to: jobro888us

Yes Larry, Ok thanks again...my problem was indeed that I used a complete path in db.insert(.......) thats why I didnt get a blockname. I used this solution but yours is better shorter at least.
where newBlk is the ObjectID.:


BlockTableRecord bd = (BlockTableRecord)trans.GetObject(NewBlkId, OpenMode.ForWrite);
bd.Name = blocknaam;

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