SRL Archive - General

Help Reading DXF Files


3Garrett3

2013-05-23 13:39:09

1) I realize this may be the wrong subforum, but I didn't see other obvious fits.

I'm trying to help my father with stuff for his work, but I'm at a loss and the obvious place to ask for help with something like this is these forums. What he's trying to do is have a 2D drawing in AutoCAD be saved as a DXF file and imported into a different program which reads the shape and turns it into useful code for moving CNC machines. Apparently they have an old program where shapes are hard coded as certain dimensions in a binary DXF file, and it can't read AutoCAD binary DXF files. I looked through the code AutoCAD generates and I'm not surprised, it's a complete mess.

Included below are two ways to get the Binary DXF of a 20x20 inch square. If anyone can find the secret to parsing the shape out of the binary file, it would be greatly appreciated.

Note: DXF files cannot be uploaded to the forums, so the following link can be used to download the file: https://www.dropbox.com/s/kc1457zd9w05ndw/SquareInches.dxf
Here is a pastebin of what is shown in notepad (the .txt is too large for the forum): http://pastebin.com/dFvyubR8

Brandon

2013-05-23 15:07:53

From: 3Garrett3
1) I realize this may be the wrong subforum, but I didn't see other obvious fits.

I'm trying to help my father with stuff for his work, but I'm at a loss and the obvious place to ask for help with something like this is these forums. What he's trying to do is have a 2D drawing in AutoCAD be saved as a DXF file and imported into a different program which reads the shape and turns it into useful code for moving CNC machines. Apparently they have an old program where shapes are hard coded as certain dimensions in a binary DXF file, and it can't read AutoCAD binary DXF files. I looked through the code AutoCAD generates and I'm not surprised, it's a complete mess.




Uhhh you're opening a Binary-File in an ASCII text reader. Unless the binary file is written fully in plain-text (which I can see it is partially), you won't get anything useful out of it other than import sections. A HexEditor is what you need really but lets skip that and cut to the chase.

You want to read a given file.

  1. Identify the format.
  2. Identify the file version & format specification.
  3. Choose a language.
  4. Write a reader.


The green one is what you have completed so far. You can find the specification for the file here:

http://usa.autodesk.com/adsk/servlet/item?siteID=123112&id=12272454&linkID=10809853

From there, it's fairly easy to read as its all bytes (hence binary). Using the information given, you can identify segments of the file using those group-codes from the PDF.

3Garrett3

2013-05-23 15:35:20

From: Brandon
Uhhh you're opening a Binary-File in an ASCII text reader. Unless the binary file is written fully in plain-text (which I can see it is partially), you won't get anything useful out of it other than import sections. A HexEditor is what you need really but lets skip that and cut to the chase.

You want to read a given file.

  1. Identify the format.
  2. Identify the file version & format specification.
  3. Choose a language.
  4. Write a reader.


The green one is what you have completed so far. You can find the specification for the file here:

http://usa.autodesk.com/adsk/servlet/item?siteID=123112&id=12272454&linkID=10809853

From there, it's fairly easy to read as its all bytes (hence binary). Using the information given, you can identify segments of the file using those group-codes from the PDF.


Yes, sorry. I wasn't exactly clear. I'm helping my dad figure out why his old program can't parse the information from the DXF. I pasted the .txt because the DXF wasn't able to be uploaded. I don't have access to his reader, so I'm only trying to figure out what I can help him with. I should be able to work out where things are from the file specification, it's AutoCAD 2013 DXF that I saved it in. I was just hoping someone here might be able to figure this out a little faster than I could. I'm just struggling through with a Hex Editor, but I'll try to figure it out.

Thanks for the lead, if anyone notices anything before I get to it, feel free to lead me in the right direction.

Brandon

2013-05-23 16:08:03

From: 3Garrett3
Yes, sorry. I wasn't exactly clear. I'm helping my dad figure out why his old program can't parse the information from the DXF. I pasted the .txt because the DXF wasn't able to be uploaded. I don't have access to his reader, so I'm only trying to figure out what I can help him with. I should be able to work out where things are from the file specification, it's AutoCAD 2013 DXF that I saved it in. I was just hoping someone here might be able to figure this out a little faster than I could. I'm just struggling through with a Hex Editor, but I'll try to figure it out.

Thanks for the lead, if anyone notices anything before I get to it, feel free to lead me in the right direction.



Hmm.. I got that much. Did he write the old program himself? Does he have access to the source code? If he doesn't then there isn't much you can do except write your own parser.

Uhh I didn't expect to post any code but this will certainly help you start (NOTE: Where "Code" = 10, 20, 30; The Point is thus Point3D(Value, Value, Value).. If you see Code = 9, then the Value following it is a String that describes the next segment/section:


#include <iostream>
#include <vector>
#include <fstream>

int main()
{
std::fstream File("C:/Users/Brandon/Desktop/sample1.dxf", std::ios::binary | std::ios::in);
if (File.is_open())
{
File.seekg(0, std::ios::end);
int Length = File.tellg();
File.seekg(0, std::ios::beg);

auto ReadDoubleLine = [](std::fstream &Stream, std::string &LineOne, std::string &LineTwo) {
std::getline(Stream, LineOne);
std::getline(Stream, LineTwo);
};

std::string Code = "0";
std::string Value = "0";

while(!File.eof())
{
ReadDoubleLine(File, Code, Value);
std::cout<<"Code: "<<Code<<"\n";
std::cout<<"Value: "<<Value<<"\n";
std::cin.get();
}

File.close();
}
}


And when it prints, it looks like:


3Garrett3

2013-05-23 16:17:49

Yes, he does have access to the source code. The programmer working on it told me that his reader wasn't working on the AutoCAD DXF, so I sent him the link you gave me and started looking through it myself. I'm assuming he will figure it out from there eventually. As expected though, someone on the forums knew what to look for from the start. If he has further concerns, I'll update you, but for now assume that he will probably figure out where to look for the shape data.