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