AnimatLab  2
Test
VsMeshMgr.cpp
1 
7 #include "StdAfx.h"
8 #include "VsMeshMgr.h"
9 
10 namespace VortexAnimatSim
11 {
12 
20 {
21 }
22 
30 {
31  try
32  {
33  m_aryMeshes.RemoveAll();
34  }
35  catch(...)
36  {Std_TraceMsg(0, "Caught Error in desctructor of VsMeshMgr\r\n", "", -1, false, true);}
37 }
38 
39 std::string VsMeshMgr::FileCreateTime(std::string strFilename)
40 {
41  std::stringstream ss;
42  WIN32_FILE_ATTRIBUTE_DATA wfad;
43  SYSTEMTIME st;
44 
45  GetFileAttributesEx(strFilename.c_str(), GetFileExInfoStandard, &wfad);
46  FileTimeToSystemTime(&wfad.ftLastWriteTime, &st);
47 
48  ss << st.wMonth << '/' << st.wDay << '/' << st.wYear << " " << st.wHour << ":" << st.wMinute << ":" << st.wSecond;
49 
50  return ss.str();
51 }
52 
53 osg::Node *VsMeshMgr::LoadMesh(std::string strFilename)
54 {
55  std::pair<std::string, osg::ref_ptr<osg::Node>> MeshPair;
56 
57  //If not found then load it.
58  if(!FindMesh(strFilename, MeshPair, false))
59  return AddMesh(strFilename);
60  else
61  {
62  std::string strCreateDate = FileCreateTime(strFilename);
63 
64  //If the create dates match then use this one.
65  //If they do not then lets reload it.
66  if(strCreateDate == MeshPair.first)
67  return MeshPair.second.get();
68  else
69  {
70  ReleaseMesh(strFilename);
71  return AddMesh(strFilename);
72  }
73  }
74 }
75 
76 osg::Node *VsMeshMgr::AddMesh(std::string strFilename)
77 {
78  osg::ref_ptr<osg::Node> lpTempMesh = osgDB::readNodeFile(strFilename.c_str());
79 
80  if(lpTempMesh)
81  {
82  std::string strFileCreateTime = FileCreateTime(strFilename);
83  std::pair<std::string, osg::ref_ptr<osg::Node>> MeshPair2(strFileCreateTime, lpTempMesh);
84 
85  m_aryMeshes.Add(Std_CheckString(strFilename), MeshPair2);
86  return lpTempMesh.get();
87  }
88  else
89  return NULL;
90 
91 }
92 
93 void VsMeshMgr::ReleaseMesh(std::string strFilename)
94 {
95  int iIndex=0;
96  std::pair<std::string, osg::ref_ptr<osg::Node>> MeshPair;
97  if(!FindMesh(strFilename, MeshPair, false)) return;
98 
99  MeshPair.second.release();
100 
101  m_aryMeshes.Remove(Std_CheckString(strFilename));
102 }
103 
104 bool VsMeshMgr::ContainesMesh(std::string strFilename)
105 {
106  std::pair<std::string, osg::ref_ptr<osg::Node>> MeshPair;
107 
108  if(FindMesh(strFilename, MeshPair, false))
109  return true;
110  else
111  return false;
112 }
113 
125 bool VsMeshMgr::FindMesh(std::string strFilename, std::pair<std::string, osg::ref_ptr<osg::Node>> &MeshPair, bool bThrowError)
126 {
127  CStdMap<std::string, std::pair<std::string, osg::ref_ptr<osg::Node>> >::iterator oPos;
128  oPos = m_aryMeshes.find(Std_CheckString(strFilename));
129 
130  if(oPos != m_aryMeshes.end())
131  {
132  MeshPair = oPos->second;
133  return true;
134  }
135  else if(bThrowError)
136  THROW_TEXT_ERROR(Vs_Err_lMeshIDNotFound, Vs_Err_strMeshNotFound, " Mesh Filename: " + strFilename);
137 
138  return false;
139 }
140 
141 
142 } //VortexAnimatSim
VsMeshMgr()
Default constructor.
Definition: VsMeshMgr.cpp:19
Classes for implementing the cm-labs vortex physics engine for AnimatLab.
void Std_TraceMsg(const int iLevel, std::string strMessage, std::string strSourceFile, int iSourceLine, bool bLogToFile, bool bPrintHeader)
Traces a message to the debugger window.
std::string Std_CheckString(std::string strVal)
Converts a string to upper case and trims it.
virtual ~VsMeshMgr()
Destructor.
Definition: VsMeshMgr.cpp:29
Declares the vortex MeshMgr class.