// STMEditorDoc.cpp : implementation of the CSTMEditorDoc class
//
#include "stdafx.h"
#include "STMEditor.h"
#include "STMEditorDoc.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#include "math.h"
/////////////////////////////////////////////////////////////////////////////
// CSTMEditorDoc
#include "MapPropDlg.h"
IMPLEMENT_DYNCREATE(CSTMEditorDoc, CDocument)
BEGIN_MESSAGE_MAP(CSTMEditorDoc, CDocument)
//{{AFX_MSG_MAP(CSTMEditorDoc)
ON_COMMAND(ID_MAP_PROP, OnMapProp)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSTMEditorDoc construction/destruction
CSTMEditorDoc::CSTMEditorDoc()
{
m_selTool=ST_BROWSE;
m_arrSel.SetSize(0,10);
}
CSTMEditorDoc::~CSTMEditorDoc()
{
ClearDataBuffer();
}
BOOL CSTMEditorDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
// TODO: add reinitialization code here
// (SDI documents will reuse this document)
ClearDataBuffer();
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CSTMEditorDoc serialization
void CSTMEditorDoc::Serialize(CArchive& ar)
{
CString filename=ar.GetFile()->GetFilePath();
CString path=filename.Left(filename.ReverseFind('\\')+1);
CString workpath=path+ar.GetFile()->GetFileTitle()+"(set)";
CString crossfilename=workpath+"\\cross.dat";
CString pathfilename=workpath+"\\path.dat";
CString bsfilename=workpath+"\\busstation.dat";
CString trfilename=workpath+"\\traffic.dat";
CString streetfilename=workpath+"\\street.dat";
CString addressfilename=workpath+"\\address.dat";
int i;
if (ar.IsStoring())
{
// TODO: add storing code here
ar.WriteString(m_Name);
ar.WriteString("\r\n");
ar.WriteString(m_Author);
ar.WriteString("\r\n");
ar.WriteString(m_Memory);
ar.WriteString("\r\n");
CString tmp;
tmp.Format("%d\r\n%d\r\n%d\r\n",m_Width,m_Height,m_Scale);
ar.WriteString(tmp);
CreateDirectory(workpath,NULL);
if(!m_bkfilename.IsEmpty())
{
CString title=m_bkfilename.Right(m_bkfilename.GetLength()-m_bkfilename.ReverseFind('\\'));
CString bkfilename=workpath+title;
CopyFile(m_bkfilename,bkfilename,TRUE);
ar.WriteString(bkfilename);
ar.WriteString("\r\n");
}
//save cross data
CFile crossfile;
crossfile.Open(crossfilename,CFile::modeCreate|CFile::modeWrite|CFile::typeBinary);
int crosses=m_arrCross.GetSize();
crossfile.Write(&crosses,sizeof(int));
CROSSINFO ci;
int namelen;
for( i=0;i<m_arrCross.GetSize();i++)
{
ci=m_arrCross.GetAt(i);
namelen=strlen(ci.name);
crossfile.Write(&ci.pos,sizeof(POINT));
crossfile.Write(&namelen,sizeof(int));
crossfile.Write(ci.name,strlen(ci.name));
}
crossfile.Close();
//save path data
CFile pathfile;
pathfile.Open(pathfilename,CFile::modeCreate|CFile::modeWrite|CFile::typeBinary);
int pathes=m_arrPath.GetSize();
pathfile.Write(&pathes,sizeof(int));
PATHINFO pi;
for( i=0;i<m_arrPath.GetSize();i++)
{
pi=m_arrPath.GetAt(i);
pathfile.Write(&pi,sizeof(PATHINFO));
}
pathfile.Close();
//save bus station data
CFile bsfile;
bsfile.Open(bsfilename,CFile::modeCreate|CFile::modeWrite|CFile::typeBinary);
int bses=m_arrBS.GetSize();
bsfile.Write(&bses,sizeof(int));
BSINFO bsi;
for( i=0;i<m_arrBS.GetSize();i++)
{
bsi=m_arrBS.GetAt(i);
bsfile.Write(&bsi,sizeof(BSINFO));
}
bsfile.Close();
//save traffic route data
CFile trfile;
trfile.Open(trfilename,CFile::modeCreate|CFile::modeWrite|CFile::typeBinary);
int trafficroutes=m_arrTR.GetSize();
trfile.Write(&trafficroutes,sizeof(int));
TRAFFICROUTEINFO tri;
int test=sizeof(ELEINFO);
for( i=0;i<m_arrTR.GetSize();i++)
{
tri=m_arrTR.GetAt(i);
trfile.Write(tri.name,10);
trfile.Write(&tri.type,sizeof(BYTE));
trfile.Write(&tri.nStations,sizeof(int));
trfile.Write(tri.pEI,sizeof(ELEINFO)*tri.nStations);
}
trfile.Close();
//save street data
CFile streetfile;
streetfile.Open(streetfilename,CFile::modeCreate|CFile::modeWrite|CFile::typeBinary);
int streets=m_arrStreet.GetSize();
streetfile.Write(&streets,sizeof(int));
STREETINFO si;
for(i=0;i<streets;i++)
{
si=m_arrStreet.GetAt(i);
streetfile.Write(si.name,50);
streetfile.Write(&si.nPathes,sizeof(int));
streetfile.Write(si.pPathID,sizeof(int)*si.nPathes);
}
streetfile.Close();
//save address data
CFile addressfile;
addressfile.Open(addressfilename,CFile::modeCreate|CFile::modeWrite|CFile::typeBinary);
int addresses=m_arrAddress.GetSize();
addressfile.Write(&addresses,sizeof(int));
ADDRESSINFO ai;
for(i=0;i<addresses;i++)
{
ai=m_arrAddress.GetAt(i);
addressfile.Write(&ai,sizeof(ADDRESSINFO));
}
addressfile.Close();
}
else
{
// TODO: add loading code here
ClearDataBuffer();
ar.ReadString(m_Name);
ar.ReadString(m_Author);
ar.ReadString(m_Memory);
CString tmp;
ar.ReadString(tmp);
m_Width=atoi(tmp.GetBuffer(tmp.GetLength()));
tmp.ReleaseBuffer();
ar.ReadString(tmp);
m_Height=atoi(tmp.GetBuffer(tmp.GetLength()));
tmp.ReleaseBuffer();
ar.ReadString(tmp);
m_Scale=atoi(tmp.GetBuffer(tmp.GetLength()));
tmp.ReleaseBuffer();
ar.ReadString(m_bkfilename);
m_Picture.Load(m_bkfilename);
//read cross data
CFile crossfile;
if(crossfile.Open(crossfilename,CFile::modeRead|CFile::typeBinary))
{
int crosses;
crossfile.Read(&crosses,sizeof(int));
m_arrCross.SetSize(crosses,10);
CROSSINFO ci;
int namelen=0;
for( i=0;i<crosses;i++)
{
crossfile.Read(&ci.pos,sizeof(POINT));
crossfile.Read(&namelen,sizeof(int));
crossfile.Read(ci.name,namelen);
ci.name[namelen]=0;
m_arrCross.SetAt(i,ci);
}
crossfile.Close();
}
//read path data
CFile pathfile;
if(pathfile.Open(pathfilename,CFile::modeRead|CFile::typeBinary))
{
int pathes;
pathfile.Read(&pathes,sizeof(int));
m_arrPath.SetSize(pathes,10);
PATHINFO pi;
for( i=0;i<pathes;i++)
{
pathfile.Read(&pi,sizeof(PATHINFO));
m_arrPath.SetAt(i,pi);
}
pathfile.Close();
}
//read bus station data
CFile bsfile;
if(bsfile.Open(bsfilename,CFile::modeRead|CFile::typeBinary))
{
int bses;
bsfile.Read(&bses,sizeof(int));
m_arrBS.SetSize(bses,10);
BSINFO bsi;
for( i=0;i<bses;i++)
{
bsfile.Read(&bsi,sizeof(BSINFO));
m_arrBS.SetAt(i,bsi);
}
bsfile.Close();
}
//read traffic route data
CFile trfile;
if(trfile.Open(trfilename,CFile::modeRead|CFile::typeBinary))
{
int trafficroutes;
trfile.Read(&trafficroutes,sizeof(int));
m_arrTR.SetSize(trafficroutes,10);
TRAFFICROUTEINFO tri;
for( i=0;i<trafficroutes;i++)
{
trfile.Read(tri.name,10);//get traffic route name
trfile.Read(&tri.type,sizeof(BYTE));
trfile.Read(&tri.nStations,sizeof(int));//get traffic route number
tri.pEI=new ELEINFO[tri.nStations];
trfile.Read(tri.pEI,sizeof(ELEINFO)*tri.nStations);
m_arrTR.SetAt(i,tri);
}
trfile.Close();
}
//read street data
CFile streetfile;
if(streetfile.Open(streetfilename,CFile::modeRead|CFile::typeBinary))
{
int streets;
streetfile.Read(&streets,sizeof(int));
m_arrStreet.SetSize(streets,10);
STREETINFO si;
for( i=0;i<streets;i++)
{
streetfile.Read(si.name,50);//get traffic route name
streetfile.Read(&si.nPathes,sizeof(int));//get traffic route number
si.pPathID =new int[si.nPathes];
streetfile.Read(si.pPathID,sizeof(int)*si.nPathes);
m_arrStreet.SetAt(i,si);
}
streetfile.Close();
}
CFile addressfile;
if(addressfile.Open(addressfilename,CFile::modeRead|CFile::typeBinary))
{
int addresses;
a