This project has moved. For the latest updates, please go here.

inserting different blocks

Nov 18, 2016 at 2:54 PM
Hi,
Thank you for writing such a great tool. My question is as follows:
I have a dxf file that contains more than one block with block names b1, b2, b3 etc. How do I insert for example b2 block using its name.
Thanks.
Coordinator
Nov 19, 2016 at 12:57 PM
First you will need to create an Insert entity with the reference of the block that you want. Something like:
DxfDocument doc = Test("MyFile.dxf");
Block b = doc.Blocks["b2"]; // it will return null if a block with that name does not exist, check it
Insert ins = new Insert(b); // later you can change its position, rotation, and scale
doc.AddEntity(ins); // and to finish 
Remember an Insert is a reference to a block, multiple instances of the same block might appear in a drawing and they are called inserts. That's how they are called in the dxf.

Daniel
Nov 19, 2016 at 1:28 PM
Thanks for the reply. What is the Test at the first line? Did you mean Load?. Let me clear what I want to do. I have the Blocks file let's say "MyBlocks.dxf". I want to insert some blocks from this file into current file let's say "MyDrawing" by using the names of the blocks.
Thanks.
Coordinator
Nov 20, 2016 at 10:43 AM
Yes, I meant DxfDocument.Load, sorry for that, blame the copy/paste.

When working with multiple documents you have to follow the same procedure. The only additional thing you have to take care is to clone the block first, entities and table objects (layers, linetypes, blocks,...) cannot be shared between documents, they must be cloned first.
DxfDocument doc1 = DxfDocument.Load("MyBlocks.dxf");
DxfDocument doc2 = new DxfDocument();
Block block = (Block) doc1.Blocks["b2"].Clone();
Insert ins = new Insert(block);
doc2.AddEntity(ins);
doc2.Save("MyDrawing.dxf");
One thing to consider when moving objects between documents is, if any copied table object from the original document has the same name (case insensitive) in the destination document they will be considered the same and, therefore, it will not override the existing one. For example, in the proposed situation, imagine that a block named "b2" already exists in doc2, after the insert "ins" is added to the document it will point to the existing "b2" block in doc2 and not to the cloned from "doc1". The same thing will happen for example with any layer, linetype, style,... referenced by the entities that compose the block.

For this cases of duplicate names, all table objects also have a clone method that takes a string as argument, so you can easily duplicate those objects with new names.

Daniel
Nov 20, 2016 at 5:26 PM
Thank you Daniel.