zoukankan      html  css  js  c++  java
  • 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-mastervs2013Debug或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-mastervs2013packages目录下找到,分为dynamic和satic,我这里分别取名:libiconv_dynamic.lib和libiconv_static.lib,然后重新设置libdxfrw的工程配置:我这里以win32 Debug配置为例

    构建成功后,重新在example工程中链接libdxfrw.lib,如果不出意外,此时就可以构建成功了。
    运行的时候需要把libdxfrw-mastervs2013packages中的libiconv.dll拷贝过去。
    Good Luck!

  • 相关阅读:
    POJ_2184_Cow_Exhibition_(动态规划,背包)
    POJ_2392_Space_Elevator_(动态规划,背包)
    POJ_2914_Minimum_Cut_(Stoer_Wagner)
    POJ_3068_Shortest_pair_of_paths_(最小费用流)
    POJ_3666_Making_the_Grade_(动态规划)
    BZOJ_1565_[NOI2009]_植物大战僵尸_(Tarjan+最大流+最大权闭合图)
    POJ_2987_Firing_(最大流+最大权闭合图)
    BZOJ_1497_[NOI2006]_最大获利_(最大流+最大权闭合图)
    BZOJ_1798_&_Codevs_2216_[AHOI_2009]_行星序列_(线段树)
    Codevs_1690_开关灯_(线段树)
  • 原文地址:https://www.cnblogs.com/MakeView660/p/12326159.html
Copyright © 2011-2022 走看看