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_FileSystem.hxx>
17 #include <OSD_Path.hxx>
18 #include <Poly_Triangulation.hxx>
19 #include <Standard_Stream.hxx>
20 #include <TColStd_HArray1OfReal.hxx>
21 #include <TopAbs_ShapeEnum.hxx>
22 #include <TopExp_Explorer.hxx>
24 #include <TopoDS_Face.hxx>
25 #include <TopoDS_Shape.hxx>
27 #include <Vrml_Group.hxx>
28 #include <Vrml_Instancing.hxx>
29 #include <Vrml_Material.hxx>
30 #include <Vrml_Separator.hxx>
31 #include <VrmlAPI_Writer.hxx>
32 #include <VrmlConverter_Drawer.hxx>
33 #include <VrmlConverter_IsoAspect.hxx>
34 #include <VrmlConverter_LineAspect.hxx>
35 #include <VrmlConverter_PointAspect.hxx>
36 #include <VrmlConverter_Projector.hxx>
37 #include <VrmlConverter_ShadedShape.hxx>
38 #include <VrmlConverter_ShadingAspect.hxx>
39 #include <VrmlConverter_WFDeflectionShape.hxx>
40 #include <VrmlData_Scene.hxx>
41 #include <VrmlData_ShapeConvert.hxx>
43 VrmlAPI_Writer::VrmlAPI_Writer()
45 myDrawer = new VrmlConverter_Drawer;
47 Handle(Quantity_HArray1OfColor) Col1 = new Quantity_HArray1OfColor (1, 1, Quantity_NOC_BLACK);
48 Handle(TColStd_HArray1OfReal) kik1 = new TColStd_HArray1OfReal(1,1,0.0);
49 Handle(TColStd_HArray1OfReal) kik2 = new TColStd_HArray1OfReal(1,1,0.1);
50 myFrontMaterial = new Vrml_Material(Col1,Col1, Col1, Col1, kik1, kik2);
51 myPointsMaterial = new Vrml_Material(Col1,Col1, Col1, Col1, kik1, kik2);
52 myUisoMaterial = new Vrml_Material(Col1,Col1, Col1, Col1, kik1, kik2);
53 myVisoMaterial = new Vrml_Material(Col1,Col1, Col1, Col1, kik1, kik2);
54 myLineMaterial = new Vrml_Material(Col1,Col1, Col1, Col1, kik1, kik2);
55 myWireMaterial = new Vrml_Material(Col1,Col1, Col1, Col1, kik1, kik2);
56 myFreeBoundsMaterial = new Vrml_Material(Col1,Col1, Col1, Col1, kik1, kik2);
57 myUnfreeBoundsMaterial = new Vrml_Material(Col1,Col1, Col1, Col1, kik1, kik2);
68 void VrmlAPI_Writer::ResetToDefaults()
72 Handle(TColStd_HArray1OfReal) kik1 = new TColStd_HArray1OfReal(1,1,myTransparency);
73 Handle(TColStd_HArray1OfReal) kik2 = new TColStd_HArray1OfReal(1,1,myShininess);
74 Handle(Quantity_HArray1OfColor) Col = new Quantity_HArray1OfColor(1, 1, Quantity_NOC_BLACK);
76 myFrontMaterial->SetAmbientColor(Col); myFrontMaterial->SetTransparency(kik1);myFrontMaterial->SetShininess(kik2);
77 myPointsMaterial->SetAmbientColor(Col); myPointsMaterial->SetTransparency(kik1);myPointsMaterial->SetShininess(kik2);
78 myUisoMaterial->SetAmbientColor(Col); myUisoMaterial->SetTransparency(kik1);myUisoMaterial->SetShininess(kik2);
79 myVisoMaterial->SetAmbientColor(Col); myVisoMaterial->SetTransparency(kik1);myVisoMaterial->SetShininess(kik2);
80 myLineMaterial->SetAmbientColor(Col); myLineMaterial->SetTransparency(kik1);myLineMaterial->SetShininess(kik2);
81 myWireMaterial->SetAmbientColor(Col); myWireMaterial->SetTransparency(kik1); myWireMaterial->SetShininess(kik2);
82 myFreeBoundsMaterial->SetAmbientColor(Col); myFreeBoundsMaterial->SetTransparency(kik1);myFreeBoundsMaterial->SetShininess(kik2);
83 myUnfreeBoundsMaterial->SetAmbientColor(Col); myUnfreeBoundsMaterial->SetTransparency(kik1);myUnfreeBoundsMaterial->SetShininess(kik2);
86 Handle(Quantity_HArray1OfColor) Col2 = new Quantity_HArray1OfColor(1, 1, Quantity_Color (0.75, 0.75, 0.75, Quantity_TOC_sRGB));
87 Handle(Quantity_HArray1OfColor) Col3 = new Quantity_HArray1OfColor(1, 1, Quantity_Color (0.82, 0.79, 0.42, Quantity_TOC_sRGB));
88 myUisoMaterial->SetDiffuseColor(Col2);
89 myVisoMaterial->SetDiffuseColor(Col2);
90 myFreeBoundsMaterial->SetDiffuseColor(Col2);
91 myUnfreeBoundsMaterial->SetDiffuseColor(Col2);
94 // Handle(Quantity_HArray1OfColor) Col3 = new Quantity_HArray1OfColor(1,1);
95 // color.SetValues(Quantity_NOC_GOLD);
96 // Col3->SetValue(1,color);
97 myLineMaterial->SetDiffuseColor(Col2);
98 myWireMaterial->SetDiffuseColor(Col2);
100 // Handle(Quantity_HArray1OfColor) Col4 = new Quantity_HArray1OfColor(1,1);
101 // color.SetValues(Quantity_NOC_GOLD);
102 // Col4->SetValue(1,color);
103 myFrontMaterial->SetDiffuseColor(Col2);
104 myPointsMaterial->SetDiffuseColor(Col2);
107 myUisoMaterial->SetSpecularColor(Col3);
108 myVisoMaterial->SetSpecularColor(Col3);
109 myFreeBoundsMaterial->SetSpecularColor(Col3);
110 myUnfreeBoundsMaterial->SetSpecularColor(Col3);
111 myLineMaterial->SetSpecularColor(Col3);
112 myWireMaterial->SetSpecularColor(Col3);
113 myFrontMaterial->SetSpecularColor(Col3);
114 myPointsMaterial->SetSpecularColor(Col3);
116 myRepresentation = VrmlAPI_BothRepresentation;
118 Handle(VrmlConverter_Drawer) VrmlAPI_Writer::Drawer() const
122 void VrmlAPI_Writer::SetDeflection(const Standard_Real aDef)
125 if (myDeflection > 0) {
126 myDrawer->SetMaximalChordialDeviation(myDeflection);
127 myDrawer->SetTypeOfDeflection(Aspect_TOD_ABSOLUTE);
129 else myDrawer->SetTypeOfDeflection(Aspect_TOD_RELATIVE);
131 void VrmlAPI_Writer::SetRepresentation(const VrmlAPI_RepresentationOfShape aRep)
133 myRepresentation = aRep;
135 void VrmlAPI_Writer::SetTransparencyToMaterial(Handle(Vrml_Material)& aMaterial,const Standard_Real aTransparency)
137 Handle(TColStd_HArray1OfReal) t = new TColStd_HArray1OfReal(1,1,aTransparency);
138 aMaterial->SetTransparency(t);
141 void VrmlAPI_Writer::SetShininessToMaterial(Handle(Vrml_Material)& aMaterial,const Standard_Real aShininess)
143 Handle(TColStd_HArray1OfReal) s = new TColStd_HArray1OfReal(1,1,aShininess);
144 aMaterial->SetShininess(s);
147 void VrmlAPI_Writer::SetAmbientColorToMaterial(Handle(Vrml_Material)& aMaterial,const Handle(Quantity_HArray1OfColor)& Color)
149 aMaterial->SetAmbientColor(Color);
152 void VrmlAPI_Writer::SetDiffuseColorToMaterial(Handle(Vrml_Material)& aMaterial,const Handle(Quantity_HArray1OfColor)& Color)
154 aMaterial->SetDiffuseColor(Color);
157 void VrmlAPI_Writer::SetSpecularColorToMaterial(Handle(Vrml_Material)& aMaterial,const Handle(Quantity_HArray1OfColor)& Color)
159 aMaterial->SetSpecularColor(Color);
162 void VrmlAPI_Writer::SetEmissiveColorToMaterial(Handle(Vrml_Material)& aMaterial,const Handle(Quantity_HArray1OfColor)& Color)
164 aMaterial->SetEmissiveColor(Color);
167 VrmlAPI_RepresentationOfShape VrmlAPI_Writer::GetRepresentation() const
169 return myRepresentation;
172 Handle(Vrml_Material) VrmlAPI_Writer::GetFrontMaterial() const
174 return myFrontMaterial;
177 Handle(Vrml_Material) VrmlAPI_Writer::GetPointsMaterial() const
179 return myPointsMaterial;
182 Handle(Vrml_Material) VrmlAPI_Writer::GetUisoMaterial() const
184 return myUisoMaterial;
187 Handle(Vrml_Material) VrmlAPI_Writer::GetVisoMaterial() const
189 return myVisoMaterial;
192 Handle(Vrml_Material) VrmlAPI_Writer::GetLineMaterial() const
194 return myLineMaterial;
197 Handle(Vrml_Material) VrmlAPI_Writer::GetWireMaterial() const
199 return myWireMaterial;
202 Handle(Vrml_Material) VrmlAPI_Writer::GetFreeBoundsMaterial() const
204 return myFreeBoundsMaterial;
207 Handle(Vrml_Material) VrmlAPI_Writer::GetUnfreeBoundsMaterial() const
209 return myUnfreeBoundsMaterial;
212 Standard_Boolean VrmlAPI_Writer::Write(const TopoDS_Shape& aShape,const Standard_CString aFile, const Standard_Integer aVersion) const
215 return write_v1 (aShape, aFile);
216 else if (aVersion == 2)
217 return write_v2 (aShape, aFile);
219 return Standard_False;
222 Standard_Boolean VrmlAPI_Writer::write_v1(const TopoDS_Shape& aShape,const Standard_CString aFile) const
224 OSD_Path thePath(aFile);
225 TCollection_AsciiString theFile;thePath.SystemName(theFile);
226 const Handle(OSD_FileSystem)& aFileSystem = OSD_FileSystem::DefaultFileSystem();
227 std::shared_ptr<std::ostream> anOutFile = aFileSystem->OpenOStream (theFile, std::ios::out | std::ios::binary);
228 if (anOutFile.get() == NULL)
230 return Standard_False;
232 Handle(VrmlConverter_IsoAspect) ia = new VrmlConverter_IsoAspect; // UIso
233 Handle(VrmlConverter_IsoAspect) ia1 = new VrmlConverter_IsoAspect; //VIso
234 ia->SetMaterial(myUisoMaterial);
235 ia->SetHasMaterial(Standard_True);
236 myDrawer->SetUIsoAspect(ia);
237 ia1->SetMaterial(myVisoMaterial);
238 ia1->SetHasMaterial(Standard_True);
239 myDrawer->SetVIsoAspect(ia1);
240 // default Number of iso lines is 10
241 //---- Definition of LineAspect (default - without own material)
242 Handle(VrmlConverter_LineAspect) la = new VrmlConverter_LineAspect;
243 la->SetMaterial(myLineMaterial);
244 la->SetHasMaterial(Standard_True);
245 myDrawer->SetLineAspect(la);
246 //---- Definition of Wire (without any neighbour)
247 Handle(VrmlConverter_LineAspect) lw = new VrmlConverter_LineAspect;
248 lw->SetMaterial(myWireMaterial);
249 lw->SetHasMaterial(Standard_True);
250 myDrawer->SetWireAspect(lw);
251 //---- Definition of Free boundaries
252 Handle(VrmlConverter_LineAspect) lf = new VrmlConverter_LineAspect;
253 lf->SetMaterial(myFreeBoundsMaterial);
254 lf->SetHasMaterial(Standard_True);
255 myDrawer->SetFreeBoundaryAspect(lf);
256 //---- Definition of Unfree boundaries
257 Handle(VrmlConverter_LineAspect) lun = new VrmlConverter_LineAspect;
258 lun->SetMaterial(myUnfreeBoundsMaterial);
259 lun->SetHasMaterial(Standard_True);
260 myDrawer->SetUnFreeBoundaryAspect(lun);
261 //---- Definition of Points (default - without own material)
262 Handle(VrmlConverter_PointAspect) pa = new VrmlConverter_PointAspect;
263 pa->SetMaterial(myPointsMaterial);
264 pa->SetHasMaterial(Standard_True);
265 myDrawer->SetPointAspect(pa);
266 //-----------------------------------------
267 Handle(VrmlConverter_ShadingAspect) sa = new VrmlConverter_ShadingAspect;
268 sa->SetFrontMaterial(myFrontMaterial);
269 sa->SetHasMaterial(Standard_True);
271 sa->SetShapeHints(sh);
272 myDrawer->SetShadingAspect(sa);
273 //-------- Shape --------------------------
274 TopTools_Array1OfShape Shapes(1,1);
275 Shapes.SetValue(1,aShape);
277 // Check shape tesselation
278 TopExp_Explorer anExp (aShape, TopAbs_FACE);
279 TopLoc_Location aLoc;
280 Standard_Boolean hasTriangles = Standard_False;
281 for (; anExp.More(); anExp.Next())
283 const TopoDS_Face& aFace = TopoDS::Face (anExp.Current());
286 Handle(Poly_Triangulation) aTri =
287 BRep_Tool::Triangulation (aFace, aLoc);
291 hasTriangles = Standard_True;
297 //=========================================
298 //---- Definition of data for Projector
299 //=========================================
301 VrmlConverter_TypeOfLight Light = VrmlConverter_NoLight;
302 VrmlConverter_TypeOfCamera Camera = VrmlConverter_PerspectiveCamera;
303 Handle(VrmlConverter_Projector) projector = new VrmlConverter_Projector (Shapes,
309 Vrml::VrmlHeaderWriter (*anOutFile);
310 if (myRepresentation == VrmlAPI_BothRepresentation)
311 Vrml::CommentWriter(" This file contents both Shaded and Wire Frame representation of selected Shape ", *anOutFile);
312 if (myRepresentation == VrmlAPI_ShadedRepresentation)
313 Vrml::CommentWriter(" This file contents only Shaded representation of selected Shape ", *anOutFile);
314 if (myRepresentation == VrmlAPI_WireFrameRepresentation)
315 Vrml::CommentWriter(" This file contents only Wire Frame representation of selected Shape ", *anOutFile);
317 S1.Print (*anOutFile);
318 projector->Add (*anOutFile);
319 Light = VrmlConverter_DirectionLight;
320 Camera = VrmlConverter_OrthographicCamera;
321 Handle(VrmlConverter_Projector) projector1 = new VrmlConverter_Projector (Shapes,
327 projector1->Add (*anOutFile);
329 S2.Print (*anOutFile);
330 if ( (myRepresentation == VrmlAPI_ShadedRepresentation || myRepresentation == VrmlAPI_BothRepresentation) && hasTriangles)
333 Group1.Print (*anOutFile);
334 Vrml_Instancing I2 ("Shaded representation of shape");
336 VrmlConverter_ShadedShape::Add (*anOutFile,aShape,myDrawer);
337 Group1.Print (*anOutFile);
339 if (myRepresentation == VrmlAPI_WireFrameRepresentation || myRepresentation == VrmlAPI_BothRepresentation)
342 Group2.Print (*anOutFile);
343 Vrml_Instancing I3 ("Wire Frame representation of shape");
345 VrmlConverter_WFDeflectionShape::Add (*anOutFile,aShape,myDrawer);
346 Group2.Print (*anOutFile);
348 S2.Print (*anOutFile);
349 S1.Print (*anOutFile);
352 return anOutFile->good();
355 Standard_Boolean VrmlAPI_Writer::write_v2(const TopoDS_Shape& aShape,const Standard_CString aFile) const
357 Standard_Boolean anExtFace = Standard_False;
358 if(myRepresentation == VrmlAPI_ShadedRepresentation || myRepresentation == VrmlAPI_BothRepresentation)
359 anExtFace = Standard_True;
361 Standard_Boolean anExtEdge = Standard_False;
362 if(myRepresentation == VrmlAPI_WireFrameRepresentation || myRepresentation == VrmlAPI_BothRepresentation)
363 anExtEdge = Standard_True;
365 VrmlData_Scene aScene;
366 VrmlData_ShapeConvert aConv(aScene);
367 aConv.AddShape(aShape);
368 aConv.Convert(anExtFace, anExtEdge);
370 const Handle(OSD_FileSystem)& aFileSystem = OSD_FileSystem::DefaultFileSystem();
371 std::shared_ptr<std::ostream> anOutStream = aFileSystem->OpenOStream (aFile, std::ios::out | std::ios::binary);
372 if (anOutStream.get() != NULL)
374 *anOutStream << aScene;
375 anOutStream->flush();
376 return anOutStream->good();
379 return Standard_False;
382 //=======================================================================
383 //function : WriteDoc
385 //=======================================================================
386 Standard_Boolean VrmlAPI_Writer::WriteDoc(
387 const Handle(TDocStd_Document) &theDoc,
388 const Standard_CString theFile,
389 const Standard_Real theScale) const
391 VrmlData_Scene aScene;
392 VrmlData_ShapeConvert aConv(aScene, theScale);
393 aConv.ConvertDocument(theDoc);
395 const Handle(OSD_FileSystem)& aFileSystem = OSD_FileSystem::DefaultFileSystem();
396 std::shared_ptr<std::ostream> anOutStream = aFileSystem->OpenOStream (theFile, std::ios::out | std::ios::binary);
397 if (anOutStream.get() != NULL)
399 *anOutStream << aScene;
400 anOutStream->flush();
401 return anOutStream->good();
404 return Standard_False;