OpenCASCADE DataExchange DWG
eryar@163.com
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是CAD軟件AutoCAD及基于AutoCAD的軟件保存設(shè)計(jì)數(shù)據(jù)所用的一種專(zhuān)有文件格式,始于1970年代的一套Interact CAD軟件。之后Autodesk公司于1982年取得版權(quán)開(kāi)始使用這種文件格式。Autodesk公司擁有、開(kāi)發(fā)并且更新DWG文件格式,通常每隔幾年DWG就會(huì)隨著在AutoCAD中添加新的特性而對(duì)DWG格式進(jìn)行更新。
DWG格式及它的ASCII格式變體DXF格式,已經(jīng)成為CAD制圖數(shù)據(jù)交換中的事實(shí)文件標(biāo)準(zhǔn),據(jù)估計(jì)全世界有超過(guò)十億個(gè)DWG文件。有幾家公司正在對(duì)DWG文件格式進(jìn)行逆向工程以試圖為其它的設(shè)計(jì)軟件提供讀寫(xiě)DWG文件的能力。Autodesk公司也提供了一套需要授權(quán)的DWG讀寫(xiě)技術(shù)開(kāi)發(fā)包“RealDWG”。
新版的AutoCAD可以打開(kāi)舊版的DWG文件,AutoCAD2007可以打開(kāi)2.0版本的DWG文件并且可以保存為R14版本。另外Autodesk公司提供一個(gè)免費(fèi)的DWG查看工個(gè)“DWG TrueView”用于查看所有版本的DWG文件。另外Autodesk公司是vendor lock-in策略的強(qiáng)力支持者,盡力保護(hù)DWG文件格式并且禁止開(kāi)發(fā)支持DWG格式的開(kāi)放源代碼庫(kù)。
2006年11月12日,Autodesk公司對(duì)Open Design Alliance-一款支持DWG格式的自由庫(kù)OpenDWG提出了訴訟。
ASCII格式的DXF文件的文檔Autodesk提供了,但是二進(jìn)制的DWG格式并沒(méi)有提供相關(guān)文檔,由上可見(jiàn)對(duì)DWG文件的讀寫(xiě)處理是非常困難的。本文主要介紹如何使用Libredwg庫(kù)來(lái)對(duì)讀取DWG中的幾何數(shù)據(jù),并將幾何數(shù)據(jù)生成為可以在OpenCASCADE中顯示的Tcl腳本,以驗(yàn)證讀取數(shù)據(jù)的正確性。
2.Modify Libredwg for Visual Studio
Libredwg是一個(gè)Free的讀寫(xiě)DWG文件的C庫(kù),這個(gè)程序是GNU項(xiàng)目的一部分,授權(quán)方式是GNU GPL3。
Libredwg是Libdwg的一個(gè)分支,其目的是創(chuàng)建OpenDWG庫(kù)的一個(gè)替代庫(kù)。也是高優(yōu)先級(jí)的Free軟件項(xiàng)目:
http://www.fsf.org/campaigns/priority-projects/priority-projects/highpriorityprojects#ReplaceOpenDWG ,更多信息可訪問(wèn)http://www.gnu.org/software/libredwg 。
從網(wǎng)上下載的Libredwg源程序是在Linux下編譯的,并沒(méi)有配置在Windows下編譯方法。為了使用Libredwg可以在Windows上的Visual Studio中編譯通過(guò),對(duì)Libredwg做了一些修改,最終編譯成功。在Visual Studio 2008上成功編譯的工程可以文后的鏈接中下載。
下面給出使用Libredwg讀取DWG文件中直線、圓及文字的例子程序:
/*
* 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;
}
因?yàn)長(zhǎng)ibredwg用的C編程風(fēng)格,沒(méi)有定義導(dǎo)出定義宏,所以決定將Libredwg編譯成靜態(tài)庫(kù)libredwg.lib,然后使用其頭文件及這個(gè)靜態(tài)庫(kù)的方式來(lái)在程序中使用Libredwg庫(kù)。
經(jīng)過(guò)測(cè)試,若DWG中只有簡(jiǎn)單的線和圓等簡(jiǎn)單實(shí)體,Libredwg還是可以正確讀取出。但是若用rewrite的例子來(lái)測(cè)試寫(xiě)DWG的功能,簡(jiǎn)單的實(shí)例如一個(gè)圓的數(shù)據(jù)都會(huì)寫(xiě)出到DWG失敗,看樣子寫(xiě)的功能還沒(méi)有完全實(shí)現(xiàn)好。
3.DWG to OCC
基于上面的例子程序,結(jié)合Libredwg的讀取功能,將DWG中的幾何數(shù)據(jù)導(dǎo)出成Tcl腳本,這樣就可以方便在OpenCASCADE的Draw Test Harness中來(lái)測(cè)試結(jié)果了。下面給出具體的程序?qū)嵗叭绾卧贒raw Test Harness中來(lái)使用生成的Tcl腳本。
/*
* Copyright (c) 2014 eryar All Rights Reserved.
*
* File : Main.cpp
* Author : eryar@163.com
* 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;
}
將生成的dwg2occ.tcl在OpenCASCADE的Draw Test Harness中顯示如下所示:
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
通過(guò)對(duì)比發(fā)現(xiàn),直線和圓已經(jīng)正確讀出,但是文字沒(méi)有讀出來(lái)。看來(lái)Libredwg的可靠性還有待提高啊。
4.Conclusion
通過(guò)使用Libredwg來(lái)讀取DWG格式中幾何數(shù)據(jù),并將其轉(zhuǎn)換成OpenCASCADE的Draw Test Harness中能執(zhí)行的Tcl腳本,以方便測(cè)試libredwg讀取數(shù)據(jù)的正確性。
通過(guò)簡(jiǎn)單測(cè)試發(fā)現(xiàn),libredwg能讀取直線和圓,但是文字內(nèi)容沒(méi)有正確讀出,看來(lái)libredwg的可靠性還有待提高,但是發(fā)現(xiàn)這個(gè)開(kāi)源庫(kù)的更新很緩慢。
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