I want to read a .dxf file. I am using c# library to read. It read the file but it can't write all layers.
I want to get all list of Layers from it and write it.
I want to read a .dxf file. I am using c# library to read. It read the file but it can't write all layers.
I want to get all list of Layers from it and write it.
You do not really need a library for this. DXF is in ASCII format and you got one line of text per DXF code followed immediately by a value.
All entities are making reference to a Layer using the DXF code 8, so you can do a regular expression search with '^8[\r\n]*([_a-zA-Z0-9-]+)$' and get the name. This method will work with partial DXF and older R12 DXF files. That will also give you the layers which are really used, like if you did purge the drawing first.
You could also use the Layer Table header to read the layer names. It is a bit more difficult since you need to find that section, but on the same principal, you search for code 0 - LAYER, then the following code 2 will give you the layer name.
I look this library. You have in public DXFTable layers
all layers. Read documentation and look structure of DXFImport.cs
I've written some code to read points/lines/arc's from all ASCII DXF versions available at the moment with the same code
You can add more objects bij just adding more subroutines with the correct name
(look inside the DXF).
What it does:
Then you can do something with the data
private void ReadDxfFile (string DxfFile)
{
string Layer = "";
string[] D = DxfFile.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
int iEntities = 0; for(int i = 0; i < D.Length; i++) { if (D[i] == "ENTITIES") {iEntities = i; break; } }
for (int i = iEntities; i < D.Length; i++)
{
if (D[i] == "POINT" || D[i] == "AcDbPoint")
{
int iEntity = i; if (D[i].StartsWith("AcDb")) { for (iEntity = i; D[iEntity] != "AcDbEntity"; iEntity--) ; }
Layer = ""; for (int iLayer = iEntity; iLayer < i + 10 && Layer == ""; iLayer++) { if (D[iLayer] == " 8") { Layer = D[iLayer + 1]; }; }
for (int iWaarden = i; iWaarden < i + 8; iWaarden++)
{
if (D[iWaarden] == " 10" && D[iWaarden + 2] == " 20")
{
//Here you can store the following data in a list for later use
//LayerName = Layer
//X = D[iWaarden + 1]
//Y = D[iWaarden + 3]
//Z = D[iWaarden + 5]
}
}
}
if (D[i] == "LINE" || D[i] == "AcDbLine")
{
int iEntity = i; if (D[i].StartsWith("AcDb")) { for (iEntity = i; D[iEntity] != "AcDbEntity"; iEntity--) ; }
Layer = ""; for (int iLayer = iEntity; iLayer < i + 10 && Layer == ""; iLayer++) { if (D[iLayer] == " 8") { Layer = D[iLayer + 1]; }; }
for (int iWaarden = i; iWaarden < i + 10; iWaarden++)
{
if (D[iWaarden] == " 10" && D[iWaarden + 2] == " 20")
{
//Here you can store the following data in a list for later use
//LayerName = Layer
//Xbegin = D[iWaarden + 1]
//Ybegin = D[iWaarden + 3]
//Zbegin = D[iWaarden + 5]
//Xend = D[iWaarden + 7]
//Yend = D[iWaarden + 9]
//Zend = D[iWaarden + 11]
}
}
}
if (D[i] == "ARC" || D[i] == "AcDbArc" || D[i] == "AcDbCircle")
{
int iEntity = i; if (D[i].StartsWith("AcDb")) { for (iEntity = i; D[iEntity] != "AcDbEntity"; iEntity--) ; }
Layer = ""; for (int iLayer = iEntity; iLayer < i + 10 && Layer == ""; iLayer++) { if (D[iLayer] == " 8") { Layer = D[iLayer + 1]; }; }
for (int iWaarden = i; iWaarden < i + 10; iWaarden++)
{
if (D[iWaarden] == " 10" && D[iWaarden + 2] == " 20" && D[iWaarden + 10] == " 51")
{
//Here you can store the following data in a list for later use
//LayerName = Layer
//Xmid = D[iWaarden + 1]
//Ymid = D[iWaarden + 3]
//Zmid = D[iWaarden + 5]
//Radius = D[iWaarden + 7]
//StartAngle = D[iWaarden + 9]
//StartAngle = D[iWaarden + 11]
}
if (D[iWaarden] == " 10" && D[iWaarden + 2] == " 20" && D[iWaarden + 12] == " 51")
{
//Here you can store the following data in a list for later use
//LayerName = Layer
//Xmid = D[iWaarden + 1]
//Ymid = D[iWaarden + 3]
//Zmid = D[iWaarden + 5]
//Radius = D[iWaarden + 7]
//StartAngle = D[iWaarden + 11]
//StartAngle = D[iWaarden + 13]
}
}
}
}
}