How to handle DWG files in C++

孤人 提交于 2019-12-23 12:34:17

问题


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.


回答1:


Why reinvent the wheel? There are plently of DWG libraries available. Try LibDWG. It's licensed under the GNU GPL (i.e. open source). There is also LibreDWG which is based on LibDWG, but available directly from the GNU project website. There is an example of using LibreDWG on github that opens a DWG file, and converts it to an SVG.

Reading the file seems pretty straight forward:

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);



回答2:


I am developing a new opensource library (under MIT license, so you can use it completely for free, unlike libredwg/lidwg/ODA Teigha) to work with CAD (DWG/DXF) files. Now it handles DWG R15 (2000) pretty well, so you can try to use it. There is no stable release yet, but any testing will help a lot for project.

Link:libopencad github

Installation is descibed in README.md Usage is very simple:

#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.
        }
    }
}


来源:https://stackoverflow.com/questions/22597111/how-to-handle-dwg-files-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!