Iges and Step to Wrml working code

Hi everybody.
I'm new to Opencascade and I'm using the code to batch convert iges and step to wrml.
I've looked a bit around to find some good code but none of that was good for my need.

Here's the result of a week of work (Googling and Copy Paste most).
It works fine, code is short and wrml is generated with the rigth colors.

I've 2 enhancement needed:

1) Conversion from Iges give sometimes not so good WRML
2) I'm trying to use progression Bar with no success.

thanks in advance,
Bye, Peppe.

#define g_f_f(X) {try{System::IO::File::AppendAllText(LOG_FILE, System::String::Concat(#X, "=-", System::Convert::ToString(X), "-",System::Environment::NewLine));}catch(System::Exception^){}}

System::Void Form1::timer1_Tick2(System::Object^ sender, System::Timers::ElapsedEventArgs^ e)
{

if(aPI)
{
g_f_f(aPI->GetValue())
progressBar1->Value = (int)aPI->GetPosition()*100.0;
}
return ;
}
Void Form1::Converti3()
{
System::Timers::Timer^myTimer = gcnew System::Timers::Timer(10);

myTimer->AutoReset = true;

myTimer->Elapsed += gcnew System::Timers::ElapsedEventHandler(this, &IE2::Form1::timer1_Tick2);

GC::KeepAlive(myTimer);

char
input[3333],
output[3333];

sprintf(input, "%s\0", inputFile);

XSControl_Reader
reader;
Handle(TDocStd_Document) doc;
Handle ( XCAFApp_Application ) anApp = XCAFApp_Application::GetApplication();
anApp->NewDocument("MDTV-XCAF", doc);

if(inputFile->ToLower()->EndsWith(".igs"))
{
IGESCAFControl_Reader igesReader;
igesReader.ReadFile( (Standard_CString) input);
igesReader.SetColorMode(true);
igesReader.SetNameMode(true);
igesReader.SetLayerMode(true);
if ( !igesReader.Transfer( doc ) )
return ;
reader = igesReader;

}
else if(inputFile->ToLower()->EndsWith(".stp"))
{
STEPCAFControl_Reader stepReader;
stepReader.ReadFile( (Standard_CString) input);

aPI = new MyProgressIndicator() ;
stepReader.Reader().WS()->MapReader()->SetProgress(aPI);
aPI->SetScale (0, 1, .001);

stepReader.SetColorMode(true);
stepReader.SetNameMode(true);
stepReader.SetLayerMode(true);

aPI->NewScope(80, "ReadFile");g_f_eve_f("ReadFile")
aPI->Show();
myTimer->Start();

if ( !stepReader.Transfer( doc ) )
{
Close();
return ;
}
reader = stepReader.Reader();
aPI->EndScope();
}
else
{
Close();
return ;
}

Handle(XCAFDoc_ColorTool) l_ColorTool = XCAFDoc_DocumentTool::ColorTool(doc->Main());

TCollection_AsciiString
ReturnMessage;

Standard_Boolean
ReturnValue = Standard_True;
Standard_Integer iShape = 1;
VrmlData_Scene
scene;
VrmlData_ShapeConvert
converter(scene);//, 0.001); // from mm to meters

int
nbs = reader.NbShapes();

List
^Lcolor;
Quantity_Color
color;

Lcolor = gcnew List;

for ( int ii = 1; ii {
// Shape
TopoDS_Shape shape = reader.Shape( ii );

if ( shape.IsNull() )
{
ReturnMessage += " Error : Invalid shape \n";
ReturnValue = Standard_False;
continue;
}

Lcolor->Clear();
// Color
int ColorCount=0;
Quantity_Color color_s=Quantity_Color(Quantity_NOC_ALICEBLUE); // yellow

Handle(AIS_Shape) aisShape = new AIS_Shape(shape);
aisShape->Color(color_s);
// Transparency
Standard_Real transparency = aisShape->Transparency();
delete aisShape;
// Give a name to the shape.
TCollection_AsciiString name("Shape");
name += TCollection_AsciiString(iShape++);
converter.AddShape(shape, name.ToCString());
//ReturnMessage += name;
//ReturnMessage += '\n';

// Check presence of faces in the shape.
TopExp_Explorer expl(shape, TopAbs_FACE);
if (expl.More())
{
converter.Convert(true, false, 100); // faces only
}
else
{
converter.Convert(false, true, 1); // edges only
}
aPI->EndScope();

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

// Make the appearance (VRML attribute)
Handle(VrmlData_Appearance) appearance = Handle(VrmlData_Appearance)::DownCast(scene.FindNode(cname.ToCString()));

if (appearance.IsNull())
{
// Not found ... create a new one.
Handle(VrmlData_Material) material = new VrmlData_Material(scene, cname.ToCString(), 0.2, 0.2, transparency);
material->SetDiffuseColor(color_s);
material->SetEmissiveColor(color_s);
material->SetSpecularColor(color_s);
scene.AddNode(material, false);
appearance = new VrmlData_Appearance(scene, cname.ToCString());
appearance->SetMaterial(material);
scene.AddNode(appearance, false);

delete appearance;
delete material;
}

int tot_count=0, count=0;
for (TopExp_Explorer ex(shape,TopAbs_FACE); ex.More(); ex.Next())
{
tot_count ++;
const TopoDS_Face &face = TopoDS::Face(ex.Current());

if(l_ColorTool->GetColor(ex.Current(), XCAFDoc_ColorSurf, color))
{
Lcolor->Add(Quantity_Color_r(color));
}
else if(l_ColorTool->GetColor(ex.Current(), XCAFDoc_ColorGen, color))
{
Lcolor->Add(Quantity_Color_r(color));
}
else
{
Lcolor->Add(Quantity_Color_r(color_s));
}
}
ColorCount=0;
// Apply the material to the shape of entity.
Handle(VrmlData_Group) group = Handle(VrmlData_Group)::DownCast(scene.FindNode(name.ToCString()));
if (!group.IsNull())
{
VrmlData_ListOfNode::Iterator itr = group->NodeIterator();
for (; itr.More(); itr.Next())
{
count++;
Handle(VrmlData_Node) node = itr.Value();
if(ColorCountCount)
color = *Lcolor[ColorCount++].color;

if (node->DynamicType() == STANDARD_TYPE(VrmlData_ShapeNode))
{
Handle(VrmlData_ShapeNode) shape = Handle(VrmlData_ShapeNode)::DownCast(node);
try
{

SetApp(scene, shape, color, transparency);
}catch(Exception^e){g_f_f(e->Message)}
}
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);
try
{

SetApp(scene, shapec, color, transparency);
}catch(Exception^e){g_f_f(e->Message)}
}
} // for of group nodes...
} // if (it is a shape node...
} // for of group nodes...
} // if (!group.IsNull...
} // iterator of shapes

sprintf(output, "%s\0", outputFile);

ofstream writer(output);
writer writer.close();

Close();
return ;

}

Michael Steitz's picture

Hi Giuseppe,

I'm wondering where SetApp(..) comes from.

try
{
SetApp(scene, shape, color, transparency);
}catch(Exception^e){g_f_f(e->Message)}

I can't find this function anywhere =(

Thanks,
Michael

Michael Steitz's picture

Hello Everybody.

Is there anyone, who could tell me what happens within the SetApp function? I got a workaround by shape->SetAppearance(appearance) but I'm not getting color information with this.

Please anyone help me.

Thanks
Michael