1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
24 #include "DriverSTL_W_SMDS_Mesh.h"
26 #include "SMDS_Mesh.hxx"
27 #include "SMDS_MeshElement.hxx"
28 #include "SMDS_MeshNode.hxx"
30 #include <OSD_Path.hxx>
31 #include <OSD_File.hxx>
32 #include <OSD_FromWhere.hxx>
33 #include <OSD_Protection.hxx>
34 #include <OSD_SingleProtection.hxx>
35 #include <TCollection_AsciiString.hxx>
36 #include <TColgp_Array1OfXYZ.hxx>
38 #include "utilities.h"
40 //using namespace std;
42 // definition des constantes
43 static const int LABEL_SIZE = 80;
45 DriverSTL_W_SMDS_Mesh::DriverSTL_W_SMDS_Mesh()
50 void DriverSTL_W_SMDS_Mesh::SetIsAscii( const bool theIsAscii )
52 myIsAscii = theIsAscii;
55 Driver_Mesh::Status DriverSTL_W_SMDS_Mesh::Perform()
57 Status aResult = DRS_OK;
60 fprintf(stderr, ">> ERROR : Mesh is null \n");
64 aResult = writeAscii();
66 aResult = writeBinary();
73 static void writeInteger( const Standard_Integer& theVal,
83 Standard_Integer entier;
84 entier = u.c[0] & 0xFF;
85 entier |= (u.c[1] & 0xFF) << 0x08;
86 entier |= (u.c[2] & 0xFF) << 0x10;
87 entier |= (u.c[3] & 0xFF) << 0x18;
89 ofile.Write((char *)&entier,sizeof(u.c));
92 static void writeFloat ( const Standard_ShortReal& theVal,
102 Standard_Integer entier;
104 entier = u.c[0] & 0xFF;
105 entier |= (u.c[1] & 0xFF) << 0x08;
106 entier |= (u.c[2] & 0xFF) << 0x10;
107 entier |= (u.c[3] & 0xFF) << 0x18;
109 ofile.Write((char *)&entier,sizeof(u.c));
112 static gp_XYZ getNormale( const SMDS_MeshFace* theFace )
115 int aNbNode = theFace->NbNodes();
116 TColgp_Array1OfXYZ anArrOfXYZ(1,4);
117 gp_XYZ p1, p2, p3, p4;
118 SMDS_ElemIteratorPtr aNodeItr = theFace->nodesIterator();
120 for ( ; aNodeItr->more() && i <= 4; i++ )
122 SMDS_MeshNode* aNode = (SMDS_MeshNode*)aNodeItr->next();
123 anArrOfXYZ.SetValue(i, gp_XYZ( aNode->X(), aNode->Y(), aNode->Z() ) );
126 gp_XYZ q1 = anArrOfXYZ.Value(2) - anArrOfXYZ.Value(1);
127 gp_XYZ q2 = anArrOfXYZ.Value(3) - anArrOfXYZ.Value(1);
131 gp_XYZ q3 = anArrOfXYZ.Value(4) - anArrOfXYZ.Value(1);
134 double len = n.Modulus();
143 Driver_Mesh::Status DriverSTL_W_SMDS_Mesh::writeAscii() const
145 Status aResult = DRS_OK;
146 TCollection_AsciiString aFileName( (char *)myFile.c_str() );
147 if ( aFileName.IsEmpty() ) {
148 fprintf(stderr, ">> ERREOR : invalid file name \n");
152 OSD_File aFile = OSD_File(OSD_Path(aFileName));
153 aFile.Build(OSD_WriteOnly,OSD_Protection());
154 TCollection_AsciiString buf = TCollection_AsciiString ("solid\n");
155 aFile.Write (buf,buf.Length());buf.Clear();
158 SMDS_FaceIteratorPtr itFaces = myMesh->facesIterator();
160 for (; itFaces->more() ;) {
161 SMDS_MeshFace* aFace = (SMDS_MeshFace*)itFaces->next();
163 if (aFace->NbNodes() == 3) {
164 gp_XYZ normale = getNormale( aFace );
166 buf += " facet normal ";
167 sprintf (sval,"% 12e",normale.X());
170 sprintf (sval,"% 12e",normale.Y());
173 sprintf (sval,"% 12e",normale.Z());
176 aFile.Write (buf,buf.Length());buf.Clear();
177 buf += " outer loop\n";
178 aFile.Write (buf,buf.Length());buf.Clear();
180 SMDS_ElemIteratorPtr aNodeIter = aFace->nodesIterator();
181 for (; aNodeIter->more(); ) {
182 SMDS_MeshNode* node = (SMDS_MeshNode*)aNodeIter->next();
184 sprintf (sval,"% 12e",node->X());
187 sprintf (sval,"% 12e",node->Y());
190 sprintf (sval,"% 12e",node->Z());
193 aFile.Write (buf,buf.Length());buf.Clear();
196 aFile.Write (buf,buf.Length());buf.Clear();
197 buf += " endfacet\n";
198 aFile.Write (buf,buf.Length());buf.Clear();
202 aFile.Write (buf,buf.Length());buf.Clear();
209 Driver_Mesh::Status DriverSTL_W_SMDS_Mesh::writeBinary() const
211 Status aResult = DRS_OK;
212 TCollection_AsciiString aFileName( (char *)myFile.c_str() );
213 if ( aFileName.IsEmpty() ) {
214 fprintf(stderr, ">> ERREOR : invalid filename \n");
218 OSD_File aFile = OSD_File(OSD_Path(aFileName));
219 aFile.Build(OSD_WriteOnly,OSD_Protection());
222 Standard_Integer nbTri = 0;
223 SMDS_FaceIteratorPtr itFaces = myMesh->facesIterator();
225 // we first count the number of triangles
226 for (;itFaces->more();) {
227 SMDS_MeshFace* aFace = (SMDS_MeshFace*)itFaces->next();
228 if (aFace->NbNodes() == 3)
232 // write number of triangles
233 //unsigned int NBT = nbTri;
234 aFile.Write((Standard_Address)sval,LABEL_SIZE);
235 writeInteger(nbTri,aFile);
237 // loop writing nodes. take face iterator again
239 itFaces = myMesh->facesIterator();
241 for (;itFaces->more();) {
242 SMDS_MeshFace* aFace = (SMDS_MeshFace*)itFaces->next();
244 if (aFace->NbNodes() == 3) {
245 gp_XYZ aNorm = getNormale( aFace );
246 writeFloat(aNorm.X(),aFile);
247 writeFloat(aNorm.Y(),aFile);
248 writeFloat(aNorm.Z(),aFile);
250 SMDS_ElemIteratorPtr aNodeIter = aFace->nodesIterator();
251 for (; aNodeIter->more(); ) {
252 SMDS_MeshNode* node = (SMDS_MeshNode*)aNodeIter->next();
253 writeFloat(node->X(),aFile);
254 writeFloat(node->Y(),aFile);
255 writeFloat(node->Z(),aFile);
257 aFile.Write (&dum,2);