0025367: IGES and BRep persistence - support unicode file names on Windows
[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 #include <OSD_OpenFile.hxx>
34
35 VrmlAPI_Writer::VrmlAPI_Writer()
36 {
37   myDrawer = new VrmlConverter_Drawer;
38   myDeflection = -1;
39   Quantity_Color color;
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);
53   DX = 1;      
54   DY = -1;  
55   DZ = 1;
56   XUp = 0;
57   YUp = 0;
58   ZUp = 1;
59   Focus = 6;
60   ResetToDefaults();
61 }
62
63 void VrmlAPI_Writer::ResetToDefaults() 
64 {
65   myTransparency = 0.0;
66   myShininess = 0.1;
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);
70   Quantity_Color color; 
71   color.SetValues(0, 0, 0, Quantity_TOC_RGB);
72   Col->SetValue(1,color);
73   //
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);
82   //
83   //
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);
90   
91   myUisoMaterial->SetDiffuseColor(Col2);
92   myVisoMaterial->SetDiffuseColor(Col2);
93   myFreeBoundsMaterial->SetDiffuseColor(Col2);
94   myUnfreeBoundsMaterial->SetDiffuseColor(Col2);
95   
96   //
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);
102   //
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);
108   //
109   
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);
118   
119   myRepresentation = VrmlAPI_BothRepresentation;
120 }
121 Handle(VrmlConverter_Drawer) VrmlAPI_Writer::Drawer() const
122 {
123   return myDrawer;
124 }
125 void VrmlAPI_Writer::SetDeflection(const Standard_Real aDef)
126 {
127   myDeflection = aDef;
128   if (myDeflection > 0) {
129     myDrawer->SetMaximalChordialDeviation(myDeflection);
130     myDrawer->SetTypeOfDeflection(Aspect_TOD_ABSOLUTE);
131   }
132   else myDrawer->SetTypeOfDeflection(Aspect_TOD_RELATIVE);
133 }
134 void VrmlAPI_Writer::SetRepresentation(const VrmlAPI_RepresentationOfShape aRep)
135 {
136   myRepresentation = aRep;
137 }
138 void VrmlAPI_Writer::SetTransparencyToMaterial(Handle(Vrml_Material)& aMaterial,const Standard_Real aTransparency) 
139 {
140   Handle(TColStd_HArray1OfReal) t = new TColStd_HArray1OfReal(1,1,aTransparency);
141   aMaterial->SetTransparency(t);
142 }
143
144 void VrmlAPI_Writer::SetShininessToMaterial(Handle(Vrml_Material)& aMaterial,const Standard_Real aShininess) 
145 {  
146   Handle(TColStd_HArray1OfReal) s = new TColStd_HArray1OfReal(1,1,aShininess);
147   aMaterial->SetShininess(s);
148 }
149
150 void VrmlAPI_Writer::SetAmbientColorToMaterial(Handle(Vrml_Material)& aMaterial,const Handle(Quantity_HArray1OfColor)& Color) 
151 {
152   aMaterial->SetAmbientColor(Color);
153 }
154
155 void VrmlAPI_Writer::SetDiffuseColorToMaterial(Handle(Vrml_Material)& aMaterial,const Handle(Quantity_HArray1OfColor)& Color) 
156 {
157   aMaterial->SetDiffuseColor(Color);
158 }
159
160 void VrmlAPI_Writer::SetSpecularColorToMaterial(Handle(Vrml_Material)& aMaterial,const Handle(Quantity_HArray1OfColor)& Color) 
161 {
162   aMaterial->SetSpecularColor(Color);
163 }
164
165 void VrmlAPI_Writer::SetEmissiveColorToMaterial(Handle(Vrml_Material)& aMaterial,const Handle(Quantity_HArray1OfColor)& Color) 
166 {
167   aMaterial->SetEmissiveColor(Color);
168 }
169
170 VrmlAPI_RepresentationOfShape VrmlAPI_Writer::GetRepresentation() const
171 {
172   return myRepresentation;  
173 }
174
175 Handle(Vrml_Material) VrmlAPI_Writer::GetFrontMaterial() const
176 {
177   return myFrontMaterial;
178 }
179
180 Handle(Vrml_Material) VrmlAPI_Writer::GetPointsMaterial() const
181 {
182   return myPointsMaterial;
183 }
184
185 Handle(Vrml_Material) VrmlAPI_Writer::GetUisoMaterial() const
186 {
187   return myUisoMaterial;
188 }
189
190 Handle(Vrml_Material) VrmlAPI_Writer::GetVisoMaterial() const
191 {
192   return myVisoMaterial;
193 }
194
195 Handle(Vrml_Material) VrmlAPI_Writer::GetLineMaterial() const
196 {
197   return myLineMaterial;
198 }
199
200 Handle(Vrml_Material) VrmlAPI_Writer::GetWireMaterial() const
201 {
202   return myWireMaterial;
203 }
204
205 Handle(Vrml_Material) VrmlAPI_Writer::GetFreeBoundsMaterial() const
206 {
207   return myFreeBoundsMaterial;
208 }
209
210 Handle(Vrml_Material) VrmlAPI_Writer::GetUnfreeBoundsMaterial() const
211 {
212   return myUnfreeBoundsMaterial;
213 }
214
215 void VrmlAPI_Writer::Write(const TopoDS_Shape& aShape,const Standard_CString aFile) const
216 {
217   OSD_Path thePath(aFile);
218   TCollection_AsciiString theFile;thePath.SystemName(theFile);
219   ofstream outfile;
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);
259   Vrml_ShapeHints  sh;
260   sa->SetShapeHints(sh);
261   myDrawer->SetShadingAspect(sa);
262 //-------- Shape --------------------------
263   TopTools_Array1OfShape Shapes(1,1);
264   Shapes.SetValue(1,aShape);
265
266 //=========================================
267 //----  Definition of data for Projector
268 //=========================================
269
270   VrmlConverter_TypeOfLight Light = VrmlConverter_NoLight;
271   VrmlConverter_TypeOfCamera Camera = VrmlConverter_PerspectiveCamera;
272   Handle(VrmlConverter_Projector) projector = new VrmlConverter_Projector (Shapes, 
273                                                                            Focus, 
274                                                                            DX,  DY,  DZ, 
275                                                                            XUp, YUp, ZUp,
276                                                                            Camera,
277                                                                            Light);
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);   
285   Vrml_Separator S1;
286   S1.Print(outfile); 
287   projector->Add(outfile);
288   Light = VrmlConverter_DirectionLight;
289   Camera = VrmlConverter_OrthographicCamera;
290   Handle(VrmlConverter_Projector) projector1 = new VrmlConverter_Projector (Shapes, 
291                                                                            Focus, 
292                                                                            DX,  DY,  DZ, 
293                                                                            XUp, YUp, ZUp,
294                                                                            Camera,
295                                                                            Light);
296   projector1->Add(outfile);
297   Vrml_Separator S2;
298   S2.Print(outfile); 
299   if (myRepresentation == VrmlAPI_ShadedRepresentation || myRepresentation == VrmlAPI_BothRepresentation)
300     {
301       Vrml_Group Group1;
302       Group1.Print(outfile);
303       Vrml_Instancing I2 ("Shaded representation of shape");
304       I2.DEF(outfile);
305       VrmlConverter_ShadedShape::Add(outfile,aShape,myDrawer);
306       Group1.Print(outfile);
307     }
308   if (myRepresentation == VrmlAPI_WireFrameRepresentation || myRepresentation == VrmlAPI_BothRepresentation)
309     {
310       Vrml_Group Group2;
311       Group2.Print(outfile);
312       Vrml_Instancing I3 ("Wire Frame representation of shape");
313       I3.DEF(outfile);
314       VrmlConverter_WFDeflectionShape::Add(outfile,aShape,myDrawer);
315       Group2.Print(outfile);
316     }
317   S2.Print(outfile); 
318   S1.Print(outfile); 
319 }
320