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

Check handle overwrites

Mar 20, 2015 at 10:10 PM
If I try something like this ( that is wrong )
var blktest = new netDxf.Blocks.Block("TEST");
dxf.Blocks.Add(blktest); // this is wrong
var l = new netDxf.Entities.Line(new Vector3(), new Vector3(100, 100, 0));
blktest.Entities.Add(l);
dxf.AddEntity(new Insert(blktest));
the generated document gives error referring the wrong handle in the database.

I wondering if is possible to notify the developer the mistake by generating an exception just when I try to add the block to the blocks list.
May the Add method to the Blocks is used in other scenarios that I don't know, may for the block copy from other files, but still useful to have a check about this mistake if possible.
Coordinator
Mar 22, 2015 at 7:43 PM
Edited Mar 23, 2015 at 1:52 PM
This is the result of a limitation with the library that has been raised before. As a rule of thumb you should not change any object once it has been added to the document, this includes both entities and table objects. While some changes will not cause any problem, the modification of properties that references other DxfObjects is not safe. So, from the code you are showing:
1     var blktest = new netDxf.Blocks.Block("TEST");
2     dxf.Blocks.Add(blktest); // <- the problem is not here
3     var l = new netDxf.Entities.Line(new Vector3(), new Vector3(100, 100, 0));
4     blktest.Entities.Add(l); // <- but here. Your are modifying a block that is already added to the document
5     dxf.AddEntity(new Insert(blktest));
You have to options:
  1. Delete line 2. The AddEntity method will take care of everything. If the Block referenced by the Insert is not in the document, it will be automatically added to the block list. This applies to layers, line types, text styles,....
  2. Move line 4 before line 2. The modifications will be done before it is added to the document.
You can check the DxfObject.Owner property. if it returns anything but null, the object already belongs to someone, and should not be edited. I have not added exceptions for this kind of scenarios since this is something I plan to fix, and it is in my number one priority list.

The best you can do, at the moment, is prepare first all entities in any way you please, so later you can add them to the document in one go.

Daniel