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.
14 #include <Standard_Stream.hxx>
15 #include <VrmlAPI_Writer.ixx>
16 #include <Vrml_Material.hxx>
17 #include <Quantity_HArray1OfColor.hxx>
18 #include <TopoDS_Shape.hxx>
19 #include <TColStd_HArray1OfReal.hxx>
20 #include <OSD_Path.hxx>
21 #include <VrmlConverter_IsoAspect.hxx>
22 #include <VrmlConverter_LineAspect.hxx>
23 #include <VrmlConverter_PointAspect.hxx>
24 #include <VrmlConverter_ShadingAspect.hxx>
25 #include <TopTools_Array1OfShape.hxx>
27 #include <VrmlConverter_Projector.hxx>
28 #include <VrmlConverter_ShadedShape.hxx>
29 #include <Vrml_Group.hxx>
30 #include <Vrml_Instancing.hxx>
31 #include <Vrml_Separator.hxx>
32 #include <VrmlConverter_WFDeflectionShape.hxx>
33 #include <OSD_OpenFile.hxx>
35 VrmlAPI_Writer::VrmlAPI_Writer()
37 myDrawer = new VrmlConverter_Drawer;
40 color.SetValues(0, 0, 0, Quantity_TOC_RGB);
41 Handle(Quantity_HArray1OfColor) Col1 = new Quantity_HArray1OfColor(1,1);
42 Col1->SetValue(1,color);
43 Handle(TColStd_HArray1OfReal) kik1 = new TColStd_HArray1OfReal(1,1,0.0);
44 Handle(TColStd_HArray1OfReal) kik2 = new TColStd_HArray1OfReal(1,1,0.1);
45 myFrontMaterial = new Vrml_Material(Col1,Col1, Col1, Col1, kik1, kik2);
46 myPointsMaterial = new Vrml_Material(Col1,Col1, Col1, Col1, kik1, kik2);
47 myUisoMaterial = new Vrml_Material(Col1,Col1, Col1, Col1, kik1, kik2);
48 myVisoMaterial = new Vrml_Material(Col1,Col1, Col1, Col1, kik1, kik2);
49 myLineMaterial = new Vrml_Material(Col1,Col1, Col1, Col1, kik1, kik2);
50 myWireMaterial = new Vrml_Material(Col1,Col1, Col1, Col1, kik1, kik2);
51 myFreeBoundsMaterial = new Vrml_Material(Col1,Col1, Col1, Col1, kik1, kik2);
52 myUnfreeBoundsMaterial = new Vrml_Material(Col1,Col1, Col1, Col1, kik1, kik2);
63 void VrmlAPI_Writer::ResetToDefaults()
67 Handle(TColStd_HArray1OfReal) kik1 = new TColStd_HArray1OfReal(1,1,myTransparency);
68 Handle(TColStd_HArray1OfReal) kik2 = new TColStd_HArray1OfReal(1,1,myShininess);
69 Handle(Quantity_HArray1OfColor) Col = new Quantity_HArray1OfColor(1,1);
71 color.SetValues(0, 0, 0, Quantity_TOC_RGB);
72 Col->SetValue(1,color);
74 myFrontMaterial->SetAmbientColor(Col); myFrontMaterial->SetTransparency(kik1);myFrontMaterial->SetShininess(kik2);
75 myPointsMaterial->SetAmbientColor(Col); myPointsMaterial->SetTransparency(kik1);myPointsMaterial->SetShininess(kik2);
76 myUisoMaterial->SetAmbientColor(Col); myUisoMaterial->SetTransparency(kik1);myUisoMaterial->SetShininess(kik2);
77 myVisoMaterial->SetAmbientColor(Col); myVisoMaterial->SetTransparency(kik1);myVisoMaterial->SetShininess(kik2);
78 myLineMaterial->SetAmbientColor(Col); myLineMaterial->SetTransparency(kik1);myLineMaterial->SetShininess(kik2);
79 myWireMaterial->SetAmbientColor(Col); myWireMaterial->SetTransparency(kik1); myWireMaterial->SetShininess(kik2);
80 myFreeBoundsMaterial->SetAmbientColor(Col); myFreeBoundsMaterial->SetTransparency(kik1);myFreeBoundsMaterial->SetShininess(kik2);
81 myUnfreeBoundsMaterial->SetAmbientColor(Col); myUnfreeBoundsMaterial->SetTransparency(kik1);myUnfreeBoundsMaterial->SetShininess(kik2);
84 Handle(Quantity_HArray1OfColor) Col2 = new Quantity_HArray1OfColor(1,1);
85 color.SetValues(0.75, 0.75, 0.75, Quantity_TOC_RGB);
86 Col2->SetValue(1,color);
87 Handle(Quantity_HArray1OfColor) Col3 = new Quantity_HArray1OfColor(1,1);
88 color.SetValues(0.82, 0.79, 0.42, Quantity_TOC_RGB);
89 Col3->SetValue(1,color);
91 myUisoMaterial->SetDiffuseColor(Col2);
92 myVisoMaterial->SetDiffuseColor(Col2);
93 myFreeBoundsMaterial->SetDiffuseColor(Col2);
94 myUnfreeBoundsMaterial->SetDiffuseColor(Col2);
97 // Handle(Quantity_HArray1OfColor) Col3 = new Quantity_HArray1OfColor(1,1);
98 // color.SetValues(Quantity_NOC_GOLD);
99 // Col3->SetValue(1,color);
100 myLineMaterial->SetDiffuseColor(Col2);
101 myWireMaterial->SetDiffuseColor(Col2);
103 // Handle(Quantity_HArray1OfColor) Col4 = new Quantity_HArray1OfColor(1,1);
104 // color.SetValues(Quantity_NOC_GOLD);
105 // Col4->SetValue(1,color);
106 myFrontMaterial->SetDiffuseColor(Col2);
107 myPointsMaterial->SetDiffuseColor(Col2);
110 myUisoMaterial->SetSpecularColor(Col3);
111 myVisoMaterial->SetSpecularColor(Col3);
112 myFreeBoundsMaterial->SetSpecularColor(Col3);
113 myUnfreeBoundsMaterial->SetSpecularColor(Col3);
114 myLineMaterial->SetSpecularColor(Col3);
115 myWireMaterial->SetSpecularColor(Col3);
116 myFrontMaterial->SetSpecularColor(Col3);
117 myPointsMaterial->SetSpecularColor(Col3);
119 myRepresentation = VrmlAPI_BothRepresentation;
121 Handle(VrmlConverter_Drawer) VrmlAPI_Writer::Drawer() const
125 void VrmlAPI_Writer::SetDeflection(const Standard_Real aDef)
128 if (myDeflection > 0) {
129 myDrawer->SetMaximalChordialDeviation(myDeflection);
130 myDrawer->SetTypeOfDeflection(Aspect_TOD_ABSOLUTE);
132 else myDrawer->SetTypeOfDeflection(Aspect_TOD_RELATIVE);
134 void VrmlAPI_Writer::SetRepresentation(const VrmlAPI_RepresentationOfShape aRep)
136 myRepresentation = aRep;
138 void VrmlAPI_Writer::SetTransparencyToMaterial(Handle(Vrml_Material)& aMaterial,const Standard_Real aTransparency)
140 Handle(TColStd_HArray1OfReal) t = new TColStd_HArray1OfReal(1,1,aTransparency);
141 aMaterial->SetTransparency(t);
144 void VrmlAPI_Writer::SetShininessToMaterial(Handle(Vrml_Material)& aMaterial,const Standard_Real aShininess)
146 Handle(TColStd_HArray1OfReal) s = new TColStd_HArray1OfReal(1,1,aShininess);
147 aMaterial->SetShininess(s);
150 void VrmlAPI_Writer::SetAmbientColorToMaterial(Handle(Vrml_Material)& aMaterial,const Handle(Quantity_HArray1OfColor)& Color)
152 aMaterial->SetAmbientColor(Color);
155 void VrmlAPI_Writer::SetDiffuseColorToMaterial(Handle(Vrml_Material)& aMaterial,const Handle(Quantity_HArray1OfColor)& Color)
157 aMaterial->SetDiffuseColor(Color);
160 void VrmlAPI_Writer::SetSpecularColorToMaterial(Handle(Vrml_Material)& aMaterial,const Handle(Quantity_HArray1OfColor)& Color)
162 aMaterial->SetSpecularColor(Color);
165 void VrmlAPI_Writer::SetEmissiveColorToMaterial(Handle(Vrml_Material)& aMaterial,const Handle(Quantity_HArray1OfColor)& Color)
167 aMaterial->SetEmissiveColor(Color);
170 VrmlAPI_RepresentationOfShape VrmlAPI_Writer::GetRepresentation() const
172 return myRepresentation;
175 Handle(Vrml_Material) VrmlAPI_Writer::GetFrontMaterial() const
177 return myFrontMaterial;
180 Handle(Vrml_Material) VrmlAPI_Writer::GetPointsMaterial() const
182 return myPointsMaterial;
185 Handle(Vrml_Material) VrmlAPI_Writer::GetUisoMaterial() const
187 return myUisoMaterial;
190 Handle(Vrml_Material) VrmlAPI_Writer::GetVisoMaterial() const
192 return myVisoMaterial;
195 Handle(Vrml_Material) VrmlAPI_Writer::GetLineMaterial() const
197 return myLineMaterial;
200 Handle(Vrml_Material) VrmlAPI_Writer::GetWireMaterial() const
202 return myWireMaterial;
205 Handle(Vrml_Material) VrmlAPI_Writer::GetFreeBoundsMaterial() const
207 return myFreeBoundsMaterial;
210 Handle(Vrml_Material) VrmlAPI_Writer::GetUnfreeBoundsMaterial() const
212 return myUnfreeBoundsMaterial;
215 void VrmlAPI_Writer::Write(const TopoDS_Shape& aShape,const Standard_CString aFile) const
217 OSD_Path thePath(aFile);
218 TCollection_AsciiString theFile;thePath.SystemName(theFile);
220 OSD_OpenStream(outfile, theFile.ToCString(), ios::out);
221 Handle(VrmlConverter_IsoAspect) ia = new VrmlConverter_IsoAspect; // UIso
222 Handle(VrmlConverter_IsoAspect) ia1 = new VrmlConverter_IsoAspect; //VIso
223 ia->SetMaterial(myUisoMaterial);
224 ia->SetHasMaterial(Standard_True);
225 myDrawer->SetUIsoAspect(ia);
226 ia1->SetMaterial(myVisoMaterial);
227 ia1->SetHasMaterial(Standard_True);
228 myDrawer->SetVIsoAspect(ia1);
229 // default Number of iso lines is 10
230 //---- Definition of LineAspect (default - without own material)
231 Handle(VrmlConverter_LineAspect) la = new VrmlConverter_LineAspect;
232 la->SetMaterial(myLineMaterial);
233 la->SetHasMaterial(Standard_True);
234 myDrawer->SetLineAspect(la);
235 //---- Definition of Wire (without any neighbour)
236 Handle(VrmlConverter_LineAspect) lw = new VrmlConverter_LineAspect;
237 lw->SetMaterial(myWireMaterial);
238 lw->SetHasMaterial(Standard_True);
239 myDrawer->SetWireAspect(lw);
240 //---- Definition of Free boundaries
241 Handle(VrmlConverter_LineAspect) lf = new VrmlConverter_LineAspect;
242 lf->SetMaterial(myFreeBoundsMaterial);
243 lf->SetHasMaterial(Standard_True);
244 myDrawer->SetFreeBoundaryAspect(lf);
245 //---- Definition of Unfree boundaries
246 Handle(VrmlConverter_LineAspect) lun = new VrmlConverter_LineAspect;
247 lun->SetMaterial(myUnfreeBoundsMaterial);
248 lun->SetHasMaterial(Standard_True);
249 myDrawer->SetUnFreeBoundaryAspect(lun);
250 //---- Definition of Points (default - without own material)
251 Handle(VrmlConverter_PointAspect) pa = new VrmlConverter_PointAspect;
252 pa->SetMaterial(myPointsMaterial);
253 pa->SetHasMaterial(Standard_True);
254 myDrawer->SetPointAspect(pa);
255 //-----------------------------------------
256 Handle(VrmlConverter_ShadingAspect) sa = new VrmlConverter_ShadingAspect;
257 sa->SetFrontMaterial(myFrontMaterial);
258 sa->SetHasMaterial(Standard_True);
260 sa->SetShapeHints(sh);
261 myDrawer->SetShadingAspect(sa);
262 //-------- Shape --------------------------
263 TopTools_Array1OfShape Shapes(1,1);
264 Shapes.SetValue(1,aShape);
266 //=========================================
267 //---- Definition of data for Projector
268 //=========================================
270 VrmlConverter_TypeOfLight Light = VrmlConverter_NoLight;
271 VrmlConverter_TypeOfCamera Camera = VrmlConverter_PerspectiveCamera;
272 Handle(VrmlConverter_Projector) projector = new VrmlConverter_Projector (Shapes,
278 Vrml::VrmlHeaderWriter(outfile);
279 if (myRepresentation == VrmlAPI_BothRepresentation)
280 Vrml::CommentWriter(" This file contents both Shaded and Wire Frame representation of selected Shape ",outfile);
281 if (myRepresentation == VrmlAPI_ShadedRepresentation)
282 Vrml::CommentWriter(" This file contents only Shaded representation of selected Shape ",outfile);
283 if (myRepresentation == VrmlAPI_WireFrameRepresentation)
284 Vrml::CommentWriter(" This file contents only Wire Frame representation of selected Shape ",outfile);
287 projector->Add(outfile);
288 Light = VrmlConverter_DirectionLight;
289 Camera = VrmlConverter_OrthographicCamera;
290 Handle(VrmlConverter_Projector) projector1 = new VrmlConverter_Projector (Shapes,
296 projector1->Add(outfile);
299 if (myRepresentation == VrmlAPI_ShadedRepresentation || myRepresentation == VrmlAPI_BothRepresentation)
302 Group1.Print(outfile);
303 Vrml_Instancing I2 ("Shaded representation of shape");
305 VrmlConverter_ShadedShape::Add(outfile,aShape,myDrawer);
306 Group1.Print(outfile);
308 if (myRepresentation == VrmlAPI_WireFrameRepresentation || myRepresentation == VrmlAPI_BothRepresentation)
311 Group2.Print(outfile);
312 Vrml_Instancing I3 ("Wire Frame representation of shape");
314 VrmlConverter_WFDeflectionShape::Add(outfile,aShape,myDrawer);
315 Group2.Print(outfile);