VRML import/reading thru OCC

We are unable to find API for reading VRML files. Neither we get any sample code to read VRML file format.In release nots of opencacade 6.3 they have specified that opencacade allows vrml read/write.
Please help !!
Thanks in advance.

Tilman Leune's picture

From what I know, OCCT can only write VRML-Files, but is unable to re-read them.

If you need to read VRML files, you probably will have to implement your own parser for vrml.

the only remote reference to reading vrml i found was here:
i found some mentioning of VRML in this post
http://www.opencascade.org/org/forum/thread_9793/

Nicholas Seward's picture

The 6.3 technical notes does reference reading VRML but I have not seen evidence of it anywhere. I am waiting for a response from OCC. In the mean time, if you would like VRML capabilities you could try out MeshLAB

Nicholas Seward
The CADDD Project
Open Source Cross Platform 3D CAD Project

Roman Lygin's picture

Quick comment on MeshLAB. It's GPL licensed and so cannot be used in proprietary projects. So pay attention to that.

Pawel's picture

Hi Nikhil,

below some code. As long as I remember, there were some issues with colors or materials. However, I was just testing the possibility to import/export vrml and it was some time ago...

Hope this helps,
Pawel

bool OCCViewer::ImportVrml2(char* filename, OCCDoc * occDoc, int *id, bool visualize)
{
if(occDoc == NULL)
{
TraceDebug("Problem importing file: no valid document pointer.");
return false;
}

Standard_CString aFileName = (Standard_CString) filename;
TraceDebugIndent("Reading VRML 2.0 file...");
TraceDebugFormattedStr("File: ", "%s", ".", aFileName);

VrmlData_Scene vrmlScene;
filebuf fb;
fb.open(filename, ios::in);
if(fb.is_open() == false)
{
TraceWarning("Problem importing file: could not open file buffer.");
TraceDebugUnIndent("Failed to process the file.");
return false;
}

Standard_IStream input(&fb);

vrmlScene << input;
fb.close();

if(vrmlScene.Status() != VrmlData_StatusOK)
{
TraceWarningFormattedStr("Problem importing file: importing file failed. Error: ", "%i", ".", vrmlScene.Status());
TraceDebugUnIndent("Failed to process the file.");
return false;
}
TraceDebug("Finished reading VRML 2.0 file.");

VrmlData_DataMapOfShapeAppearance M;
TopoDS_Shape aTShape = vrmlScene.GetShape(M);

if(aTShape.IsNull() == Standard_True)
{
TraceWarning("Problem importing file: getting TopoDS_Shape failed.");
TraceDebugUnIndent("Failed to process the file.");
return false;
}

TraceDebug("Creating GeoEntity...");
int entityIdentifier = occDoc->AddEntity(aTShape);

*id = entityIdentifier;

occDoc->SetEntityNameWithIdentifierFromPath(entityIdentifier, aFileName);

if(visualize == true)
{
TraceDebug("Displaying...");
occDoc->DisplayEntityWithIdentifier(entityIdentifier, myAISContext);
}
TraceDebugUnIndent("VRML 2.0 file ready.");

return true;
}

bool OCCViewer::ExportVrml2(char* filename)
{
TraceDebugIndent("Writing VRML 2.0 file...");
TraceDebugFormattedStr("File: ", "%s", ".", filename);

VrmlData_Scene vrmlScene;
VrmlData_ShapeConvert shapeConvert(vrmlScene);

TraceDebug("Grouping objects to export...");
int iShape = 0;
for ( myAISContext->InitCurrent(); myAISContext->MoreCurrent(); myAISContext->NextCurrent() )
{
Handle_AIS_InteractiveObject anIO = myAISContext->Current();
Handle_AIS_Shape anIS = Handle_AIS_Shape::DownCast(anIO);

if(anIS.IsNull() == Standard_False)
{
TopoDS_Shape shape = anIS->Shape();
if ( shape.IsNull() )
{
TraceDebugUnIndent("Failed to export VRML 2.0 file. Couldn't access object.");
return false;
}

Quantity_Color color = myAISContext->Current()->Color();
Standard_Real transparency = myAISContext->Current()->Transparency();

// Give a name to the shape.
TCollection_AsciiString name("Shape");
name += TCollection_AsciiString(iShape++);
shapeConvert.AddShape(shape, name.ToCString());

// Check presence of faces in the shape.
TopExp_Explorer expl(shape, TopAbs_FACE);
if (expl.More())
shapeConvert.Convert(true, false, 0.01); // faces only
else
shapeConvert.Convert(false, true, 0.01); // edges only

// Name of the color & transparency.
// It will be uniquely saved in VRML file.
TCollection_AsciiString cname = Quantity_Color::StringName(color.Name());
cname += transparency;

// Make the appearance (VRML attribute)
Handle(VrmlData_Appearance) appearance = Handle(VrmlData_Appearance)::DownCast(vrmlScene.FindNode(cname.ToCString()));
if (appearance.IsNull())
{
// Not found ... create a new one.
Handle(VrmlData_Material) material = new VrmlData_Material(vrmlScene, cname.ToCString(), 0.2, 0.2, transparency);
material->SetDiffuseColor(color);
material->SetEmissiveColor(color);
material->SetSpecularColor(color);
vrmlScene.AddNode(material, false);
appearance = new VrmlData_Appearance(vrmlScene, cname.ToCString());
appearance->SetMaterial(material);
vrmlScene.AddNode(appearance, false);
}

// Apply the material to the shape of entity.
Handle(VrmlData_Group) group = Handle(VrmlData_Group)::DownCast(vrmlScene.FindNode(name.ToCString()));
if (!group.IsNull())
{
VrmlData_ListOfNode::Iterator itr = group->NodeIterator();
for (; itr.More(); itr.Next())
{
Handle(VrmlData_Node) node = itr.Value();
if (node->DynamicType() == STANDARD_TYPE(VrmlData_ShapeNode))
{
Handle(VrmlData_ShapeNode) shape = Handle(VrmlData_ShapeNode)::DownCast(node);
shape->SetAppearance(appearance);
}
else if (itr.Value()->DynamicType() == STANDARD_TYPE(VrmlData_Group))
{
Handle(VrmlData_Group) groupc = Handle(VrmlData_Group)::DownCast(itr.Value());
VrmlData_ListOfNode::Iterator itrc = groupc->NodeIterator();
for (; itrc.More(); itrc.Next())
{
Handle(VrmlData_Node) nodec = itrc.Value();
if (nodec->DynamicType() == STANDARD_TYPE(VrmlData_ShapeNode))
{
Handle(VrmlData_ShapeNode) shapec = Handle(VrmlData_ShapeNode)::DownCast(nodec);
shapec->SetAppearance(appearance);
}
} // for of group nodes...
} // if (it is a shape node...
} // for of group nodes...
} // if (!group.IsNull...
}
else
TraceWarning("Impossible to gain object representation.");
}

// Call VRML writer
Standard_CString aFileName = (Standard_CString) filename;
ofstream writer(aFileName);
if(writer.is_open() == false)
{
TraceWarning("Problem exporting file: could not open file buffer.");
TraceDebugUnIndent("Failed to process the file.");
return false;
}
writer<