integrating dwg with open scene graph
integrating dwg with open scene graph
hi there !
i'm completely new to dwg format, but i've been working a lot with the open scene graph library (basically a scene graph to display 3d worlds with opengl).
i'm trying to build a "dwg" plugin to access this kind of data directly in my software... (awfully intricate task for me :-( ).
i'm successfully "importing" some dwg files using a vectorization process... but as i have very little knowledge on the dwg database handling i'm facing strange behaviors :
in fact, i have multiple problems:
1) when i load a file and vectorize the entities, some of them appear correctly, but others are just missing : i have some "acdbblockreference", from which i retrieve a "acdbblocktablerecord" containing no entities at all... is this possible/normal ?
do i have to do something to access all the entities in the database (even if they are invisible/desactivated/hidden/ etc... (?)) ? could this be linked to the layer handling system in dwg files ? (i don't know anything about those layers lol).
2) for some entities (acdb3dsolid), i'm building a 3d objects using the results from a "entity->worlddraw(wd)" call (where "wd" is my "odgiworlddraw" derived class): i end in a call of the shell() function of my "odgiworldgeometry" derived class :
the code is :
void odgiworldgeometrydumper::shell(odint32 numvertices,
const odgepoint3d* vertexlist,
odint32 facelistsize,
const odint32* facelist,
const odgiedgedata* pedgedata,
const odgifacedata* pfacedata,
const odgivertexdata* pvertexdata)
{
osg::geode* geode = new osg::geode;
osg::geometry* geom = new osg::geometry;
osg::vec3array* vertices = new osg::vec3array();
osg::vec3array* normals = new osg::vec3array();
osg::vec4array* colors = new osg::vec4array();
geom->setvertexarray(vertices);
for(int f=0; f< facelistsize {
// pour chaque face on ajoute les vertices, et les normales:
int numverts = facelist[f++];
for(int v =0; v <abs(numverts); ++v) {
int id = facelist[f++];
vertices->push_back(converttovec3d(vertexlist[id]));
if(pfacedata && pfacedata->normals()) {
osg::vec3d n = converttovec3d(pfacedata->normals()[id]);
normals->push_back(forcevalidnormal(n));
}
if(pvertexdata && pvertexdata->normals()) {
osg::vec3d n = converttovec3d(pvertexdata->normals()[id]);
normals->push_back(forcevalidnormal(n));
}
if(pvertexdata && pvertexdata->truecolors()) {
colors->push_back(converttovec4d(pvertexdata->truecolors()[id]));
}
}
}
geom->setnormalarray(normals);
geom->setnormalbinding(osg::geometry::bind_per_vertex );
if(normals->empty()) {
normals->push_back(osg::vec3d(0,0,1));
geom->setnormalbinding(osg::geometry::bind_overall);
osg::notify(osg::notice) << "didn't find vertices normals in shell vectorization.\n";
}
if(!colors->empty()) {
geom->setcolorarray(colors);
geom->setcolorbinding(osg::geometry::bind_per_vertex) ;
osg::notify(osg::notice) << "adding vertices colors in shell vectorization.\n";
}
geom->addprimitiveset(new osg:rawarrays(osg:

rimitiveset::triangles,0,ver tices->getnumelements()));
geode->adddrawable(geom);
dwgconverter::instance()->setcurrentnode(geode);
}
in this code i try to read the normals from the pvertexdata or pfacedata... : it seems to me that sometimes one is present when the other is not (how is this possible ?!)... and may i consider that when both are valid, the same normals values are obtained ?
any way, the major problem here is that i have some invalid normals for some calls to this function !!! for example i found things like:
found invalid normal: (6.2654e-294, -1.#qnan, 2.41908e-312)
found invalid normal: (-1.#qnan, -1.#qnan, 4.24399e-314)
found invalid normal: (6.2654e-294, -1.#qnan, 2.41908e-312)
found invalid normal: (-1.37524, 4.69823e-294, -1.#qnan)
found invalid normal: (-1.37524, 4.69823e-294, -1.#qnan)
found invalid normal: (0, 0, 0)
found invalid normal: (4.69856e-294, -1.#qnan, 1.6976e-312)
found invalid normal: (0, 0, 0)
found invalid normal: (6.06878e-294, -1.#qnan, 6.59516e-311)
how is this possible ?? apparently those normals are not initialized or even set to (0,0,0)... snif...
3) sometimes if i simply change a small output text, then i don't have the same import results ! am i facing a threding issue here ?
my conversion process is simple, but maybe i'm doing something wrong :
here is the whole code i use:
a- main entry :
oddbdatabaseptr pdb = svcs.readfile(file.c_str(), true, false, oda::ksharedenyno);
if (!pdb.isnull())
{
osg::notify(osg::info) << "database loaded.\n";
osg::notify(osg::info) << "\nfile version: " << pdb->originalfileversion() << "\n";
dwgconverter* dwg = dwgconverter::instance(pdb);
osg::ref_ptr<osg::group> root = dwg->getparent();
oddbblocktableptr ptable = pdb->getblocktableid().safeopenobject();
osg::notify(osg::info) << "blocktable description: " << ptable->desc()->name() << "\n";
oddbsymboltableiteratorptr pblkiter = ptable->newiterator();
for (pblkiter->start(); ! pblkiter->done(); pblkiter->step())
{
oddbblocktablerecordptr pblock = pblkiter->getrecordid().safeopenobject();
dwg->convert(pblock);
}
if(root->getnumchildren())
return root.release();
}
}
catch(oderror& e)
{
osg::notify(osg::fatal) << "\ndwgdirect error: " << svcs.geterrordescription(e.code()) << "\n";
}
catch (...)
{
osg::notify(osg::fatal) << "\nunknown error.\n";
}
b - the dwgconverter constructor:
dwgconverter:wgconverter(oddbdatabase* db)
: ctx(db) // ctx is a "odgicontextdumper" object (found in the odreadex sample)
, wd(0) // wd is a "odgiworlddrawdumper" object
{
wd.setcontext(&ctx);
parent = new osg::group;
}
c - the dwgconverter::convert(oddbblocktablerecordptr&) function:
void dwgconverter::convert(oddbblocktablerecordptr& pblock)
{
odprintconsolestring(dd_t("blockrecord description: %s\n"),pblock->desc()->name().c_str());
oddbobjectiteratorptr pentiter = pblock->newiterator();
int count = 0;
int good = 0;
osg::node* node;
for (; !pentiter->done(); pentiter->step())
{
oddbentityptr pent = pentiter->objectid().safeopenobject();
odprintconsolestring(dd_t("starting convertion of %s\n"),pent->isa()->name().c_str());
if((node = convert(pent)) || pent->isa()->name() == "acdbattributedefinition") {
if(node)
parent->addchild(node);
good++;
odprintconsolestring(dd_t("success for %s!\n"),pent->isa()->name().c_str());
}
else {
odprintconsolestring(dd_t("failure for %s!\n"),pent->isa()->name().c_str());
}
odprintconsolestring(dd_t("convertion of %s done.\n"),pent->isa()->name().c_str());
count++;
}
odprintconsolestring(dd_t("blocktablerecord done (%d on %d items done)\n"),good,count);
}
and finally:
d - the dwgconverter::convert(oddbentityptr&) function:
inline osg::node* convert(oddbentityptr& pent)
{
currentnode = null;
pent->worlddraw(&wd);
return currentnode.get();
}
... you see, its really not that complicated, and so i really don't see where those errors may come from... please someone help me !! :-(
regards,
emmanuel.
1. blocktablerecord can be empty but the reasons why entities are not vectorized may be different:
- odvectorizeex sample uses current view of active layout. other layouts are not vectorized. entities are clipped by view borders. entities outside of current view are not vectorized.
- blockreference can have spatial filter (clip applied to the block reference entity).
- enities on "off" layers are not vectorized.
- entities on layers "frozen" in current viewport are not vectorized.
2. normals can be specified per vertex (one normal per each vertex) or per face (one normal per each face). surely they are not the same and even their number is different (number of faces or number of normals).
face data contains number of elements equal to number of faces.
vertex data contains number of elements equal to number of vertices.
you simply access memory ouside of valid data using vertex index to access face data. (cube has 8 vertices and 6 faces).
btw, maybe looking into implementation of shell() primitive in our opengl or directx devices (availble with sources) would be helpful.
sergey slezkin
hi again !
thanks for this quite answer :-)
concerning the point 1)
- if a blocktablerecord has some "frozen" or "off" entities, will they still be found when i use an interator ? ("block->newiterator()")
- in fact i'm not using the vectorization framework as in the odvectorizeex sample : as i'm using the worlddraw() function directly, correct me if i'm wrong but i should have a viewport independant vectorization result, no ?
concerning the point 2): thanks !! it's probably an out of bound access indead :-).
i'm giving a look at the opengl device sample, thanks for the advise !
manu.
it's me again:
sorry but i can't find the definition i would need for the shell() function sample in opengl device :-( i have :
void odgsopenglvectorizeview::shell(odint32 nvertices,
const odgepoint3d* vertexlist,
odint32 facelistsize,
const odint32* facelist,
const odgiedgedata* edgedata,
const odgifacedata* facedata,
const odgivertexdata* vertexdata)
{
bool bbackfacesdiscarded = false;
if(device()->discardbackfaces() && mode() >= kflatshaded &&
((facedata && facedata->normals()) || (vertexdata && vertexdata->normals())) )
{
// ::gllightmodeli(gl_light_model_two_side, gl_false);
::glenable(gl_cull_face);
::glcullface(gl_back);
bbackfacesdiscarded = true;
}
odgsbasevectorizeview::shell(nvertices, vertexlist, facelistsize, facelist, edgedata, facedata, vertexdata);
if(bbackfacesdiscarded)
{
// ::gllightmodeli(gl_light_model_two_side, gl_true);
::gldisable(gl_cull_face);
}
}
but i don't have the source for :
odgsbasevectorizeview::shell(nvertices, vertexlist, facelistsize, facelist, edgedata, facedata, vertexdata);
i guess this means i don't access to the sources you're speaking about...
manu.