CAD DWG/DXF文件C++解析庫libdxfrw


libdxfrw是一個免費的C ++庫,可以讀寫ascii和二進制格式的DXF文件。
也可以讀取從R14到最后一個V2015的DWG文件。
它是根據GNU通用公共許可版本2(或您選擇的任何更高版本)的條款許可的。
如果您正在尋找有關該項目的一般信息,請訪問網站:http://sourceforge.net/projects/libdxfrw
github地址:https://github.com/codelibs/libdxfrw
libdxfrw是由libreCAD的一位開發者創建,libreCAD使用了該庫來解析CAD文件。
libreCAD官網:https://librecad.org/
在github上下載最新源碼,解壓

編譯libdxfrw
通用腳本(unix)

autoreconf -vfi (optional)
./configure
make
make install (as root)

Windows VC++編譯

  • 使用VS2013打開vs2013 \ libdxfrw.sln
  • 構建解決方案還有一個依賴於libdxfrw的dwg到dxf轉換器,可以以相同的方式構建。
  • 使用VS2013打開dwg2dxf \ vs2013 \ dwg2dxf.sln
  • 構建解決方案

我采用VS2017版本,直接打開libdxfrw-master/vs2013/libdxfrw.sln,如下圖所示:


源文件存放在src目錄下,比之前dxflib的源文件多了很多!
直接構建build,我這里有個編譯錯誤:

參數類型不匹配,無法將“char **”轉換為“const char **”
解決辦法:非const轉const,強轉一下即可!

再次編譯通過,在libdxfrw-master\vs2013\Debug或Release生成靜態庫文件libdxfrw.lib。

如何使用libdxfrw:https://forum.librecad.org/How-to-use-libdxfrw-td5713395.html
此外,壓縮包里還包含兩個examples:
1、dwg2text:從dwg/dxf文件中提取文本(read)

 1 /******************************************************************************
 2 **  dwg2text - Program to extract text from dwg/dxf                          **
 3 **                                                                           **
 4 **  Copyright (C) 2015 José F. Soriano, rallazz@gmail.com                    **
 5 **                                                                           **
 6 **  This library is free software, licensed under the terms of the GNU       **
 7 **  General Public License as published by the Free Software Foundation,     **
 8 **  either version 2 of the License, or (at your option) any later version.  **
 9 **  You should have received a copy of the GNU General Public License        **
10 **  along with this program.  If not, see <http://www.gnu.org/licenses/>.    **
11 ******************************************************************************/
12 
13 #include <iostream>
14 #include <fstream>
15 #include <sys/stat.h>
16 
17 #include "dx_iface.h"
18 #include "dx_data.h"
19 
20 void usage(){
21     std::cout << "Usage: " << std::endl;
22     std::cout << "   dwg2text <input>" << std::endl << std::endl;
23     std::cout << "   input      dwg or dxf file to extract text" << std::endl;
24 }
25 
26 bool extractText(std::string inName){
27     bool badState = false;
28     //verify if input file exist
29     std::ifstream ifs;
30     ifs.open (inName.c_str(), std::ifstream::in);
31     badState = ifs.fail();
32     ifs.close();
33     if (badState) {
34         std::cout << "Error can't open " << inName << std::endl;
35         return false;
36     }
37 
38     dx_data fData;  // 存儲cad數據的容器類對象 39     dx_iface *input = new dx_iface();
40     badState = input->printText( inName, &fData ); // 讀取cad文件並填充到cad數據容器類對象中,再找出text實體打印出來 41     if (!badState) {
42         std::cout << "Error reading file " << inName << std::endl;
43         return false;
44     }
45     delete input;
46 
47     return badState;
48 }
49 
50 int main(int argc, char *argv[]) {
51     bool badState = false;
52     std::string outName;
53     if (argc != 2) {
54         usage();
55         return 1;
56     }
57 
58     std::string fileName = argv[1];
59 
60     if (badState) {
61         std::cout << "Bad options." << std::endl;
62         usage();
63         return 1;
64     }
65 
66     bool ok = extractText(fileName);
67     if (ok)
68         return 0;
69     else
70         return 1;
71 }

2、dwg2dxf:將dwg轉換為dxf(read/write)

 1 bool convertFile(std::string inName, std::string outName, DRW::Version ver, bool binary, bool overwrite){
 2     bool badState = false;
 3     //verify if input file exist
 4     std::ifstream ifs;
 5     ifs.open (inName.c_str(), std::ifstream::in);
 6     badState = ifs.fail();
 7     ifs.close();
 8     if (badState) {
 9         std::cout << "Error can't open " << inName << std::endl;
10         return false;
11     }
12     //verify if output file exist
13     std::ifstream ofs;
14     ofs.open (outName.c_str(), std::ifstream::in);
15     badState = ofs.fail();
16     ofs.close();
17     if (!badState) {
18         if (!overwrite){
19             std::cout << "File " << outName << " already exist, overwrite Y/N ?" << std::endl;
20             int c = getchar();
21             if (c == 'Y' || c=='y')
22                 ;
23             else {
24                 std::cout << "Cancelled.";
25                 return false;
26             }
27         }
28     }
29     //All ok proceed whit conversion
30     //class to store file read:
31     dx_data fData;
32     //First read a dwg or dxf file
33     dx_iface *input = new dx_iface();
34     badState = input->fileImport( inName, &fData );
35     if (!badState) {
36         std::cout << "Error reading file " << inName << std::endl;
37         return false;
38     }
39 
40     //And write a dxf file
41     dx_iface *output = new dx_iface();
42     badState = output->fileExport(outName, ver, binary, &fData);
43     delete input;
44     delete output;
45 
46     return badState;
47 }

在編譯example工程的時候,出現了鏈接錯誤:

參考 https://blog.csdn.net/qq_38940896/article/details/88181435
在編譯lindxfrw.lib的時候需要鏈接一下libiconv.lib,該lib文件在libdxfrw-master\vs2013\packages目錄下找到,分為dynamic和satic,我這里分別取名:libiconv_dynamic.lib和libiconv_static.lib,然后重新設置libdxfrw的工程配置:我這里以win32 Debug配置為例

構建成功后,重新在example工程中鏈接libdxfrw.lib,如果不出意外,此時就可以構建成功了。
運行的時候需要把libdxfrw-master\vs2013\packages中的libiconv.dll拷貝過去。
Good Luck!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM