Version: 6.3.1
Public Types | Public Member Functions | Protected Attributes | Private Member Functions | Private Attributes

DriverSTL_R_SMDS_Mesh Class Reference

#include <DriverSTL_R_SMDS_Mesh.h>

Inheritance diagram for DriverSTL_R_SMDS_Mesh:
Inheritance graph
[legend]

Public Types

enum  Status {
  DRS_OK, DRS_EMPTY, DRS_WARN_RENUMBER, DRS_WARN_SKIP_ELEM,
  DRS_FAIL
}

Public Member Functions

 DriverSTL_R_SMDS_Mesh ()
virtual Status Perform ()
void SetIsCreateFaces (const bool theIsCreate=true)
void SetMesh (SMDS_Mesh *theMesh)
void SetMeshId (int theMeshId)
void SetFile (const std::string &theFileName)

Protected Attributes

SMDS_MeshmyMesh
std::string myFile
int myMeshId

Private Member Functions

Status readAscii () const
Status readBinary () const

Private Attributes

bool myIsCreateFaces
bool myIsAscii

Detailed Description

Definition at line 32 of file DriverSTL_R_SMDS_Mesh.h.


Member Enumeration Documentation

enum Driver_Mesh::Status [inherited]
Enumerator:
DRS_OK 
DRS_EMPTY 
DRS_WARN_RENUMBER 
DRS_WARN_SKIP_ELEM 
DRS_FAIL 

Definition at line 48 of file Driver_Mesh.h.

              {
    DRS_OK,
    DRS_EMPTY,          // a file contains no mesh with the given name
    DRS_WARN_RENUMBER,  // a file has overlapped ranges of element numbers,
                        // so the numbers from the file are ignored
    DRS_WARN_SKIP_ELEM, // some elements were skipped due to incorrect file data
    DRS_FAIL            // general failure (exception etc.)
  };

Constructor & Destructor Documentation

DriverSTL_R_SMDS_Mesh::DriverSTL_R_SMDS_Mesh ( )

Definition at line 86 of file DriverSTL_R_SMDS_Mesh.cxx.

References myIsAscii, and myIsCreateFaces.

{
  myIsCreateFaces = true;
  myIsAscii = Standard_True;
}

Member Function Documentation

Driver_Mesh::Status DriverSTL_R_SMDS_Mesh::Perform ( ) [virtual]

Implements Driver_Mesh.

Definition at line 105 of file DriverSTL_R_SMDS_Mesh.cxx.

References SMESH_test5.aFileName, SMESH_test5.aPath, Driver_Mesh.DRS_FAIL, Driver_Mesh.DRS_OK, Driver_Mesh.myFile, myIsAscii, Driver_SMDS_Mesh.myMesh, readAscii(), and readBinary().

Referenced by SMESH_Mesh.STLToMesh().

{
  Status aResult = DRS_OK;

  TCollection_AsciiString aFileName( (char *)myFile.c_str() );
  if ( aFileName.IsEmpty() ) {
    fprintf(stderr, ">> ERREOR : invalid file name \n");
    return DRS_FAIL;
  }

  filebuf fic;
  Standard_IStream is(&fic);
  if (!fic.open(aFileName.ToCString(),ios::in)) {
    fprintf(stderr, ">> ERROR : cannot open file %s \n", aFileName.ToCString());
    return DRS_FAIL;
  }


  OSD_Path aPath( aFileName );
  OSD_File file = OSD_File( aPath );
  file.Open(OSD_ReadOnly,OSD_Protection(OSD_RWD,OSD_RWD,OSD_RWD,OSD_RWD));
  unsigned char str[128];
  Standard_Integer lread,i;
  Standard_Address ach;
  ach = (Standard_Address)str;
  // we skip the header which is in Ascii for both modes
  file.Read(ach,HEADER_SIZE,lread);

  // we read 128 characters to detect if we have a non-ascii char
  file.Read(ach,sizeof(str),lread);
  
  myIsAscii = Standard_True;
  for (i = 0; i < lread; ++i) {
    if (str[i] > '~') {
      myIsAscii = Standard_False;
      break;
    }
  }
      
  file.Close();

  if ( !myMesh ) {
    fprintf(stderr, ">> ERREOR : cannot create mesh \n");
    return DRS_FAIL;
  }

  if ( myIsAscii )
    aResult = readAscii();
  else
    aResult = readBinary();

  return aResult;
}
Driver_Mesh::Status DriverSTL_R_SMDS_Mesh::readAscii ( ) const [private]

Definition at line 226 of file DriverSTL_R_SMDS_Mesh.cxx.

References SMDS_Mesh.AddFace(), SMESH_test5.aFileName, ASCII_LINES_PER_FACET, Driver_Mesh.DRS_OK, Driver_Mesh.myFile, myIsCreateFaces, Driver_SMDS_Mesh.myMesh, and readNode().

Referenced by Perform().

{
  Status aResult = DRS_OK;
  long ipos;
  Standard_Integer nbLines = 0;

  // Open the file 
  TCollection_AsciiString aFileName( (char *)myFile.c_str() );
  FILE* file = fopen(aFileName.ToCString(),"r");
  fseek(file,0L,SEEK_END);
  // get the file size
  long filesize = ftell(file);
  fclose(file);
  file = fopen(aFileName.ToCString(),"r");
  
  // count the number of lines
  for (ipos = 0; ipos < filesize; ++ipos) {
    if (getc(file) == '\n')
      nbLines++;
  }

  // go back to the beginning of the file
//  fclose(file);
//  file = fopen(aFileName.ToCString(),"r");
  rewind(file);
  
  Standard_Integer nbTri = (nbLines / ASCII_LINES_PER_FACET);

  DriverSTL_DataMapOfPntNodePtr uniqnodes;
  // skip header
  while (getc(file) != '\n');

  // main reading
  for (Standard_Integer iTri = 0; iTri < nbTri; ++iTri) {

    // skipping the facet normal
    Standard_ShortReal normal[3];
    fscanf(file,"%*s %*s %f %f %f\n",&normal[0],&normal[1],&normal[2]);

    // skip the keywords "outer loop"
    fscanf(file,"%*s %*s");

    // reading nodes
    SMDS_MeshNode* node1 = readNode( file, uniqnodes, myMesh );
    SMDS_MeshNode* node2 = readNode( file, uniqnodes, myMesh );
    SMDS_MeshNode* node3 = readNode( file, uniqnodes, myMesh );

    if (myIsCreateFaces)
      myMesh->AddFace(node1,node2,node3);

    // skip the keywords "endloop"
    fscanf(file,"%*s");

    // skip the keywords "endfacet"
    fscanf(file,"%*s");
  }

  fclose(file);
  return aResult;
}
Driver_Mesh::Status DriverSTL_R_SMDS_Mesh::readBinary ( ) const [private]

Definition at line 292 of file DriverSTL_R_SMDS_Mesh.cxx.

References SMDS_Mesh.AddFace(), SMESH_test5.aFileName, Driver_Mesh.DRS_OK, HEADER_SIZE, Driver_Mesh.myFile, myIsCreateFaces, Driver_SMDS_Mesh.myMesh, readFloat(), and readNode().

Referenced by Perform().

{
  Status aResult = DRS_OK;

  char buftest[5];
  Standard_Address adr;
  adr = (Standard_Address)buftest;

  TCollection_AsciiString aFileName( (char *)myFile.c_str() );
  OSD_File aFile = OSD_File(OSD_Path( aFileName ));
  aFile.Open(OSD_ReadOnly,OSD_Protection(OSD_RWD,OSD_RWD,OSD_RWD,OSD_RWD));

  // the size of the file (minus the header size)
  // must be a multiple of SIZEOF_STL_FACET

  // compute file size
  Standard_Integer filesize = aFile.Size();

  if ( (filesize - HEADER_SIZE) % SIZEOF_STL_FACET !=0 
      // Commented to allow reading small files (ex: 1 face)
      /*|| (filesize < STL_MIN_FILE_SIZE)*/) {
    Standard_NoMoreObject::Raise("DriverSTL_R_SMDS_MESH::readBinary (wrong file size)");
  }

  // don't trust the number of triangles which is coded in the file
  // sometimes it is wrong, and with this technique we don't need to swap endians for integer
  Standard_Integer nbTri = ((filesize - HEADER_SIZE) / SIZEOF_STL_FACET);

  // skip the header
  aFile.Seek(HEADER_SIZE,OSD_FromBeginning);

  DriverSTL_DataMapOfPntNodePtr uniqnodes;
  Standard_Integer lread;
  
  for (Standard_Integer iTri = 0; iTri < nbTri; ++iTri) {

    // ignore normals
    readFloat(aFile);
    readFloat(aFile);
    readFloat(aFile);

    // read vertices
    SMDS_MeshNode* node1 = readNode( aFile, uniqnodes, myMesh );
    SMDS_MeshNode* node2 = readNode( aFile, uniqnodes, myMesh );
    SMDS_MeshNode* node3 = readNode( aFile, uniqnodes, myMesh );

    if (myIsCreateFaces)
      myMesh->AddFace(node1,node2,node3);

    // skip extra bytes
    aFile.Read(adr,2,lread);
  }
  aFile.Close();
  return aResult;
}
void Driver_Mesh::SetFile ( const std::string &  theFileName) [inherited]
void DriverSTL_R_SMDS_Mesh::SetIsCreateFaces ( const bool  theIsCreate = true)

Definition at line 97 of file DriverSTL_R_SMDS_Mesh.cxx.

References myIsCreateFaces.

{ myIsCreateFaces = theIsCreate; }
void Driver_SMDS_Mesh::SetMesh ( SMDS_Mesh theMesh) [inherited]
void Driver_Mesh::SetMeshId ( int  theMeshId) [inherited]

Field Documentation

std::string Driver_Mesh.myFile [protected, inherited]

Definition at line 47 of file DriverSTL_R_SMDS_Mesh.h.

Referenced by DriverSTL_R_SMDS_Mesh(), and Perform().

SMDS_Mesh* Driver_SMDS_Mesh.myMesh [protected, inherited]
int Driver_Mesh.myMeshId [protected, inherited]

Definition at line 63 of file Driver_Mesh.h.

Referenced by DriverMED_W_SMESHDS_Mesh.Perform(), and Driver_Mesh.SetMeshId().

Copyright © 2007-2011 CEA/DEN, EDF R&D, OPEN CASCADE
Copyright © 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS