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