dxf格式(R12/LT2)文本手动解析

dxf格式(R12/LT2)文本手动解析自己写的 dxf 格式 R12 LT2 文本手动解析 dxf 格式 R12 LT2 用记事本等文本编辑器打开 类似于下面这种通过分析文本特点进行解析 提取多边形信息 直接上代码 pragmaonce include StdStrFile h typedefstruc cadPtInfo std stringstrLay double

自己写的dxf格式(R12/LT2)文本手动解析。

dxf格式(R12/LT2),用记事本等文本编辑器打开,类似于下面这种

dxf格式(R12/LT2)文本手动解析

通过分析文本特点进行解析,提取多边形信息。

直接上代码:

#pragma once #include "StdStrFile.h" typedef struct _cadPtInfo { std::string strLayerName; double x; double y; double z; _cadPtInfo(const double& _x = 0.0, const double& _y = 0.0, const double& _z = 0.0, const std::string& _layerName = "") { x = _x; y = _y; z = _z; strLayerName = _layerName; } }cadPtInfo; class cadPolyInfo { public: std::string strName; std::string strLayerName; std::vector 
  
    vPts; public: int SaveAsRrlx(const std::string& _strDirPath, const std::string _strName = ""); }; int ReadDxfPolys(const char* szDxfPath, const char* szLayerName, std::vector 
   
     & vPolys, bool bIndexName = false); int SaveDxfPolys(const char* szDirPath, std::vector 
    
      & vPolys); 
     
    
  

接着是cpp文件:

#include "ReadPolys.h" //内部使用,外部不使用 typedef struct _cadPt { std::string strLayerName; std::vector 
  
    vPts; _cadPt() { } }_innerCadPt; int ReadDxfPolys(const char* szDxfPath, const char* szLayerName, std::vector 
   
     & vPolys, bool bIndexName/* = false*/) { std::vector 
    
      vDxfContents; size_t nLineNum = CStdFile::ParseTXTFile(szDxfPath, vDxfContents); if (nLineNum == 0) { return -1; } for (size_t i = 0; i < nLineNum; ++i) { const std::string strCurLine = vDxfContents[i]; //判断坐标内容 if (strCurLine == "POLYLINE") { //开始寻找多边形 cadPolyInfo poly; for (size_t j = i + 1; j < nLineNum; ++j) { const std::string strCurLineSub = vDxfContents[j]; if (strCurLineSub == "EOF") { //最后一个多边形已经结束 poly.strName = vDxfContents[j - 4]; break; } if (strCurLineSub == "POLYLINE") { //当前多边形已经结束 poly.strName = vDxfContents[j - 2]; i = --j; break; } if (strCurLineSub == szLayerName) { _innerCadPt pt; for (size_t k = j +1; k < nLineNum; ++k) { //此时开始读取接下来的行,并且不超过总行数 double dCoor = 0.0; if (CStdTpl::ConvertFromString(dCoor, vDxfContents[k]) == nullptr) { pt.vPts.clear(); break; } else { pt.vPts.push_back(dCoor); } if (pt.vPts.size() == 5) { size_t nNextLineIndex = k + 1; if (nNextLineIndex < nLineNum && CStdTpl::ConvertFromString(dCoor, vDxfContents[nNextLineIndex]) == nullptr) { pt.strLayerName = szLayerName; } break; } } //判断当前读取的是否是坐标 if (pt.vPts.size() == 5 && fabs(pt.vPts[0] - 10.0) < 1e-6 && fabs(pt.vPts[2] - 20.0) < 1e-6) { cadPtInfo ptinfo; ptinfo.strLayerName = pt.strLayerName; ptinfo.x = pt.vPts[1]; ptinfo.y = pt.vPts[3]; ptinfo.z = pt.vPts[4]; poly.vPts.push_back(ptinfo); i = ++j; } } } //添加最终结果,如果没有层名,则添加索引层名 if (bIndexName) { static int nIndex = 0; poly.strName = CStdTpl::ConvertToString(nIndex++); } vPolys.push_back(poly); } } return 0; } int SaveDxfPolys(const char* szDirPath, std::vector 
     
       & vPolys) { size_t nPolyNum = vPolys.size(); for (size_t i = 0; i < nPolyNum; ++i) { vPolys[i].SaveAsRrlx(szDirPath); } return (int)nPolyNum; } int cadPolyInfo::SaveAsRrlx(const std::string& _strDirPath, const std::string _strName /*= ""*/) { std::string strSaveName(_strName); if (strSaveName.length() == 0) { if (strName.length() > 0) { strSaveName = strName; } else { static int nNum; strName = CStdTpl::ConvertToString(nNum++); } } //如果后缀不是.rrlx则添加后缀 std::string strSuffix = CStdStr::ToUpperLower(CStdStr::GetSuffixOfFile(strSaveName)); if (strSuffix != ".rrlx") { strSaveName += ".rrlx"; } std::string strSavePath = CStdStr::AddSlashIfNeeded(_strDirPath) + strSaveName; std::vector 
      
        vSaveContent; size_t nPtNum = vPts.size(); //首先添加点的个数 vSaveContent.push_back(CStdTpl::ConvertToString(nPtNum) + '\n'); //添加点的坐标 for (size_t i = 0; i < nPtNum; ++i) { const cadPtInfo& pt = vPts[i]; std::string strLine = ToString(pt.x) + '\t' + ToString(pt.y) + '\t' + '0'; vSaveContent.push_back(strLine + '\n'); } CStdFile::SaveTXTFile(strSavePath, vSaveContent); return 0; } 
       
      
     
    
  

更多的交流,欢迎留言。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/219351.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月17日 下午10:46
下一篇 2026年3月17日 下午10:46


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号