
Tue, 02/09/2016 - 13:32
Hi all,
I finally managed to load a STEP file (with colors and labels) and export it as a VRML file using this code below. It works fine with most of the models I tested it, but there are a few models that seem to be composed of various parts that don't work. What happens is, that all parts seem to stick at the origin and are not placed correctly. What is wrong with my code?
I have attached two screenshots. One showing the file in FreeWRL (which renders and displayes VRML files) and the file opened in FreeCAD. When I export the STEP file from FreeCAD it's displayed correctly in FreeWRL. I also attached the STEP file. It happens with a lot of STEP files. I think the position is not transfered from the STEP file. I actually just want to convert from STEP to VRML.
Handle(TopTools_HSequenceOfShape) aHSequenceOfShape= new TopTools_HSequenceOfShape();
aHSequenceOfShape->Clear();
//Create Document
Handle(TDocStd_Document) aDoc;
Handle(XCAFApp_Application) anApp = XCAFApp_Application::GetApplication();
anApp->NewDocument("MDTV-XCAF",aDoc);
// create additional log file
STEPCAFControl_Reader aReader;
aReader.SetColorMode(true);
aReader.SetNameMode(true);
aReader.SetLayerMode(true);
IFSelect_ReturnStatus status = aReader.ReadFile([stepFilePath cStringUsingEncoding:NSASCIIStringEncoding]);
if (status != IFSelect_RetDone)
return NO;
if (!aReader.Transfer(aDoc))
{
NSLog(@"STEP File could not be transfered");
return NO;
}
Handle(XCAFDoc_ColorTool) myColors = XCAFDoc_DocumentTool::ColorTool(aDoc->Main ());
Handle (XCAFDoc_ShapeTool) myAssembly = XCAFDoc_DocumentTool::ShapeTool (aDoc->Main());
TDF_LabelSequence frshapes;
myAssembly->GetShapes(frshapes);
// set presentations and show
for (Standard_Integer i=1; i <= frshapes.Length(); i++ ) {
// get the shapes and attributes
const TDF_Label& label = frshapes.Value(i);
Handle(TDataStd_Name) name;
if (label.FindAttribute(TDataStd_Name::GetID(),name))
{
TCollection_ExtendedString extstr = name->Get();
cout << extstr;
}
TopoDS_Shape shape;
myAssembly->GetShape(label, shape);
BRepMesh_IncrementalMesh(shape, 0.1, Standard_True);
aHSequenceOfShape->Append(shape);
}
//Export VRML
Standard_Boolean ReturnValue = Standard_True;
if (aHSequenceOfShape->Length() == 0)
{
return NO;
}
// VRML scene.
VrmlData_Scene scene;
AFVrmlData_ShapeConvert converter(scene, myColors/*, 0.001*/); // from mm to meters
Standard_Integer iShape = 1; // Counter of shapes
for ( int i = 1; i <= aHSequenceOfShape->Length(); i++ )
{
// Shape
TopoDS_Shape shape = aHSequenceOfShape->Value( i );
if ( shape.IsNull() )
{
continue;
}
// Give a name to the shape.
TCollection_AsciiString name("Shape");
name += TCollection_AsciiString(iShape++);
converter.AddShape(shape, name.ToCString());
} // iterator of shapes
converter.Convert(true, false, 0.1); // faces only
// Call VRML writer
ofstream writer([vrmlFilePath cStringUsingEncoding:NSASCIIStringEncoding]);
writer<<scene;
writer.close();
AFVrmlData_ShapeConvert is a class that sets VRML materials based on shape color but the issue stays the same when using the "stock" VrmlData_ShapeConvert class. Thank you very much for your help!
Phillip
Wed, 02/10/2016 - 11:59
Hi all,
I solved it myself. You have to replace this:
myAssembly->GetShapes(frshapes);
with this:
myAssembly->GetFreeShapes(frshapes);
Looks like GetShapes returns all shapes (referenced building blocks) while GetFreeShapes only exports the "final representation" of the Assembly.
Wed, 02/10/2016 - 13:36
Hi Phillip,
I tried your code. It is building successfully but not displaying the model.
Wed, 02/10/2016 - 17:12
What do you mean with "display"? My code only loads a STEP model and generates a VRML file.
Thu, 02/11/2016 - 07:32
Actually I am using opencascade with qt.
I am able to read and display a complete assembly (STEP) using stepcontrol reader.
but when I try to read same assembly (STEP) with names of components, colors etc. using stepcafcontrol reader, I am unable to do so.
So I was trying to load your step file and display in viewer.