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>
34 VrmlAPI_Writer::VrmlAPI_Writer()
36 myDrawer = new VrmlConverter_Drawer;
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);
62 void VrmlAPI_Writer::ResetToDefaults()
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);
70 color.SetValues(0, 0, 0, Quantity_TOC_RGB);
71 Col->SetValue(1,color);
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);
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);
90 myUisoMaterial->SetDiffuseColor(Col2);
91 myVisoMaterial->SetDiffuseColor(Col2);
92 myFreeBoundsMaterial->SetDiffuseColor(Col2);
93 myUnfreeBoundsMaterial->SetDiffuseColor(Col2);
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);
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);
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);
118 myRepresentation = VrmlAPI_BothRepresentation;
120 Handle(VrmlConverter_Drawer) VrmlAPI_Writer::Drawer() const
124 void VrmlAPI_Writer::SetDeflection(const Standard_Real aDef)
127 if (myDeflection > 0) {
128 myDrawer->SetMaximalChordialDeviation(myDeflection);
129 myDrawer->SetTypeOfDeflection(Aspect_TOD_ABSOLUTE);
131 else myDrawer->SetTypeOfDeflection(Aspect_TOD_RELATIVE);
133 void VrmlAPI_Writer::SetRepresentation(const VrmlAPI_RepresentationOfShape aRep)
135 myRepresentation = aRep;
137 void VrmlAPI_Writer::SetTransparencyToMaterial(Handle(Vrml_Material)& aMaterial,const Standard_Real aTransparency)
139 Handle(TColStd_HArray1OfReal) t = new TColStd_HArray1OfReal(1,1,aTransparency);
140 aMaterial->SetTransparency(t);
143 void VrmlAPI_Writer::SetShininessToMaterial(Handle(Vrml_Material)& aMaterial,const Standard_Real aShininess)
145 Handle(TColStd_HArray1OfReal) s = new TColStd_HArray1OfReal(1,1,aShininess);
146 aMaterial->SetShininess(s);
149 void VrmlAPI_Writer::SetAmbientColorToMaterial(Handle(Vrml_Material)& aMaterial,const Handle(Quantity_HArray1OfColor)& Color)
151 aMaterial->SetAmbientColor(Color);
154 void VrmlAPI_Writer::SetDiffuseColorToMaterial(Handle(Vrml_Material)& aMaterial,const Handle(Quantity_HArray1OfColor)& Color)
156 aMaterial->SetDiffuseColor(Color);
159 void VrmlAPI_Writer::SetSpecularColorToMaterial(Handle(Vrml_Material)& aMaterial,const Handle(Quantity_HArray1OfColor)& Color)
161 aMaterial->SetSpecularColor(Color);
164 void VrmlAPI_Writer::SetEmissiveColorToMaterial(Handle(Vrml_Material)& aMaterial,const Handle(Quantity_HArray1OfColor)& Color)
166 aMaterial->SetEmissiveColor(Color);
169 VrmlAPI_RepresentationOfShape VrmlAPI_Writer::GetRepresentation() const
171 return myRepresentation;
174 Handle(Vrml_Material) VrmlAPI_Writer::GetFrontMaterial() const
176 return myFrontMaterial;
179 Handle(Vrml_Material) VrmlAPI_Writer::GetPointsMaterial() const
181 return myPointsMaterial;
184 Handle(Vrml_Material) VrmlAPI_Writer::GetUisoMaterial() const
186 return myUisoMaterial;
189 Handle(Vrml_Material) VrmlAPI_Writer::GetVisoMaterial() const
191 return myVisoMaterial;
194 Handle(Vrml_Material) VrmlAPI_Writer::GetLineMaterial() const
196 return myLineMaterial;
199 Handle(Vrml_Material) VrmlAPI_Writer::GetWireMaterial() const
201 return myWireMaterial;
204 Handle(Vrml_Material) VrmlAPI_Writer::GetFreeBoundsMaterial() const
206 return myFreeBoundsMaterial;
209 Handle(Vrml_Material) VrmlAPI_Writer::GetUnfreeBoundsMaterial() const
211 return myUnfreeBoundsMaterial;
214 void VrmlAPI_Writer::Write(const TopoDS_Shape& aShape,const Standard_CString aFile) const
216 OSD_Path thePath(aFile);
217 TCollection_AsciiString theFile;thePath.SystemName(theFile);
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);
259 sa->SetShapeHints(sh);
260 myDrawer->SetShadingAspect(sa);
261 //-------- Shape --------------------------
262 TopTools_Array1OfShape Shapes(1,1);
263 Shapes.SetValue(1,aShape);
265 //=========================================
266 //---- Definition of data for Projector
267 //=========================================
269 VrmlConverter_TypeOfLight Light = VrmlConverter_NoLight;
270 VrmlConverter_TypeOfCamera Camera = VrmlConverter_PerspectiveCamera;
271 Handle(VrmlConverter_Projector) projector = new VrmlConverter_Projector (Shapes,
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);
286 projector->Add(outfile);
287 Light = VrmlConverter_DirectionLight;
288 Camera = VrmlConverter_OrthographicCamera;
289 Handle(VrmlConverter_Projector) projector1 = new VrmlConverter_Projector (Shapes,
295 projector1->Add(outfile);
298 if (myRepresentation == VrmlAPI_ShadedRepresentation || myRepresentation == VrmlAPI_BothRepresentation)
301 Group1.Print(outfile);
302 Vrml_Instancing I2 ("Shaded representation of shape");
304 VrmlConverter_ShadedShape::Add(outfile,aShape,myDrawer);
305 Group1.Print(outfile);
307 if (myRepresentation == VrmlAPI_WireFrameRepresentation || myRepresentation == VrmlAPI_BothRepresentation)
310 Group2.Print(outfile);
311 Vrml_Instancing I3 ("Wire Frame representation of shape");
313 VrmlConverter_WFDeflectionShape::Add(outfile,aShape,myDrawer);
314 Group2.Print(outfile);