1 // Copyright (c) 1999-2014 OPEN CASCADE SAS
3 // This file is part of Open CASCADE Technology software library.
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
15 #include <BRep_Tool.hxx>
16 #include <OSD_OpenFile.hxx>
17 #include <OSD_Path.hxx>
18 #include <Poly_Triangulation.hxx>
19 #include <Quantity_HArray1OfColor.hxx>
20 #include <Standard_Stream.hxx>
21 #include <TColStd_HArray1OfReal.hxx>
22 #include <TopAbs_ShapeEnum.hxx>
23 #include <TopExp_Explorer.hxx>
25 #include <TopoDS_Face.hxx>
26 #include <TopoDS_Shape.hxx>
27 #include <TopTools_Array1OfShape.hxx>
29 #include <Vrml_Group.hxx>
30 #include <Vrml_Instancing.hxx>
31 #include <Vrml_Material.hxx>
32 #include <Vrml_Separator.hxx>
33 #include <VrmlAPI_Writer.hxx>
34 #include <VrmlConverter_Drawer.hxx>
35 #include <VrmlConverter_IsoAspect.hxx>
36 #include <VrmlConverter_LineAspect.hxx>
37 #include <VrmlConverter_PointAspect.hxx>
38 #include <VrmlConverter_Projector.hxx>
39 #include <VrmlConverter_ShadedShape.hxx>
40 #include <VrmlConverter_ShadingAspect.hxx>
41 #include <VrmlConverter_WFDeflectionShape.hxx>
42 #include <VrmlData_Scene.hxx>
43 #include <VrmlData_ShapeConvert.hxx>
45 VrmlAPI_Writer::VrmlAPI_Writer()
47 myDrawer = new VrmlConverter_Drawer;
50 color.SetValues(0, 0, 0, Quantity_TOC_RGB);
51 Handle(Quantity_HArray1OfColor) Col1 = new Quantity_HArray1OfColor(1,1);
52 Col1->SetValue(1,color);
53 Handle(TColStd_HArray1OfReal) kik1 = new TColStd_HArray1OfReal(1,1,0.0);
54 Handle(TColStd_HArray1OfReal) kik2 = new TColStd_HArray1OfReal(1,1,0.1);
55 myFrontMaterial = new Vrml_Material(Col1,Col1, Col1, Col1, kik1, kik2);
56 myPointsMaterial = new Vrml_Material(Col1,Col1, Col1, Col1, kik1, kik2);
57 myUisoMaterial = new Vrml_Material(Col1,Col1, Col1, Col1, kik1, kik2);
58 myVisoMaterial = new Vrml_Material(Col1,Col1, Col1, Col1, kik1, kik2);
59 myLineMaterial = new Vrml_Material(Col1,Col1, Col1, Col1, kik1, kik2);
60 myWireMaterial = new Vrml_Material(Col1,Col1, Col1, Col1, kik1, kik2);
61 myFreeBoundsMaterial = new Vrml_Material(Col1,Col1, Col1, Col1, kik1, kik2);
62 myUnfreeBoundsMaterial = new Vrml_Material(Col1,Col1, Col1, Col1, kik1, kik2);
73 void VrmlAPI_Writer::ResetToDefaults()
77 Handle(TColStd_HArray1OfReal) kik1 = new TColStd_HArray1OfReal(1,1,myTransparency);
78 Handle(TColStd_HArray1OfReal) kik2 = new TColStd_HArray1OfReal(1,1,myShininess);
79 Handle(Quantity_HArray1OfColor) Col = new Quantity_HArray1OfColor(1,1);
81 color.SetValues(0, 0, 0, Quantity_TOC_RGB);
82 Col->SetValue(1,color);
84 myFrontMaterial->SetAmbientColor(Col); myFrontMaterial->SetTransparency(kik1);myFrontMaterial->SetShininess(kik2);
85 myPointsMaterial->SetAmbientColor(Col); myPointsMaterial->SetTransparency(kik1);myPointsMaterial->SetShininess(kik2);
86 myUisoMaterial->SetAmbientColor(Col); myUisoMaterial->SetTransparency(kik1);myUisoMaterial->SetShininess(kik2);
87 myVisoMaterial->SetAmbientColor(Col); myVisoMaterial->SetTransparency(kik1);myVisoMaterial->SetShininess(kik2);
88 myLineMaterial->SetAmbientColor(Col); myLineMaterial->SetTransparency(kik1);myLineMaterial->SetShininess(kik2);
89 myWireMaterial->SetAmbientColor(Col); myWireMaterial->SetTransparency(kik1); myWireMaterial->SetShininess(kik2);
90 myFreeBoundsMaterial->SetAmbientColor(Col); myFreeBoundsMaterial->SetTransparency(kik1);myFreeBoundsMaterial->SetShininess(kik2);
91 myUnfreeBoundsMaterial->SetAmbientColor(Col); myUnfreeBoundsMaterial->SetTransparency(kik1);myUnfreeBoundsMaterial->SetShininess(kik2);
94 Handle(Quantity_HArray1OfColor) Col2 = new Quantity_HArray1OfColor(1,1);
95 color.SetValues(0.75, 0.75, 0.75, Quantity_TOC_RGB);
96 Col2->SetValue(1,color);
97 Handle(Quantity_HArray1OfColor) Col3 = new Quantity_HArray1OfColor(1,1);
98 color.SetValues(0.82, 0.79, 0.42, Quantity_TOC_RGB);
99 Col3->SetValue(1,color);
101 myUisoMaterial->SetDiffuseColor(Col2);
102 myVisoMaterial->SetDiffuseColor(Col2);
103 myFreeBoundsMaterial->SetDiffuseColor(Col2);
104 myUnfreeBoundsMaterial->SetDiffuseColor(Col2);
107 // Handle(Quantity_HArray1OfColor) Col3 = new Quantity_HArray1OfColor(1,1);
108 // color.SetValues(Quantity_NOC_GOLD);
109 // Col3->SetValue(1,color);
110 myLineMaterial->SetDiffuseColor(Col2);
111 myWireMaterial->SetDiffuseColor(Col2);
113 // Handle(Quantity_HArray1OfColor) Col4 = new Quantity_HArray1OfColor(1,1);
114 // color.SetValues(Quantity_NOC_GOLD);
115 // Col4->SetValue(1,color);
116 myFrontMaterial->SetDiffuseColor(Col2);
117 myPointsMaterial->SetDiffuseColor(Col2);
120 myUisoMaterial->SetSpecularColor(Col3);
121 myVisoMaterial->SetSpecularColor(Col3);
122 myFreeBoundsMaterial->SetSpecularColor(Col3);
123 myUnfreeBoundsMaterial->SetSpecularColor(Col3);
124 myLineMaterial->SetSpecularColor(Col3);
125 myWireMaterial->SetSpecularColor(Col3);
126 myFrontMaterial->SetSpecularColor(Col3);
127 myPointsMaterial->SetSpecularColor(Col3);
129 myRepresentation = VrmlAPI_BothRepresentation;
131 Handle(VrmlConverter_Drawer) VrmlAPI_Writer::Drawer() const
135 void VrmlAPI_Writer::SetDeflection(const Standard_Real aDef)
138 if (myDeflection > 0) {
139 myDrawer->SetMaximalChordialDeviation(myDeflection);
140 myDrawer->SetTypeOfDeflection(Aspect_TOD_ABSOLUTE);
142 else myDrawer->SetTypeOfDeflection(Aspect_TOD_RELATIVE);
144 void VrmlAPI_Writer::SetRepresentation(const VrmlAPI_RepresentationOfShape aRep)
146 myRepresentation = aRep;
148 void VrmlAPI_Writer::SetTransparencyToMaterial(Handle(Vrml_Material)& aMaterial,const Standard_Real aTransparency)
150 Handle(TColStd_HArray1OfReal) t = new TColStd_HArray1OfReal(1,1,aTransparency);
151 aMaterial->SetTransparency(t);
154 void VrmlAPI_Writer::SetShininessToMaterial(Handle(Vrml_Material)& aMaterial,const Standard_Real aShininess)
156 Handle(TColStd_HArray1OfReal) s = new TColStd_HArray1OfReal(1,1,aShininess);
157 aMaterial->SetShininess(s);
160 void VrmlAPI_Writer::SetAmbientColorToMaterial(Handle(Vrml_Material)& aMaterial,const Handle(Quantity_HArray1OfColor)& Color)
162 aMaterial->SetAmbientColor(Color);
165 void VrmlAPI_Writer::SetDiffuseColorToMaterial(Handle(Vrml_Material)& aMaterial,const Handle(Quantity_HArray1OfColor)& Color)
167 aMaterial->SetDiffuseColor(Color);
170 void VrmlAPI_Writer::SetSpecularColorToMaterial(Handle(Vrml_Material)& aMaterial,const Handle(Quantity_HArray1OfColor)& Color)
172 aMaterial->SetSpecularColor(Color);
175 void VrmlAPI_Writer::SetEmissiveColorToMaterial(Handle(Vrml_Material)& aMaterial,const Handle(Quantity_HArray1OfColor)& Color)
177 aMaterial->SetEmissiveColor(Color);
180 VrmlAPI_RepresentationOfShape VrmlAPI_Writer::GetRepresentation() const
182 return myRepresentation;
185 Handle(Vrml_Material) VrmlAPI_Writer::GetFrontMaterial() const
187 return myFrontMaterial;
190 Handle(Vrml_Material) VrmlAPI_Writer::GetPointsMaterial() const
192 return myPointsMaterial;
195 Handle(Vrml_Material) VrmlAPI_Writer::GetUisoMaterial() const
197 return myUisoMaterial;
200 Handle(Vrml_Material) VrmlAPI_Writer::GetVisoMaterial() const
202 return myVisoMaterial;
205 Handle(Vrml_Material) VrmlAPI_Writer::GetLineMaterial() const
207 return myLineMaterial;
210 Handle(Vrml_Material) VrmlAPI_Writer::GetWireMaterial() const
212 return myWireMaterial;
215 Handle(Vrml_Material) VrmlAPI_Writer::GetFreeBoundsMaterial() const
217 return myFreeBoundsMaterial;
220 Handle(Vrml_Material) VrmlAPI_Writer::GetUnfreeBoundsMaterial() const
222 return myUnfreeBoundsMaterial;
225 void VrmlAPI_Writer::Write(const TopoDS_Shape& aShape,const Standard_CString aFile, const Standard_Integer aVersion) const
228 write_v1(aShape, aFile);
229 else if (aVersion == 2)
230 write_v2(aShape, aFile);
233 void VrmlAPI_Writer::write_v1(const TopoDS_Shape& aShape,const Standard_CString aFile) const
235 OSD_Path thePath(aFile);
236 TCollection_AsciiString theFile;thePath.SystemName(theFile);
238 OSD_OpenStream(outfile, theFile.ToCString(), ios::out);
239 Handle(VrmlConverter_IsoAspect) ia = new VrmlConverter_IsoAspect; // UIso
240 Handle(VrmlConverter_IsoAspect) ia1 = new VrmlConverter_IsoAspect; //VIso
241 ia->SetMaterial(myUisoMaterial);
242 ia->SetHasMaterial(Standard_True);
243 myDrawer->SetUIsoAspect(ia);
244 ia1->SetMaterial(myVisoMaterial);
245 ia1->SetHasMaterial(Standard_True);
246 myDrawer->SetVIsoAspect(ia1);
247 // default Number of iso lines is 10
248 //---- Definition of LineAspect (default - without own material)
249 Handle(VrmlConverter_LineAspect) la = new VrmlConverter_LineAspect;
250 la->SetMaterial(myLineMaterial);
251 la->SetHasMaterial(Standard_True);
252 myDrawer->SetLineAspect(la);
253 //---- Definition of Wire (without any neighbour)
254 Handle(VrmlConverter_LineAspect) lw = new VrmlConverter_LineAspect;
255 lw->SetMaterial(myWireMaterial);
256 lw->SetHasMaterial(Standard_True);
257 myDrawer->SetWireAspect(lw);
258 //---- Definition of Free boundaries
259 Handle(VrmlConverter_LineAspect) lf = new VrmlConverter_LineAspect;
260 lf->SetMaterial(myFreeBoundsMaterial);
261 lf->SetHasMaterial(Standard_True);
262 myDrawer->SetFreeBoundaryAspect(lf);
263 //---- Definition of Unfree boundaries
264 Handle(VrmlConverter_LineAspect) lun = new VrmlConverter_LineAspect;
265 lun->SetMaterial(myUnfreeBoundsMaterial);
266 lun->SetHasMaterial(Standard_True);
267 myDrawer->SetUnFreeBoundaryAspect(lun);
268 //---- Definition of Points (default - without own material)
269 Handle(VrmlConverter_PointAspect) pa = new VrmlConverter_PointAspect;
270 pa->SetMaterial(myPointsMaterial);
271 pa->SetHasMaterial(Standard_True);
272 myDrawer->SetPointAspect(pa);
273 //-----------------------------------------
274 Handle(VrmlConverter_ShadingAspect) sa = new VrmlConverter_ShadingAspect;
275 sa->SetFrontMaterial(myFrontMaterial);
276 sa->SetHasMaterial(Standard_True);
278 sa->SetShapeHints(sh);
279 myDrawer->SetShadingAspect(sa);
280 //-------- Shape --------------------------
281 TopTools_Array1OfShape Shapes(1,1);
282 Shapes.SetValue(1,aShape);
284 // Check shape tesselation
285 TopExp_Explorer anExp (aShape, TopAbs_FACE);
286 TopLoc_Location aLoc;
287 Standard_Boolean hasTriangles = Standard_False;
288 for (; anExp.More(); anExp.Next())
290 const TopoDS_Face& aFace = TopoDS::Face (anExp.Current());
293 Handle(Poly_Triangulation) aTri =
294 BRep_Tool::Triangulation (aFace, aLoc);
298 hasTriangles = Standard_True;
304 //=========================================
305 //---- Definition of data for Projector
306 //=========================================
308 VrmlConverter_TypeOfLight Light = VrmlConverter_NoLight;
309 VrmlConverter_TypeOfCamera Camera = VrmlConverter_PerspectiveCamera;
310 Handle(VrmlConverter_Projector) projector = new VrmlConverter_Projector (Shapes,
316 Vrml::VrmlHeaderWriter(outfile);
317 if (myRepresentation == VrmlAPI_BothRepresentation)
318 Vrml::CommentWriter(" This file contents both Shaded and Wire Frame representation of selected Shape ",outfile);
319 if (myRepresentation == VrmlAPI_ShadedRepresentation)
320 Vrml::CommentWriter(" This file contents only Shaded representation of selected Shape ",outfile);
321 if (myRepresentation == VrmlAPI_WireFrameRepresentation)
322 Vrml::CommentWriter(" This file contents only Wire Frame representation of selected Shape ",outfile);
325 projector->Add(outfile);
326 Light = VrmlConverter_DirectionLight;
327 Camera = VrmlConverter_OrthographicCamera;
328 Handle(VrmlConverter_Projector) projector1 = new VrmlConverter_Projector (Shapes,
334 projector1->Add(outfile);
337 if ( (myRepresentation == VrmlAPI_ShadedRepresentation || myRepresentation == VrmlAPI_BothRepresentation) && hasTriangles)
340 Group1.Print(outfile);
341 Vrml_Instancing I2 ("Shaded representation of shape");
343 VrmlConverter_ShadedShape::Add(outfile,aShape,myDrawer);
344 Group1.Print(outfile);
346 if (myRepresentation == VrmlAPI_WireFrameRepresentation || myRepresentation == VrmlAPI_BothRepresentation)
349 Group2.Print(outfile);
350 Vrml_Instancing I3 ("Wire Frame representation of shape");
352 VrmlConverter_WFDeflectionShape::Add(outfile,aShape,myDrawer);
353 Group2.Print(outfile);
359 void VrmlAPI_Writer::write_v2(const TopoDS_Shape& aShape,const Standard_CString aFile) const
361 Standard_Boolean anExtFace = Standard_False;
362 if(myRepresentation == VrmlAPI_ShadedRepresentation || myRepresentation == VrmlAPI_BothRepresentation)
363 anExtFace = Standard_True;
365 Standard_Boolean anExtEdge = Standard_False;
366 if(myRepresentation == VrmlAPI_WireFrameRepresentation|| myRepresentation == VrmlAPI_BothRepresentation)
367 anExtEdge = Standard_True;
369 VrmlData_Scene aScene;
370 VrmlData_ShapeConvert aConv(aScene);
371 aConv.AddShape(aShape);
372 aConv.Convert(anExtFace, anExtEdge);
375 ostream outStream (&aFoc);
376 if (aFoc.open (aFile, ios::out))