4

I'm working on a project where I will need to import line data from a .dwg file in C++ and am struggling to know where to start. I've had a look at this http://opendesign.com/files/guestdownloads/OpenDesign_Specification_for_.dwg_files.pdf, and I think it is possibly too hardcore for me unless anyone knows of a way of describing, simply, in code the strategy for decoding it? For instance, presumably every single operation would have to be bit wise?

Other than that, I might have to rely on some third-party libraries, but the question is: Are there any such (open source) libraries which are licensed with a permissive license? I can't use copy left code in this project.

To clarify in response to comments, I'm looking for permissive licensed libraries (see http://en.m.wikipedia.org/wiki/Permissive_free_software_licence). This includes for example MIT and BSD licenses, but not GPL (LGPL would work perhaps, but only if there are exceptions for static linking). And of course public domain would work too. GPL is strongly copyleft, meaning even if you don't alter it, but link to it with separate original code that code has to also be licensed under GPL.

4

2 回答 2

2

为什么要重新发明轮子?有很多可用的 DWG 库。尝试LibDWG。它在GNU GPL(即开源)下获得许可。还有基于 LibDWG 的 LibreDWG,但可直接从 GNU 项目网站获得在 github 上有一个使用 LibreDWG 的示例,它打开一个 DWG 文件,并将其转换为 SVG

阅读文件似乎很简单:

int error;
Dwg_Data dwg;

error = dwg_read_file(filename, &dwg);

if (!error)
{
    model_xmin = dwg_model_x_min(&dwg);
    model_ymin = dwg_model_y_min(&dwg);
    double dx = (dwg_model_x_max(&dwg) - dwg_model_x_min(&dwg));
    double dy = (dwg_model_y_max(&dwg) - dwg_model_y_min(&dwg));
    double scale_x = dx / (dwg_page_x_max(&dwg) - dwg_page_x_min(&dwg));
    double scale_y = dy / (dwg_page_y_max(&dwg) - dwg_page_y_min(&dwg));
    //...
}

dwg_free(&dwg);
于 2014-03-23T21:14:00.370 回答
2

我正在开发一个新的开源库(在 MIT 许可下,因此您可以完全免费使用它,不像 libredwg/lidwg/ODA Teigha)来处理 CAD (DWG/DXF) 文件。现在它可以很好地处理 DWG R15 (2000),因此您可以尝试使用它。目前还没有稳定的版本,但是任何测试都会对项目有很大帮助。

链接:libopencad github

安装在 README.md 中有描述,用法很简单:

#include <iostream>
# include "lib/opencad_api.h"

// returns nullptr on fail. GetLastErrorCode() returns an error code.
CADFile *pCADFile = OpenCADFile( pszCADFilePath,
                                      CADFile::OpenOptions::READ_ALL ); 

const CADHeader& header = pCADFile->getHeader ();
header.print (); // prints CAD Header variables.
cout << endl;

const CADClasses& classes = pCADFile->getClasses ();
classes.print (); // prints custom CAD classes
cout << endl;

for ( size_t i = 0; i < pCADFile->getLayersCount (); ++i )
{
    CADLayer &layer = pCADFile->getLayer (i);
    cout << "Layer #" << i << " contains "
         << layer.getGeometryCount () << " geometries" << endl;

    for ( size_t j = 0; j < layer.getGeometryCount (); ++j )
    {
        unique_ptr<CADGeometry> geom(layer.getGeometry (j));

        if ( geom == nullptr )
            continue;

        switch ( geom->getType() ) // returns GeometryType enum.
        {
            case CADGeometry::CIRCLE:
                CADCircle * poCADCircle = ( CADCircle* ) geom.get();
                std::cout << poCADCircle->getPosition().getX() << std::endl;
                std::cout << poCADCircle->getPosition().getY() << std::endl;
                std::cout << poCADCircle->getPosition().getZ() << std::endl;
                break;
            // any other geometry type you need.
        }
    }
}
于 2016-06-24T20:15:54.700 回答