Get geometry from blocks?

Jun 1, 2014 at 12:03 PM
Hi, I have a file I need to extract the geometries from. Most of it works fine, except where I have some inserts, which reference blocks. I can't figure out how to get the geometry of the blocks, to then translate/rotate at the location of the insert.

Also, when i extract the colour the of the layer, it dark colours always seem to come out white? Is there a flag to tell if the colour should be inverted somehow?

Thanks
Coordinator
Jun 1, 2014 at 4:45 PM
The block property Entities will return the list of entities that make the block, so from an Insert entity you can access the information you are seeking with
List<EntityObject> entities = ins.Block.Entities;
There is some commented code in the Insert class under the #region Explode, that might help on what are you trying to achieve, the code might be a little obsolete and/ or contain bugs.

About the layer color, I am not sure. Are you using indexed colors? The color index 7 (AciColor.Default) might represent a white or black color depending on the background, the index 7 is white if the background is dark and black if it is light. The AciColor index 7 always return white since it doesn't know what is the background, and the default in AutoCad is dark (use to be black in the old versions now it is rgb(33,40,48)). In AutoCad this color index is also the default color for layers, the pure black and white colors are index 250 and 255 respectively.

There is no flag to check it, but you can convert your background color to HSL and use the lightness to decide if the color index 7 must be drawn in black or white.

Daniel
Jun 1, 2014 at 6:05 PM
Thanks for the quick reply.

I've looked at the entities property of the block and they all seem to be empty? I'm using a version of the code that is about 5 months old now, so could this be an old bug that has been fixed?

If i load the dxf into teigha viewer it all looks fine, so i know the file is good.

I'm just getting the colour from the layer.colour property. Should i be getting it from the indexes?

Where is the background colour property?

Thanks for your help.
Coordinator
Jun 2, 2014 at 3:57 PM
The blocks should work just fine even in an older version, but I would recommend you to update to last commit. If the version is not stable enough I would say so.

Are your blocks external references (xref), externally dependent or something alike? Because this kind of blocks are not supported at the moment. You can always email me the problematic file so I can take it a look.

About your color question. In the old days in AutoCad you only had 255 possible colors (0 and 256 are for special porpoises only), those are the ones represented by the AciColor.Index. Later the possibility of assigning rgb values was introduced but the index representation is still, I guess, the most common use, at the end all plans are plotted in black. So, going back to business, if rgb color information is found when reading the dxf, that color will have its UseTrueColor set to true, otherwise it will be false. If it is false, in the particular case of index 7 you will have to use black or white depending on your background color. The background color depends on your UI is not stored in the dxf file, at least I haven't found it yet; in any case that's something the user should be able to change and act accordingly.

Each index color has its own rgb representation and that is what you get through the R, G, B properties. If you use true color and index will also be automatically generated as the most approximated color equivalent (the R, G, B properties will still contain the original values), since AutoCad2000 does not support true color this approximated index will be use. The IndexRgb() will return a dictionary with the 255 possible indexed colors.

Daniel
Jun 2, 2014 at 5:02 PM
Hi Daniel,

I'm looking at the block collection as I load it in and all the Entities are empty. I'm sure that this is not using any xrefs, as it is the only file I have on my drive from this source, and it works fine in other viewers.

You were right about the colours, the problem ones were index 7, so I was able to catch this and use it to invert the colour of the background.

Is there anyway we can take this discussion offline, as the data I'm dealing with is confidential?

Thanks,

Rob Smart
Coordinator
Jun 2, 2014 at 10:56 PM
Edited Jun 2, 2014 at 11:04 PM
Are you using proxy objects, perhaps? If you open the file with a text editor, in the block section you will find the object definitions and here is where you will find the list of entities, that is what is being read. Just as a test, inside AutoCad, if this is what are you using, create a typical block through the block command, save the file and try to open it. This should work.

If you want or need to email me a file you can just send it to my email that you can find at the header of the source code files or simply haplokuon@gmail.com. The discussion about the library usage, I just prefer to maintain it public somebody else might be also interested. If you are worried because your file is too private, you can try to simplified it, and include only the block or blocks that are giving you problems. You can check the block name that should contain entities and delete everything else from the drawing.

Instead of inverting the background color what you should do is invert the color index 7, use black or white according with the background color lightness.

Daniel
Jun 3, 2014 at 2:57 PM
I understand, and would add the resolution to the post.

If i open the file in a text editor, what section am i looking for? Just the block section?

I do just invert the default colour rather than the background. I set the line colour to be inverse of the background colour.

Thanks
Coordinator
Jun 3, 2014 at 5:46 PM
Yes, you should just look for block definitions in the BLOCKS section, other kinds of blocks might store information somewhere else. The common blocks store the list of entities there; other types, I am not sure, I still need to study them, the documentation is so obtuse in places that, many times, the only way is to take a look at the raw dxf output.

Daniel
Jun 9, 2014 at 8:58 AM
Hi, I've looked in the blocks section and I'm seeing some binary data in there which I'm assuming is the geometry like this:

BLOCK_RECORD
5
A18D
330
1
100
AcDbSymbolTableRecord
100
AcDbBlockTableRecord
2
BASIN
340
0
310
2800000020000000200000000100080000000000000400000000000000000000000000000000000000000000000080000080000000808000800000008000800080800000C0C0C000C0DCC000F0CAA6000020400000206000002080000020A0000020C0000020E00000400000004020000040400000406000004080000040A0
310
000040C0000040E00000600000006020000060400000606000006080000060A0000060C0000060E00000800000008020000080400000806000008080000080A0000080C0000080E00000A0000000A0200000A0400000A0600000A0800000A0A00000A0C00000A0E00000C0000000C0200000C0400000C0600000C0800000C0
310
A00000C0C00000C0E00000E0000000E0200000E0400000E0600000E0800000E0A00000E0C00000E0E00040000000400020004000400040006000400080004000A0004000C0004000E00040200000402020004020400040206000402080004020A0004020C0004020E000404000004040200040404000404060004040800040
310
40A0004040C0004040E00040600000406020004060400040606000406080004060A0004060C0004060E00040800000408020004080400040806000408080004080A0004080C0004080E00040A0000040A0200040A0400040A0600040A0800040A0A00040A0C00040A0E00040C0000040C0200040C0400040C0600040C08000
310
40C0A00040C0C00040C0E00040E0000040E0200040E0400040E0600040E0800040E0A00040E0C00040E0E00080000000800020008000400080006000800080008000A0008000C0008000E00080200000802020008020400080206000802080008020A0008020C0008020E00080400000804020008040400080406000804080
310
008040A0008040C0008040E00080600000806020008060400080606000806080008060A0008060C0008060E00080800000808020008080400080806000808080008080A0008080C0008080E00080A0000080A0200080A0400080A0600080A0800080A0A00080A0C00080A0E00080C0000080C0200080C0400080C0600080C0
310
800080C0A00080C0C00080C0E00080E0000080E0200080E0400080E0600080E0800080E0A00080E0C00080E0E000C0000000C0002000C0004000C0006000C0008000C000A000C000C000C000E000C0200000C0202000C0204000C0206000C0208000C020A000C020C000C020E000C0400000C0402000C0404000C0406000C0
310
408000C040A000C040C000C040E000C0600000C0602000C0604000C0606000C0608000C060A000C060C000C060E000C0800000C0802000C0804000C0806000C0808000C080A000C080C000C080E000C0A00000C0A02000C0A04000C0A06000C0A08000C0A0A000C0A0C000C0A0E000C0C00000C0C02000C0C04000C0C06000
310
C0C08000C0C0A000F0FBFF00A4A0A000808080000000FF0000FF000000FFFF00FF000000FF00FF00FFFF0000FFFFFF0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
310
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000FFFFFFFFFF000000000000000000000000000000000000000000000000FFFFFF00
310
00000000FFFFFFFF000000000000000000000000000000000000FFFF000000000000000000000000FFFF0000000000000000000000000000FFFF00000000000000000000000000000000FFFF0000000000000000000000FF000000000000000000FFFFFF0000000000000000FF000000000000000000FF000000000000FFFF
310
FFFF000000FFFFFF000000000000FF00000000000000FF0000000000FFFF00000000000000000000FFFF0000000000FF0000000000FF0000000000FF0000000000000000000000000000FF0000000000FF00000000FF00000000FF00000000000000000000000000000000FF00000000FF00000000FF00000000FF00000000
310
00000000000000000000000000FF00000000FF0000FF00000000FF000000000000000000000000000000000000FF00000000FF0000FF0000000000FF0000000000000000000000000000000000FF00000000FF0000FF0000000000FF00FFFF00000000FFFF00000000FFFF00FF0000000000FF0000FF000000000000FF00FF
310
000000FF0000FF000000FF00FFFF0000000000FF0000FF000000FFFFFF00FF00000000FF0000FF00000000FF00FFFFFF000000FF0000FF000000FFFF00FFFFFF000000FFFFFFFF000000FFFFFF00FFFF000000FF0000FF00FFFFFFFFFFFF0000FFFFFFFFFFFFFFFFFFFF0000FFFFFFFFFFFF00FF0000FF00FFFFFFFFFFFF00
310
00000000000000000000000000FFFFFFFFFFFF00FF0000FF000000FFFF000000000000000000000000000000000000FFFF000000FF0000FF000000FFFF000000000000000000000000000000000000FFFF000000FF0000FF00000000000000000000000000000000000000000000000000000000FF0000FFFFFFFFFFFFFFFF
310
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
310
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
102
{BLKREFS
331
AEEF
331
AEF0
331
AEF1
331
AEF2
331
AEF3
331
AEF4
331
AEF5
331
AEF6
331
AEF7
331
AEF8
331
AEF9
331
AEFA
331
AEFB
331
AEFC
331
AAF3
331
AAF4
331
AAF5
102
}
70
 0
280
 1
281
 0
0
BLOCK_RECORD


I would expect then that the block would have a geometry, and that the inserts link to that block, but when I find that block reference in the code, the entities list is always empty. I have checked and the blocks do link to a valid insert, so it looks like there might be an issue in the code some where?
Coordinator
Jun 9, 2014 at 5:58 PM
You are looking in the wrong place. The BLOCK_RECORD entries are defined in the TABLES section. You need to look in the BLOCKS section, it is there where you will find the BLOCK definitions. The block record code 310 is binary data, but it is a bitmap preview that AutoCad uses for its interface, the block geometry is in the block definition not in the block record.

When I said that you could edit the file and delete everything else, what I meant is that you edit your drawing with your cad program, for example, in AutoCad you can delete all the entities of your drawing, this operation will not delete the block definitions just the entities, then you can save the drawing as a dxf and email me this new file. The blocks are deleted with the purge command and only when there are no inserts that references them. You could even purge them and leave just one of the problematic blocks. Other people have sent me their own drawings to check them, and I will never make them public, they are only for testing purposes. They are really a great way to test the project, since, in a unconscious level, and do not make things that I know will cause problems.

Remember that this library does not support all possible entities, and there are some like 3dsolids and regions that, most likely, will not be able to read since they depend in proprietary data that is based in the ACIS-Sat format, and, as far as I know, it has not been made public. If your blocks are made of this kind of geometry the blocks will return an empty entities list. In the netDxf project front page you will find the complete list of supported entities.

Daniel
Jun 10, 2014 at 8:38 AM
I have sent an email to you containing the file in question.

Thanks,

Rob
Coordinator
Jun 11, 2014 at 6:00 PM
I have checked your file, and I haven't seen anything wrong with the blocks. It worked as expected, the only thing worth mentioning is that it contained region entities that were not imported since these kind of entities are not supported.

My only recommendation is to update your code, if you haven't done it already. May be there was something weird in a previous update that was causing your problems, but I cannot say why or where.

The "sample.dxf", included in the project, is a testing file that contains all supported entities by the library, and between other things, I always test it to make sure all known entities are imported and exported correctly. You can try it with your actual code to see if the block of this drawing also returns an empty Block.Entities list, if this works perhaps there was something strange in your file that made the importer to not work properly.

Daniel