How I may get block from other file dxf?

Jul 2, 2015 at 10:58 AM
Edited Jul 2, 2015 at 10:59 AM
I have a .dxf file with the blocks. Before, I used an old library version 0.6. I downloaded the file blocks, and then used the names are inserted into the current file. My code:
DxfDocument dxf_template = DxfDocument.Load ("template_bloks.dxf");
Blocks Locked = DXF template ["IFS"];
Insert new InsertBlock = Insert (block new Vector2 (CoordStart.X, CoordStart.Y))
{Layer = new netDxf.Tables.Layer ("ob_gs"),
Rotation = 0 ,
Scale = new Vector3 ( 10, 10 , 0 )} ;
dxf.AddEntity(insertBlock);
But now I use a new library, and this code is not working. Help!
Coordinator
Jul 2, 2015 at 5:52 PM
To move any DxfObject between different DxfDocuments you must clone them first. This includes all objects that derives from it: Layers, Linetypes, TextStyles, Entities, ..., and Blocks; sharing some of the information stored in those classes can cause unwanted problems. You should do something like:
DxfDocument doc = DxfDocument.Load("MyDxfFile");
Block block = doc.Blocks["MyBlock"];
// you must make a copy of the block before creating the Insert
Block newBlock = (Block) block.Clone();
// optionally you can give a new name to the copy
// Block newBlock = (Block)block.Clone("MyNewBlock");
DxfDocument newDoc = new DxfDocument();
Insert insert = new Insert(newBlock);
newDoc.AddEntity(insert);
newDoc.Save("NewMyDxfFile.dxf");
As a side note, I have seen that you are setting the block scale to Vector3(10, 10, 0), if it is contained on a plane (no Z coordinate is needed) you should just apply a uniform scale (new Vector3(10) or new Vector3(10, 10, 10) they are the same). In any other case, it is OK if you want to force all block entities to lay on a plane since all Z coordinates will be multiplied by 0.

Daniel
Jul 3, 2015 at 3:46 AM
haplokuon wrote:
To move any DxfObject between different DxfDocuments you must clone them first. This includes all objects that derives from it: Layers, Linetypes, TextStyles, Entities, ..., and Blocks; sharing some of the information stored in those classes can cause unwanted problems. You should do something like:
DxfDocument doc = DxfDocument.Load("MyDxfFile");
Block block = doc.Blocks["MyBlock"];
// you must make a copy of the block before creating the Insert
Block newBlock = (Block) block.Clone();
// optionally you can give a new name to the copy
// Block newBlock = (Block)block.Clone("MyNewBlock");
DxfDocument newDoc = new DxfDocument();
Insert insert = new Insert(newBlock);
newDoc.AddEntity(insert);
newDoc.Save("NewMyDxfFile.dxf");
As a side note, I have seen that you are setting the block scale to Vector3(10, 10, 0), if it is contained on a plane (no Z coordinate is needed) you should just apply a uniform scale (new Vector3(10) or new Vector3(10, 10, 10) they are the same). In any other case, it is OK if you want to force all block entities to lay on a plane since all Z coordinates will be multiplied by 0.

Daniel
Thanks! Everything works!