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 <XSDRAWSTLVRML_ToVRML.ixx>
16 #include <Standard_Stream.hxx>
17 #include <Bnd_Box.hxx>
18 #include <BRepBndLib.hxx>
19 #include <BRep_Tool.hxx>
20 #include <BRepTools.hxx>
21 #include <BRepMesh_DiscretFactory.hxx>
22 #include <Poly_Triangulation.hxx>
23 #include <TopExp_Explorer.hxx>
25 #include <TopoDS_Face.hxx>
27 #include <gp_Pnt.hxx> //ied_modif_for_compil_Nov-20-1998
29 XSDRAWSTLVRML_ToVRML::XSDRAWSTLVRML_ToVRML ()
31 myEmissiveColorRed = 0.3;
32 myEmissiveColorGreen = 0.3;
33 myEmissiveColorBlue = 0.3;
34 myDiffuseColorRed = 0.3;
35 myDiffuseColorGreen = 0.3;
36 myDiffuseColorBlue = 0.5;
38 myAmbientIntensity = 0.3;
39 mySpecularColorRed = 0.7;
40 mySpecularColorGreen = 0.7;
41 mySpecularColorBlue = 0.8;
48 Standard_Real& XSDRAWSTLVRML_ToVRML::EmissiveColorRed () { return myEmissiveColorRed ; }
49 Standard_Real& XSDRAWSTLVRML_ToVRML::EmissiveColorGreen() { return myEmissiveColorGreen; }
50 Standard_Real& XSDRAWSTLVRML_ToVRML::EmissiveColorBlue () { return myEmissiveColorBlue ; }
51 Standard_Real& XSDRAWSTLVRML_ToVRML::DiffuseColorRed () { return myDiffuseColorRed ; }
52 Standard_Real& XSDRAWSTLVRML_ToVRML::DiffuseColorGreen () { return myDiffuseColorGreen ; }
53 Standard_Real& XSDRAWSTLVRML_ToVRML::DiffuseColorBlue () { return myDiffuseColorBlue ; }
54 Standard_Real& XSDRAWSTLVRML_ToVRML::Transparency () { return myTransparency ; }
55 Standard_Real& XSDRAWSTLVRML_ToVRML::AmbientIntensity () { return myAmbientIntensity ; }
56 Standard_Real& XSDRAWSTLVRML_ToVRML::SpecularColorRed () { return mySpecularColorRed ; }
57 Standard_Real& XSDRAWSTLVRML_ToVRML::SpecularColorGreen() { return mySpecularColorGreen; }
58 Standard_Real& XSDRAWSTLVRML_ToVRML::SpecularColorBlue () { return mySpecularColorBlue ; }
59 Standard_Real& XSDRAWSTLVRML_ToVRML::Shininess () { return myShininess ; }
60 TCollection_AsciiString& XSDRAWSTLVRML_ToVRML::Texture () { return myTexture ; }
61 Standard_Real& XSDRAWSTLVRML_ToVRML::CreaseAngle() { return myCreaseAngle; }
62 Standard_Real& XSDRAWSTLVRML_ToVRML::Deflection () { return myDeflection ; }
64 //=======================================================================
65 // function : ToVRML::Write
66 // purpose : conversion of a Shape into VRML format for 3d visualisation
67 //=======================================================================
68 Standard_Boolean XSDRAWSTLVRML_ToVRML::Write
69 (const TopoDS_Shape& theShape, const Standard_CString theFileName) const
72 ostream anOut(&aFile);
74 if ( aFile.open(theFileName,ios::out) )
76 // Creates facets from the shape
77 // Create (defle : Real from Standard;
78 // shape : Shape from TopoDS;
79 // angl : Real from Standard = 0.17;
80 // withShare : Boolean from Standard = Standard_True;
81 // inshape : Boolean from Standard = Standard_False;
82 // relative : Boolean from Standard = Standard_False;
83 // shapetrigu: Boolean from Standard = Standard_False)
84 // returns mutable Discret from BRepMesh;
86 if ( !BRepTools::Triangulation (theShape, myDeflection) )
88 // retrieve meshing tool from Factory
89 BRepTools::Clean (theShape);
90 Handle(BRepMesh_DiscretRoot) aMeshAlgo =
91 BRepMesh_DiscretFactory::Get().Discret(theShape, myDeflection, 0.17);
93 if ( !aMeshAlgo.IsNull() )
100 BRepBndLib::Add(theShape, aBox);
102 // Header of the VRML file
103 anOut << "#VRML V2.0 utf8" << endl;
104 anOut << "Group {" << endl;
105 anOut << " children [ " << endl;
106 anOut << " NavigationInfo {" << endl;
107 anOut << " type \"EXAMINE\" " << endl;
108 anOut << " }," << endl;
109 anOut << " Shape {" << endl;
111 anOut << " appearance Appearance {" << endl;
112 anOut << " texture ImageTexture {" << endl;
113 anOut << " url " << myTexture.ToCString() << endl;
114 anOut << " }" << endl;
115 anOut << " material Material {" << endl;
116 anOut << " diffuseColor " << myDiffuseColorRed << " "
117 << myDiffuseColorGreen << " "
118 << myDiffuseColorBlue << " " << endl;
119 anOut << " emissiveColor " << myEmissiveColorRed << " "
120 << myEmissiveColorGreen << " "
121 << myEmissiveColorBlue << " " << endl;
122 anOut << " transparency " << myTransparency << endl;
123 anOut << " ambientIntensity " << myAmbientIntensity << " " << endl;
124 anOut << " specularColor " << mySpecularColorRed << " "
125 << mySpecularColorGreen << " "
126 << mySpecularColorBlue << " " << endl;
127 anOut << " shininess " << myShininess << " " << endl;
128 anOut << " }" << endl;
129 anOut << " }" << endl;
131 anOut << " geometry IndexedFaceSet {" << endl;
132 anOut << " coord Coordinate {" << endl;
133 anOut << " point [" << endl;
135 // Puts the coordinates of all the vertices using the order
136 // given during the discretisation
137 TopExp_Explorer aFaceIt(theShape, TopAbs_FACE);
138 for (; aFaceIt.More(); aFaceIt.Next())
140 const TopoDS_Face& aFace = TopoDS::Face(aFaceIt.Current());
142 TopLoc_Location aLoc = aFace.Location();
143 Handle(Poly_Triangulation) aTriangulation =
144 BRep_Tool::Triangulation(aFace, aLoc);
146 const Standard_Integer aLength = aTriangulation->NbNodes();
147 const TColgp_Array1OfPnt& aNodes = aTriangulation->Nodes();
148 for ( Standard_Integer i = 1; i <= aLength; ++i )
150 const gp_Pnt& aPoint = aNodes(i);
155 << aPoint.Z() << "," << endl;
159 anOut << " ]" << endl;
160 anOut << " }" << endl;
161 anOut << " coordIndex [" << endl;
163 // Retrieves all the triangles in order to draw the facets
164 for (aFaceIt.Init(theShape, TopAbs_FACE); aFaceIt.More(); aFaceIt.Next())
166 TopoDS_Face aFace = TopoDS::Face(aFaceIt.Current());
167 aFace.Orientation(TopAbs_FORWARD);
169 TopLoc_Location aLoc = aFace.Location();
170 Handle(Poly_Triangulation) aTriangulation =
171 BRep_Tool::Triangulation(aFace, aLoc);
173 const Standard_Integer aNbTriangles = aTriangulation->NbTriangles();
174 const Poly_Array1OfTriangle& aTriangles = aTriangulation->Triangles();
175 for ( Standard_Integer i = 1, v[3]; i <= aNbTriangles; ++i )
177 aTriangles(i).Get(v[0], v[1], v[2]);
182 << v[2] - 1 << ", -1," << endl;
186 anOut << " ]" << endl;
187 anOut << " solid FALSE" << endl; // it is not a closed solid
188 anOut << " creaseAngle " << myCreaseAngle << " " << endl; // for smooth shading
189 anOut << " }" << endl;
190 anOut << " }" << endl;
191 anOut << " ]" << endl;
192 anOut << "}" << endl;
196 // Failure when opening file
197 return Standard_False;
201 return Standard_True;