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,
Commenter-4
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<