haplokuon / netDxf

.net dxf Reader-Writer
MIT License
978 stars 399 forks source link

Error reading SHP file #256

Open iborzenkov opened 3 years ago

iborzenkov commented 3 years ago

I have a problem reading the SHP file. The problem is with the internal short ShapeNumber(string name) method of the ShapeStyle class.

string[] tokens = line.TrimStart('*').Split(',');
// the third item is the name of the shape
if (string.Equals(tokens[2], name, StringComparison.InvariantCultureIgnoreCase))
{
return short.Parse(tokens[0]);
}

The string value tokens[0] is a hexadecimal number. You can see this on the Autodesk website, where you can find an example of the SHP file: http://docs.autodesk.com/ACD/2011/ENU/filesACG/WS73099cc142f4875513fb5cd10c4aa30d6b-7eeb.htm

hex

When calling the Parse() method, you must specify a sign that the string contains a number in the hexadecimal number system.

I suggest making the following changes:

string[] tokens = line.TrimStart('*').Split(',');
// the third item is the name of the shape
if (string.Equals(tokens[2], name, StringComparison.InvariantCultureIgnoreCase))
{
return short.Parse(tokens[0], NumberStyles.AllowHexSpecifier);
}
haplokuon commented 3 years ago

The file you are pointing to corresponds to a font SHP (SHX) file and while they share many similarities with the common shape SHP (SHX) files there have a few differences. Shape files are handled by the ShapeStyle table while the font files should be used with the TextStyle in netDxf. The shape description, the line that starts with an '*' character, is composed by three elements a shape number, the number of bytes that follows that defines the geometry of the shape, and a shape name.

For the font SHP (SHX) files this is not always true. The first entry describes the font itself, in your case it shows "*UNIFONT,6,ROMANS Copyright 1997 by Autodesk, Inc." and clearly the first element of the description is not a number but a string. Each subsequent entries describe each character of the font and the shape number corresponds to an ASCII code.

About the shape number, the documentation does not clearly state if the number is decimal or hexadecimal, it says "A number, unique to the file, between 1 and 258 (and up to 32768 for Unicode fonts)", but perhaps it is only hexadecimal for Unicode fonts or perhaps it follows a similar rule as the number of bytes, if it starts with a 0 it is hexadecimal. In another part of the documentation it shows an example where it says that the numbers are hexadecimal values for Unicode font. Personally, I always have seen decimal numbers in common (not fonts) shape files. I am not sure about this I will have to check it and perhaps it will solve my problem reading SHX files.

Resuming, font files cannot be used in the ShapeStyle only in the TextStyle. As far as I know AutoCad do not supply the SHP files for their SHX fonts, so it shouldn't be a problem, but I can check the first entry and raise an exception if the file is incompatible.