973c2be1 |
1 | // Copyright (c) 1999-2014 OPEN CASCADE SAS |
b311480e |
2 | // |
973c2be1 |
3 | // This file is part of Open CASCADE Technology software library. |
b311480e |
4 | // |
d5f74e42 |
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 |
973c2be1 |
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. |
b311480e |
10 | // |
973c2be1 |
11 | // Alternatively, this file may be used under the terms of Open CASCADE |
12 | // commercial license or contractual agreement. |
b311480e |
13 | |
7da5f7b1 |
14 | #include <XSDRAWSTLVRML_ToVRML.ixx> |
7fd59977 |
15 | |
16 | #include <Standard_Stream.hxx> |
7fd59977 |
17 | #include <Bnd_Box.hxx> |
18 | #include <BRepBndLib.hxx> |
fcf15f5c |
19 | #include <BRep_Tool.hxx> |
20 | #include <BRepTools.hxx> |
21 | #include <BRepMesh_DiscretFactory.hxx> |
22 | #include <Poly_Triangulation.hxx> |
ceb418e1 |
23 | #include <TopExp_Explorer.hxx> |
24 | #include <TopoDS.hxx> |
fcf15f5c |
25 | #include <TopoDS_Face.hxx> |
7fd59977 |
26 | |
27 | #include <gp_Pnt.hxx> //ied_modif_for_compil_Nov-20-1998 |
28 | |
7da5f7b1 |
29 | XSDRAWSTLVRML_ToVRML::XSDRAWSTLVRML_ToVRML () |
7fd59977 |
30 | { |
31 | myEmissiveColorRed = 0.3; |
32 | myEmissiveColorGreen = 0.3; |
33 | myEmissiveColorBlue = 0.3; |
34 | myDiffuseColorRed = 0.3; |
35 | myDiffuseColorGreen = 0.3; |
36 | myDiffuseColorBlue = 0.5; |
37 | myTransparency = 0.0; |
38 | myAmbientIntensity = 0.3; |
39 | mySpecularColorRed = 0.7; |
40 | mySpecularColorGreen = 0.7; |
41 | mySpecularColorBlue = 0.8; |
42 | myShininess = 0.1; |
43 | myTexture = " [] " ; |
44 | myCreaseAngle = 1.57; |
45 | myDeflection = 0.005; |
46 | } |
47 | |
7da5f7b1 |
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 ; } |
7fd59977 |
63 | |
64 | //======================================================================= |
65 | // function : ToVRML::Write |
66 | // purpose : conversion of a Shape into VRML format for 3d visualisation |
67 | //======================================================================= |
7da5f7b1 |
68 | Standard_Boolean XSDRAWSTLVRML_ToVRML::Write |
ceb418e1 |
69 | (const TopoDS_Shape& theShape, const Standard_CString theFileName) const |
7fd59977 |
70 | { |
ceb418e1 |
71 | filebuf aFile; |
72 | ostream anOut(&aFile); |
73 | |
74 | if ( aFile.open(theFileName,ios::out) ) |
fc9b36d6 |
75 | { |
ceb418e1 |
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; |
85 | |
fcf15f5c |
86 | if ( !BRepTools::Triangulation (theShape, myDeflection) ) |
87 | { |
88 | // retrieve meshing tool from Factory |
89 | BRepTools::Clean (theShape); |
90 | Handle(BRepMesh_DiscretRoot) aMeshAlgo = |
91 | BRepMesh_DiscretFactory::Get().Discret(theShape, myDeflection, 0.17); |
ceb418e1 |
92 | |
fcf15f5c |
93 | if ( !aMeshAlgo.IsNull() ) |
ceb418e1 |
94 | { |
fcf15f5c |
95 | aMeshAlgo->Perform(); |
ceb418e1 |
96 | } |
fcf15f5c |
97 | } |
ceb418e1 |
98 | |
fcf15f5c |
99 | Bnd_Box aBox; |
100 | BRepBndLib::Add(theShape, aBox); |
ceb418e1 |
101 | |
fcf15f5c |
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; |
110 | |
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; |
130 | |
131 | anOut << " geometry IndexedFaceSet {" << endl; |
132 | anOut << " coord Coordinate {" << endl; |
133 | anOut << " point [" << endl; |
134 | |
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()) |
139 | { |
140 | const TopoDS_Face& aFace = TopoDS::Face(aFaceIt.Current()); |
141 | |
142 | TopLoc_Location aLoc = aFace.Location(); |
143 | Handle(Poly_Triangulation) aTriangulation = |
144 | BRep_Tool::Triangulation(aFace, aLoc); |
145 | |
146 | const Standard_Integer aLength = aTriangulation->NbNodes(); |
147 | const TColgp_Array1OfPnt& aNodes = aTriangulation->Nodes(); |
148 | for ( Standard_Integer i = 1; i <= aLength; ++i ) |
ceb418e1 |
149 | { |
fcf15f5c |
150 | const gp_Pnt& aPoint = aNodes(i); |
ceb418e1 |
151 | |
fcf15f5c |
152 | anOut << " " |
153 | << aPoint.X() << " " |
154 | << aPoint.Y() << " " |
155 | << aPoint.Z() << "," << endl; |
156 | } |
157 | } |
ceb418e1 |
158 | |
fcf15f5c |
159 | anOut << " ]" << endl; |
160 | anOut << " }" << endl; |
161 | anOut << " coordIndex [" << endl; |
ceb418e1 |
162 | |
fcf15f5c |
163 | // Retrieves all the triangles in order to draw the facets |
164 | for (aFaceIt.Init(theShape, TopAbs_FACE); aFaceIt.More(); aFaceIt.Next()) |
165 | { |
166 | TopoDS_Face aFace = TopoDS::Face(aFaceIt.Current()); |
167 | aFace.Orientation(TopAbs_FORWARD); |
ceb418e1 |
168 | |
fcf15f5c |
169 | TopLoc_Location aLoc = aFace.Location(); |
170 | Handle(Poly_Triangulation) aTriangulation = |
171 | BRep_Tool::Triangulation(aFace, aLoc); |
ceb418e1 |
172 | |
fcf15f5c |
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 ) |
176 | { |
177 | aTriangles(i).Get(v[0], v[1], v[2]); |
ceb418e1 |
178 | |
fcf15f5c |
179 | anOut << " " |
180 | << v[0] - 1 << ", " |
181 | << v[1] - 1 << ", " |
182 | << v[2] - 1 << ", -1," << endl; |
183 | } |
184 | } |
185 | |
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; |
fc9b36d6 |
193 | } |
ceb418e1 |
194 | else |
fc9b36d6 |
195 | { |
ceb418e1 |
196 | // Failure when opening file |
197 | return Standard_False; |
fc9b36d6 |
198 | } |
7fd59977 |
199 | |
ceb418e1 |
200 | aFile.close(); |
7fd59977 |
201 | return Standard_True; |
202 | } |
203 | |