PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/src/contrib/smesh-5.1.2.2/src/DriverSTL/DriverSTL_W_SMDS_Mesh.cpp

http://pythonocc.googlecode.com/
C++ | 263 lines | 191 code | 43 blank | 29 comment | 19 complexity | b686b75c3e73ea4f22af572074a1e03f MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1, Apache-2.0
  1. // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
  2. //
  3. // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
  4. // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
  5. //
  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.
  10. //
  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.
  15. //
  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
  19. //
  20. // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
  21. //
  22. #include <stdio.h>
  23. #include "DriverSTL_W_SMDS_Mesh.h"
  24. #include "SMDS_Mesh.hxx"
  25. #include "SMDS_MeshElement.hxx"
  26. #include "SMDS_MeshNode.hxx"
  27. #include <gp_XYZ.hxx>
  28. #include <OSD_Path.hxx>
  29. #include <OSD_File.hxx>
  30. #include <OSD_FromWhere.hxx>
  31. #include <OSD_Protection.hxx>
  32. #include <OSD_SingleProtection.hxx>
  33. #include <TCollection_AsciiString.hxx>
  34. #include <TColgp_Array1OfXYZ.hxx>
  35. #include "utilities.h"
  36. //using namespace std;
  37. // definition des constantes
  38. static const int LABEL_SIZE = 80;
  39. DriverSTL_W_SMDS_Mesh::DriverSTL_W_SMDS_Mesh()
  40. {
  41. myIsAscii = false;
  42. }
  43. void DriverSTL_W_SMDS_Mesh::SetIsAscii( const bool theIsAscii )
  44. {
  45. myIsAscii = theIsAscii;
  46. }
  47. Driver_Mesh::Status DriverSTL_W_SMDS_Mesh::Perform()
  48. {
  49. Status aResult = DRS_OK;
  50. if ( !myMesh ) {
  51. fprintf(stderr, ">> ERROR : Mesh is null \n");
  52. return DRS_FAIL;
  53. }
  54. if ( myIsAscii )
  55. aResult = writeAscii();
  56. else
  57. aResult = writeBinary();
  58. return aResult;
  59. }
  60. // static methods
  61. static void writeInteger( const Standard_Integer& theVal,
  62. OSD_File& ofile )
  63. {
  64. union {
  65. Standard_Integer i;
  66. char c[4];
  67. }u;
  68. u.i = theVal;
  69. Standard_Integer entier;
  70. entier = u.c[0] & 0xFF;
  71. entier |= (u.c[1] & 0xFF) << 0x08;
  72. entier |= (u.c[2] & 0xFF) << 0x10;
  73. entier |= (u.c[3] & 0xFF) << 0x18;
  74. ofile.Write((char *)&entier,sizeof(u.c));
  75. }
  76. static void writeFloat ( const Standard_ShortReal& theVal,
  77. OSD_File& ofile)
  78. {
  79. union {
  80. Standard_ShortReal f;
  81. char c[4];
  82. }u;
  83. u.f = 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;
  89. ofile.Write((char *)&entier,sizeof(u.c));
  90. }
  91. static gp_XYZ getNormale( const SMDS_MeshFace* theFace )
  92. {
  93. gp_XYZ n;
  94. int aNbNode = theFace->NbNodes();
  95. TColgp_Array1OfXYZ anArrOfXYZ(1,4);
  96. gp_XYZ p1, p2, p3, p4;
  97. SMDS_ElemIteratorPtr aNodeItr = theFace->nodesIterator();
  98. int i = 1;
  99. for ( ; aNodeItr->more() && i <= 4; i++ )
  100. {
  101. SMDS_MeshNode* aNode = (SMDS_MeshNode*)aNodeItr->next();
  102. anArrOfXYZ.SetValue(i, gp_XYZ( aNode->X(), aNode->Y(), aNode->Z() ) );
  103. }
  104. gp_XYZ q1 = anArrOfXYZ.Value(2) - anArrOfXYZ.Value(1);
  105. gp_XYZ q2 = anArrOfXYZ.Value(3) - anArrOfXYZ.Value(1);
  106. n = q1 ^ q2;
  107. if ( aNbNode > 3 )
  108. {
  109. gp_XYZ q3 = anArrOfXYZ.Value(4) - anArrOfXYZ.Value(1);
  110. n += q2 ^ q3;
  111. }
  112. double len = n.Modulus();
  113. if ( len > 0 )
  114. n /= len;
  115. return n;
  116. }
  117. // private methods
  118. Driver_Mesh::Status DriverSTL_W_SMDS_Mesh::writeAscii() const
  119. {
  120. Status aResult = DRS_OK;
  121. TCollection_AsciiString aFileName( (char *)myFile.c_str() );
  122. if ( aFileName.IsEmpty() ) {
  123. fprintf(stderr, ">> ERREOR : invalid file name \n");
  124. return DRS_FAIL;
  125. }
  126. OSD_File aFile = OSD_File(OSD_Path(aFileName));
  127. aFile.Build(OSD_WriteOnly,OSD_Protection());
  128. TCollection_AsciiString buf = TCollection_AsciiString ("solid\n");
  129. aFile.Write (buf,buf.Length());buf.Clear();
  130. char sval[16];
  131. SMDS_FaceIteratorPtr itFaces = myMesh->facesIterator();
  132. for (; itFaces->more() ;) {
  133. SMDS_MeshFace* aFace = (SMDS_MeshFace*)itFaces->next();
  134. if (aFace->NbNodes() == 3) {
  135. gp_XYZ normale = getNormale( aFace );
  136. buf += " facet normal ";
  137. sprintf (sval,"% 12e",normale.X());
  138. buf += sval;
  139. buf += " ";
  140. sprintf (sval,"% 12e",normale.Y());
  141. buf += sval;
  142. buf += " ";
  143. sprintf (sval,"% 12e",normale.Z());
  144. buf += sval;
  145. buf += '\n';
  146. aFile.Write (buf,buf.Length());buf.Clear();
  147. buf += " outer loop\n";
  148. aFile.Write (buf,buf.Length());buf.Clear();
  149. SMDS_ElemIteratorPtr aNodeIter = aFace->nodesIterator();
  150. for (; aNodeIter->more(); ) {
  151. SMDS_MeshNode* node = (SMDS_MeshNode*)aNodeIter->next();
  152. buf += " vertex ";
  153. sprintf (sval,"% 12e",node->X());
  154. buf += sval;
  155. buf += " ";
  156. sprintf (sval,"% 12e",node->Y());
  157. buf += sval;
  158. buf += " ";
  159. sprintf (sval,"% 12e",node->Z());
  160. buf += sval;
  161. buf += '\n';
  162. aFile.Write (buf,buf.Length());buf.Clear();
  163. }
  164. buf += " endloop\n";
  165. aFile.Write (buf,buf.Length());buf.Clear();
  166. buf += " endfacet\n";
  167. aFile.Write (buf,buf.Length());buf.Clear();
  168. }
  169. }
  170. buf += "endsolid\n";
  171. aFile.Write (buf,buf.Length());buf.Clear();
  172. aFile.Close ();
  173. return aResult;
  174. }
  175. Driver_Mesh::Status DriverSTL_W_SMDS_Mesh::writeBinary() const
  176. {
  177. Status aResult = DRS_OK;
  178. TCollection_AsciiString aFileName( (char *)myFile.c_str() );
  179. if ( aFileName.IsEmpty() ) {
  180. fprintf(stderr, ">> ERREOR : invalid filename \n");
  181. return DRS_FAIL;
  182. }
  183. OSD_File aFile = OSD_File(OSD_Path(aFileName));
  184. aFile.Build(OSD_WriteOnly,OSD_Protection());
  185. char sval[80];
  186. Standard_Integer nbTri = 0;
  187. SMDS_FaceIteratorPtr itFaces = myMesh->facesIterator();
  188. // we first count the number of triangles
  189. for (;itFaces->more();) {
  190. SMDS_MeshFace* aFace = (SMDS_MeshFace*)itFaces->next();
  191. if (aFace->NbNodes() == 3)
  192. nbTri++;
  193. }
  194. // write number of triangles
  195. //unsigned int NBT = nbTri;
  196. aFile.Write((Standard_Address)sval,LABEL_SIZE);
  197. writeInteger(nbTri,aFile);
  198. // loop writing nodes. take face iterator again
  199. int dum=0;
  200. itFaces = myMesh->facesIterator();
  201. for (;itFaces->more();) {
  202. SMDS_MeshFace* aFace = (SMDS_MeshFace*)itFaces->next();
  203. if (aFace->NbNodes() == 3) {
  204. gp_XYZ aNorm = getNormale( aFace );
  205. writeFloat(aNorm.X(),aFile);
  206. writeFloat(aNorm.Y(),aFile);
  207. writeFloat(aNorm.Z(),aFile);
  208. SMDS_ElemIteratorPtr aNodeIter = aFace->nodesIterator();
  209. for (; aNodeIter->more(); ) {
  210. SMDS_MeshNode* node = (SMDS_MeshNode*)aNodeIter->next();
  211. writeFloat(node->X(),aFile);
  212. writeFloat(node->Y(),aFile);
  213. writeFloat(node->Z(),aFile);
  214. }
  215. aFile.Write (&dum,2);
  216. }
  217. }
  218. aFile.Close ();
  219. return aResult;
  220. }