Opencascade dataexchange DWG
[Email protected]
Abstract.DWG is a file format created in the 70's for the emerging CAD applications. currently it is the native file format of AutoCAD, a proprietary CAD program developed by Autodesk. libredwg is a free C library to read and write DWG files. this program is part of GNU project, released under the aegis of GNU. the paper focus on the usage of libredwg, and use the libredwg to read a DWG file and output the entities of the DWG to TCL script for opencascade draw test harness visualization.
Key words.Opencascade, DWG, libredwg, dataexchange, Windows
1. Introduction
DWG is a proprietary file format used for CAD Software AUTOCAD and AutoCAD-based software to save design data. It was a set of Interact CAD software that began in the 1970 s. Later, Autodesk began to use this file format in 1982 after obtaining the copyright. Autodesk owns, develops, and updates the DWG file format. Generally, the DWG format is updated every several years as new features are added to AutoCAD.
The DWG format and its ASCII format variant DXF format have become the de facto file standard for Caddy drawing data exchange. It is estimated that there are more than one billion DWG files worldwide. Several companies are reverse engineering the DWG file format to try to provide other design software with the ability to read and write DWG files. Autodesk also provides a set of DWG read/write Technology Development Kit "realdwg" that requires authorization ".
The new version of AutoCAD can open the old version of DWG, and autocad2007 can open the DWG file of Version 2.0 and save it as R14. In addition, Autodesk provides a free DWG viewer "DWG trueview" to view all versions of DWG files. In addition, Autodesk is a powerful supporter of the vendor lock-in policy and tries its best to protect the DWG file format and prohibit the development of open source code libraries that support the DWG format.
In November 12, 2006, Autodesk filed a lawsuit against open design Alliance-a free library opendwg that supports the DWG format.
Autodesk provides documents for DXF files in ASCII format, but the DWG format in binary format does not provide relevant documents. It is very difficult to read and write the DWG files. This article describes how to use the libredwg library to read the geometric data in DWG and generate the geometric data into a Tcl script that can be displayed in opencascade to verify the correctness of the read data.
2. Modify libredwg for Visual Studio
Libredwg is a free C library for reading and writing DWG files. This program is part of the GNU project and the authorization method is GNU gpl3.
Libredwg is a branch of libdwg. It aims to create an alternative library for the opendwg library. It is also a high-priority free software project:
Http://www.fsf.org/campaigns/priority-projects/priority-projects/highpriorityprojects#ReplaceOpenDWG for more information to access the http://www.gnu.org/software/libredwg.
The libredwg source program downloaded from the internet is compiled in Linux and is not configured in windows. To use libredwg, You can compile libredwg in Visual Studio on Windows and make some modifications to libredwg. Projects successfully compiled on Visual Studio 2008 can be downloaded from the link after the document.
The following is an example program that uses libredwg to read straight lines, circles, and texts in a DWG file:
/* * load_dwg.c: load a DWG, get lines, text and circles * written by Felipe Castro * modified by Felipe Corrêa da Silva Sances * modified by Thien-Thi Nguyen */#include <dwg.h>#include "suffix.c"void add_line(double x1, double y1, double x2, double y2){ // Make something with that}void add_circle(double x, double y, double R){ // Make something with that}void add_text(double x, double y, char *txt){ // Make something with that}int load_dwg(char *filename){ unsigned int i; int success; Dwg_Data dwg; dwg.num_objects = 0; success = dwg_read_file(filename, &dwg); for (i = 0; i < dwg.num_objects; i++) { Dwg_Entity_LINE *line; Dwg_Entity_CIRCLE *circle; Dwg_Entity_TEXT *text; switch (dwg.object[i].type) { case DWG_TYPE_LINE: line = dwg.object[i].tio.entity->tio.LINE; add_line(line->start.x, line->end.x, line->start.y, line->end.y); break; case DWG_TYPE_CIRCLE: circle = dwg.object[i].tio.entity->tio.CIRCLE; add_circle(circle->center.x, circle->center.y, circle->radius); break; case DWG_TYPE_TEXT: text = dwg.object[i].tio.entity->tio.TEXT; add_text(text->insertion_pt.x, text->insertion_pt.y, text->text_value); break; } } dwg_free(&dwg); return success;}int main (int argc, char *argv[]){ REQUIRE_INPUT_FILE_ARG (argc); load_dwg (argv[1]); return 0;}
Because libredwg uses the C programming style and does not define the export definition macro, it is decided to compile libredwg into a static library libredwg. lib, and then use the header file and the static library to use the libredwg library in the program.
After testing, libredwg can still read and retrieve simple objects such as simple lines and circles in DWG. However, if you use the rewrite example to test the DWG writing function, a simple instance, such as a circle, will write the data to DWG and fail. It seems that the writing function has not been fully implemented yet.
3. DWG to OCC
Based on the above example program and the libredwg reading function, export the geometric data in DWG to a Tcl script, so that you can conveniently test the results in the draw test harness of opencascade. The following describes the specific program instance and how to use the generated TCL script in draw test harness.
/** Copyright (c) 2014 eryar All Rights Reserved.** File : Main.cpp* Author : [email protected]* Date : 2014-10-15 20:46* Version : 1.0v** Description : Use libredwg to read data from DWG and* output them to Tcl script for Draw.** Key words : OpenCASCADE, libredwg, Draw Test Harness*/#include "dwg.h"#include <fstream>#include <iostream>#pragma comment(lib, "../Debug/libredwg.lib")// Output the entities to Tcl for OpenCASCADE Draw.static std::ofstream theTclExporter("d:/dwg2occ.tcl");void OutputLine(int id, const Dwg_Entity_LINE* theLine){ // Draw Tcl command: vline name xa ya za xb yb zb theTclExporter << "vline line" << id << " " << theLine->start.x << " " << theLine->start.y << " " << theLine->start.z << " " << theLine->end.x << " " << theLine->end.y << " " << theLine->end.z << std::endl;}void OutputCircle(int id, const Dwg_Entity_CIRCLE* theCircle){ // Draw Tcl command: circle name x y [z [dx dy dz]] [ux uy [uz]] radius // 1. make a curve theTclExporter << "circle circle" << id << " " << theCircle->center.x << " " << theCircle->center.y << " " << theCircle->center.z << " " << "0 0 1 " << theCircle->radius << std::endl; // 2. make edge from the circle theTclExporter << "mkedge e" << id << " " << "circle" << id << std::endl; // 3. display the circle edge theTclExporter << "vdisplay e" << id << std::endl;}void OutputText(int id, const Dwg_Entity_TEXT* theText){ // vdrawtext : vdrawtext : vdrawtext name X Y Z R G B hor_align ver_align angle zoomable height Aspect [Font [isMultiByte]] theTclExporter << "vdrawtext " << theText->text_value << " " << theText->insertion_pt.x << " " << theText->insertion_pt.y << " 0" << " 255 255 000 " << theText->horiz_alignment << " " << theText->vert_alignment << " " << theText->height << " 1 Times-Roman"<< std::endl;}int LoadDwg(char* theDwgFile){ int aResult = 0; Dwg_Data aDwgData; aResult = dwg_read_file(theDwgFile, &aDwgData); for (unsigned int i = 0; i < aDwgData.num_objects; ++i) { switch (aDwgData.object[i].type) { case DWG_TYPE_LINE: OutputLine(i, aDwgData.object[i].tio.entity->tio.LINE); break; case DWG_TYPE_CIRCLE: OutputCircle(i, aDwgData.object[i].tio.entity->tio.CIRCLE); break; case DWG_TYPE_TEXT: OutputText(i, aDwgData.object[i].tio.entity->tio.TEXT); break; } } return aResult;}int main(int argc, char* argv[]){ theTclExporter.flags(std::ios::fixed); theTclExporter << "pload ALL" << std::endl; theTclExporter << "vinit" << std::endl; theTclExporter << "vtrihedron tr" << std::endl; if (argc < 1) { std::cout << "please input the dwg file name!" << std::endl; } else { LoadDwg(argv[1]); } theTclExporter << "vdisplayall" << std::endl; theTclExporter << "vfit" << std::endl; theTclExporter << "vhelp" << std::endl; return 0;}
The generated dwg2occ. TCL is shown as follows in the draw test harness of opencascade:
Figure 3.1 import the Tcl script in the draw test hanress of opencascade
Figure 3.2 enitites In the DWG File
Figure 3.3 entities in the draw test harness of opencascade
Through comparison, we found that the straight lines and circles have been correctly read, but the text has not been read. It seems that the reliability of libredwg remains to be improved.
4. Conclusion
You can use libredwg to read the geometric data in DWG format and convert it to the Tcl script that can be executed in draw test harness of opencascade to test the correctness of libredwg's Data Reading.
Through a simple test, it is found that libredwg can read straight lines and circles, but the text content is not correctly read. It seems that the reliability of libredwg remains to be improved, but it is found that the update of this open source library is very slow.
5. References
1. DWG wiki: http://en.wikipedia.org/wiki/.dwg
2. libredwg: http://www.gnu.org/software/libredwg
3. opencascade test harness User's Guide 6.7.1
Libredwg for Visual Studio: opencascade dataexchange DWG
Opencascade dataexchange DWG