0024624: Lost word in license statement in source files
[occt.git] / src / VrmlAPI / VrmlAPI_Writer.cxx
1 // Copyright (c) 1999-2014 OPEN CASCADE SAS
2 //
3 // This file is part of Open CASCADE Technology software library.
4 //
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.
10 //
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
13
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>
26 #include <Vrml.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
34 VrmlAPI_Writer::VrmlAPI_Writer()
35 {
36   myDrawer = new VrmlConverter_Drawer;
37   myDeflection = -1;
38   Quantity_Color color;
39   color.SetValues(0, 0, 0, Quantity_TOC_RGB);
40   Handle(Quantity_HArray1OfColor) Col1 = new Quantity_HArray1OfColor(1,1);
41   Col1->SetValue(1,color);
42   Handle(TColStd_HArray1OfReal) kik1 = new TColStd_HArray1OfReal(1,1,0.0);
43   Handle(TColStd_HArray1OfReal) kik2 = new TColStd_HArray1OfReal(1,1,0.1);
44   myFrontMaterial = new Vrml_Material(Col1,Col1, Col1, Col1, kik1, kik2);
45   myPointsMaterial = new Vrml_Material(Col1,Col1, Col1, Col1, kik1, kik2);
46   myUisoMaterial = new Vrml_Material(Col1,Col1, Col1, Col1, kik1, kik2);
47   myVisoMaterial = new Vrml_Material(Col1,Col1, Col1, Col1, kik1, kik2);
48   myLineMaterial = new Vrml_Material(Col1,Col1, Col1, Col1, kik1, kik2);
49   myWireMaterial = new Vrml_Material(Col1,Col1, Col1, Col1, kik1, kik2);
50   myFreeBoundsMaterial = new Vrml_Material(Col1,Col1, Col1, Col1, kik1, kik2);
51   myUnfreeBoundsMaterial = new Vrml_Material(Col1,Col1, Col1, Col1, kik1, kik2);
52   DX = 1;      
53   DY = -1;  
54   DZ = 1;
55   XUp = 0;
56   YUp = 0;
57   ZUp = 1;
58   Focus = 6;
59   ResetToDefaults();
60 }
61
62 void VrmlAPI_Writer::ResetToDefaults() 
63 {
64   myTransparency = 0.0;
65   myShininess = 0.1;
66   Handle(TColStd_HArray1OfReal) kik1 = new TColStd_HArray1OfReal(1,1,myTransparency);
67   Handle(TColStd_HArray1OfReal) kik2 = new TColStd_HArray1OfReal(1,1,myShininess);
68   Handle(Quantity_HArray1OfColor) Col = new Quantity_HArray1OfColor(1,1);
69   Quantity_Color color; 
70   color.SetValues(0, 0, 0, Quantity_TOC_RGB);
71   Col->SetValue(1,color);
72   //
73   myFrontMaterial->SetAmbientColor(Col); myFrontMaterial->SetTransparency(kik1);myFrontMaterial->SetShininess(kik2);
74   myPointsMaterial->SetAmbientColor(Col); myPointsMaterial->SetTransparency(kik1);myPointsMaterial->SetShininess(kik2);
75   myUisoMaterial->SetAmbientColor(Col); myUisoMaterial->SetTransparency(kik1);myUisoMaterial->SetShininess(kik2);
76   myVisoMaterial->SetAmbientColor(Col); myVisoMaterial->SetTransparency(kik1);myVisoMaterial->SetShininess(kik2);
77   myLineMaterial->SetAmbientColor(Col); myLineMaterial->SetTransparency(kik1);myLineMaterial->SetShininess(kik2);
78   myWireMaterial->SetAmbientColor(Col); myWireMaterial->SetTransparency(kik1); myWireMaterial->SetShininess(kik2);
79   myFreeBoundsMaterial->SetAmbientColor(Col); myFreeBoundsMaterial->SetTransparency(kik1);myFreeBoundsMaterial->SetShininess(kik2);
80   myUnfreeBoundsMaterial->SetAmbientColor(Col); myUnfreeBoundsMaterial->SetTransparency(kik1);myUnfreeBoundsMaterial->SetShininess(kik2);
81   //
82   //
83   Handle(Quantity_HArray1OfColor) Col2 = new Quantity_HArray1OfColor(1,1);
84   color.SetValues(0.75, 0.75, 0.75, Quantity_TOC_RGB);
85   Col2->SetValue(1,color);
86   Handle(Quantity_HArray1OfColor) Col3 = new Quantity_HArray1OfColor(1,1);
87   color.SetValues(0.82, 0.79, 0.42, Quantity_TOC_RGB);
88   Col3->SetValue(1,color);
89   
90   myUisoMaterial->SetDiffuseColor(Col2);
91   myVisoMaterial->SetDiffuseColor(Col2);
92   myFreeBoundsMaterial->SetDiffuseColor(Col2);
93   myUnfreeBoundsMaterial->SetDiffuseColor(Col2);
94   
95   //
96 //  Handle(Quantity_HArray1OfColor) Col3 = new Quantity_HArray1OfColor(1,1);
97 //  color.SetValues(Quantity_NOC_GOLD);
98 //  Col3->SetValue(1,color);
99   myLineMaterial->SetDiffuseColor(Col2);
100   myWireMaterial->SetDiffuseColor(Col2);
101   //
102 //  Handle(Quantity_HArray1OfColor) Col4 = new Quantity_HArray1OfColor(1,1);
103 //  color.SetValues(Quantity_NOC_GOLD);
104 //  Col4->SetValue(1,color);
105   myFrontMaterial->SetDiffuseColor(Col2);
106   myPointsMaterial->SetDiffuseColor(Col2);
107   //
108   
109   myUisoMaterial->SetSpecularColor(Col3);
110   myVisoMaterial->SetSpecularColor(Col3);
111   myFreeBoundsMaterial->SetSpecularColor(Col3);
112   myUnfreeBoundsMaterial->SetSpecularColor(Col3);
113   myLineMaterial->SetSpecularColor(Col3);
114   myWireMaterial->SetSpecularColor(Col3);
115   myFrontMaterial->SetSpecularColor(Col3);
116   myPointsMaterial->SetSpecularColor(Col3);
117   
118   myRepresentation = VrmlAPI_BothRepresentation;
119 }
120 Handle(VrmlConverter_Drawer) VrmlAPI_Writer::Drawer() const
121 {
122   return myDrawer;
123 }
124 void VrmlAPI_Writer::SetDeflection(const Standard_Real aDef)
125 {
126   myDeflection = aDef;
127   if (myDeflection > 0) {
128     myDrawer->SetMaximalChordialDeviation(myDeflection);
129     myDrawer->SetTypeOfDeflection(Aspect_TOD_ABSOLUTE);
130   }
131   else myDrawer->SetTypeOfDeflection(Aspect_TOD_RELATIVE);
132 }
133 void VrmlAPI_Writer::SetRepresentation(const VrmlAPI_RepresentationOfShape aRep)
134 {
135   myRepresentation = aRep;
136 }
137 void VrmlAPI_Writer::SetTransparencyToMaterial(Handle(Vrml_Material)& aMaterial,const Standard_Real aTransparency) 
138 {
139   Handle(TColStd_HArray1OfReal) t = new TColStd_HArray1OfReal(1,1,aTransparency);
140   aMaterial->SetTransparency(t);
141 }
142
143 void VrmlAPI_Writer::SetShininessToMaterial(Handle(Vrml_Material)& aMaterial,const Standard_Real aShininess) 
144 {  
145   Handle(TColStd_HArray1OfReal) s = new TColStd_HArray1OfReal(1,1,aShininess);
146   aMaterial->SetShininess(s);
147 }
148
149 void VrmlAPI_Writer::SetAmbientColorToMaterial(Handle(Vrml_Material)& aMaterial,const Handle(Quantity_HArray1OfColor)& Color) 
150 {
151   aMaterial->SetAmbientColor(Color);
152 }
153
154 void VrmlAPI_Writer::SetDiffuseColorToMaterial(Handle(Vrml_Material)& aMaterial,const Handle(Quantity_HArray1OfColor)& Color) 
155 {
156   aMaterial->SetDiffuseColor(Color);
157 }
158
159 void VrmlAPI_Writer::SetSpecularColorToMaterial(Handle(Vrml_Material)& aMaterial,const Handle(Quantity_HArray1OfColor)& Color) 
160 {
161   aMaterial->SetSpecularColor(Color);
162 }
163
164 void VrmlAPI_Writer::SetEmissiveColorToMaterial(Handle(Vrml_Material)& aMaterial,const Handle(Quantity_HArray1OfColor)& Color) 
165 {
166   aMaterial->SetEmissiveColor(Color);
167 }
168
169 VrmlAPI_RepresentationOfShape VrmlAPI_Writer::GetRepresentation() const
170 {
171   return myRepresentation;  
172 }
173
174 Handle(Vrml_Material) VrmlAPI_Writer::GetFrontMaterial() const
175 {
176   return myFrontMaterial;
177 }
178
179 Handle(Vrml_Material) VrmlAPI_Writer::GetPointsMaterial() const
180 {
181   return myPointsMaterial;
182 }
183
184 Handle(Vrml_Material) VrmlAPI_Writer::GetUisoMaterial() const
185 {
186   return myUisoMaterial;
187 }
188
189 Handle(Vrml_Material) VrmlAPI_Writer::GetVisoMaterial() const
190 {
191   return myVisoMaterial;
192 }
193
194 Handle(Vrml_Material) VrmlAPI_Writer::GetLineMaterial() const
195 {
196   return myLineMaterial;
197 }
198
199 Handle(Vrml_Material) VrmlAPI_Writer::GetWireMaterial() const
200 {
201   return myWireMaterial;
202 }
203
204 Handle(Vrml_Material) VrmlAPI_Writer::GetFreeBoundsMaterial() const
205 {
206   return myFreeBoundsMaterial;
207 }
208
209 Handle(Vrml_Material) VrmlAPI_Writer::GetUnfreeBoundsMaterial() const
210 {
211   return myUnfreeBoundsMaterial;
212 }
213
214 void VrmlAPI_Writer::Write(const TopoDS_Shape& aShape,const Standard_CString aFile) const
215 {
216   OSD_Path thePath(aFile);
217   TCollection_AsciiString theFile;thePath.SystemName(theFile);
218   ofstream outfile;
219   outfile.open(theFile.ToCString(),  ios::out);
220   Handle(VrmlConverter_IsoAspect) ia = new VrmlConverter_IsoAspect;  // UIso
221   Handle(VrmlConverter_IsoAspect) ia1 = new VrmlConverter_IsoAspect; //VIso
222   ia->SetMaterial(myUisoMaterial);
223   ia->SetHasMaterial(Standard_True);
224   myDrawer->SetUIsoAspect(ia);
225   ia1->SetMaterial(myVisoMaterial);
226   ia1->SetHasMaterial(Standard_True);
227   myDrawer->SetVIsoAspect(ia1);
228 // default Number of iso lines is 10
229 //----  Definition of LineAspect (default - without own material)
230   Handle(VrmlConverter_LineAspect) la = new VrmlConverter_LineAspect;
231   la->SetMaterial(myLineMaterial);
232   la->SetHasMaterial(Standard_True);
233   myDrawer->SetLineAspect(la);
234 //----  Definition of Wire (without any neighbour)
235   Handle(VrmlConverter_LineAspect) lw = new VrmlConverter_LineAspect;
236   lw->SetMaterial(myWireMaterial);
237   lw->SetHasMaterial(Standard_True);
238   myDrawer->SetWireAspect(lw);
239 //----  Definition of Free boundaries
240   Handle(VrmlConverter_LineAspect) lf = new VrmlConverter_LineAspect;
241   lf->SetMaterial(myFreeBoundsMaterial);
242   lf->SetHasMaterial(Standard_True);
243   myDrawer->SetFreeBoundaryAspect(lf);
244 //----  Definition of Unfree boundaries
245   Handle(VrmlConverter_LineAspect) lun = new VrmlConverter_LineAspect;
246   lun->SetMaterial(myUnfreeBoundsMaterial);
247   lun->SetHasMaterial(Standard_True);
248   myDrawer->SetUnFreeBoundaryAspect(lun);
249 //----  Definition of Points (default - without own material)
250   Handle(VrmlConverter_PointAspect) pa = new VrmlConverter_PointAspect;
251   pa->SetMaterial(myPointsMaterial);
252   pa->SetHasMaterial(Standard_True);
253   myDrawer->SetPointAspect(pa);
254 //-----------------------------------------
255   Handle(VrmlConverter_ShadingAspect) sa = new VrmlConverter_ShadingAspect;
256   sa->SetFrontMaterial(myFrontMaterial);
257   sa->SetHasMaterial(Standard_True);
258   Vrml_ShapeHints  sh;
259   sa->SetShapeHints(sh);
260   myDrawer->SetShadingAspect(sa);
261 //-------- Shape --------------------------
262   TopTools_Array1OfShape Shapes(1,1);
263   Shapes.SetValue(1,aShape);
264
265 //=========================================
266 //----  Definition of data for Projector
267 //=========================================
268
269   VrmlConverter_TypeOfLight Light = VrmlConverter_NoLight;
270   VrmlConverter_TypeOfCamera Camera = VrmlConverter_PerspectiveCamera;
271   Handle(VrmlConverter_Projector) projector = new VrmlConverter_Projector (Shapes, 
272                                                                            Focus, 
273                                                                            DX,  DY,  DZ, 
274                                                                            XUp, YUp, ZUp,
275                                                                            Camera,
276                                                                            Light);
277   Vrml::VrmlHeaderWriter(outfile);
278   if (myRepresentation == VrmlAPI_BothRepresentation)
279     Vrml::CommentWriter(" This file contents both Shaded and Wire Frame representation of selected Shape ",outfile);   
280   if (myRepresentation == VrmlAPI_ShadedRepresentation)
281     Vrml::CommentWriter(" This file contents only Shaded representation of selected Shape ",outfile);   
282   if (myRepresentation == VrmlAPI_WireFrameRepresentation)
283     Vrml::CommentWriter(" This file contents only Wire Frame representation of selected Shape ",outfile);   
284   Vrml_Separator S1;
285   S1.Print(outfile); 
286   projector->Add(outfile);
287   Light = VrmlConverter_DirectionLight;
288   Camera = VrmlConverter_OrthographicCamera;
289   Handle(VrmlConverter_Projector) projector1 = new VrmlConverter_Projector (Shapes, 
290                                                                            Focus, 
291                                                                            DX,  DY,  DZ, 
292                                                                            XUp, YUp, ZUp,
293                                                                            Camera,
294                                                                            Light);
295   projector1->Add(outfile);
296   Vrml_Separator S2;
297   S2.Print(outfile); 
298   if (myRepresentation == VrmlAPI_ShadedRepresentation || myRepresentation == VrmlAPI_BothRepresentation)
299     {
300       Vrml_Group Group1;
301       Group1.Print(outfile);
302       Vrml_Instancing I2 ("Shaded representation of shape");
303       I2.DEF(outfile);
304       VrmlConverter_ShadedShape::Add(outfile,aShape,myDrawer);
305       Group1.Print(outfile);
306     }
307   if (myRepresentation == VrmlAPI_WireFrameRepresentation || myRepresentation == VrmlAPI_BothRepresentation)
308     {
309       Vrml_Group Group2;
310       Group2.Print(outfile);
311       Vrml_Instancing I3 ("Wire Frame representation of shape");
312       I3.DEF(outfile);
313       VrmlConverter_WFDeflectionShape::Add(outfile,aShape,myDrawer);
314       Group2.Print(outfile);
315     }
316   S2.Print(outfile); 
317   S1.Print(outfile); 
318 }
319