在c#中读取.DXF / .DWG文件

时间:2016-04-28 07:12:32

标签: c# asp.net autocad

我想读一个.dxf文件。我正在使用c#库来阅读。它读取文件,但无法写入所有图层。

我想从中获取所有图层列表并编写它。

3 个答案:

答案 0 :(得分:2)

你真的不需要一个库。 DXF是ASCII格式,每个DXF代码有一行文本,后面紧跟一个值。

所有实体都使用DXF代码8引用图层,因此您可以使用'^ 8 [\ r \ n] *([_ a-zA-Z0-9 - ] +)$进行正则表达式搜索'并得到名字。此方法适用于部分DXF和较旧的R12 DXF文件。这也将为您提供真正使用的图层,就像您首先清除图形一样。

您还可以使用图层表标题来读取图层名称。由于你需要找到那个部分,但是在同一个主体上搜索代码0 - LAYER,然后下面的代码2将为你提供图层名称,这有点困难。

答案 1 :(得分:1)

我看这个library。您有public DXFTable layers所有图层。阅读DXFImport.cs的文档和外观结构

答案 2 :(得分:0)

我已经编写了一些代码来读取目前可用的所有ASCII DXF版本中的点/线/弧,代码相同 您可以添加更多对象bij,只需添加更多具有正确名称的子例程 (看看DXF内部)。

它的作用:

  1. 您将使用阅读器导入的整个文件发送到此例程
  2. 然后它搜索ENTITIES块(这是几何存储的位置)
  3. 然后搜索对象(点/线/弧)
    (R12至R14使用POINT / LINE / ARC)
    (R2000至R2013使用AcDbPoint / AcDbLine / AcDbArc或AcDbCircle,如果它包含角度)
  4. 然后检查图层名称
  5. 然后检查变量代码是否存在
  6. 然后你可以对数据做些什么

        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]
                    }
                }
            }
    
        }
    }