1 // Copyright (C) 2007-2010 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
25 #include "DriverSTL_W_SMDS_Mesh.h"
27 #include "SMDS_Mesh.hxx"
28 #include "SMDS_MeshElement.hxx"
29 #include "SMDS_MeshNode.hxx"
31 #include <OSD_Path.hxx>
32 #include <OSD_File.hxx>
33 #include <OSD_FromWhere.hxx>
34 #include <OSD_Protection.hxx>
35 #include <OSD_SingleProtection.hxx>
36 #include <TCollection_AsciiString.hxx>
37 #include <TColgp_Array1OfXYZ.hxx>
39 #include "utilities.h"
41 //using namespace std;
43 // definition des constantes
44 static const int LABEL_SIZE = 80;
46 DriverSTL_W_SMDS_Mesh::DriverSTL_W_SMDS_Mesh()
51 void DriverSTL_W_SMDS_Mesh::SetIsAscii( const bool theIsAscii )
53 myIsAscii = theIsAscii;
56 Driver_Mesh::Status DriverSTL_W_SMDS_Mesh::Perform()
58 Status aResult = DRS_OK;
61 fprintf(stderr, ">> ERROR : Mesh is null \n");
65 aResult = writeAscii();
67 aResult = writeBinary();
74 static void writeInteger( const Standard_Integer& theVal,
84 Standard_Integer entier;
85 entier = u.c[0] & 0xFF;
86 entier |= (u.c[1] & 0xFF) << 0x08;
87 entier |= (u.c[2] & 0xFF) << 0x10;
88 entier |= (u.c[3] & 0xFF) << 0x18;
90 ofile.Write((char *)&entier,sizeof(u.c));
93 static void writeFloat ( const Standard_ShortReal& theVal,
103 Standard_Integer entier;
105 entier = u.c[0] & 0xFF;
106 entier |= (u.c[1] & 0xFF) << 0x08;
107 entier |= (u.c[2] & 0xFF) << 0x10;
108 entier |= (u.c[3] & 0xFF) << 0x18;
110 ofile.Write((char *)&entier,sizeof(u.c));
113 static gp_XYZ getNormale( const SMDS_MeshFace* theFace )
116 int aNbNode = theFace->NbNodes();
117 TColgp_Array1OfXYZ anArrOfXYZ(1,4);
118 gp_XYZ p1, p2, p3, p4;
119 SMDS_ElemIteratorPtr aNodeItr = theFace->nodesIterator();
121 for ( ; aNodeItr->more() && i <= 4; i++ )
123 SMDS_MeshNode* aNode = (SMDS_MeshNode*)aNodeItr->next();
124 anArrOfXYZ.SetValue(i, gp_XYZ( aNode->X(), aNode->Y(), aNode->Z() ) );
127 gp_XYZ q1 = anArrOfXYZ.Value(2) - anArrOfXYZ.Value(1);
128 gp_XYZ q2 = anArrOfXYZ.Value(3) - anArrOfXYZ.Value(1);
132 gp_XYZ q3 = anArrOfXYZ.Value(4) - anArrOfXYZ.Value(1);
135 double len = n.Modulus();
144 Driver_Mesh::Status DriverSTL_W_SMDS_Mesh::writeAscii() const
146 Status aResult = DRS_OK;
147 TCollection_AsciiString aFileName( (char *)myFile.c_str() );
148 if ( aFileName.IsEmpty() ) {
149 fprintf(stderr, ">> ERREOR : invalid file name \n");
153 OSD_File aFile = OSD_File(OSD_Path(aFileName));
154 aFile.Build(OSD_WriteOnly,OSD_Protection());
155 TCollection_AsciiString buf = TCollection_AsciiString ("solid\n");
156 aFile.Write (buf,buf.Length());buf.Clear();
159 SMDS_FaceIteratorPtr itFaces = myMesh->facesIterator();
161 for (; itFaces->more() ;) {
162 SMDS_MeshFace* aFace = (SMDS_MeshFace*)itFaces->next();
164 if (aFace->NbNodes() == 3) {
165 gp_XYZ normale = getNormale( aFace );
167 buf += " facet normal ";
168 sprintf (sval,"% 12e",normale.X());
171 sprintf (sval,"% 12e",normale.Y());
174 sprintf (sval,"% 12e",normale.Z());
177 aFile.Write (buf,buf.Length());buf.Clear();
178 buf += " outer loop\n";
179 aFile.Write (buf,buf.Length());buf.Clear();
181 SMDS_ElemIteratorPtr aNodeIter = aFace->nodesIterator();
182 for (; aNodeIter->more(); ) {
183 SMDS_MeshNode* node = (SMDS_MeshNode*)aNodeIter->next();
185 sprintf (sval,"% 12e",node->X());
188 sprintf (sval,"% 12e",node->Y());
191 sprintf (sval,"% 12e",node->Z());
194 aFile.Write (buf,buf.Length());buf.Clear();
197 aFile.Write (buf,buf.Length());buf.Clear();
198 buf += " endfacet\n";
199 aFile.Write (buf,buf.Length());buf.Clear();
203 aFile.Write (buf,buf.Length());buf.Clear();
210 Driver_Mesh::Status DriverSTL_W_SMDS_Mesh::writeBinary() const
212 Status aResult = DRS_OK;
213 TCollection_AsciiString aFileName( (char *)myFile.c_str() );
214 if ( aFileName.IsEmpty() ) {
215 fprintf(stderr, ">> ERREOR : invalid filename \n");
219 OSD_File aFile = OSD_File(OSD_Path(aFileName));
220 aFile.Build(OSD_WriteOnly,OSD_Protection());
223 Standard_Integer nbTri = 0;
224 SMDS_FaceIteratorPtr itFaces = myMesh->facesIterator();
226 // we first count the number of triangles
227 for (;itFaces->more();) {
228 SMDS_MeshFace* aFace = (SMDS_MeshFace*)itFaces->next();
229 if (aFace->NbNodes() == 3)
233 // write number of triangles
234 //unsigned int NBT = nbTri;
235 aFile.Write((Standard_Address)sval,LABEL_SIZE);
236 writeInteger(nbTri,aFile);
238 // loop writing nodes. take face iterator again
240 itFaces = myMesh->facesIterator();
242 for (;itFaces->more();) {
243 SMDS_MeshFace* aFace = (SMDS_MeshFace*)itFaces->next();
245 if (aFace->NbNodes() == 3) {
246 gp_XYZ aNorm = getNormale( aFace );
247 writeFloat(aNorm.X(),aFile);
248 writeFloat(aNorm.Y(),aFile);
249 writeFloat(aNorm.Z(),aFile);
251 SMDS_ElemIteratorPtr aNodeIter = aFace->nodesIterator();
252 for (; aNodeIter->more(); ) {
253 SMDS_MeshNode* node = (SMDS_MeshNode*)aNodeIter->next();
254 writeFloat(node->X(),aFile);
255 writeFloat(node->Y(),aFile);
256 writeFloat(node->Z(),aFile);
258 aFile.Write (&dum,2);